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,
326 struct snd_soc_dapm_widget *w;
328 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
333 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, _widget->name);
335 w->name = kstrdup_const(_widget->name, GFP_KERNEL);
341 if (_widget->sname) {
342 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
344 kfree_const(w->name);
352 struct dapm_kcontrol_data {
354 struct snd_soc_dapm_widget *widget;
355 struct list_head paths;
356 struct snd_soc_dapm_widget_list *wlist;
359 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
360 struct snd_kcontrol *kcontrol, const char *ctrl_name)
362 struct dapm_kcontrol_data *data;
363 struct soc_mixer_control *mc;
368 data = kzalloc(sizeof(*data), GFP_KERNEL);
372 INIT_LIST_HEAD(&data->paths);
374 switch (widget->id) {
375 case snd_soc_dapm_switch:
376 case snd_soc_dapm_mixer:
377 case snd_soc_dapm_mixer_named_ctl:
378 mc = (struct soc_mixer_control *)kcontrol->private_value;
380 if (mc->autodisable) {
381 struct snd_soc_dapm_widget template;
383 if (snd_soc_volsw_is_stereo(mc))
384 dev_warn(widget->dapm->dev,
385 "ASoC: Unsupported stereo autodisable control '%s'\n",
388 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
395 memset(&template, 0, sizeof(template));
396 template.reg = mc->reg;
397 template.mask = (1 << fls(mc->max)) - 1;
398 template.shift = mc->shift;
400 template.off_val = mc->max;
402 template.off_val = 0;
403 template.on_val = template.off_val;
404 template.id = snd_soc_dapm_kcontrol;
405 template.name = name;
407 data->value = template.on_val;
410 snd_soc_dapm_new_control_unlocked(widget->dapm,
413 if (IS_ERR(data->widget)) {
414 ret = PTR_ERR(data->widget);
419 case snd_soc_dapm_demux:
420 case snd_soc_dapm_mux:
421 e = (struct soc_enum *)kcontrol->private_value;
423 if (e->autodisable) {
424 struct snd_soc_dapm_widget template;
426 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
433 memset(&template, 0, sizeof(template));
434 template.reg = e->reg;
435 template.mask = e->mask;
436 template.shift = e->shift_l;
437 template.off_val = snd_soc_enum_item_to_val(e, 0);
438 template.on_val = template.off_val;
439 template.id = snd_soc_dapm_kcontrol;
440 template.name = name;
442 data->value = template.on_val;
444 data->widget = snd_soc_dapm_new_control_unlocked(
445 widget->dapm, &template);
447 if (IS_ERR(data->widget)) {
448 ret = PTR_ERR(data->widget);
452 snd_soc_dapm_add_path(widget->dapm, data->widget,
454 } else if (e->reg != SND_SOC_NOPM) {
455 data->value = soc_dapm_read(widget->dapm, e->reg) &
456 (e->mask << e->shift_l);
463 kcontrol->private_data = data;
472 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
474 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
476 list_del(&data->paths);
481 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
482 const struct snd_kcontrol *kcontrol)
484 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
489 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
490 struct snd_soc_dapm_widget *widget)
492 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
493 struct snd_soc_dapm_widget_list *new_wlist;
497 n = data->wlist->num_widgets + 1;
501 new_wlist = krealloc(data->wlist,
502 struct_size(new_wlist, widgets, n),
507 new_wlist->num_widgets = n;
508 new_wlist->widgets[n - 1] = widget;
510 data->wlist = new_wlist;
515 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
516 struct snd_soc_dapm_path *path)
518 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
520 list_add_tail(&path->list_kcontrol, &data->paths);
523 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
525 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
530 return data->widget->power;
533 static struct list_head *dapm_kcontrol_get_path_list(
534 const struct snd_kcontrol *kcontrol)
536 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
541 #define dapm_kcontrol_for_each_path(path, kcontrol) \
542 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
545 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
547 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
551 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
553 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
556 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
558 if (data->value == value)
562 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
563 case snd_soc_dapm_switch:
564 case snd_soc_dapm_mixer:
565 case snd_soc_dapm_mixer_named_ctl:
566 data->widget->on_val = value & data->widget->mask;
568 case snd_soc_dapm_demux:
569 case snd_soc_dapm_mux:
570 data->widget->on_val = value >> data->widget->shift;
573 data->widget->on_val = value;
584 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
586 * @kcontrol: The kcontrol
588 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
589 struct snd_kcontrol *kcontrol)
591 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
593 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
596 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
598 * @kcontrol: The kcontrol
600 * Note: This function must only be used on kcontrols that are known to have
601 * been registered for a CODEC. Otherwise the behaviour is undefined.
603 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
604 struct snd_kcontrol *kcontrol)
606 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
608 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
610 static void dapm_reset(struct snd_soc_card *card)
612 struct snd_soc_dapm_widget *w;
614 snd_soc_dapm_mutex_assert_held(card);
616 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
618 for_each_card_widgets(card, w) {
619 w->new_power = w->power;
620 w->power_checked = false;
624 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
626 if (!dapm->component)
628 return dapm->component->name_prefix;
631 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg)
633 if (!dapm->component)
635 return snd_soc_component_read(dapm->component, reg);
638 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
639 int reg, unsigned int mask, unsigned int value)
641 if (!dapm->component)
643 return snd_soc_component_update_bits(dapm->component, reg,
647 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
648 int reg, unsigned int mask, unsigned int value)
650 if (!dapm->component)
652 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
655 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
658 snd_soc_component_async_complete(dapm->component);
661 static struct snd_soc_dapm_widget *
662 dapm_wcache_lookup(struct snd_soc_dapm_widget *w, const char *name)
665 struct list_head *wlist = &w->dapm->card->widgets;
669 list_for_each_entry_from(w, wlist, list) {
670 if (!strcmp(name, w->name))
682 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
683 * @dapm: The DAPM context for which to set the level
684 * @level: The level to set
686 * Forces the DAPM bias level to a specific state. It will call the bias level
687 * callback of DAPM context with the specified level. This will even happen if
688 * the context is already at the same level. Furthermore it will not go through
689 * the normal bias level sequencing, meaning any intermediate states between the
690 * current and the target state will not be entered.
692 * Note that the change in bias level is only temporary and the next time
693 * snd_soc_dapm_sync() is called the state will be set to the level as
694 * determined by the DAPM core. The function is mainly intended to be used to
695 * used during probe or resume from suspend to power up the device so
696 * initialization can be done, before the DAPM core takes over.
698 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
699 enum snd_soc_bias_level level)
704 ret = snd_soc_component_set_bias_level(dapm->component, level);
707 dapm->bias_level = level;
711 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
714 * snd_soc_dapm_set_bias_level - set the bias level for the system
715 * @dapm: DAPM context
716 * @level: level to configure
718 * Configure the bias (power) levels for the SoC audio device.
720 * Returns 0 for success else error.
722 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
723 enum snd_soc_bias_level level)
725 struct snd_soc_card *card = dapm->card;
728 trace_snd_soc_bias_level_start(dapm, level);
730 ret = snd_soc_card_set_bias_level(card, dapm, level);
734 if (!card || dapm != &card->dapm)
735 ret = snd_soc_dapm_force_bias_level(dapm, level);
740 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
742 trace_snd_soc_bias_level_done(dapm, level);
747 /* connect mux widget to its interconnecting audio paths */
748 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
749 struct snd_soc_dapm_path *path, const char *control_name,
750 struct snd_soc_dapm_widget *w)
752 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
753 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
757 if (e->reg != SND_SOC_NOPM) {
759 val = soc_dapm_read(dapm, e->reg);
760 val = (val >> e->shift_l) & e->mask;
761 item = snd_soc_enum_val_to_item(e, val);
763 /* since a virtual mux has no backing registers to
764 * decide which path to connect, it will try to match
765 * with the first enumeration. This is to ensure
766 * that the default mux choice (the first) will be
767 * correctly powered up during initialization.
772 i = match_string(e->texts, e->items, control_name);
776 path->name = e->texts[i];
777 path->connect = (i == item);
782 /* set up initial codec paths */
783 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
786 struct soc_mixer_control *mc = (struct soc_mixer_control *)
787 p->sink->kcontrol_news[i].private_value;
788 unsigned int reg = mc->reg;
789 unsigned int invert = mc->invert;
791 if (reg != SND_SOC_NOPM) {
792 unsigned int shift = mc->shift;
793 unsigned int max = mc->max;
794 unsigned int mask = (1 << fls(max)) - 1;
795 unsigned int val = soc_dapm_read(p->sink->dapm, reg);
798 * The nth_path argument allows this function to know
799 * which path of a kcontrol it is setting the initial
800 * status for. Ideally this would support any number
801 * of paths and channels. But since kcontrols only come
802 * in mono and stereo variants, we are limited to 2
805 * The following code assumes for stereo controls the
806 * first path is the left channel, and all remaining
807 * paths are the right channel.
809 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
811 val = soc_dapm_read(p->sink->dapm, mc->rreg);
812 val = (val >> mc->rshift) & mask;
814 val = (val >> shift) & mask;
820 /* since a virtual mixer has no backing registers to
821 * decide which path to connect, it will try to match
822 * with initial state. This is to ensure
823 * that the default mixer choice will be
824 * correctly powered up during initialization.
830 /* connect mixer widget to its interconnecting audio paths */
831 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
832 struct snd_soc_dapm_path *path, const char *control_name)
836 /* search for mixer kcontrol */
837 for (i = 0; i < path->sink->num_kcontrols; i++) {
838 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
839 path->name = path->sink->kcontrol_news[i].name;
840 dapm_set_mixer_path_status(path, i, nth_path++);
847 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
848 struct snd_soc_dapm_widget *kcontrolw,
849 const struct snd_kcontrol_new *kcontrol_new,
850 struct snd_kcontrol **kcontrol)
852 struct snd_soc_dapm_widget *w;
857 for_each_card_widgets(dapm->card, w) {
858 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
860 for (i = 0; i < w->num_kcontrols; i++) {
861 if (&w->kcontrol_news[i] == kcontrol_new) {
863 *kcontrol = w->kcontrols[i];
873 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
874 * create it. Either way, add the widget into the control's widget list
876 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
879 struct snd_soc_dapm_context *dapm = w->dapm;
880 struct snd_card *card = dapm->card->snd_card;
884 struct snd_kcontrol *kcontrol;
885 bool wname_in_long_name, kcname_in_long_name;
886 char *long_name = NULL;
890 prefix = soc_dapm_prefix(dapm);
892 prefix_len = strlen(prefix) + 1;
896 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
901 wname_in_long_name = false;
902 kcname_in_long_name = true;
905 case snd_soc_dapm_switch:
906 case snd_soc_dapm_mixer:
907 case snd_soc_dapm_pga:
908 case snd_soc_dapm_effect:
909 case snd_soc_dapm_out_drv:
910 wname_in_long_name = true;
911 kcname_in_long_name = true;
913 case snd_soc_dapm_mixer_named_ctl:
914 wname_in_long_name = false;
915 kcname_in_long_name = true;
917 case snd_soc_dapm_demux:
918 case snd_soc_dapm_mux:
919 wname_in_long_name = true;
920 kcname_in_long_name = false;
926 if (w->no_wname_in_kcontrol_name)
927 wname_in_long_name = false;
929 if (wname_in_long_name && kcname_in_long_name) {
931 * The control will get a prefix from the control
932 * creation process but we're also using the same
933 * prefix for widgets so cut the prefix off the
934 * front of the widget name.
936 long_name = kasprintf(GFP_KERNEL, "%s %s",
937 w->name + prefix_len,
938 w->kcontrol_news[kci].name);
939 if (long_name == NULL)
943 } else if (wname_in_long_name) {
945 name = w->name + prefix_len;
948 name = w->kcontrol_news[kci].name;
951 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
958 kcontrol->private_free = dapm_kcontrol_free;
960 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
962 snd_ctl_free_one(kcontrol);
966 ret = snd_ctl_add(card, kcontrol);
969 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
975 ret = dapm_kcontrol_add_widget(kcontrol, w);
977 w->kcontrols[kci] = kcontrol;
985 /* create new dapm mixer control */
986 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
989 struct snd_soc_dapm_path *path;
990 struct dapm_kcontrol_data *data;
993 for (i = 0; i < w->num_kcontrols; i++) {
995 snd_soc_dapm_widget_for_each_source_path(w, path) {
996 /* mixer/mux paths name must match control name */
997 if (path->name != (char *)w->kcontrol_news[i].name)
1000 if (!w->kcontrols[i]) {
1001 ret = dapm_create_or_share_kcontrol(w, i);
1006 dapm_kcontrol_add_path(w->kcontrols[i], path);
1008 data = snd_kcontrol_chip(w->kcontrols[i]);
1010 snd_soc_dapm_add_path(data->widget->dapm,
1020 /* create new dapm mux control */
1021 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1023 struct snd_soc_dapm_context *dapm = w->dapm;
1024 enum snd_soc_dapm_direction dir;
1025 struct snd_soc_dapm_path *path;
1030 case snd_soc_dapm_mux:
1031 dir = SND_SOC_DAPM_DIR_OUT;
1034 case snd_soc_dapm_demux:
1035 dir = SND_SOC_DAPM_DIR_IN;
1042 if (w->num_kcontrols != 1) {
1044 "ASoC: %s %s has incorrect number of controls\n", type,
1049 if (list_empty(&w->edges[dir])) {
1050 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1054 ret = dapm_create_or_share_kcontrol(w, 0);
1058 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1060 dapm_kcontrol_add_path(w->kcontrols[0], path);
1066 /* create new dapm volume control */
1067 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1071 for (i = 0; i < w->num_kcontrols; i++) {
1072 int ret = dapm_create_or_share_kcontrol(w, i);
1080 /* create new dapm dai link control */
1081 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1084 struct snd_soc_pcm_runtime *rtd = w->priv;
1086 /* create control for links with > 1 config */
1087 if (rtd->dai_link->num_c2c_params <= 1)
1091 for (i = 0; i < w->num_kcontrols; i++) {
1092 struct snd_soc_dapm_context *dapm = w->dapm;
1093 struct snd_card *card = dapm->card->snd_card;
1094 struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
1096 int ret = snd_ctl_add(card, kcontrol);
1100 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1101 w->name, w->kcontrol_news[i].name, ret);
1104 kcontrol->private_data = w;
1105 w->kcontrols[i] = kcontrol;
1111 /* We implement power down on suspend by checking the power state of
1112 * the ALSA card - when we are suspending the ALSA state for the card
1115 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1117 int level = snd_power_get_state(widget->dapm->card->snd_card);
1120 case SNDRV_CTL_POWER_D3hot:
1121 case SNDRV_CTL_POWER_D3cold:
1122 if (widget->ignore_suspend)
1123 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1125 return widget->ignore_suspend;
1131 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1136 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1137 struct list_head *widgets)
1139 struct snd_soc_dapm_widget *w;
1140 struct list_head *it;
1141 unsigned int size = 0;
1144 list_for_each(it, widgets)
1147 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1151 list_for_each_entry(w, widgets, work_list)
1152 (*list)->widgets[i++] = w;
1154 (*list)->num_widgets = i;
1160 * Recursively reset the cached number of inputs or outputs for the specified
1161 * widget and all widgets that can be reached via incoming or outcoming paths
1164 static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1165 enum snd_soc_dapm_direction dir)
1167 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1168 struct snd_soc_dapm_path *path;
1170 widget->endpoints[dir] = -1;
1172 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1173 if (path->weak || path->is_supply)
1179 if (path->connect) {
1181 invalidate_paths_ep(path->node[dir], dir);
1188 * Common implementation for is_connected_output_ep() and
1189 * is_connected_input_ep(). The function is inlined since the combined size of
1190 * the two specialized functions is only marginally larger then the size of the
1191 * generic function and at the same time the fast path of the specialized
1192 * functions is significantly smaller than the generic function.
1194 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1195 struct list_head *list, enum snd_soc_dapm_direction dir,
1196 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1197 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1198 enum snd_soc_dapm_direction)),
1199 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1200 enum snd_soc_dapm_direction))
1202 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1203 struct snd_soc_dapm_path *path;
1206 if (widget->endpoints[dir] >= 0)
1207 return widget->endpoints[dir];
1209 DAPM_UPDATE_STAT(widget, path_checks);
1211 /* do we need to add this widget to the list ? */
1213 list_add_tail(&widget->work_list, list);
1215 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1217 custom_stop_condition = NULL;
1220 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1221 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1222 return widget->endpoints[dir];
1225 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1226 DAPM_UPDATE_STAT(widget, neighbour_checks);
1228 if (path->weak || path->is_supply)
1234 trace_snd_soc_dapm_path(widget, dir, path);
1236 if (path->connect) {
1238 con += fn(path->node[dir], list, custom_stop_condition);
1243 widget->endpoints[dir] = con;
1249 * Recursively check for a completed path to an active or physically connected
1250 * output widget. Returns number of complete paths.
1252 * Optionally, can be supplied with a function acting as a stopping condition.
1253 * This function takes the dapm widget currently being examined and the walk
1254 * direction as an arguments, it should return true if widgets from that point
1255 * in the graph onwards should not be added to the widget list.
1257 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1258 struct list_head *list,
1259 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1260 enum snd_soc_dapm_direction))
1262 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1263 is_connected_output_ep, custom_stop_condition);
1267 * Recursively check for a completed path to an active or physically connected
1268 * input widget. Returns number of complete paths.
1270 * Optionally, can be supplied with a function acting as a stopping condition.
1271 * This function takes the dapm widget currently being examined and the walk
1272 * direction as an arguments, it should return true if the walk should be
1273 * stopped and false otherwise.
1275 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1276 struct list_head *list,
1277 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1278 enum snd_soc_dapm_direction))
1280 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1281 is_connected_input_ep, custom_stop_condition);
1285 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
1286 * @dai: the soc DAI.
1287 * @stream: stream direction.
1288 * @list: list of active widgets for this stream.
1289 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1290 * walk based on custom logic.
1292 * Queries DAPM graph as to whether a valid audio stream path exists for
1293 * the initial stream specified by name. This takes into account
1294 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1296 * Optionally, can be supplied with a function acting as a stopping condition.
1297 * This function takes the dapm widget currently being examined and the walk
1298 * direction as an arguments, it should return true if the walk should be
1299 * stopped and false otherwise.
1301 * Returns the number of valid paths or negative error.
1303 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1304 struct snd_soc_dapm_widget_list **list,
1305 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1306 enum snd_soc_dapm_direction))
1308 struct snd_soc_card *card = dai->component->card;
1309 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(dai, stream);
1314 snd_soc_dapm_mutex_lock(card);
1316 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1317 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1318 paths = is_connected_output_ep(w, &widgets,
1319 custom_stop_condition);
1321 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1322 paths = is_connected_input_ep(w, &widgets,
1323 custom_stop_condition);
1326 /* Drop starting point */
1327 list_del(widgets.next);
1329 ret = dapm_widget_list_create(list, &widgets);
1333 trace_snd_soc_dapm_connected(paths, stream);
1334 snd_soc_dapm_mutex_unlock(card);
1338 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_get_connected_widgets);
1340 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1342 dapm_widget_list_free(list);
1344 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_free_widgets);
1347 * Handler for regulator supply widget.
1349 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1350 struct snd_kcontrol *kcontrol, int event)
1354 soc_dapm_async_complete(w->dapm);
1356 if (SND_SOC_DAPM_EVENT_ON(event)) {
1357 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1358 ret = regulator_allow_bypass(w->regulator, false);
1360 dev_warn(w->dapm->dev,
1361 "ASoC: Failed to unbypass %s: %d\n",
1365 return regulator_enable(w->regulator);
1367 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1368 ret = regulator_allow_bypass(w->regulator, true);
1370 dev_warn(w->dapm->dev,
1371 "ASoC: Failed to bypass %s: %d\n",
1375 return regulator_disable_deferred(w->regulator, w->shift);
1378 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1381 * Handler for pinctrl widget.
1383 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1384 struct snd_kcontrol *kcontrol, int event)
1386 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1387 struct pinctrl *p = w->pinctrl;
1388 struct pinctrl_state *s;
1393 if (SND_SOC_DAPM_EVENT_ON(event))
1394 s = pinctrl_lookup_state(p, priv->active_state);
1396 s = pinctrl_lookup_state(p, priv->sleep_state);
1401 return pinctrl_select_state(p, s);
1403 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1406 * Handler for clock supply widget.
1408 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1409 struct snd_kcontrol *kcontrol, int event)
1414 soc_dapm_async_complete(w->dapm);
1416 if (SND_SOC_DAPM_EVENT_ON(event)) {
1417 return clk_prepare_enable(w->clk);
1419 clk_disable_unprepare(w->clk);
1425 EXPORT_SYMBOL_GPL(dapm_clock_event);
1427 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1429 if (w->power_checked)
1430 return w->new_power;
1435 w->new_power = w->power_check(w);
1437 w->power_checked = true;
1439 return w->new_power;
1442 /* Generic check to see if a widget should be powered. */
1443 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1447 DAPM_UPDATE_STAT(w, power_checks);
1449 in = is_connected_input_ep(w, NULL, NULL);
1450 out = is_connected_output_ep(w, NULL, NULL);
1451 return out != 0 && in != 0;
1454 /* Check to see if a power supply is needed */
1455 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1457 struct snd_soc_dapm_path *path;
1459 DAPM_UPDATE_STAT(w, power_checks);
1461 /* Check if one of our outputs is connected */
1462 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1463 DAPM_UPDATE_STAT(w, neighbour_checks);
1468 if (path->connected &&
1469 !path->connected(path->source, path->sink))
1472 if (dapm_widget_power_check(path->sink))
1479 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1481 return w->connected;
1484 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1485 struct snd_soc_dapm_widget *b,
1490 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1491 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1496 sort = dapm_down_seq;
1498 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1499 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1501 if (sort[a->id] != sort[b->id])
1502 return sort[a->id] - sort[b->id];
1503 if (a->subseq != b->subseq) {
1505 return a->subseq - b->subseq;
1507 return b->subseq - a->subseq;
1509 if (a->reg != b->reg)
1510 return a->reg - b->reg;
1511 if (a->dapm != b->dapm)
1512 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1517 /* Insert a widget in order into a DAPM power sequence. */
1518 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1519 struct list_head *list,
1522 struct snd_soc_dapm_widget *w;
1524 list_for_each_entry(w, list, power_list)
1525 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1526 list_add_tail(&new_widget->power_list, &w->power_list);
1530 list_add_tail(&new_widget->power_list, list);
1533 static void dapm_seq_check_event(struct snd_soc_card *card,
1534 struct snd_soc_dapm_widget *w, int event)
1536 const char *ev_name;
1540 case SND_SOC_DAPM_PRE_PMU:
1541 ev_name = "PRE_PMU";
1544 case SND_SOC_DAPM_POST_PMU:
1545 ev_name = "POST_PMU";
1548 case SND_SOC_DAPM_PRE_PMD:
1549 ev_name = "PRE_PMD";
1552 case SND_SOC_DAPM_POST_PMD:
1553 ev_name = "POST_PMD";
1556 case SND_SOC_DAPM_WILL_PMU:
1557 ev_name = "WILL_PMU";
1560 case SND_SOC_DAPM_WILL_PMD:
1561 ev_name = "WILL_PMD";
1565 WARN(1, "Unknown event %d\n", event);
1569 if (w->new_power != power)
1572 if (w->event && (w->event_flags & event)) {
1575 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1577 soc_dapm_async_complete(w->dapm);
1578 trace_snd_soc_dapm_widget_event_start(w, event);
1579 ret = w->event(w, NULL, event);
1580 trace_snd_soc_dapm_widget_event_done(w, event);
1582 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1583 ev_name, w->name, ret);
1587 /* Apply the coalesced changes from a DAPM sequence */
1588 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1589 struct list_head *pending)
1591 struct snd_soc_dapm_context *dapm;
1592 struct snd_soc_dapm_widget *w;
1594 unsigned int value = 0;
1595 unsigned int mask = 0;
1597 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1601 list_for_each_entry(w, pending, power_list) {
1602 WARN_ON(reg != w->reg || dapm != w->dapm);
1603 w->power = w->new_power;
1605 mask |= w->mask << w->shift;
1607 value |= w->on_val << w->shift;
1609 value |= w->off_val << w->shift;
1611 pop_dbg(dapm->dev, card->pop_time,
1612 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1613 w->name, reg, value, mask);
1615 /* Check for events */
1616 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1617 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1621 /* Any widget will do, they should all be updating the
1625 pop_dbg(dapm->dev, card->pop_time,
1626 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1627 value, mask, reg, card->pop_time);
1628 pop_wait(card->pop_time);
1629 soc_dapm_update_bits(dapm, reg, mask, value);
1632 list_for_each_entry(w, pending, power_list) {
1633 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1634 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1638 /* Apply a DAPM power sequence.
1640 * We walk over a pre-sorted list of widgets to apply power to. In
1641 * order to minimise the number of writes to the device required
1642 * multiple widgets will be updated in a single write where possible.
1643 * Currently anything that requires more than a single write is not
1646 static void dapm_seq_run(struct snd_soc_card *card,
1647 struct list_head *list, int event, bool power_up)
1649 struct snd_soc_dapm_widget *w, *n;
1650 struct snd_soc_dapm_context *d;
1653 int cur_subseq = -1;
1654 int cur_reg = SND_SOC_NOPM;
1655 struct snd_soc_dapm_context *cur_dapm = NULL;
1662 sort = dapm_down_seq;
1664 list_for_each_entry_safe(w, n, list, power_list) {
1667 /* Do we need to apply any queued changes? */
1668 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1669 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1670 if (!list_empty(&pending))
1671 dapm_seq_run_coalesced(card, &pending);
1673 if (cur_dapm && cur_dapm->component) {
1674 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1675 if (sort[i] == cur_sort)
1676 snd_soc_component_seq_notifier(
1677 cur_dapm->component,
1681 if (cur_dapm && w->dapm != cur_dapm)
1682 soc_dapm_async_complete(cur_dapm);
1684 INIT_LIST_HEAD(&pending);
1686 cur_subseq = INT_MIN;
1687 cur_reg = SND_SOC_NOPM;
1692 case snd_soc_dapm_pre:
1696 if (event == SND_SOC_DAPM_STREAM_START)
1698 NULL, SND_SOC_DAPM_PRE_PMU);
1699 else if (event == SND_SOC_DAPM_STREAM_STOP)
1701 NULL, SND_SOC_DAPM_PRE_PMD);
1704 case snd_soc_dapm_post:
1708 if (event == SND_SOC_DAPM_STREAM_START)
1710 NULL, SND_SOC_DAPM_POST_PMU);
1711 else if (event == SND_SOC_DAPM_STREAM_STOP)
1713 NULL, SND_SOC_DAPM_POST_PMD);
1717 /* Queue it up for application */
1718 cur_sort = sort[w->id];
1719 cur_subseq = w->subseq;
1722 list_move(&w->power_list, &pending);
1727 dev_err(w->dapm->dev,
1728 "ASoC: Failed to apply widget power: %d\n", ret);
1731 if (!list_empty(&pending))
1732 dapm_seq_run_coalesced(card, &pending);
1734 if (cur_dapm && cur_dapm->component) {
1735 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1736 if (sort[i] == cur_sort)
1737 snd_soc_component_seq_notifier(
1738 cur_dapm->component,
1742 for_each_card_dapms(card, d)
1743 soc_dapm_async_complete(d);
1746 static void dapm_widget_update(struct snd_soc_card *card)
1748 struct snd_soc_dapm_update *update = card->update;
1749 struct snd_soc_dapm_widget_list *wlist;
1750 struct snd_soc_dapm_widget *w = NULL;
1754 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1757 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1759 for_each_dapm_widgets(wlist, wi, w) {
1760 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1761 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1763 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1771 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1774 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1777 if (update->has_second_set) {
1778 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1779 update->mask2, update->val2);
1781 dev_err(w->dapm->dev,
1782 "ASoC: %s DAPM update failed: %d\n",
1786 for_each_dapm_widgets(wlist, wi, w) {
1787 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1788 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1790 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1796 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1797 * they're changing state.
1799 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1801 struct snd_soc_dapm_context *d = data;
1804 /* If we're off and we're not supposed to go into STANDBY */
1805 if (d->bias_level == SND_SOC_BIAS_OFF &&
1806 d->target_bias_level != SND_SOC_BIAS_OFF) {
1807 if (d->dev && cookie)
1808 pm_runtime_get_sync(d->dev);
1810 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1813 "ASoC: Failed to turn on bias: %d\n", ret);
1816 /* Prepare for a transition to ON or away from ON */
1817 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1818 d->bias_level != SND_SOC_BIAS_ON) ||
1819 (d->target_bias_level != SND_SOC_BIAS_ON &&
1820 d->bias_level == SND_SOC_BIAS_ON)) {
1821 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1824 "ASoC: Failed to prepare bias: %d\n", ret);
1828 /* Async callback run prior to DAPM sequences - brings to their final
1831 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1833 struct snd_soc_dapm_context *d = data;
1836 /* If we just powered the last thing off drop to standby bias */
1837 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1838 (d->target_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_STANDBY);
1842 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1846 /* If we're in standby and can support bias off then do that */
1847 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1848 d->target_bias_level == SND_SOC_BIAS_OFF) {
1849 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1851 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1854 if (d->dev && cookie)
1855 pm_runtime_put(d->dev);
1858 /* If we just powered up then move to active bias */
1859 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1860 d->target_bias_level == SND_SOC_BIAS_ON) {
1861 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1863 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1868 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1869 bool power, bool connect)
1871 /* If a connection is being made or broken then that update
1872 * will have marked the peer dirty, otherwise the widgets are
1873 * not connected and this update has no impact. */
1877 /* If the peer is already in the state we're moving to then we
1878 * won't have an impact on it. */
1879 if (power != peer->power)
1880 dapm_mark_dirty(peer, "peer state change");
1883 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1884 struct list_head *up_list,
1885 struct list_head *down_list)
1887 struct snd_soc_dapm_path *path;
1891 case snd_soc_dapm_pre:
1894 case snd_soc_dapm_post:
1901 power = dapm_widget_power_check(w);
1903 if (w->power == power)
1906 trace_snd_soc_dapm_widget_power(w, power);
1909 * If we changed our power state perhaps our neigbours
1912 snd_soc_dapm_widget_for_each_source_path(w, path)
1913 dapm_widget_set_peer_power(path->source, power, path->connect);
1916 * Supplies can't affect their outputs, only their inputs
1919 snd_soc_dapm_widget_for_each_sink_path(w, path)
1920 dapm_widget_set_peer_power(path->sink, power, path->connect);
1924 dapm_seq_insert(w, up_list, true);
1926 dapm_seq_insert(w, down_list, false);
1929 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1931 if (dapm->idle_bias_off)
1934 switch (snd_power_get_state(dapm->card->snd_card)) {
1935 case SNDRV_CTL_POWER_D3hot:
1936 case SNDRV_CTL_POWER_D3cold:
1937 return dapm->suspend_bias_off;
1946 * Scan each dapm widget for complete audio path.
1947 * A complete path is a route that has valid endpoints i.e.:-
1949 * o DAC to output pin.
1950 * o Input pin to ADC.
1951 * o Input pin to Output pin (bypass, sidetone)
1952 * o DAC to ADC (loopback).
1954 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1956 struct snd_soc_dapm_widget *w;
1957 struct snd_soc_dapm_context *d;
1959 LIST_HEAD(down_list);
1960 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1961 enum snd_soc_bias_level bias;
1964 snd_soc_dapm_mutex_assert_held(card);
1966 trace_snd_soc_dapm_start(card, event);
1968 for_each_card_dapms(card, d) {
1969 if (dapm_idle_bias_off(d))
1970 d->target_bias_level = SND_SOC_BIAS_OFF;
1972 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1977 /* Check which widgets we need to power and store them in
1978 * lists indicating if they should be powered up or down. We
1979 * only check widgets that have been flagged as dirty but note
1980 * that new widgets may be added to the dirty list while we
1983 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1984 dapm_power_one_widget(w, &up_list, &down_list);
1987 for_each_card_widgets(card, w) {
1989 case snd_soc_dapm_pre:
1990 case snd_soc_dapm_post:
1991 /* These widgets always need to be powered */
1994 list_del_init(&w->dirty);
2001 /* Supplies and micbiases only bring the
2002 * context up to STANDBY as unless something
2003 * else is active and passing audio they
2004 * generally don't require full power. Signal
2005 * generators are virtual pins and have no
2006 * power impact themselves.
2009 case snd_soc_dapm_siggen:
2010 case snd_soc_dapm_vmid:
2012 case snd_soc_dapm_supply:
2013 case snd_soc_dapm_regulator_supply:
2014 case snd_soc_dapm_pinctrl:
2015 case snd_soc_dapm_clock_supply:
2016 case snd_soc_dapm_micbias:
2017 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2018 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2021 d->target_bias_level = SND_SOC_BIAS_ON;
2028 /* Force all contexts in the card to the same bias state if
2029 * they're not ground referenced.
2031 bias = SND_SOC_BIAS_OFF;
2032 for_each_card_dapms(card, d)
2033 if (d->target_bias_level > bias)
2034 bias = d->target_bias_level;
2035 for_each_card_dapms(card, d)
2036 if (!dapm_idle_bias_off(d))
2037 d->target_bias_level = bias;
2039 trace_snd_soc_dapm_walk_done(card);
2041 /* Run card bias changes at first */
2042 dapm_pre_sequence_async(&card->dapm, 0);
2043 /* Run other bias changes in parallel */
2044 for_each_card_dapms(card, d) {
2045 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2046 async_schedule_domain(dapm_pre_sequence_async, d,
2049 async_synchronize_full_domain(&async_domain);
2051 list_for_each_entry(w, &down_list, power_list) {
2052 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2055 list_for_each_entry(w, &up_list, power_list) {
2056 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2059 /* Power down widgets first; try to avoid amplifying pops. */
2060 dapm_seq_run(card, &down_list, event, false);
2062 dapm_widget_update(card);
2065 dapm_seq_run(card, &up_list, event, true);
2067 /* Run all the bias changes in parallel */
2068 for_each_card_dapms(card, d) {
2069 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2070 async_schedule_domain(dapm_post_sequence_async, d,
2073 async_synchronize_full_domain(&async_domain);
2074 /* Run card bias changes at last */
2075 dapm_post_sequence_async(&card->dapm, 0);
2077 /* do we need to notify any clients that DAPM event is complete */
2078 for_each_card_dapms(card, d) {
2082 ret = snd_soc_component_stream_event(d->component, event);
2087 pop_dbg(card->dev, card->pop_time,
2088 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2089 pop_wait(card->pop_time);
2091 trace_snd_soc_dapm_done(card, event);
2096 #ifdef CONFIG_DEBUG_FS
2097 static ssize_t dapm_widget_power_read_file(struct file *file,
2098 char __user *user_buf,
2099 size_t count, loff_t *ppos)
2101 struct snd_soc_dapm_widget *w = file->private_data;
2102 enum snd_soc_dapm_direction dir, rdir;
2106 struct snd_soc_dapm_path *p = NULL;
2108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2112 snd_soc_dapm_mutex_lock_root(w->dapm);
2114 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2119 in = is_connected_input_ep(w, NULL, NULL);
2120 out = is_connected_output_ep(w, NULL, NULL);
2123 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2124 w->name, w->power ? "On" : "Off",
2125 w->force ? " (forced)" : "", in, out);
2128 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2129 " - R%d(0x%x) mask 0x%x",
2130 w->reg, w->reg, w->mask << w->shift);
2132 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2135 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2137 w->active ? "active" : "inactive");
2139 snd_soc_dapm_for_each_direction(dir) {
2140 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2141 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2142 if (p->connected && !p->connected(p->source, p->sink))
2148 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2149 " %s \"%s\" \"%s\"\n",
2150 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2151 p->name ? p->name : "static",
2152 p->node[rdir]->name);
2156 snd_soc_dapm_mutex_unlock(w->dapm);
2158 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2164 static const struct file_operations dapm_widget_power_fops = {
2165 .open = simple_open,
2166 .read = dapm_widget_power_read_file,
2167 .llseek = default_llseek,
2170 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2171 size_t count, loff_t *ppos)
2173 struct snd_soc_dapm_context *dapm = file->private_data;
2176 switch (dapm->bias_level) {
2177 case SND_SOC_BIAS_ON:
2180 case SND_SOC_BIAS_PREPARE:
2181 level = "Prepare\n";
2183 case SND_SOC_BIAS_STANDBY:
2184 level = "Standby\n";
2186 case SND_SOC_BIAS_OFF:
2190 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2191 level = "Unknown\n";
2195 return simple_read_from_buffer(user_buf, count, ppos, level,
2199 static const struct file_operations dapm_bias_fops = {
2200 .open = simple_open,
2201 .read = dapm_bias_read_file,
2202 .llseek = default_llseek,
2205 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2206 struct dentry *parent)
2208 if (!parent || IS_ERR(parent))
2211 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2213 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2217 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2219 struct snd_soc_dapm_context *dapm = w->dapm;
2221 if (!dapm->debugfs_dapm || !w->name)
2224 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2225 &dapm_widget_power_fops);
2228 static void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
2230 struct snd_soc_dapm_context *dapm = w->dapm;
2232 if (!dapm->debugfs_dapm || !w->name)
2235 debugfs_lookup_and_remove(w->name, dapm->debugfs_dapm);
2238 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2240 debugfs_remove_recursive(dapm->debugfs_dapm);
2241 dapm->debugfs_dapm = NULL;
2245 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2246 struct dentry *parent)
2250 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2254 static inline void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
2258 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2265 * soc_dapm_connect_path() - Connects or disconnects a path
2266 * @path: The path to update
2267 * @connect: The new connect state of the path. True if the path is connected,
2268 * false if it is disconnected.
2269 * @reason: The reason why the path changed (for debugging only)
2271 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2272 bool connect, const char *reason)
2274 if (path->connect == connect)
2277 path->connect = connect;
2278 dapm_mark_dirty(path->source, reason);
2279 dapm_mark_dirty(path->sink, reason);
2280 dapm_path_invalidate(path);
2283 /* test and update the power status of a mux widget */
2284 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2285 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2287 struct snd_soc_dapm_path *path;
2291 snd_soc_dapm_mutex_assert_held(card);
2293 /* find dapm widget path assoc with kcontrol */
2294 dapm_kcontrol_for_each_path(path, kcontrol) {
2296 /* we now need to match the string in the enum to the path */
2297 if (e && !(strcmp(path->name, e->texts[mux])))
2302 soc_dapm_connect_path(path, connect, "mux update");
2306 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2311 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2312 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2313 struct snd_soc_dapm_update *update)
2315 struct snd_soc_card *card = dapm->card;
2318 snd_soc_dapm_mutex_lock(card);
2319 card->update = update;
2320 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2321 card->update = NULL;
2322 snd_soc_dapm_mutex_unlock(card);
2324 snd_soc_dpcm_runtime_update(card);
2327 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2329 /* test and update the power status of a mixer or switch widget */
2330 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2331 struct snd_kcontrol *kcontrol,
2332 int connect, int rconnect)
2334 struct snd_soc_dapm_path *path;
2337 snd_soc_dapm_mutex_assert_held(card);
2339 /* find dapm widget path assoc with kcontrol */
2340 dapm_kcontrol_for_each_path(path, kcontrol) {
2342 * Ideally this function should support any number of
2343 * paths and channels. But since kcontrols only come
2344 * in mono and stereo variants, we are limited to 2
2347 * The following code assumes for stereo controls the
2348 * first path (when 'found == 0') is the left channel,
2349 * and all remaining paths (when 'found == 1') are the
2352 * A stereo control is signified by a valid 'rconnect'
2353 * value, either 0 for unconnected, or >= 0 for connected.
2354 * This is chosen instead of using snd_soc_volsw_is_stereo,
2355 * so that the behavior of snd_soc_dapm_mixer_update_power
2356 * doesn't change even when the kcontrol passed in is
2359 * It passes 'connect' as the path connect status for
2360 * the left channel, and 'rconnect' for the right
2363 if (found && rconnect >= 0)
2364 soc_dapm_connect_path(path, rconnect, "mixer update");
2366 soc_dapm_connect_path(path, connect, "mixer update");
2371 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2376 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2377 struct snd_kcontrol *kcontrol, int connect,
2378 struct snd_soc_dapm_update *update)
2380 struct snd_soc_card *card = dapm->card;
2383 snd_soc_dapm_mutex_lock(card);
2384 card->update = update;
2385 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2386 card->update = NULL;
2387 snd_soc_dapm_mutex_unlock(card);
2389 snd_soc_dpcm_runtime_update(card);
2392 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2394 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2395 char *buf, int count)
2397 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2398 struct snd_soc_dapm_widget *w;
2399 char *state = "not set";
2401 /* card won't be set for the dummy component, as a spot fix
2402 * we're checking for that case specifically here but in future
2403 * we will ensure that the dummy component looks like others.
2408 for_each_card_widgets(cmpnt->card, w) {
2409 if (w->dapm != dapm)
2412 /* only display widgets that burn power */
2414 case snd_soc_dapm_hp:
2415 case snd_soc_dapm_mic:
2416 case snd_soc_dapm_spk:
2417 case snd_soc_dapm_line:
2418 case snd_soc_dapm_micbias:
2419 case snd_soc_dapm_dac:
2420 case snd_soc_dapm_adc:
2421 case snd_soc_dapm_pga:
2422 case snd_soc_dapm_effect:
2423 case snd_soc_dapm_out_drv:
2424 case snd_soc_dapm_mixer:
2425 case snd_soc_dapm_mixer_named_ctl:
2426 case snd_soc_dapm_supply:
2427 case snd_soc_dapm_regulator_supply:
2428 case snd_soc_dapm_pinctrl:
2429 case snd_soc_dapm_clock_supply:
2431 count += sysfs_emit_at(buf, count, "%s: %s\n",
2432 w->name, w->power ? "On":"Off");
2439 switch (snd_soc_dapm_get_bias_level(dapm)) {
2440 case SND_SOC_BIAS_ON:
2443 case SND_SOC_BIAS_PREPARE:
2446 case SND_SOC_BIAS_STANDBY:
2449 case SND_SOC_BIAS_OFF:
2453 count += sysfs_emit_at(buf, count, "PM State: %s\n", state);
2458 /* show dapm widget status in sys fs */
2459 static ssize_t dapm_widget_show(struct device *dev,
2460 struct device_attribute *attr, char *buf)
2462 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2463 struct snd_soc_dai *codec_dai;
2466 snd_soc_dapm_mutex_lock_root(rtd->card);
2468 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2469 struct snd_soc_component *cmpnt = codec_dai->component;
2471 count = dapm_widget_show_component(cmpnt, buf, count);
2474 snd_soc_dapm_mutex_unlock(rtd->card);
2479 static DEVICE_ATTR_RO(dapm_widget);
2481 struct attribute *soc_dapm_dev_attrs[] = {
2482 &dev_attr_dapm_widget.attr,
2486 static void dapm_free_path(struct snd_soc_dapm_path *path)
2488 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2489 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2490 list_del(&path->list_kcontrol);
2491 list_del(&path->list);
2496 * snd_soc_dapm_free_widget - Free specified widget
2497 * @w: widget to free
2499 * Removes widget from all paths and frees memory occupied by it.
2501 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2503 struct snd_soc_dapm_path *p, *next_p;
2504 enum snd_soc_dapm_direction dir;
2510 list_del(&w->dirty);
2512 * remove source and sink paths associated to this widget.
2513 * While removing the path, remove reference to it from both
2514 * source and sink widgets so that path is removed only once.
2516 snd_soc_dapm_for_each_direction(dir) {
2517 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2521 dapm_debugfs_free_widget(w);
2523 kfree(w->kcontrols);
2524 kfree_const(w->name);
2525 kfree_const(w->sname);
2528 EXPORT_SYMBOL_GPL(snd_soc_dapm_free_widget);
2530 /* free all dapm widgets and resources */
2531 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2533 struct snd_soc_dapm_widget *w, *next_w;
2535 for_each_card_widgets_safe(dapm->card, w, next_w) {
2536 if (w->dapm != dapm)
2538 snd_soc_dapm_free_widget(w);
2541 dapm->wcache_sink = NULL;
2542 dapm->wcache_source = NULL;
2545 static struct snd_soc_dapm_widget *dapm_find_widget(
2546 struct snd_soc_dapm_context *dapm, const char *pin,
2547 bool search_other_contexts)
2549 struct snd_soc_dapm_widget *w;
2550 struct snd_soc_dapm_widget *fallback = NULL;
2551 char prefixed_pin[80];
2552 const char *pin_name;
2553 const char *prefix = soc_dapm_prefix(dapm);
2556 snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s",
2558 pin_name = prefixed_pin;
2563 for_each_card_widgets(dapm->card, w) {
2564 if (!strcmp(w->name, pin_name)) {
2565 if (w->dapm == dapm)
2572 if (search_other_contexts)
2579 * set the DAPM pin status:
2580 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2581 * error code; called from kcontrol put callback
2583 static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2584 const char *pin, int status)
2586 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2589 dapm_assert_locked(dapm);
2592 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2596 if (w->connected != status) {
2597 dapm_mark_dirty(w, "pin configuration");
2598 dapm_widget_invalidate_input_paths(w);
2599 dapm_widget_invalidate_output_paths(w);
2603 w->connected = status;
2611 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2612 * called from several API functions below
2614 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2615 const char *pin, int status)
2617 int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2619 return ret < 0 ? ret : 0;
2623 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2624 * @dapm: DAPM context
2626 * Walks all dapm audio paths and powers widgets according to their
2627 * stream or path usage.
2629 * Requires external locking.
2631 * Returns 0 for success.
2633 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2636 * Suppress early reports (eg, jacks syncing their state) to avoid
2637 * silly DAPM runs during card startup.
2639 if (!snd_soc_card_is_instantiated(dapm->card))
2642 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2644 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2647 * snd_soc_dapm_sync - scan and power dapm paths
2648 * @dapm: DAPM context
2650 * Walks all dapm audio paths and powers widgets according to their
2651 * stream or path usage.
2653 * Returns 0 for success.
2655 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2659 snd_soc_dapm_mutex_lock(dapm);
2660 ret = snd_soc_dapm_sync_unlocked(dapm);
2661 snd_soc_dapm_mutex_unlock(dapm);
2664 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2666 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2667 struct snd_soc_dapm_widget *w,
2671 case snd_soc_dapm_aif_out:
2672 case snd_soc_dapm_aif_in:
2678 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2679 w->channel < channels ? "Connecting" : "Disconnecting",
2680 p->source->name, p->sink->name);
2682 if (w->channel < channels)
2683 soc_dapm_connect_path(p, true, "dai update");
2685 soc_dapm_connect_path(p, false, "dai update");
2690 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2691 struct snd_pcm_hw_params *params,
2692 struct snd_soc_dai *dai)
2694 int dir = substream->stream;
2695 int channels = params_channels(params);
2696 struct snd_soc_dapm_path *p;
2697 struct snd_soc_dapm_widget *w;
2700 w = snd_soc_dai_get_widget(dai, dir);
2705 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2706 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2708 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2709 ret = dapm_update_dai_chan(p, p->sink, channels);
2714 snd_soc_dapm_widget_for_each_source_path(w, p) {
2715 ret = dapm_update_dai_chan(p, p->source, channels);
2723 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2724 struct snd_pcm_hw_params *params,
2725 struct snd_soc_dai *dai)
2727 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
2730 snd_soc_dapm_mutex_lock(rtd->card);
2731 ret = dapm_update_dai_unlocked(substream, params, dai);
2732 snd_soc_dapm_mutex_unlock(rtd->card);
2736 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2738 int snd_soc_dapm_widget_name_cmp(struct snd_soc_dapm_widget *widget, const char *s)
2740 struct snd_soc_component *component = snd_soc_dapm_to_component(widget->dapm);
2741 const char *wname = widget->name;
2743 if (component->name_prefix)
2744 wname += strlen(component->name_prefix) + 1; /* plus space */
2746 return strcmp(wname, s);
2748 EXPORT_SYMBOL_GPL(snd_soc_dapm_widget_name_cmp);
2751 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2752 * @w: The widget for which to update the flags
2754 * Some widgets have a dynamic category which depends on which neighbors they
2755 * are connected to. This function update the category for these widgets.
2757 * This function must be called whenever a path is added or removed to a widget.
2759 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2761 enum snd_soc_dapm_direction dir;
2762 struct snd_soc_dapm_path *p;
2766 case snd_soc_dapm_input:
2767 /* On a fully routed card an input is never a source */
2768 if (w->dapm->card->fully_routed)
2770 ep = SND_SOC_DAPM_EP_SOURCE;
2771 snd_soc_dapm_widget_for_each_source_path(w, p) {
2772 if (p->source->id == snd_soc_dapm_micbias ||
2773 p->source->id == snd_soc_dapm_mic ||
2774 p->source->id == snd_soc_dapm_line ||
2775 p->source->id == snd_soc_dapm_output) {
2781 case snd_soc_dapm_output:
2782 /* On a fully routed card a output is never a sink */
2783 if (w->dapm->card->fully_routed)
2785 ep = SND_SOC_DAPM_EP_SINK;
2786 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2787 if (p->sink->id == snd_soc_dapm_spk ||
2788 p->sink->id == snd_soc_dapm_hp ||
2789 p->sink->id == snd_soc_dapm_line ||
2790 p->sink->id == snd_soc_dapm_input) {
2796 case snd_soc_dapm_line:
2798 snd_soc_dapm_for_each_direction(dir) {
2799 if (!list_empty(&w->edges[dir]))
2800 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2810 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2811 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2812 const char *control)
2814 bool dynamic_source = false;
2815 bool dynamic_sink = false;
2820 switch (source->id) {
2821 case snd_soc_dapm_demux:
2822 dynamic_source = true;
2829 case snd_soc_dapm_mux:
2830 case snd_soc_dapm_switch:
2831 case snd_soc_dapm_mixer:
2832 case snd_soc_dapm_mixer_named_ctl:
2833 dynamic_sink = true;
2839 if (dynamic_source && dynamic_sink) {
2841 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2842 source->name, control, sink->name);
2844 } else if (!dynamic_source && !dynamic_sink) {
2846 "Control not supported for path %s -> [%s] -> %s\n",
2847 source->name, control, sink->name);
2854 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2855 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2856 const char *control,
2857 int (*connected)(struct snd_soc_dapm_widget *source,
2858 struct snd_soc_dapm_widget *sink))
2860 enum snd_soc_dapm_direction dir;
2861 struct snd_soc_dapm_path *path;
2864 if (wsink->is_supply && !wsource->is_supply) {
2866 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2867 wsource->name, wsink->name);
2871 if (connected && !wsource->is_supply) {
2873 "connected() callback only supported for supply widgets (%s -> %s)\n",
2874 wsource->name, wsink->name);
2878 if (wsource->is_supply && control) {
2880 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2881 wsource->name, control, wsink->name);
2885 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2889 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2893 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2894 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2896 path->connected = connected;
2897 INIT_LIST_HEAD(&path->list);
2898 INIT_LIST_HEAD(&path->list_kcontrol);
2900 if (wsource->is_supply || wsink->is_supply)
2901 path->is_supply = 1;
2903 /* connect static paths */
2904 if (control == NULL) {
2907 switch (wsource->id) {
2908 case snd_soc_dapm_demux:
2909 ret = dapm_connect_mux(dapm, path, control, wsource);
2917 switch (wsink->id) {
2918 case snd_soc_dapm_mux:
2919 ret = dapm_connect_mux(dapm, path, control, wsink);
2923 case snd_soc_dapm_switch:
2924 case snd_soc_dapm_mixer:
2925 case snd_soc_dapm_mixer_named_ctl:
2926 ret = dapm_connect_mixer(dapm, path, control);
2935 list_add(&path->list, &dapm->card->paths);
2937 snd_soc_dapm_for_each_direction(dir)
2938 list_add(&path->list_node[dir], &path->node[dir]->edges[dir]);
2940 snd_soc_dapm_for_each_direction(dir) {
2941 dapm_update_widget_flags(path->node[dir]);
2942 dapm_mark_dirty(path->node[dir], "Route added");
2945 if (snd_soc_card_is_instantiated(dapm->card) && path->connect)
2946 dapm_path_invalidate(path);
2954 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2955 const struct snd_soc_dapm_route *route)
2957 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2958 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2961 char prefixed_sink[80];
2962 char prefixed_source[80];
2964 unsigned int sink_ref = 0;
2965 unsigned int source_ref = 0;
2968 prefix = soc_dapm_prefix(dapm);
2970 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2971 prefix, route->sink);
2972 sink = prefixed_sink;
2973 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2974 prefix, route->source);
2975 source = prefixed_source;
2978 source = route->source;
2981 wsource = dapm_wcache_lookup(dapm->wcache_source, source);
2982 wsink = dapm_wcache_lookup(dapm->wcache_sink, sink);
2984 if (wsink && wsource)
2988 * find src and dest widgets over all widgets but favor a widget from
2989 * current DAPM context
2991 for_each_card_widgets(dapm->card, w) {
2992 if (!wsink && !(strcmp(w->name, sink))) {
2994 if (w->dapm == dapm) {
3002 "ASoC: sink widget %s overwritten\n",
3006 if (!wsource && !(strcmp(w->name, source))) {
3008 if (w->dapm == dapm) {
3016 "ASoC: source widget %s overwritten\n",
3020 /* use widget from another DAPM context if not found from this */
3034 dapm->wcache_sink = wsink;
3035 dapm->wcache_source = wsource;
3037 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
3041 dev_err(dapm->dev, "ASoC: Failed to add route %s%s -%s%s%s> %s%s\n",
3042 source, !wsource ? "(*)" : "",
3043 !route->control ? "" : "> [",
3044 !route->control ? "" : route->control,
3045 !route->control ? "" : "] -",
3046 sink, !wsink ? "(*)" : "");
3050 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3051 const struct snd_soc_dapm_route *route)
3053 struct snd_soc_dapm_path *path, *p;
3056 char prefixed_sink[80];
3057 char prefixed_source[80];
3060 if (route->control) {
3062 "ASoC: Removal of routes with controls not supported\n");
3066 prefix = soc_dapm_prefix(dapm);
3068 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3069 prefix, route->sink);
3070 sink = prefixed_sink;
3071 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3072 prefix, route->source);
3073 source = prefixed_source;
3076 source = route->source;
3080 list_for_each_entry(p, &dapm->card->paths, list) {
3081 if (strcmp(p->source->name, source) != 0)
3083 if (strcmp(p->sink->name, sink) != 0)
3090 struct snd_soc_dapm_widget *wsource = path->source;
3091 struct snd_soc_dapm_widget *wsink = path->sink;
3093 dapm_mark_dirty(wsource, "Route removed");
3094 dapm_mark_dirty(wsink, "Route removed");
3096 dapm_path_invalidate(path);
3098 dapm_free_path(path);
3100 /* Update any path related flags */
3101 dapm_update_widget_flags(wsource);
3102 dapm_update_widget_flags(wsink);
3104 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3112 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3113 * @dapm: DAPM context
3114 * @route: audio routes
3115 * @num: number of routes
3117 * Connects 2 dapm widgets together via a named audio path. The sink is
3118 * the widget receiving the audio signal, whilst the source is the sender
3119 * of the audio signal.
3121 * Returns 0 for success else error. On error all resources can be freed
3122 * with a call to snd_soc_card_free().
3124 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3125 const struct snd_soc_dapm_route *route, int num)
3129 snd_soc_dapm_mutex_lock(dapm);
3130 for (i = 0; i < num; i++) {
3131 int r = snd_soc_dapm_add_route(dapm, route);
3136 snd_soc_dapm_mutex_unlock(dapm);
3140 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3143 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3144 * @dapm: DAPM context
3145 * @route: audio routes
3146 * @num: number of routes
3148 * Removes routes from the DAPM context.
3150 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3151 const struct snd_soc_dapm_route *route, int num)
3155 snd_soc_dapm_mutex_lock(dapm);
3156 for (i = 0; i < num; i++) {
3157 snd_soc_dapm_del_route(dapm, route);
3160 snd_soc_dapm_mutex_unlock(dapm);
3164 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3166 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3167 const struct snd_soc_dapm_route *route)
3169 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3172 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3175 struct snd_soc_dapm_path *path;
3179 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3185 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3190 if (route->control || route->connected)
3191 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3192 route->source, route->sink);
3194 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3195 if (path->sink == sink) {
3202 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3203 route->source, route->sink);
3205 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3206 count, route->source, route->sink);
3212 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3213 * @dapm: DAPM context
3214 * @route: audio routes
3215 * @num: number of routes
3217 * Mark existing routes matching those specified in the passed array
3218 * as being weak, meaning that they are ignored for the purpose of
3219 * power decisions. The main intended use case is for sidetone paths
3220 * which couple audio between other independent paths if they are both
3221 * active in order to make the combination work better at the user
3222 * level but which aren't intended to be "used".
3224 * Note that CODEC drivers should not use this as sidetone type paths
3225 * can frequently also be used as bypass paths.
3227 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3228 const struct snd_soc_dapm_route *route, int num)
3233 snd_soc_dapm_mutex_lock_root(dapm);
3234 for (i = 0; i < num; i++) {
3235 int err = snd_soc_dapm_weak_route(dapm, route);
3240 snd_soc_dapm_mutex_unlock(dapm);
3244 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3247 * snd_soc_dapm_new_widgets - add new dapm widgets
3248 * @card: card to be checked for new dapm widgets
3250 * Checks the codec for any new dapm widgets and creates them if found.
3252 * Returns 0 for success.
3254 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3256 struct snd_soc_dapm_widget *w;
3259 snd_soc_dapm_mutex_lock_root(card);
3261 for_each_card_widgets(card, w)
3266 if (w->num_kcontrols) {
3267 w->kcontrols = kcalloc(w->num_kcontrols,
3268 sizeof(struct snd_kcontrol *),
3270 if (!w->kcontrols) {
3271 snd_soc_dapm_mutex_unlock(card);
3277 case snd_soc_dapm_switch:
3278 case snd_soc_dapm_mixer:
3279 case snd_soc_dapm_mixer_named_ctl:
3282 case snd_soc_dapm_mux:
3283 case snd_soc_dapm_demux:
3286 case snd_soc_dapm_pga:
3287 case snd_soc_dapm_effect:
3288 case snd_soc_dapm_out_drv:
3291 case snd_soc_dapm_dai_link:
3292 dapm_new_dai_link(w);
3298 /* Read the initial power state from the device */
3300 val = soc_dapm_read(w->dapm, w->reg);
3301 val = val >> w->shift;
3303 if (val == w->on_val)
3309 dapm_mark_dirty(w, "new widget");
3310 dapm_debugfs_add_widget(w);
3313 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3314 snd_soc_dapm_mutex_unlock(card);
3317 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3320 * snd_soc_dapm_get_volsw - dapm mixer get callback
3321 * @kcontrol: mixer control
3322 * @ucontrol: control element information
3324 * Callback to get the value of a dapm mixer control.
3326 * Returns 0 for success.
3328 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3329 struct snd_ctl_elem_value *ucontrol)
3331 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3332 struct soc_mixer_control *mc =
3333 (struct soc_mixer_control *)kcontrol->private_value;
3335 unsigned int shift = mc->shift;
3337 unsigned int width = fls(max);
3338 unsigned int mask = (1 << fls(max)) - 1;
3339 unsigned int invert = mc->invert;
3340 unsigned int reg_val, val, rval = 0;
3342 snd_soc_dapm_mutex_lock(dapm);
3343 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3344 reg_val = soc_dapm_read(dapm, reg);
3345 val = (reg_val >> shift) & mask;
3347 if (reg != mc->rreg)
3348 reg_val = soc_dapm_read(dapm, mc->rreg);
3350 if (snd_soc_volsw_is_stereo(mc))
3351 rval = (reg_val >> mc->rshift) & mask;
3353 reg_val = dapm_kcontrol_get_value(kcontrol);
3354 val = reg_val & mask;
3356 if (snd_soc_volsw_is_stereo(mc))
3357 rval = (reg_val >> width) & mask;
3359 snd_soc_dapm_mutex_unlock(dapm);
3362 ucontrol->value.integer.value[0] = max - val;
3364 ucontrol->value.integer.value[0] = val;
3366 if (snd_soc_volsw_is_stereo(mc)) {
3368 ucontrol->value.integer.value[1] = max - rval;
3370 ucontrol->value.integer.value[1] = rval;
3375 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3378 * snd_soc_dapm_put_volsw - dapm mixer set callback
3379 * @kcontrol: mixer control
3380 * @ucontrol: control element information
3382 * Callback to set the value of a dapm mixer control.
3384 * Returns 0 for success.
3386 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3387 struct snd_ctl_elem_value *ucontrol)
3389 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3390 struct snd_soc_card *card = dapm->card;
3391 struct soc_mixer_control *mc =
3392 (struct soc_mixer_control *)kcontrol->private_value;
3394 unsigned int shift = mc->shift;
3396 unsigned int width = fls(max);
3397 unsigned int mask = (1 << width) - 1;
3398 unsigned int invert = mc->invert;
3399 unsigned int val, rval = 0;
3400 int connect, rconnect = -1, change, reg_change = 0;
3401 struct snd_soc_dapm_update update = {};
3404 val = (ucontrol->value.integer.value[0] & mask);
3410 if (snd_soc_volsw_is_stereo(mc)) {
3411 rval = (ucontrol->value.integer.value[1] & mask);
3417 snd_soc_dapm_mutex_lock(card);
3419 /* This assumes field width < (bits in unsigned int / 2) */
3420 if (width > sizeof(unsigned int) * 8 / 2)
3422 "ASoC: control %s field width limit exceeded\n",
3424 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3426 if (reg != SND_SOC_NOPM) {
3428 rval = rval << mc->rshift;
3430 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3432 if (snd_soc_volsw_is_stereo(mc))
3433 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3438 if (change || reg_change) {
3440 if (snd_soc_volsw_is_stereo(mc)) {
3441 update.has_second_set = true;
3442 update.reg2 = mc->rreg;
3443 update.mask2 = mask << mc->rshift;
3446 update.kcontrol = kcontrol;
3448 update.mask = mask << shift;
3450 card->update = &update;
3453 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3456 card->update = NULL;
3459 snd_soc_dapm_mutex_unlock(card);
3462 snd_soc_dpcm_runtime_update(card);
3466 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3469 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3470 * @kcontrol: mixer control
3471 * @ucontrol: control element information
3473 * Callback to get the value of a dapm enumerated double mixer control.
3475 * Returns 0 for success.
3477 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3478 struct snd_ctl_elem_value *ucontrol)
3480 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3481 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3482 unsigned int reg_val, val;
3484 snd_soc_dapm_mutex_lock(dapm);
3485 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3486 reg_val = soc_dapm_read(dapm, e->reg);
3488 reg_val = dapm_kcontrol_get_value(kcontrol);
3490 snd_soc_dapm_mutex_unlock(dapm);
3492 val = (reg_val >> e->shift_l) & e->mask;
3493 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3494 if (e->shift_l != e->shift_r) {
3495 val = (reg_val >> e->shift_r) & e->mask;
3496 val = snd_soc_enum_val_to_item(e, val);
3497 ucontrol->value.enumerated.item[1] = val;
3502 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3505 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3506 * @kcontrol: mixer control
3507 * @ucontrol: control element information
3509 * Callback to set the value of a dapm enumerated double mixer control.
3511 * Returns 0 for success.
3513 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3514 struct snd_ctl_elem_value *ucontrol)
3516 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3517 struct snd_soc_card *card = dapm->card;
3518 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3519 unsigned int *item = ucontrol->value.enumerated.item;
3520 unsigned int val, change, reg_change = 0;
3522 struct snd_soc_dapm_update update = {};
3525 if (item[0] >= e->items)
3528 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3529 mask = e->mask << e->shift_l;
3530 if (e->shift_l != e->shift_r) {
3531 if (item[1] > e->items)
3533 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3534 mask |= e->mask << e->shift_r;
3537 snd_soc_dapm_mutex_lock(card);
3539 change = dapm_kcontrol_set_value(kcontrol, val);
3541 if (e->reg != SND_SOC_NOPM)
3542 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3544 if (change || reg_change) {
3546 update.kcontrol = kcontrol;
3547 update.reg = e->reg;
3550 card->update = &update;
3553 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3555 card->update = NULL;
3558 snd_soc_dapm_mutex_unlock(card);
3561 snd_soc_dpcm_runtime_update(card);
3565 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3568 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3570 * @kcontrol: mixer control
3571 * @uinfo: control element information
3573 * Callback to provide information about a pin switch control.
3575 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3576 struct snd_ctl_elem_info *uinfo)
3578 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3580 uinfo->value.integer.min = 0;
3581 uinfo->value.integer.max = 1;
3585 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3588 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3590 * @kcontrol: mixer control
3593 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3594 struct snd_ctl_elem_value *ucontrol)
3596 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3597 const char *pin = (const char *)kcontrol->private_value;
3599 snd_soc_dapm_mutex_lock(card);
3601 ucontrol->value.integer.value[0] =
3602 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3604 snd_soc_dapm_mutex_unlock(card);
3608 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3611 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3613 * @kcontrol: mixer control
3616 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3617 struct snd_ctl_elem_value *ucontrol)
3619 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3620 const char *pin = (const char *)kcontrol->private_value;
3623 snd_soc_dapm_mutex_lock(card);
3624 ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3625 !!ucontrol->value.integer.value[0]);
3626 snd_soc_dapm_mutex_unlock(card);
3628 snd_soc_dapm_sync(&card->dapm);
3631 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3633 struct snd_soc_dapm_widget *
3634 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3635 const struct snd_soc_dapm_widget *widget)
3637 enum snd_soc_dapm_direction dir;
3638 struct snd_soc_dapm_widget *w;
3641 w = dapm_cnew_widget(widget, soc_dapm_prefix(dapm));
3646 case snd_soc_dapm_regulator_supply:
3647 w->regulator = devm_regulator_get(dapm->dev, widget->name);
3648 if (IS_ERR(w->regulator)) {
3649 ret = PTR_ERR(w->regulator);
3650 goto request_failed;
3653 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3654 ret = regulator_allow_bypass(w->regulator, true);
3657 "ASoC: Failed to bypass %s: %d\n",
3661 case snd_soc_dapm_pinctrl:
3662 w->pinctrl = devm_pinctrl_get(dapm->dev);
3663 if (IS_ERR(w->pinctrl)) {
3664 ret = PTR_ERR(w->pinctrl);
3665 goto request_failed;
3668 /* set to sleep_state when initializing */
3669 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3671 case snd_soc_dapm_clock_supply:
3672 w->clk = devm_clk_get(dapm->dev, widget->name);
3673 if (IS_ERR(w->clk)) {
3674 ret = PTR_ERR(w->clk);
3675 goto request_failed;
3683 case snd_soc_dapm_mic:
3684 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3685 w->power_check = dapm_generic_check_power;
3687 case snd_soc_dapm_input:
3688 if (!dapm->card->fully_routed)
3689 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3690 w->power_check = dapm_generic_check_power;
3692 case snd_soc_dapm_spk:
3693 case snd_soc_dapm_hp:
3694 w->is_ep = SND_SOC_DAPM_EP_SINK;
3695 w->power_check = dapm_generic_check_power;
3697 case snd_soc_dapm_output:
3698 if (!dapm->card->fully_routed)
3699 w->is_ep = SND_SOC_DAPM_EP_SINK;
3700 w->power_check = dapm_generic_check_power;
3702 case snd_soc_dapm_vmid:
3703 case snd_soc_dapm_siggen:
3704 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3705 w->power_check = dapm_always_on_check_power;
3707 case snd_soc_dapm_sink:
3708 w->is_ep = SND_SOC_DAPM_EP_SINK;
3709 w->power_check = dapm_always_on_check_power;
3712 case snd_soc_dapm_mux:
3713 case snd_soc_dapm_demux:
3714 case snd_soc_dapm_switch:
3715 case snd_soc_dapm_mixer:
3716 case snd_soc_dapm_mixer_named_ctl:
3717 case snd_soc_dapm_adc:
3718 case snd_soc_dapm_aif_out:
3719 case snd_soc_dapm_dac:
3720 case snd_soc_dapm_aif_in:
3721 case snd_soc_dapm_pga:
3722 case snd_soc_dapm_buffer:
3723 case snd_soc_dapm_scheduler:
3724 case snd_soc_dapm_effect:
3725 case snd_soc_dapm_src:
3726 case snd_soc_dapm_asrc:
3727 case snd_soc_dapm_encoder:
3728 case snd_soc_dapm_decoder:
3729 case snd_soc_dapm_out_drv:
3730 case snd_soc_dapm_micbias:
3731 case snd_soc_dapm_line:
3732 case snd_soc_dapm_dai_link:
3733 case snd_soc_dapm_dai_out:
3734 case snd_soc_dapm_dai_in:
3735 w->power_check = dapm_generic_check_power;
3737 case snd_soc_dapm_supply:
3738 case snd_soc_dapm_regulator_supply:
3739 case snd_soc_dapm_pinctrl:
3740 case snd_soc_dapm_clock_supply:
3741 case snd_soc_dapm_kcontrol:
3743 w->power_check = dapm_supply_check_power;
3746 w->power_check = dapm_always_on_check_power;
3751 INIT_LIST_HEAD(&w->list);
3752 INIT_LIST_HEAD(&w->dirty);
3753 /* see for_each_card_widgets */
3754 list_add_tail(&w->list, &dapm->card->widgets);
3756 snd_soc_dapm_for_each_direction(dir) {
3757 INIT_LIST_HEAD(&w->edges[dir]);
3758 w->endpoints[dir] = -1;
3761 /* machine layer sets up unconnected pins and insertions */
3766 dev_err_probe(dapm->dev, ret, "ASoC: Failed to request %s\n",
3768 kfree_const(w->name);
3769 kfree_const(w->sname);
3772 return ERR_PTR(ret);
3776 * snd_soc_dapm_new_control - create new dapm control
3777 * @dapm: DAPM context
3778 * @widget: widget template
3780 * Creates new DAPM control based upon a template.
3782 * Returns a widget pointer on success or an error pointer on failure
3784 struct snd_soc_dapm_widget *
3785 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3786 const struct snd_soc_dapm_widget *widget)
3788 struct snd_soc_dapm_widget *w;
3790 snd_soc_dapm_mutex_lock(dapm);
3791 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3792 snd_soc_dapm_mutex_unlock(dapm);
3796 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3799 * snd_soc_dapm_new_controls - create new dapm controls
3800 * @dapm: DAPM context
3801 * @widget: widget array
3802 * @num: number of widgets
3804 * Creates new DAPM controls based upon the templates.
3806 * Returns 0 for success else error.
3808 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3809 const struct snd_soc_dapm_widget *widget,
3815 snd_soc_dapm_mutex_lock_root(dapm);
3816 for (i = 0; i < num; i++) {
3817 struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3824 snd_soc_dapm_mutex_unlock(dapm);
3827 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3830 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3831 struct snd_pcm_substream *substream)
3833 struct snd_soc_dapm_path *path;
3834 struct snd_soc_dai *source, *sink;
3835 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
3836 struct snd_pcm_hw_params *params = NULL;
3837 const struct snd_soc_pcm_stream *config = NULL;
3838 struct snd_pcm_runtime *runtime = NULL;
3845 * snd_pcm_hw_params is quite large (608 bytes on arm64) and is
3846 * starting to get a bit excessive for allocation on the stack,
3847 * especially when you're building with some of the KASAN type
3848 * stuff that increases stack usage.
3849 * So, we use kzalloc()/kfree() for params in this function.
3851 params = kzalloc(sizeof(*params), GFP_KERNEL);
3855 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3861 substream->runtime = runtime;
3863 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3864 snd_soc_dapm_widget_for_each_source_path(w, path) {
3865 source = path->source->priv;
3867 ret = snd_soc_dai_startup(source, substream);
3871 snd_soc_dai_activate(source, substream->stream);
3874 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3875 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3876 sink = path->sink->priv;
3878 ret = snd_soc_dai_startup(sink, substream);
3882 snd_soc_dai_activate(sink, substream->stream);
3885 substream->hw_opened = 1;
3888 * Note: getting the config after .startup() gives a chance to
3889 * either party on the link to alter the configuration if
3892 config = rtd->dai_link->c2c_params + rtd->c2c_params_select;
3894 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3899 /* Be a little careful as we don't want to overflow the mask array */
3900 if (!config->formats) {
3901 dev_warn(w->dapm->dev, "ASoC: Invalid format was specified\n");
3907 fmt = ffs(config->formats) - 1;
3909 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3910 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3912 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3914 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3915 = config->channels_min;
3916 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3917 = config->channels_max;
3919 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3920 snd_soc_dapm_widget_for_each_source_path(w, path) {
3921 source = path->source->priv;
3923 ret = snd_soc_dai_hw_params(source, substream, params);
3927 dapm_update_dai_unlocked(substream, params, source);
3930 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3931 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3932 sink = path->sink->priv;
3934 ret = snd_soc_dai_hw_params(sink, substream, params);
3938 dapm_update_dai_unlocked(substream, params, sink);
3941 runtime->format = params_format(params);
3942 runtime->subformat = params_subformat(params);
3943 runtime->channels = params_channels(params);
3944 runtime->rate = params_rate(params);
3947 /* see above NOTE */
3953 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3954 struct snd_kcontrol *kcontrol, int event)
3956 struct snd_soc_dapm_path *path;
3957 struct snd_soc_dai *source, *sink;
3958 struct snd_pcm_substream *substream = w->priv;
3959 int ret = 0, saved_stream = substream->stream;
3961 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3962 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3966 case SND_SOC_DAPM_PRE_PMU:
3967 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
3973 case SND_SOC_DAPM_POST_PMU:
3974 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3975 sink = path->sink->priv;
3977 snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
3982 case SND_SOC_DAPM_PRE_PMD:
3983 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3984 sink = path->sink->priv;
3986 snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
3990 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3991 snd_soc_dapm_widget_for_each_source_path(w, path) {
3992 source = path->source->priv;
3993 snd_soc_dai_hw_free(source, substream, 0);
3996 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3997 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3998 sink = path->sink->priv;
3999 snd_soc_dai_hw_free(sink, substream, 0);
4002 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4003 snd_soc_dapm_widget_for_each_source_path(w, path) {
4004 source = path->source->priv;
4005 snd_soc_dai_deactivate(source, substream->stream);
4006 snd_soc_dai_shutdown(source, substream, 0);
4009 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4010 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4011 sink = path->sink->priv;
4012 snd_soc_dai_deactivate(sink, substream->stream);
4013 snd_soc_dai_shutdown(sink, substream, 0);
4017 case SND_SOC_DAPM_POST_PMD:
4018 kfree(substream->runtime);
4022 WARN(1, "Unknown event %d\n", event);
4027 /* Restore the substream direction */
4028 substream->stream = saved_stream;
4032 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4033 struct snd_ctl_elem_value *ucontrol)
4035 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4036 struct snd_soc_pcm_runtime *rtd = w->priv;
4038 ucontrol->value.enumerated.item[0] = rtd->c2c_params_select;
4043 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4044 struct snd_ctl_elem_value *ucontrol)
4046 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4047 struct snd_soc_pcm_runtime *rtd = w->priv;
4049 /* Can't change the config when widget is already powered */
4053 if (ucontrol->value.enumerated.item[0] == rtd->c2c_params_select)
4056 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_c2c_params)
4059 rtd->c2c_params_select = ucontrol->value.enumerated.item[0];
4065 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4066 unsigned long *private_value,
4068 const char **w_param_text)
4072 devm_kfree(card->dev, (void *)*private_value);
4077 for (count = 0 ; count < num_c2c_params; count++)
4078 devm_kfree(card->dev, (void *)w_param_text[count]);
4079 devm_kfree(card->dev, w_param_text);
4082 static struct snd_kcontrol_new *
4083 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4085 const struct snd_soc_pcm_stream *c2c_params,
4086 int num_c2c_params, const char **w_param_text,
4087 unsigned long *private_value)
4089 struct soc_enum w_param_enum[] = {
4090 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4092 struct snd_kcontrol_new kcontrol_dai_link[] = {
4093 SOC_ENUM_EXT(NULL, w_param_enum[0],
4094 snd_soc_dapm_dai_link_get,
4095 snd_soc_dapm_dai_link_put),
4097 struct snd_kcontrol_new *kcontrol_news;
4098 const struct snd_soc_pcm_stream *config = c2c_params;
4101 for (count = 0 ; count < num_c2c_params; count++) {
4102 if (!config->stream_name) {
4103 dev_warn(card->dapm.dev,
4104 "ASoC: anonymous config %d for dai link %s\n",
4106 w_param_text[count] =
4107 devm_kasprintf(card->dev, GFP_KERNEL,
4108 "Anonymous Configuration %d",
4111 w_param_text[count] = devm_kmemdup(card->dev,
4112 config->stream_name,
4113 strlen(config->stream_name) + 1,
4116 if (!w_param_text[count])
4117 goto outfree_w_param;
4121 w_param_enum[0].items = num_c2c_params;
4122 w_param_enum[0].texts = w_param_text;
4125 (unsigned long) devm_kmemdup(card->dev,
4126 (void *)(kcontrol_dai_link[0].private_value),
4127 sizeof(struct soc_enum), GFP_KERNEL);
4128 if (!*private_value) {
4129 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4131 goto outfree_w_param;
4133 kcontrol_dai_link[0].private_value = *private_value;
4134 /* duplicate kcontrol_dai_link on heap so that memory persists */
4135 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4136 sizeof(struct snd_kcontrol_new),
4138 if (!kcontrol_news) {
4139 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4141 goto outfree_w_param;
4143 return kcontrol_news;
4146 snd_soc_dapm_free_kcontrol(card, private_value, num_c2c_params, w_param_text);
4150 static struct snd_soc_dapm_widget *
4151 snd_soc_dapm_new_dai(struct snd_soc_card *card,
4152 struct snd_pcm_substream *substream,
4155 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
4156 struct snd_soc_dapm_widget template;
4157 struct snd_soc_dapm_widget *w;
4158 const struct snd_kcontrol_new *kcontrol_news;
4160 const char **w_param_text;
4161 unsigned long private_value = 0;
4165 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4166 rtd->dai_link->name, id);
4170 /* allocate memory for control, only in case of multiple configs */
4171 w_param_text = NULL;
4172 kcontrol_news = NULL;
4174 if (rtd->dai_link->num_c2c_params > 1) {
4175 w_param_text = devm_kcalloc(card->dev,
4176 rtd->dai_link->num_c2c_params,
4177 sizeof(char *), GFP_KERNEL);
4182 kcontrol_news = snd_soc_dapm_alloc_kcontrol(card, link_name,
4183 rtd->dai_link->c2c_params,
4184 rtd->dai_link->num_c2c_params,
4185 w_param_text, &private_value);
4190 memset(&template, 0, sizeof(template));
4191 template.reg = SND_SOC_NOPM;
4192 template.id = snd_soc_dapm_dai_link;
4193 template.name = link_name;
4194 template.event = snd_soc_dai_link_event;
4195 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4196 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4197 template.kcontrol_news = kcontrol_news;
4198 template.num_kcontrols = num_kcontrols;
4200 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4202 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4205 goto outfree_kcontrol_news;
4208 w->priv = substream;
4212 outfree_kcontrol_news:
4213 devm_kfree(card->dev, (void *)template.kcontrol_news);
4214 snd_soc_dapm_free_kcontrol(card, &private_value,
4215 rtd->dai_link->num_c2c_params, w_param_text);
4217 devm_kfree(card->dev, link_name);
4219 dev_err(rtd->dev, "ASoC: Failed to create %s-%s widget: %d\n",
4220 rtd->dai_link->name, id, ret);
4221 return ERR_PTR(ret);
4225 * snd_soc_dapm_new_dai_widgets - Create new DAPM widgets
4226 * @dapm: DAPM context
4229 * Returns 0 on success, error code otherwise.
4231 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4232 struct snd_soc_dai *dai)
4234 struct snd_soc_dapm_widget template;
4235 struct snd_soc_dapm_widget *w;
4237 WARN_ON(dapm->dev != dai->dev);
4239 memset(&template, 0, sizeof(template));
4240 template.reg = SND_SOC_NOPM;
4242 if (dai->driver->playback.stream_name) {
4243 template.id = snd_soc_dapm_dai_in;
4244 template.name = dai->driver->playback.stream_name;
4245 template.sname = dai->driver->playback.stream_name;
4247 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4250 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4255 snd_soc_dai_set_widget_playback(dai, w);
4258 if (dai->driver->capture.stream_name) {
4259 template.id = snd_soc_dapm_dai_out;
4260 template.name = dai->driver->capture.stream_name;
4261 template.sname = dai->driver->capture.stream_name;
4263 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4266 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4271 snd_soc_dai_set_widget_capture(dai, w);
4276 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_dai_widgets);
4278 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4280 struct snd_soc_dapm_widget *dai_w, *w;
4281 struct snd_soc_dapm_widget *src, *sink;
4282 struct snd_soc_dai *dai;
4284 /* For each DAI widget... */
4285 for_each_card_widgets(card, dai_w) {
4286 switch (dai_w->id) {
4287 case snd_soc_dapm_dai_in:
4288 case snd_soc_dapm_dai_out:
4294 /* let users know there is no DAI to link */
4296 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4303 /* ...find all widgets with the same stream and link them */
4304 for_each_card_widgets(card, w) {
4305 if (w->dapm != dai_w->dapm)
4309 case snd_soc_dapm_dai_in:
4310 case snd_soc_dapm_dai_out:
4316 if (!w->sname || !strstr(w->sname, dai_w->sname))
4319 if (dai_w->id == snd_soc_dapm_dai_in) {
4326 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4327 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4334 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4335 struct snd_soc_dai *src_dai,
4336 struct snd_soc_dapm_widget *src,
4337 struct snd_soc_dapm_widget *dai,
4338 struct snd_soc_dai *sink_dai,
4339 struct snd_soc_dapm_widget *sink)
4341 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4342 src_dai->component->name, src->name,
4343 sink_dai->component->name, sink->name);
4346 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4350 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4353 static void dapm_connect_dai_pair(struct snd_soc_card *card,
4354 struct snd_soc_pcm_runtime *rtd,
4355 struct snd_soc_dai *codec_dai,
4356 struct snd_soc_dai *cpu_dai)
4358 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4359 struct snd_soc_dapm_widget *codec, *cpu;
4360 struct snd_soc_dai *src_dai[] = { cpu_dai, codec_dai };
4361 struct snd_soc_dai *sink_dai[] = { codec_dai, cpu_dai };
4362 struct snd_soc_dapm_widget **src[] = { &cpu, &codec };
4363 struct snd_soc_dapm_widget **sink[] = { &codec, &cpu };
4364 char *widget_name[] = { "playback", "capture" };
4367 for_each_pcm_streams(stream) {
4368 int stream_cpu, stream_codec;
4370 stream_cpu = snd_soc_get_stream_cpu(dai_link, stream);
4371 stream_codec = stream;
4373 /* connect BE DAI playback if widgets are valid */
4374 cpu = snd_soc_dai_get_widget(cpu_dai, stream_cpu);
4375 codec = snd_soc_dai_get_widget(codec_dai, stream_codec);
4380 /* special handling for [Codec2Codec] */
4381 if (dai_link->c2c_params && !rtd->c2c_widget[stream]) {
4382 struct snd_pcm_substream *substream = rtd->pcm->streams[stream].substream;
4383 struct snd_soc_dapm_widget *dai = snd_soc_dapm_new_dai(card, substream,
4384 widget_name[stream]);
4389 rtd->c2c_widget[stream] = dai;
4392 dapm_connect_dai_routes(&card->dapm, src_dai[stream], *src[stream],
4393 rtd->c2c_widget[stream],
4394 sink_dai[stream], *sink[stream]);
4398 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4401 struct snd_soc_dapm_widget *w;
4403 w = snd_soc_dai_get_widget(dai, stream);
4408 dapm_mark_dirty(w, "stream event");
4410 if (w->id == snd_soc_dapm_dai_in) {
4411 ep = SND_SOC_DAPM_EP_SOURCE;
4412 dapm_widget_invalidate_input_paths(w);
4414 ep = SND_SOC_DAPM_EP_SINK;
4415 dapm_widget_invalidate_output_paths(w);
4419 case SND_SOC_DAPM_STREAM_START:
4423 case SND_SOC_DAPM_STREAM_STOP:
4427 case SND_SOC_DAPM_STREAM_SUSPEND:
4428 case SND_SOC_DAPM_STREAM_RESUME:
4429 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4430 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4436 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4438 struct snd_soc_pcm_runtime *rtd;
4439 struct snd_soc_dai *cpu_dai;
4440 struct snd_soc_dai *codec_dai;
4442 /* for each BE DAI link... */
4443 for_each_card_rtds(card, rtd) {
4444 struct snd_soc_dai_link_ch_map *ch_maps;
4448 * dynamic FE links have no fixed DAI mapping.
4449 * CODEC<->CODEC links have no direct connection.
4451 if (rtd->dai_link->dynamic)
4456 * soc.h :: [dai_link->ch_maps Image sample]
4458 for_each_rtd_ch_maps(rtd, i, ch_maps) {
4459 cpu_dai = snd_soc_rtd_to_cpu(rtd, ch_maps->cpu);
4460 codec_dai = snd_soc_rtd_to_codec(rtd, ch_maps->codec);
4462 dapm_connect_dai_pair(card, rtd, codec_dai, cpu_dai);
4467 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4470 struct snd_soc_dai *dai;
4473 for_each_rtd_dais(rtd, i, dai)
4474 soc_dapm_dai_stream_event(dai, stream, event);
4476 dapm_power_widgets(rtd->card, event);
4480 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4481 * @rtd: PCM runtime data
4482 * @stream: stream name
4483 * @event: stream event
4485 * Sends a stream event to the dapm core. The core then makes any
4486 * necessary widget power changes.
4488 * Returns 0 for success else error.
4490 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4493 struct snd_soc_card *card = rtd->card;
4495 snd_soc_dapm_mutex_lock(card);
4496 soc_dapm_stream_event(rtd, stream, event);
4497 snd_soc_dapm_mutex_unlock(card);
4500 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4502 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4503 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4504 /* powered down playback stream now */
4505 snd_soc_dapm_stream_event(rtd,
4506 SNDRV_PCM_STREAM_PLAYBACK,
4507 SND_SOC_DAPM_STREAM_STOP);
4509 /* start delayed pop wq here for playback streams */
4511 queue_delayed_work(system_power_efficient_wq,
4513 msecs_to_jiffies(rtd->pmdown_time));
4516 /* capture streams can be powered down now */
4517 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4518 SND_SOC_DAPM_STREAM_STOP);
4521 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4524 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4525 * @dapm: DAPM context
4528 * Enables input/output pin and its parents or children widgets iff there is
4529 * a valid audio route and active audio stream.
4531 * Requires external locking.
4533 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4534 * do any widget power switching.
4536 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4539 return snd_soc_dapm_set_pin(dapm, pin, 1);
4541 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4544 * snd_soc_dapm_enable_pin - enable pin.
4545 * @dapm: DAPM context
4548 * Enables input/output pin and its parents or children widgets iff there is
4549 * a valid audio route and active audio stream.
4551 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4552 * do any widget power switching.
4554 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4558 snd_soc_dapm_mutex_lock(dapm);
4560 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4562 snd_soc_dapm_mutex_unlock(dapm);
4566 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4569 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4570 * @dapm: DAPM context
4573 * Enables input/output pin regardless of any other state. This is
4574 * intended for use with microphone bias supplies used in microphone
4577 * Requires external locking.
4579 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4580 * do any widget power switching.
4582 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4585 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4588 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4592 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4593 if (!w->connected) {
4595 * w->force does not affect the number of input or output paths,
4596 * so we only have to recheck if w->connected is changed
4598 dapm_widget_invalidate_input_paths(w);
4599 dapm_widget_invalidate_output_paths(w);
4603 dapm_mark_dirty(w, "force enable");
4607 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4610 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4611 * @dapm: DAPM context
4614 * Enables input/output pin regardless of any other state. This is
4615 * intended for use with microphone bias supplies used in microphone
4618 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4619 * do any widget power switching.
4621 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4626 snd_soc_dapm_mutex_lock(dapm);
4628 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4630 snd_soc_dapm_mutex_unlock(dapm);
4634 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4637 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4638 * @dapm: DAPM context
4641 * Disables input/output pin and its parents or children widgets.
4643 * Requires external locking.
4645 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4646 * do any widget power switching.
4648 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4651 return snd_soc_dapm_set_pin(dapm, pin, 0);
4653 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4656 * snd_soc_dapm_disable_pin - disable pin.
4657 * @dapm: DAPM context
4660 * Disables input/output pin and its parents or children widgets.
4662 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4663 * do any widget power switching.
4665 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4670 snd_soc_dapm_mutex_lock(dapm);
4672 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4674 snd_soc_dapm_mutex_unlock(dapm);
4678 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4681 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4682 * @dapm: DAPM context
4685 * Marks the specified pin as being not connected, disabling it along
4686 * any parent or child widgets. At present this is identical to
4687 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4688 * additional things such as disabling controls which only affect
4689 * paths through the pin.
4691 * Requires external locking.
4693 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4694 * do any widget power switching.
4696 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4699 return snd_soc_dapm_set_pin(dapm, pin, 0);
4701 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4704 * snd_soc_dapm_nc_pin - permanently disable pin.
4705 * @dapm: DAPM context
4708 * Marks the specified pin as being not connected, disabling it along
4709 * any parent or child widgets. At present this is identical to
4710 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4711 * additional things such as disabling controls which only affect
4712 * paths through the pin.
4714 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4715 * do any widget power switching.
4717 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4721 snd_soc_dapm_mutex_lock(dapm);
4723 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4725 snd_soc_dapm_mutex_unlock(dapm);
4729 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4732 * snd_soc_dapm_get_pin_status - get audio pin status
4733 * @dapm: DAPM context
4734 * @pin: audio signal pin endpoint (or start point)
4736 * Get audio pin status - connected or disconnected.
4738 * Returns 1 for connected otherwise 0.
4740 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4743 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4746 return w->connected;
4750 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4753 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4754 * @dapm: DAPM context
4755 * @pin: audio signal pin endpoint (or start point)
4757 * Mark the given endpoint or pin as ignoring suspend. When the
4758 * system is disabled a path between two endpoints flagged as ignoring
4759 * suspend will not be disabled. The path must already be enabled via
4760 * normal means at suspend time, it will not be turned on if it was not
4763 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4766 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4769 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4773 w->ignore_suspend = 1;
4777 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4780 * snd_soc_dapm_free - free dapm resources
4781 * @dapm: DAPM context
4783 * Free all dapm widgets and resources.
4785 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4787 dapm_debugfs_cleanup(dapm);
4788 dapm_free_widgets(dapm);
4789 list_del(&dapm->list);
4791 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4793 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4794 struct snd_soc_card *card,
4795 struct snd_soc_component *component)
4798 dapm->component = component;
4799 dapm->bias_level = SND_SOC_BIAS_OFF;
4802 dapm->dev = component->dev;
4803 dapm->idle_bias_off = !component->driver->idle_bias_on;
4804 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4806 dapm->dev = card->dev;
4809 INIT_LIST_HEAD(&dapm->list);
4810 /* see for_each_card_dapms */
4811 list_add(&dapm->list, &card->dapm_list);
4813 EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4815 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4817 struct snd_soc_card *card = dapm->card;
4818 struct snd_soc_dapm_widget *w;
4819 LIST_HEAD(down_list);
4822 snd_soc_dapm_mutex_lock_root(card);
4824 for_each_card_widgets(dapm->card, w) {
4825 if (w->dapm != dapm)
4828 dapm_seq_insert(w, &down_list, false);
4834 /* If there were no widgets to power down we're already in
4838 if (dapm->bias_level == SND_SOC_BIAS_ON)
4839 snd_soc_dapm_set_bias_level(dapm,
4840 SND_SOC_BIAS_PREPARE);
4841 dapm_seq_run(card, &down_list, 0, false);
4842 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4843 snd_soc_dapm_set_bias_level(dapm,
4844 SND_SOC_BIAS_STANDBY);
4847 snd_soc_dapm_mutex_unlock(card);
4851 * snd_soc_dapm_shutdown - callback for system shutdown
4853 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4855 struct snd_soc_dapm_context *dapm;
4857 for_each_card_dapms(card, dapm) {
4858 if (dapm != &card->dapm) {
4859 soc_dapm_shutdown_dapm(dapm);
4860 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4861 snd_soc_dapm_set_bias_level(dapm,
4866 soc_dapm_shutdown_dapm(&card->dapm);
4867 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4868 snd_soc_dapm_set_bias_level(&card->dapm,
4872 /* Module information */
4873 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4874 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4875 MODULE_LICENSE("GPL");