2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/headphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed power down of audio subsystem to reduce pops between a quick
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/clk.h>
39 #include <linux/slab.h>
40 #include <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44 #include <sound/initval.h>
46 #include <trace/events/asoc.h>
48 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
50 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
51 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
53 #define snd_soc_dapm_for_each_direction(dir) \
54 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
57 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
58 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
60 int (*connected)(struct snd_soc_dapm_widget *source,
61 struct snd_soc_dapm_widget *sink));
63 struct snd_soc_dapm_widget *
64 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
65 const struct snd_soc_dapm_widget *widget);
67 struct snd_soc_dapm_widget *
68 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
69 const struct snd_soc_dapm_widget *widget);
71 /* dapm power sequences - make this per codec in the future */
72 static int dapm_up_seq[] = {
73 [snd_soc_dapm_pre] = 0,
74 [snd_soc_dapm_regulator_supply] = 1,
75 [snd_soc_dapm_clock_supply] = 1,
76 [snd_soc_dapm_supply] = 2,
77 [snd_soc_dapm_micbias] = 3,
78 [snd_soc_dapm_vmid] = 3,
79 [snd_soc_dapm_dai_link] = 2,
80 [snd_soc_dapm_dai_in] = 4,
81 [snd_soc_dapm_dai_out] = 4,
82 [snd_soc_dapm_aif_in] = 4,
83 [snd_soc_dapm_aif_out] = 4,
84 [snd_soc_dapm_mic] = 5,
85 [snd_soc_dapm_siggen] = 5,
86 [snd_soc_dapm_input] = 5,
87 [snd_soc_dapm_output] = 5,
88 [snd_soc_dapm_mux] = 6,
89 [snd_soc_dapm_demux] = 6,
90 [snd_soc_dapm_dac] = 7,
91 [snd_soc_dapm_switch] = 8,
92 [snd_soc_dapm_mixer] = 8,
93 [snd_soc_dapm_mixer_named_ctl] = 8,
94 [snd_soc_dapm_pga] = 9,
95 [snd_soc_dapm_buffer] = 9,
96 [snd_soc_dapm_scheduler] = 9,
97 [snd_soc_dapm_effect] = 9,
98 [snd_soc_dapm_src] = 9,
99 [snd_soc_dapm_asrc] = 9,
100 [snd_soc_dapm_encoder] = 9,
101 [snd_soc_dapm_decoder] = 9,
102 [snd_soc_dapm_adc] = 10,
103 [snd_soc_dapm_out_drv] = 11,
104 [snd_soc_dapm_hp] = 11,
105 [snd_soc_dapm_spk] = 11,
106 [snd_soc_dapm_line] = 11,
107 [snd_soc_dapm_sink] = 11,
108 [snd_soc_dapm_kcontrol] = 12,
109 [snd_soc_dapm_post] = 13,
112 static int dapm_down_seq[] = {
113 [snd_soc_dapm_pre] = 0,
114 [snd_soc_dapm_kcontrol] = 1,
115 [snd_soc_dapm_adc] = 2,
116 [snd_soc_dapm_hp] = 3,
117 [snd_soc_dapm_spk] = 3,
118 [snd_soc_dapm_line] = 3,
119 [snd_soc_dapm_out_drv] = 3,
120 [snd_soc_dapm_sink] = 3,
121 [snd_soc_dapm_pga] = 4,
122 [snd_soc_dapm_buffer] = 4,
123 [snd_soc_dapm_scheduler] = 4,
124 [snd_soc_dapm_effect] = 4,
125 [snd_soc_dapm_src] = 4,
126 [snd_soc_dapm_asrc] = 4,
127 [snd_soc_dapm_encoder] = 4,
128 [snd_soc_dapm_decoder] = 4,
129 [snd_soc_dapm_switch] = 5,
130 [snd_soc_dapm_mixer_named_ctl] = 5,
131 [snd_soc_dapm_mixer] = 5,
132 [snd_soc_dapm_dac] = 6,
133 [snd_soc_dapm_mic] = 7,
134 [snd_soc_dapm_siggen] = 7,
135 [snd_soc_dapm_input] = 7,
136 [snd_soc_dapm_output] = 7,
137 [snd_soc_dapm_micbias] = 8,
138 [snd_soc_dapm_vmid] = 8,
139 [snd_soc_dapm_mux] = 9,
140 [snd_soc_dapm_demux] = 9,
141 [snd_soc_dapm_aif_in] = 10,
142 [snd_soc_dapm_aif_out] = 10,
143 [snd_soc_dapm_dai_in] = 10,
144 [snd_soc_dapm_dai_out] = 10,
145 [snd_soc_dapm_dai_link] = 11,
146 [snd_soc_dapm_supply] = 12,
147 [snd_soc_dapm_clock_supply] = 13,
148 [snd_soc_dapm_regulator_supply] = 13,
149 [snd_soc_dapm_post] = 14,
152 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
154 if (dapm->card && dapm->card->instantiated)
155 lockdep_assert_held(&dapm->card->dapm_mutex);
158 static void pop_wait(u32 pop_time)
161 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
164 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
172 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
177 vsnprintf(buf, PAGE_SIZE, fmt, args);
178 dev_info(dev, "%s", buf);
184 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
186 return !list_empty(&w->dirty);
189 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
191 dapm_assert_locked(w->dapm);
193 if (!dapm_dirty_widget(w)) {
194 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
196 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
201 * Common implementation for dapm_widget_invalidate_input_paths() and
202 * dapm_widget_invalidate_output_paths(). The function is inlined since the
203 * combined size of the two specialized functions is only marginally larger then
204 * the size of the generic function and at the same time the fast path of the
205 * specialized functions is significantly smaller than the generic function.
207 static __always_inline void dapm_widget_invalidate_paths(
208 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
210 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
211 struct snd_soc_dapm_widget *node;
212 struct snd_soc_dapm_path *p;
215 dapm_assert_locked(w->dapm);
217 if (w->endpoints[dir] == -1)
220 list_add_tail(&w->work_list, &list);
221 w->endpoints[dir] = -1;
223 list_for_each_entry(w, &list, work_list) {
224 snd_soc_dapm_widget_for_each_path(w, dir, p) {
225 if (p->is_supply || p->weak || !p->connect)
227 node = p->node[rdir];
228 if (node->endpoints[dir] != -1) {
229 node->endpoints[dir] = -1;
230 list_add_tail(&node->work_list, &list);
237 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
239 * @w: The widget for which to invalidate the cached number of input paths
241 * Resets the cached number of inputs for the specified widget and all widgets
242 * that can be reached via outcoming paths from the widget.
244 * This function must be called if the number of output paths for a widget might
245 * have changed. E.g. if the source state of a widget changes or a path is added
246 * or activated with the widget as the sink.
248 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
250 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
254 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
256 * @w: The widget for which to invalidate the cached number of output paths
258 * Resets the cached number of outputs for the specified widget and all widgets
259 * that can be reached via incoming paths from the widget.
261 * This function must be called if the number of output paths for a widget might
262 * have changed. E.g. if the sink state of a widget changes or a path is added
263 * or activated with the widget as the source.
265 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
267 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
271 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
272 * for the widgets connected to a path
273 * @p: The path to invalidate
275 * Resets the cached number of inputs for the sink of the path and the cached
276 * number of outputs for the source of the path.
278 * This function must be called when a path is added, removed or the connected
281 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
284 * Weak paths or supply paths do not influence the number of input or
285 * output paths of their neighbors.
287 if (p->weak || p->is_supply)
291 * The number of connected endpoints is the sum of the number of
292 * connected endpoints of all neighbors. If a node with 0 connected
293 * endpoints is either connected or disconnected that sum won't change,
294 * so there is no need to re-check the path.
296 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
297 dapm_widget_invalidate_input_paths(p->sink);
298 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
299 dapm_widget_invalidate_output_paths(p->source);
302 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
304 struct snd_soc_dapm_widget *w;
306 mutex_lock(&card->dapm_mutex);
308 list_for_each_entry(w, &card->widgets, list) {
310 dapm_mark_dirty(w, "Rechecking endpoints");
311 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
312 dapm_widget_invalidate_output_paths(w);
313 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
314 dapm_widget_invalidate_input_paths(w);
318 mutex_unlock(&card->dapm_mutex);
320 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
322 /* create a new dapm widget */
323 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
324 const struct snd_soc_dapm_widget *_widget)
326 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
329 struct dapm_kcontrol_data {
331 struct snd_soc_dapm_widget *widget;
332 struct list_head paths;
333 struct snd_soc_dapm_widget_list *wlist;
336 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
337 struct snd_kcontrol *kcontrol, const char *ctrl_name)
339 struct dapm_kcontrol_data *data;
340 struct soc_mixer_control *mc;
345 data = kzalloc(sizeof(*data), GFP_KERNEL);
349 INIT_LIST_HEAD(&data->paths);
351 switch (widget->id) {
352 case snd_soc_dapm_switch:
353 case snd_soc_dapm_mixer:
354 case snd_soc_dapm_mixer_named_ctl:
355 mc = (struct soc_mixer_control *)kcontrol->private_value;
357 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
358 dev_warn(widget->dapm->dev,
359 "ASoC: Unsupported stereo autodisable control '%s'\n",
362 if (mc->autodisable) {
363 struct snd_soc_dapm_widget template;
365 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
372 memset(&template, 0, sizeof(template));
373 template.reg = mc->reg;
374 template.mask = (1 << fls(mc->max)) - 1;
375 template.shift = mc->shift;
377 template.off_val = mc->max;
379 template.off_val = 0;
380 template.on_val = template.off_val;
381 template.id = snd_soc_dapm_kcontrol;
382 template.name = name;
384 data->value = template.on_val;
387 snd_soc_dapm_new_control_unlocked(widget->dapm,
390 if (IS_ERR(data->widget)) {
391 ret = PTR_ERR(data->widget);
400 case snd_soc_dapm_demux:
401 case snd_soc_dapm_mux:
402 e = (struct soc_enum *)kcontrol->private_value;
404 if (e->autodisable) {
405 struct snd_soc_dapm_widget template;
407 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
414 memset(&template, 0, sizeof(template));
415 template.reg = e->reg;
416 template.mask = e->mask;
417 template.shift = e->shift_l;
418 template.off_val = snd_soc_enum_item_to_val(e, 0);
419 template.on_val = template.off_val;
420 template.id = snd_soc_dapm_kcontrol;
421 template.name = name;
423 data->value = template.on_val;
425 data->widget = snd_soc_dapm_new_control_unlocked(
426 widget->dapm, &template);
428 if (IS_ERR(data->widget)) {
429 ret = PTR_ERR(data->widget);
437 snd_soc_dapm_add_path(widget->dapm, data->widget,
445 kcontrol->private_data = data;
454 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
456 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
458 list_del(&data->paths);
463 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
464 const struct snd_kcontrol *kcontrol)
466 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
471 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
472 struct snd_soc_dapm_widget *widget)
474 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
475 struct snd_soc_dapm_widget_list *new_wlist;
479 n = data->wlist->num_widgets + 1;
483 new_wlist = krealloc(data->wlist,
484 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
488 new_wlist->widgets[n - 1] = widget;
489 new_wlist->num_widgets = n;
491 data->wlist = new_wlist;
496 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
497 struct snd_soc_dapm_path *path)
499 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
501 list_add_tail(&path->list_kcontrol, &data->paths);
504 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
506 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
511 return data->widget->power;
514 static struct list_head *dapm_kcontrol_get_path_list(
515 const struct snd_kcontrol *kcontrol)
517 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
522 #define dapm_kcontrol_for_each_path(path, kcontrol) \
523 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
526 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
528 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
532 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
534 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
537 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
539 if (data->value == value)
543 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
544 case snd_soc_dapm_switch:
545 case snd_soc_dapm_mixer:
546 case snd_soc_dapm_mixer_named_ctl:
547 data->widget->on_val = value & data->widget->mask;
549 case snd_soc_dapm_demux:
550 case snd_soc_dapm_mux:
551 data->widget->on_val = value >> data->widget->shift;
554 data->widget->on_val = value;
565 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
567 * @kcontrol: The kcontrol
569 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
570 struct snd_kcontrol *kcontrol)
572 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
574 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
577 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
579 * @kcontrol: The kcontrol
581 * Note: This function must only be used on kcontrols that are known to have
582 * been registered for a CODEC. Otherwise the behaviour is undefined.
584 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
585 struct snd_kcontrol *kcontrol)
587 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
589 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
591 static void dapm_reset(struct snd_soc_card *card)
593 struct snd_soc_dapm_widget *w;
595 lockdep_assert_held(&card->dapm_mutex);
597 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
599 list_for_each_entry(w, &card->widgets, list) {
600 w->new_power = w->power;
601 w->power_checked = false;
605 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
607 if (!dapm->component)
609 return dapm->component->name_prefix;
612 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
615 if (!dapm->component)
617 return snd_soc_component_read(dapm->component, reg, value);
620 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
621 int reg, unsigned int mask, unsigned int value)
623 if (!dapm->component)
625 return snd_soc_component_update_bits(dapm->component, reg,
629 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
630 int reg, unsigned int mask, unsigned int value)
632 if (!dapm->component)
634 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
637 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
640 snd_soc_component_async_complete(dapm->component);
643 static struct snd_soc_dapm_widget *
644 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
646 struct snd_soc_dapm_widget *w = wcache->widget;
647 struct list_head *wlist;
652 wlist = &w->dapm->card->widgets;
654 list_for_each_entry_from(w, wlist, list) {
655 if (!strcmp(name, w->name))
666 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
667 struct snd_soc_dapm_widget *w)
673 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
674 * @dapm: The DAPM context for which to set the level
675 * @level: The level to set
677 * Forces the DAPM bias level to a specific state. It will call the bias level
678 * callback of DAPM context with the specified level. This will even happen if
679 * the context is already at the same level. Furthermore it will not go through
680 * the normal bias level sequencing, meaning any intermediate states between the
681 * current and the target state will not be entered.
683 * Note that the change in bias level is only temporary and the next time
684 * snd_soc_dapm_sync() is called the state will be set to the level as
685 * determined by the DAPM core. The function is mainly intended to be used to
686 * used during probe or resume from suspend to power up the device so
687 * initialization can be done, before the DAPM core takes over.
689 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
690 enum snd_soc_bias_level level)
694 if (dapm->set_bias_level)
695 ret = dapm->set_bias_level(dapm, level);
698 dapm->bias_level = level;
702 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
705 * snd_soc_dapm_set_bias_level - set the bias level for the system
706 * @dapm: DAPM context
707 * @level: level to configure
709 * Configure the bias (power) levels for the SoC audio device.
711 * Returns 0 for success else error.
713 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
714 enum snd_soc_bias_level level)
716 struct snd_soc_card *card = dapm->card;
719 trace_snd_soc_bias_level_start(card, level);
721 if (card && card->set_bias_level)
722 ret = card->set_bias_level(card, dapm, level);
726 if (!card || dapm != &card->dapm)
727 ret = snd_soc_dapm_force_bias_level(dapm, level);
732 if (card && card->set_bias_level_post)
733 ret = card->set_bias_level_post(card, dapm, level);
735 trace_snd_soc_bias_level_done(card, level);
740 /* connect mux widget to its interconnecting audio paths */
741 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
742 struct snd_soc_dapm_path *path, const char *control_name,
743 struct snd_soc_dapm_widget *w)
745 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
746 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
747 unsigned int val, item;
750 if (e->reg != SND_SOC_NOPM) {
751 soc_dapm_read(dapm, e->reg, &val);
752 val = (val >> e->shift_l) & e->mask;
753 item = snd_soc_enum_val_to_item(e, val);
755 /* since a virtual mux has no backing registers to
756 * decide which path to connect, it will try to match
757 * with the first enumeration. This is to ensure
758 * that the default mux choice (the first) will be
759 * correctly powered up during initialization.
764 for (i = 0; i < e->items; i++) {
765 if (!(strcmp(control_name, e->texts[i]))) {
766 path->name = e->texts[i];
778 /* set up initial codec paths */
779 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
782 struct soc_mixer_control *mc = (struct soc_mixer_control *)
783 p->sink->kcontrol_news[i].private_value;
784 unsigned int reg = mc->reg;
785 unsigned int shift = mc->shift;
786 unsigned int max = mc->max;
787 unsigned int mask = (1 << fls(max)) - 1;
788 unsigned int invert = mc->invert;
791 if (reg != SND_SOC_NOPM) {
792 soc_dapm_read(p->sink->dapm, reg, &val);
794 * The nth_path argument allows this function to know
795 * which path of a kcontrol it is setting the initial
796 * status for. Ideally this would support any number
797 * of paths and channels. But since kcontrols only come
798 * in mono and stereo variants, we are limited to 2
801 * The following code assumes for stereo controls the
802 * first path is the left channel, and all remaining
803 * paths are the right channel.
805 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
807 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
808 val = (val >> mc->rshift) & mask;
810 val = (val >> shift) & mask;
816 /* since a virtual mixer has no backing registers to
817 * decide which path to connect, it will try to match
818 * with initial state. This is to ensure
819 * that the default mixer choice will be
820 * correctly powered up during initialization.
826 /* connect mixer widget to its interconnecting audio paths */
827 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
828 struct snd_soc_dapm_path *path, const char *control_name)
832 /* search for mixer kcontrol */
833 for (i = 0; i < path->sink->num_kcontrols; i++) {
834 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
835 path->name = path->sink->kcontrol_news[i].name;
836 dapm_set_mixer_path_status(path, i, nth_path++);
843 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
844 struct snd_soc_dapm_widget *kcontrolw,
845 const struct snd_kcontrol_new *kcontrol_new,
846 struct snd_kcontrol **kcontrol)
848 struct snd_soc_dapm_widget *w;
853 list_for_each_entry(w, &dapm->card->widgets, list) {
854 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
856 for (i = 0; i < w->num_kcontrols; i++) {
857 if (&w->kcontrol_news[i] == kcontrol_new) {
859 *kcontrol = w->kcontrols[i];
869 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
870 * create it. Either way, add the widget into the control's widget list
872 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
875 struct snd_soc_dapm_context *dapm = w->dapm;
876 struct snd_card *card = dapm->card->snd_card;
880 struct snd_kcontrol *kcontrol;
881 bool wname_in_long_name, kcname_in_long_name;
882 char *long_name = NULL;
886 prefix = soc_dapm_prefix(dapm);
888 prefix_len = strlen(prefix) + 1;
892 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
897 wname_in_long_name = false;
898 kcname_in_long_name = true;
901 case snd_soc_dapm_switch:
902 case snd_soc_dapm_mixer:
903 case snd_soc_dapm_pga:
904 case snd_soc_dapm_out_drv:
905 wname_in_long_name = true;
906 kcname_in_long_name = true;
908 case snd_soc_dapm_mixer_named_ctl:
909 wname_in_long_name = false;
910 kcname_in_long_name = true;
912 case snd_soc_dapm_demux:
913 case snd_soc_dapm_mux:
914 wname_in_long_name = true;
915 kcname_in_long_name = false;
922 if (wname_in_long_name && kcname_in_long_name) {
924 * The control will get a prefix from the control
925 * creation process but we're also using the same
926 * prefix for widgets so cut the prefix off the
927 * front of the widget name.
929 long_name = kasprintf(GFP_KERNEL, "%s %s",
930 w->name + prefix_len,
931 w->kcontrol_news[kci].name);
932 if (long_name == NULL)
936 } else if (wname_in_long_name) {
938 name = w->name + prefix_len;
941 name = w->kcontrol_news[kci].name;
944 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
951 kcontrol->private_free = dapm_kcontrol_free;
953 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
955 snd_ctl_free_one(kcontrol);
959 ret = snd_ctl_add(card, kcontrol);
962 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
968 ret = dapm_kcontrol_add_widget(kcontrol, w);
970 w->kcontrols[kci] = kcontrol;
978 /* create new dapm mixer control */
979 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
982 struct snd_soc_dapm_path *path;
983 struct dapm_kcontrol_data *data;
986 for (i = 0; i < w->num_kcontrols; i++) {
988 snd_soc_dapm_widget_for_each_source_path(w, path) {
989 /* mixer/mux paths name must match control name */
990 if (path->name != (char *)w->kcontrol_news[i].name)
993 if (!w->kcontrols[i]) {
994 ret = dapm_create_or_share_kcontrol(w, i);
999 dapm_kcontrol_add_path(w->kcontrols[i], path);
1001 data = snd_kcontrol_chip(w->kcontrols[i]);
1003 snd_soc_dapm_add_path(data->widget->dapm,
1013 /* create new dapm mux control */
1014 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1016 struct snd_soc_dapm_context *dapm = w->dapm;
1017 enum snd_soc_dapm_direction dir;
1018 struct snd_soc_dapm_path *path;
1023 case snd_soc_dapm_mux:
1024 dir = SND_SOC_DAPM_DIR_OUT;
1027 case snd_soc_dapm_demux:
1028 dir = SND_SOC_DAPM_DIR_IN;
1035 if (w->num_kcontrols != 1) {
1037 "ASoC: %s %s has incorrect number of controls\n", type,
1042 if (list_empty(&w->edges[dir])) {
1043 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1047 ret = dapm_create_or_share_kcontrol(w, 0);
1051 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1053 dapm_kcontrol_add_path(w->kcontrols[0], path);
1059 /* create new dapm volume control */
1060 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1064 for (i = 0; i < w->num_kcontrols; i++) {
1065 ret = dapm_create_or_share_kcontrol(w, i);
1073 /* create new dapm dai link control */
1074 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1077 struct snd_kcontrol *kcontrol;
1078 struct snd_soc_dapm_context *dapm = w->dapm;
1079 struct snd_card *card = dapm->card->snd_card;
1081 /* create control for links with > 1 config */
1082 if (w->num_params <= 1)
1086 for (i = 0; i < w->num_kcontrols; i++) {
1087 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1089 ret = snd_ctl_add(card, kcontrol);
1092 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1093 w->name, w->kcontrol_news[i].name, ret);
1096 kcontrol->private_data = w;
1097 w->kcontrols[i] = kcontrol;
1103 /* We implement power down on suspend by checking the power state of
1104 * the ALSA card - when we are suspending the ALSA state for the card
1107 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1109 int level = snd_power_get_state(widget->dapm->card->snd_card);
1112 case SNDRV_CTL_POWER_D3hot:
1113 case SNDRV_CTL_POWER_D3cold:
1114 if (widget->ignore_suspend)
1115 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1117 return widget->ignore_suspend;
1123 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1124 struct list_head *widgets)
1126 struct snd_soc_dapm_widget *w;
1127 struct list_head *it;
1128 unsigned int size = 0;
1131 list_for_each(it, widgets)
1134 *list = kzalloc(sizeof(**list) + size * sizeof(*w), GFP_KERNEL);
1138 list_for_each_entry(w, widgets, work_list)
1139 (*list)->widgets[i++] = w;
1141 (*list)->num_widgets = i;
1147 * Common implementation for is_connected_output_ep() and
1148 * is_connected_input_ep(). The function is inlined since the combined size of
1149 * the two specialized functions is only marginally larger then the size of the
1150 * generic function and at the same time the fast path of the specialized
1151 * functions is significantly smaller than the generic function.
1153 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1154 struct list_head *list, enum snd_soc_dapm_direction dir,
1155 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1156 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1157 enum snd_soc_dapm_direction)),
1158 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1159 enum snd_soc_dapm_direction))
1161 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1162 struct snd_soc_dapm_path *path;
1165 if (widget->endpoints[dir] >= 0)
1166 return widget->endpoints[dir];
1168 DAPM_UPDATE_STAT(widget, path_checks);
1170 /* do we need to add this widget to the list ? */
1172 list_add_tail(&widget->work_list, list);
1174 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1176 custom_stop_condition = NULL;
1179 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1180 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1181 return widget->endpoints[dir];
1184 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1185 DAPM_UPDATE_STAT(widget, neighbour_checks);
1187 if (path->weak || path->is_supply)
1193 trace_snd_soc_dapm_path(widget, dir, path);
1195 if (path->connect) {
1197 con += fn(path->node[dir], list, custom_stop_condition);
1202 widget->endpoints[dir] = con;
1208 * Recursively check for a completed path to an active or physically connected
1209 * output widget. Returns number of complete paths.
1211 * Optionally, can be supplied with a function acting as a stopping condition.
1212 * This function takes the dapm widget currently being examined and the walk
1213 * direction as an arguments, it should return true if widgets from that point
1214 * in the graph onwards should not be added to the widget list.
1216 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1217 struct list_head *list,
1218 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1219 enum snd_soc_dapm_direction))
1221 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1222 is_connected_output_ep, custom_stop_condition);
1226 * Recursively check for a completed path to an active or physically connected
1227 * input widget. Returns number of complete paths.
1229 * Optionally, can be supplied with a function acting as a stopping condition.
1230 * This function takes the dapm widget currently being examined and the walk
1231 * direction as an arguments, it should return true if the walk should be
1232 * stopped and false otherwise.
1234 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1235 struct list_head *list,
1236 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1237 enum snd_soc_dapm_direction))
1239 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1240 is_connected_input_ep, custom_stop_condition);
1244 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1245 * @dai: the soc DAI.
1246 * @stream: stream direction.
1247 * @list: list of active widgets for this stream.
1248 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1249 * walk based on custom logic.
1251 * Queries DAPM graph as to whether a valid audio stream path exists for
1252 * the initial stream specified by name. This takes into account
1253 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1255 * Optionally, can be supplied with a function acting as a stopping condition.
1256 * This function takes the dapm widget currently being examined and the walk
1257 * direction as an arguments, it should return true if the walk should be
1258 * stopped and false otherwise.
1260 * Returns the number of valid paths or negative error.
1262 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1263 struct snd_soc_dapm_widget_list **list,
1264 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1265 enum snd_soc_dapm_direction))
1267 struct snd_soc_card *card = dai->component->card;
1268 struct snd_soc_dapm_widget *w;
1273 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1276 * For is_connected_{output,input}_ep fully discover the graph we need
1277 * to reset the cached number of inputs and outputs.
1279 list_for_each_entry(w, &card->widgets, list) {
1280 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1281 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1284 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1285 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1286 custom_stop_condition);
1288 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1289 custom_stop_condition);
1291 /* Drop starting point */
1292 list_del(widgets.next);
1294 ret = dapm_widget_list_create(list, &widgets);
1298 trace_snd_soc_dapm_connected(paths, stream);
1299 mutex_unlock(&card->dapm_mutex);
1305 * Handler for regulator supply widget.
1307 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1308 struct snd_kcontrol *kcontrol, int event)
1312 soc_dapm_async_complete(w->dapm);
1314 if (SND_SOC_DAPM_EVENT_ON(event)) {
1315 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1316 ret = regulator_allow_bypass(w->regulator, false);
1318 dev_warn(w->dapm->dev,
1319 "ASoC: Failed to unbypass %s: %d\n",
1323 return regulator_enable(w->regulator);
1325 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1326 ret = regulator_allow_bypass(w->regulator, true);
1328 dev_warn(w->dapm->dev,
1329 "ASoC: Failed to bypass %s: %d\n",
1333 return regulator_disable_deferred(w->regulator, w->shift);
1336 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1339 * Handler for clock supply widget.
1341 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1342 struct snd_kcontrol *kcontrol, int event)
1347 soc_dapm_async_complete(w->dapm);
1349 #ifdef CONFIG_HAVE_CLK
1350 if (SND_SOC_DAPM_EVENT_ON(event)) {
1351 return clk_prepare_enable(w->clk);
1353 clk_disable_unprepare(w->clk);
1359 EXPORT_SYMBOL_GPL(dapm_clock_event);
1361 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1363 if (w->power_checked)
1364 return w->new_power;
1369 w->new_power = w->power_check(w);
1371 w->power_checked = true;
1373 return w->new_power;
1376 /* Generic check to see if a widget should be powered. */
1377 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1381 DAPM_UPDATE_STAT(w, power_checks);
1383 in = is_connected_input_ep(w, NULL, NULL);
1384 out = is_connected_output_ep(w, NULL, NULL);
1385 return out != 0 && in != 0;
1388 /* Check to see if a power supply is needed */
1389 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1391 struct snd_soc_dapm_path *path;
1393 DAPM_UPDATE_STAT(w, power_checks);
1395 /* Check if one of our outputs is connected */
1396 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1397 DAPM_UPDATE_STAT(w, neighbour_checks);
1402 if (path->connected &&
1403 !path->connected(path->source, path->sink))
1406 if (dapm_widget_power_check(path->sink))
1413 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1415 return w->connected;
1418 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1419 struct snd_soc_dapm_widget *b,
1427 sort = dapm_down_seq;
1429 if (sort[a->id] != sort[b->id])
1430 return sort[a->id] - sort[b->id];
1431 if (a->subseq != b->subseq) {
1433 return a->subseq - b->subseq;
1435 return b->subseq - a->subseq;
1437 if (a->reg != b->reg)
1438 return a->reg - b->reg;
1439 if (a->dapm != b->dapm)
1440 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1445 /* Insert a widget in order into a DAPM power sequence. */
1446 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1447 struct list_head *list,
1450 struct snd_soc_dapm_widget *w;
1452 list_for_each_entry(w, list, power_list)
1453 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1454 list_add_tail(&new_widget->power_list, &w->power_list);
1458 list_add_tail(&new_widget->power_list, list);
1461 static void dapm_seq_check_event(struct snd_soc_card *card,
1462 struct snd_soc_dapm_widget *w, int event)
1464 const char *ev_name;
1468 case SND_SOC_DAPM_PRE_PMU:
1469 ev_name = "PRE_PMU";
1472 case SND_SOC_DAPM_POST_PMU:
1473 ev_name = "POST_PMU";
1476 case SND_SOC_DAPM_PRE_PMD:
1477 ev_name = "PRE_PMD";
1480 case SND_SOC_DAPM_POST_PMD:
1481 ev_name = "POST_PMD";
1484 case SND_SOC_DAPM_WILL_PMU:
1485 ev_name = "WILL_PMU";
1488 case SND_SOC_DAPM_WILL_PMD:
1489 ev_name = "WILL_PMD";
1493 WARN(1, "Unknown event %d\n", event);
1497 if (w->new_power != power)
1500 if (w->event && (w->event_flags & event)) {
1501 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1503 soc_dapm_async_complete(w->dapm);
1504 trace_snd_soc_dapm_widget_event_start(w, event);
1505 ret = w->event(w, NULL, event);
1506 trace_snd_soc_dapm_widget_event_done(w, event);
1508 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1509 ev_name, w->name, ret);
1513 /* Apply the coalesced changes from a DAPM sequence */
1514 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1515 struct list_head *pending)
1517 struct snd_soc_dapm_context *dapm;
1518 struct snd_soc_dapm_widget *w;
1520 unsigned int value = 0;
1521 unsigned int mask = 0;
1523 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1527 list_for_each_entry(w, pending, power_list) {
1528 WARN_ON(reg != w->reg || dapm != w->dapm);
1529 w->power = w->new_power;
1531 mask |= w->mask << w->shift;
1533 value |= w->on_val << w->shift;
1535 value |= w->off_val << w->shift;
1537 pop_dbg(dapm->dev, card->pop_time,
1538 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1539 w->name, reg, value, mask);
1541 /* Check for events */
1542 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1543 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1547 /* Any widget will do, they should all be updating the
1551 pop_dbg(dapm->dev, card->pop_time,
1552 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1553 value, mask, reg, card->pop_time);
1554 pop_wait(card->pop_time);
1555 soc_dapm_update_bits(dapm, reg, mask, value);
1558 list_for_each_entry(w, pending, power_list) {
1559 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1560 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1564 /* Apply a DAPM power sequence.
1566 * We walk over a pre-sorted list of widgets to apply power to. In
1567 * order to minimise the number of writes to the device required
1568 * multiple widgets will be updated in a single write where possible.
1569 * Currently anything that requires more than a single write is not
1572 static void dapm_seq_run(struct snd_soc_card *card,
1573 struct list_head *list, int event, bool power_up)
1575 struct snd_soc_dapm_widget *w, *n;
1576 struct snd_soc_dapm_context *d;
1579 int cur_subseq = -1;
1580 int cur_reg = SND_SOC_NOPM;
1581 struct snd_soc_dapm_context *cur_dapm = NULL;
1588 sort = dapm_down_seq;
1590 list_for_each_entry_safe(w, n, list, power_list) {
1593 /* Do we need to apply any queued changes? */
1594 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1595 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1596 if (!list_empty(&pending))
1597 dapm_seq_run_coalesced(card, &pending);
1599 if (cur_dapm && cur_dapm->seq_notifier) {
1600 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1601 if (sort[i] == cur_sort)
1602 cur_dapm->seq_notifier(cur_dapm,
1607 if (cur_dapm && w->dapm != cur_dapm)
1608 soc_dapm_async_complete(cur_dapm);
1610 INIT_LIST_HEAD(&pending);
1612 cur_subseq = INT_MIN;
1613 cur_reg = SND_SOC_NOPM;
1618 case snd_soc_dapm_pre:
1620 list_for_each_entry_safe_continue(w, n, list,
1623 if (event == SND_SOC_DAPM_STREAM_START)
1625 NULL, SND_SOC_DAPM_PRE_PMU);
1626 else if (event == SND_SOC_DAPM_STREAM_STOP)
1628 NULL, SND_SOC_DAPM_PRE_PMD);
1631 case snd_soc_dapm_post:
1633 list_for_each_entry_safe_continue(w, n, list,
1636 if (event == SND_SOC_DAPM_STREAM_START)
1638 NULL, SND_SOC_DAPM_POST_PMU);
1639 else if (event == SND_SOC_DAPM_STREAM_STOP)
1641 NULL, SND_SOC_DAPM_POST_PMD);
1645 /* Queue it up for application */
1646 cur_sort = sort[w->id];
1647 cur_subseq = w->subseq;
1650 list_move(&w->power_list, &pending);
1655 dev_err(w->dapm->dev,
1656 "ASoC: Failed to apply widget power: %d\n", ret);
1659 if (!list_empty(&pending))
1660 dapm_seq_run_coalesced(card, &pending);
1662 if (cur_dapm && cur_dapm->seq_notifier) {
1663 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1664 if (sort[i] == cur_sort)
1665 cur_dapm->seq_notifier(cur_dapm,
1669 list_for_each_entry(d, &card->dapm_list, list) {
1670 soc_dapm_async_complete(d);
1674 static void dapm_widget_update(struct snd_soc_card *card)
1676 struct snd_soc_dapm_update *update = card->update;
1677 struct snd_soc_dapm_widget_list *wlist;
1678 struct snd_soc_dapm_widget *w = NULL;
1682 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1685 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1687 for (wi = 0; wi < wlist->num_widgets; wi++) {
1688 w = wlist->widgets[wi];
1690 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1691 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1693 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1701 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1704 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1707 if (update->has_second_set) {
1708 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1709 update->mask2, update->val2);
1711 dev_err(w->dapm->dev,
1712 "ASoC: %s DAPM update failed: %d\n",
1716 for (wi = 0; wi < wlist->num_widgets; wi++) {
1717 w = wlist->widgets[wi];
1719 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1720 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1722 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1728 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1729 * they're changing state.
1731 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1733 struct snd_soc_dapm_context *d = data;
1736 /* If we're off and we're not supposed to go into STANDBY */
1737 if (d->bias_level == SND_SOC_BIAS_OFF &&
1738 d->target_bias_level != SND_SOC_BIAS_OFF) {
1740 pm_runtime_get_sync(d->dev);
1742 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1745 "ASoC: Failed to turn on bias: %d\n", ret);
1748 /* Prepare for a transition to ON or away from ON */
1749 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1750 d->bias_level != SND_SOC_BIAS_ON) ||
1751 (d->target_bias_level != SND_SOC_BIAS_ON &&
1752 d->bias_level == SND_SOC_BIAS_ON)) {
1753 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1756 "ASoC: Failed to prepare bias: %d\n", ret);
1760 /* Async callback run prior to DAPM sequences - brings to their final
1763 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1765 struct snd_soc_dapm_context *d = data;
1768 /* If we just powered the last thing off drop to standby bias */
1769 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1770 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1771 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1772 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1774 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1778 /* If we're in standby and can support bias off then do that */
1779 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1780 d->target_bias_level == SND_SOC_BIAS_OFF) {
1781 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1783 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1787 pm_runtime_put(d->dev);
1790 /* If we just powered up then move to active bias */
1791 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1792 d->target_bias_level == SND_SOC_BIAS_ON) {
1793 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1795 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1800 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1801 bool power, bool connect)
1803 /* If a connection is being made or broken then that update
1804 * will have marked the peer dirty, otherwise the widgets are
1805 * not connected and this update has no impact. */
1809 /* If the peer is already in the state we're moving to then we
1810 * won't have an impact on it. */
1811 if (power != peer->power)
1812 dapm_mark_dirty(peer, "peer state change");
1815 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1816 struct list_head *up_list,
1817 struct list_head *down_list)
1819 struct snd_soc_dapm_path *path;
1821 if (w->power == power)
1824 trace_snd_soc_dapm_widget_power(w, power);
1826 /* If we changed our power state perhaps our neigbours changed
1829 snd_soc_dapm_widget_for_each_source_path(w, path)
1830 dapm_widget_set_peer_power(path->source, power, path->connect);
1832 /* Supplies can't affect their outputs, only their inputs */
1833 if (!w->is_supply) {
1834 snd_soc_dapm_widget_for_each_sink_path(w, path)
1835 dapm_widget_set_peer_power(path->sink, power,
1840 dapm_seq_insert(w, up_list, true);
1842 dapm_seq_insert(w, down_list, false);
1845 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1846 struct list_head *up_list,
1847 struct list_head *down_list)
1852 case snd_soc_dapm_pre:
1853 dapm_seq_insert(w, down_list, false);
1855 case snd_soc_dapm_post:
1856 dapm_seq_insert(w, up_list, true);
1860 power = dapm_widget_power_check(w);
1862 dapm_widget_set_power(w, power, up_list, down_list);
1867 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1869 if (dapm->idle_bias_off)
1872 switch (snd_power_get_state(dapm->card->snd_card)) {
1873 case SNDRV_CTL_POWER_D3hot:
1874 case SNDRV_CTL_POWER_D3cold:
1875 return dapm->suspend_bias_off;
1884 * Scan each dapm widget for complete audio path.
1885 * A complete path is a route that has valid endpoints i.e.:-
1887 * o DAC to output pin.
1888 * o Input pin to ADC.
1889 * o Input pin to Output pin (bypass, sidetone)
1890 * o DAC to ADC (loopback).
1892 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1894 struct snd_soc_dapm_widget *w;
1895 struct snd_soc_dapm_context *d;
1897 LIST_HEAD(down_list);
1898 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1899 enum snd_soc_bias_level bias;
1901 lockdep_assert_held(&card->dapm_mutex);
1903 trace_snd_soc_dapm_start(card);
1905 list_for_each_entry(d, &card->dapm_list, list) {
1906 if (dapm_idle_bias_off(d))
1907 d->target_bias_level = SND_SOC_BIAS_OFF;
1909 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1914 /* Check which widgets we need to power and store them in
1915 * lists indicating if they should be powered up or down. We
1916 * only check widgets that have been flagged as dirty but note
1917 * that new widgets may be added to the dirty list while we
1920 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1921 dapm_power_one_widget(w, &up_list, &down_list);
1924 list_for_each_entry(w, &card->widgets, list) {
1926 case snd_soc_dapm_pre:
1927 case snd_soc_dapm_post:
1928 /* These widgets always need to be powered */
1931 list_del_init(&w->dirty);
1938 /* Supplies and micbiases only bring the
1939 * context up to STANDBY as unless something
1940 * else is active and passing audio they
1941 * generally don't require full power. Signal
1942 * generators are virtual pins and have no
1943 * power impact themselves.
1946 case snd_soc_dapm_siggen:
1947 case snd_soc_dapm_vmid:
1949 case snd_soc_dapm_supply:
1950 case snd_soc_dapm_regulator_supply:
1951 case snd_soc_dapm_clock_supply:
1952 case snd_soc_dapm_micbias:
1953 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1954 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1957 d->target_bias_level = SND_SOC_BIAS_ON;
1964 /* Force all contexts in the card to the same bias state if
1965 * they're not ground referenced.
1967 bias = SND_SOC_BIAS_OFF;
1968 list_for_each_entry(d, &card->dapm_list, list)
1969 if (d->target_bias_level > bias)
1970 bias = d->target_bias_level;
1971 list_for_each_entry(d, &card->dapm_list, list)
1972 if (!dapm_idle_bias_off(d))
1973 d->target_bias_level = bias;
1975 trace_snd_soc_dapm_walk_done(card);
1977 /* Run card bias changes at first */
1978 dapm_pre_sequence_async(&card->dapm, 0);
1979 /* Run other bias changes in parallel */
1980 list_for_each_entry(d, &card->dapm_list, list) {
1981 if (d != &card->dapm)
1982 async_schedule_domain(dapm_pre_sequence_async, d,
1985 async_synchronize_full_domain(&async_domain);
1987 list_for_each_entry(w, &down_list, power_list) {
1988 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1991 list_for_each_entry(w, &up_list, power_list) {
1992 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1995 /* Power down widgets first; try to avoid amplifying pops. */
1996 dapm_seq_run(card, &down_list, event, false);
1998 dapm_widget_update(card);
2001 dapm_seq_run(card, &up_list, event, true);
2003 /* Run all the bias changes in parallel */
2004 list_for_each_entry(d, &card->dapm_list, list) {
2005 if (d != &card->dapm)
2006 async_schedule_domain(dapm_post_sequence_async, d,
2009 async_synchronize_full_domain(&async_domain);
2010 /* Run card bias changes at last */
2011 dapm_post_sequence_async(&card->dapm, 0);
2013 /* do we need to notify any clients that DAPM event is complete */
2014 list_for_each_entry(d, &card->dapm_list, list) {
2015 if (d->stream_event)
2016 d->stream_event(d, event);
2019 pop_dbg(card->dev, card->pop_time,
2020 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2021 pop_wait(card->pop_time);
2023 trace_snd_soc_dapm_done(card);
2028 #ifdef CONFIG_DEBUG_FS
2029 static ssize_t dapm_widget_power_read_file(struct file *file,
2030 char __user *user_buf,
2031 size_t count, loff_t *ppos)
2033 struct snd_soc_dapm_widget *w = file->private_data;
2034 struct snd_soc_card *card = w->dapm->card;
2035 enum snd_soc_dapm_direction dir, rdir;
2039 struct snd_soc_dapm_path *p = NULL;
2041 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2045 mutex_lock(&card->dapm_mutex);
2047 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2052 in = is_connected_input_ep(w, NULL, NULL);
2053 out = is_connected_output_ep(w, NULL, NULL);
2056 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2057 w->name, w->power ? "On" : "Off",
2058 w->force ? " (forced)" : "", in, out);
2061 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2062 " - R%d(0x%x) mask 0x%x",
2063 w->reg, w->reg, w->mask << w->shift);
2065 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2068 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2070 w->active ? "active" : "inactive");
2072 snd_soc_dapm_for_each_direction(dir) {
2073 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2074 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2075 if (p->connected && !p->connected(w, p->node[rdir]))
2081 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2082 " %s \"%s\" \"%s\"\n",
2083 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2084 p->name ? p->name : "static",
2085 p->node[rdir]->name);
2089 mutex_unlock(&card->dapm_mutex);
2091 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2097 static const struct file_operations dapm_widget_power_fops = {
2098 .open = simple_open,
2099 .read = dapm_widget_power_read_file,
2100 .llseek = default_llseek,
2103 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2104 size_t count, loff_t *ppos)
2106 struct snd_soc_dapm_context *dapm = file->private_data;
2109 switch (dapm->bias_level) {
2110 case SND_SOC_BIAS_ON:
2113 case SND_SOC_BIAS_PREPARE:
2114 level = "Prepare\n";
2116 case SND_SOC_BIAS_STANDBY:
2117 level = "Standby\n";
2119 case SND_SOC_BIAS_OFF:
2123 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2124 level = "Unknown\n";
2128 return simple_read_from_buffer(user_buf, count, ppos, level,
2132 static const struct file_operations dapm_bias_fops = {
2133 .open = simple_open,
2134 .read = dapm_bias_read_file,
2135 .llseek = default_llseek,
2138 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2139 struct dentry *parent)
2143 if (!parent || IS_ERR(parent))
2146 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2148 if (IS_ERR(dapm->debugfs_dapm)) {
2150 "ASoC: Failed to create DAPM debugfs directory %ld\n",
2151 PTR_ERR(dapm->debugfs_dapm));
2155 d = debugfs_create_file("bias_level", 0444,
2156 dapm->debugfs_dapm, dapm,
2160 "ASoC: Failed to create bias level debugfs file: %ld\n",
2164 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2166 struct snd_soc_dapm_context *dapm = w->dapm;
2169 if (!dapm->debugfs_dapm || !w->name)
2172 d = debugfs_create_file(w->name, 0444,
2173 dapm->debugfs_dapm, w,
2174 &dapm_widget_power_fops);
2176 dev_warn(w->dapm->dev,
2177 "ASoC: Failed to create %s debugfs file: %ld\n",
2178 w->name, PTR_ERR(d));
2181 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2183 debugfs_remove_recursive(dapm->debugfs_dapm);
2187 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2188 struct dentry *parent)
2192 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2196 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2203 * soc_dapm_connect_path() - Connects or disconnects a path
2204 * @path: The path to update
2205 * @connect: The new connect state of the path. True if the path is connected,
2206 * false if it is disconnected.
2207 * @reason: The reason why the path changed (for debugging only)
2209 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2210 bool connect, const char *reason)
2212 if (path->connect == connect)
2215 path->connect = connect;
2216 dapm_mark_dirty(path->source, reason);
2217 dapm_mark_dirty(path->sink, reason);
2218 dapm_path_invalidate(path);
2221 /* test and update the power status of a mux widget */
2222 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2223 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2225 struct snd_soc_dapm_path *path;
2229 lockdep_assert_held(&card->dapm_mutex);
2231 /* find dapm widget path assoc with kcontrol */
2232 dapm_kcontrol_for_each_path(path, kcontrol) {
2234 /* we now need to match the string in the enum to the path */
2235 if (!(strcmp(path->name, e->texts[mux])))
2240 soc_dapm_connect_path(path, connect, "mux update");
2244 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2249 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2250 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2251 struct snd_soc_dapm_update *update)
2253 struct snd_soc_card *card = dapm->card;
2256 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2257 card->update = update;
2258 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2259 card->update = NULL;
2260 mutex_unlock(&card->dapm_mutex);
2262 soc_dpcm_runtime_update(card);
2265 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2267 /* test and update the power status of a mixer or switch widget */
2268 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2269 struct snd_kcontrol *kcontrol,
2270 int connect, int rconnect)
2272 struct snd_soc_dapm_path *path;
2275 lockdep_assert_held(&card->dapm_mutex);
2277 /* find dapm widget path assoc with kcontrol */
2278 dapm_kcontrol_for_each_path(path, kcontrol) {
2280 * Ideally this function should support any number of
2281 * paths and channels. But since kcontrols only come
2282 * in mono and stereo variants, we are limited to 2
2285 * The following code assumes for stereo controls the
2286 * first path (when 'found == 0') is the left channel,
2287 * and all remaining paths (when 'found == 1') are the
2290 * A stereo control is signified by a valid 'rconnect'
2291 * value, either 0 for unconnected, or >= 0 for connected.
2292 * This is chosen instead of using snd_soc_volsw_is_stereo,
2293 * so that the behavior of snd_soc_dapm_mixer_update_power
2294 * doesn't change even when the kcontrol passed in is
2297 * It passes 'connect' as the path connect status for
2298 * the left channel, and 'rconnect' for the right
2301 if (found && rconnect >= 0)
2302 soc_dapm_connect_path(path, rconnect, "mixer update");
2304 soc_dapm_connect_path(path, connect, "mixer update");
2309 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2314 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2315 struct snd_kcontrol *kcontrol, int connect,
2316 struct snd_soc_dapm_update *update)
2318 struct snd_soc_card *card = dapm->card;
2321 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2322 card->update = update;
2323 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2324 card->update = NULL;
2325 mutex_unlock(&card->dapm_mutex);
2327 soc_dpcm_runtime_update(card);
2330 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2332 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2335 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2336 struct snd_soc_dapm_widget *w;
2338 char *state = "not set";
2340 /* card won't be set for the dummy component, as a spot fix
2341 * we're checking for that case specifically here but in future
2342 * we will ensure that the dummy component looks like others.
2347 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2348 if (w->dapm != dapm)
2351 /* only display widgets that burn power */
2353 case snd_soc_dapm_hp:
2354 case snd_soc_dapm_mic:
2355 case snd_soc_dapm_spk:
2356 case snd_soc_dapm_line:
2357 case snd_soc_dapm_micbias:
2358 case snd_soc_dapm_dac:
2359 case snd_soc_dapm_adc:
2360 case snd_soc_dapm_pga:
2361 case snd_soc_dapm_out_drv:
2362 case snd_soc_dapm_mixer:
2363 case snd_soc_dapm_mixer_named_ctl:
2364 case snd_soc_dapm_supply:
2365 case snd_soc_dapm_regulator_supply:
2366 case snd_soc_dapm_clock_supply:
2368 count += sprintf(buf + count, "%s: %s\n",
2369 w->name, w->power ? "On":"Off");
2376 switch (snd_soc_dapm_get_bias_level(dapm)) {
2377 case SND_SOC_BIAS_ON:
2380 case SND_SOC_BIAS_PREPARE:
2383 case SND_SOC_BIAS_STANDBY:
2386 case SND_SOC_BIAS_OFF:
2390 count += sprintf(buf + count, "PM State: %s\n", state);
2395 /* show dapm widget status in sys fs */
2396 static ssize_t dapm_widget_show(struct device *dev,
2397 struct device_attribute *attr, char *buf)
2399 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2402 mutex_lock(&rtd->card->dapm_mutex);
2404 for (i = 0; i < rtd->num_codecs; i++) {
2405 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2407 count += dapm_widget_show_component(cmpnt, buf + count);
2410 mutex_unlock(&rtd->card->dapm_mutex);
2415 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2417 struct attribute *soc_dapm_dev_attrs[] = {
2418 &dev_attr_dapm_widget.attr,
2422 static void dapm_free_path(struct snd_soc_dapm_path *path)
2424 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2425 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2426 list_del(&path->list_kcontrol);
2427 list_del(&path->list);
2431 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2433 struct snd_soc_dapm_path *p, *next_p;
2434 enum snd_soc_dapm_direction dir;
2437 list_del(&w->dirty);
2439 * remove source and sink paths associated to this widget.
2440 * While removing the path, remove reference to it from both
2441 * source and sink widgets so that path is removed only once.
2443 snd_soc_dapm_for_each_direction(dir) {
2444 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2448 kfree(w->kcontrols);
2449 kfree_const(w->name);
2453 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2455 dapm->path_sink_cache.widget = NULL;
2456 dapm->path_source_cache.widget = NULL;
2459 /* free all dapm widgets and resources */
2460 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2462 struct snd_soc_dapm_widget *w, *next_w;
2464 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2465 if (w->dapm != dapm)
2467 snd_soc_dapm_free_widget(w);
2469 snd_soc_dapm_reset_cache(dapm);
2472 static struct snd_soc_dapm_widget *dapm_find_widget(
2473 struct snd_soc_dapm_context *dapm, const char *pin,
2474 bool search_other_contexts)
2476 struct snd_soc_dapm_widget *w;
2477 struct snd_soc_dapm_widget *fallback = NULL;
2479 list_for_each_entry(w, &dapm->card->widgets, list) {
2480 if (!strcmp(w->name, pin)) {
2481 if (w->dapm == dapm)
2488 if (search_other_contexts)
2495 * set the DAPM pin status:
2496 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2497 * error code; called from kcontrol put callback
2499 static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2500 const char *pin, int status)
2502 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2505 dapm_assert_locked(dapm);
2508 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2512 if (w->connected != status) {
2513 dapm_mark_dirty(w, "pin configuration");
2514 dapm_widget_invalidate_input_paths(w);
2515 dapm_widget_invalidate_output_paths(w);
2519 w->connected = status;
2527 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2528 * called from several API functions below
2530 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2531 const char *pin, int status)
2533 int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2535 return ret < 0 ? ret : 0;
2539 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2540 * @dapm: DAPM context
2542 * Walks all dapm audio paths and powers widgets according to their
2543 * stream or path usage.
2545 * Requires external locking.
2547 * Returns 0 for success.
2549 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2552 * Suppress early reports (eg, jacks syncing their state) to avoid
2553 * silly DAPM runs during card startup.
2555 if (!dapm->card || !dapm->card->instantiated)
2558 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2560 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2563 * snd_soc_dapm_sync - scan and power dapm paths
2564 * @dapm: DAPM context
2566 * Walks all dapm audio paths and powers widgets according to their
2567 * stream or path usage.
2569 * Returns 0 for success.
2571 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2575 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2576 ret = snd_soc_dapm_sync_unlocked(dapm);
2577 mutex_unlock(&dapm->card->dapm_mutex);
2580 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2583 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2584 * @w: The widget for which to update the flags
2586 * Some widgets have a dynamic category which depends on which neighbors they
2587 * are connected to. This function update the category for these widgets.
2589 * This function must be called whenever a path is added or removed to a widget.
2591 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2593 enum snd_soc_dapm_direction dir;
2594 struct snd_soc_dapm_path *p;
2598 case snd_soc_dapm_input:
2599 /* On a fully routed card an input is never a source */
2600 if (w->dapm->card->fully_routed)
2602 ep = SND_SOC_DAPM_EP_SOURCE;
2603 snd_soc_dapm_widget_for_each_source_path(w, p) {
2604 if (p->source->id == snd_soc_dapm_micbias ||
2605 p->source->id == snd_soc_dapm_mic ||
2606 p->source->id == snd_soc_dapm_line ||
2607 p->source->id == snd_soc_dapm_output) {
2613 case snd_soc_dapm_output:
2614 /* On a fully routed card a output is never a sink */
2615 if (w->dapm->card->fully_routed)
2617 ep = SND_SOC_DAPM_EP_SINK;
2618 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2619 if (p->sink->id == snd_soc_dapm_spk ||
2620 p->sink->id == snd_soc_dapm_hp ||
2621 p->sink->id == snd_soc_dapm_line ||
2622 p->sink->id == snd_soc_dapm_input) {
2628 case snd_soc_dapm_line:
2630 snd_soc_dapm_for_each_direction(dir) {
2631 if (!list_empty(&w->edges[dir]))
2632 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2642 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2643 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2644 const char *control)
2646 bool dynamic_source = false;
2647 bool dynamic_sink = false;
2652 switch (source->id) {
2653 case snd_soc_dapm_demux:
2654 dynamic_source = true;
2661 case snd_soc_dapm_mux:
2662 case snd_soc_dapm_switch:
2663 case snd_soc_dapm_mixer:
2664 case snd_soc_dapm_mixer_named_ctl:
2665 dynamic_sink = true;
2671 if (dynamic_source && dynamic_sink) {
2673 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2674 source->name, control, sink->name);
2676 } else if (!dynamic_source && !dynamic_sink) {
2678 "Control not supported for path %s -> [%s] -> %s\n",
2679 source->name, control, sink->name);
2686 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2687 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2688 const char *control,
2689 int (*connected)(struct snd_soc_dapm_widget *source,
2690 struct snd_soc_dapm_widget *sink))
2692 struct snd_soc_dapm_widget *widgets[2];
2693 enum snd_soc_dapm_direction dir;
2694 struct snd_soc_dapm_path *path;
2697 if (wsink->is_supply && !wsource->is_supply) {
2699 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2700 wsource->name, wsink->name);
2704 if (connected && !wsource->is_supply) {
2706 "connected() callback only supported for supply widgets (%s -> %s)\n",
2707 wsource->name, wsink->name);
2711 if (wsource->is_supply && control) {
2713 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2714 wsource->name, control, wsink->name);
2718 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2722 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2726 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2727 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2728 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2729 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2731 path->connected = connected;
2732 INIT_LIST_HEAD(&path->list);
2733 INIT_LIST_HEAD(&path->list_kcontrol);
2735 if (wsource->is_supply || wsink->is_supply)
2736 path->is_supply = 1;
2738 /* connect static paths */
2739 if (control == NULL) {
2742 switch (wsource->id) {
2743 case snd_soc_dapm_demux:
2744 ret = dapm_connect_mux(dapm, path, control, wsource);
2752 switch (wsink->id) {
2753 case snd_soc_dapm_mux:
2754 ret = dapm_connect_mux(dapm, path, control, wsink);
2758 case snd_soc_dapm_switch:
2759 case snd_soc_dapm_mixer:
2760 case snd_soc_dapm_mixer_named_ctl:
2761 ret = dapm_connect_mixer(dapm, path, control);
2770 list_add(&path->list, &dapm->card->paths);
2771 snd_soc_dapm_for_each_direction(dir)
2772 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2774 snd_soc_dapm_for_each_direction(dir) {
2775 dapm_update_widget_flags(widgets[dir]);
2776 dapm_mark_dirty(widgets[dir], "Route added");
2779 if (dapm->card->instantiated && path->connect)
2780 dapm_path_invalidate(path);
2788 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2789 const struct snd_soc_dapm_route *route)
2791 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2792 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2795 char prefixed_sink[80];
2796 char prefixed_source[80];
2800 prefix = soc_dapm_prefix(dapm);
2802 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2803 prefix, route->sink);
2804 sink = prefixed_sink;
2805 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2806 prefix, route->source);
2807 source = prefixed_source;
2810 source = route->source;
2813 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2814 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2816 if (wsink && wsource)
2820 * find src and dest widgets over all widgets but favor a widget from
2821 * current DAPM context
2823 list_for_each_entry(w, &dapm->card->widgets, list) {
2824 if (!wsink && !(strcmp(w->name, sink))) {
2826 if (w->dapm == dapm) {
2833 if (!wsource && !(strcmp(w->name, source))) {
2835 if (w->dapm == dapm) {
2842 /* use widget from another DAPM context if not found from this */
2848 if (wsource == NULL) {
2849 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2853 if (wsink == NULL) {
2854 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2860 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2861 dapm_wcache_update(&dapm->path_source_cache, wsource);
2863 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2870 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2871 source, route->control, sink);
2875 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2876 const struct snd_soc_dapm_route *route)
2878 struct snd_soc_dapm_widget *wsource, *wsink;
2879 struct snd_soc_dapm_path *path, *p;
2882 char prefixed_sink[80];
2883 char prefixed_source[80];
2886 if (route->control) {
2888 "ASoC: Removal of routes with controls not supported\n");
2892 prefix = soc_dapm_prefix(dapm);
2894 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2895 prefix, route->sink);
2896 sink = prefixed_sink;
2897 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2898 prefix, route->source);
2899 source = prefixed_source;
2902 source = route->source;
2906 list_for_each_entry(p, &dapm->card->paths, list) {
2907 if (strcmp(p->source->name, source) != 0)
2909 if (strcmp(p->sink->name, sink) != 0)
2916 wsource = path->source;
2919 dapm_mark_dirty(wsource, "Route removed");
2920 dapm_mark_dirty(wsink, "Route removed");
2922 dapm_path_invalidate(path);
2924 dapm_free_path(path);
2926 /* Update any path related flags */
2927 dapm_update_widget_flags(wsource);
2928 dapm_update_widget_flags(wsink);
2930 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2938 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2939 * @dapm: DAPM context
2940 * @route: audio routes
2941 * @num: number of routes
2943 * Connects 2 dapm widgets together via a named audio path. The sink is
2944 * the widget receiving the audio signal, whilst the source is the sender
2945 * of the audio signal.
2947 * Returns 0 for success else error. On error all resources can be freed
2948 * with a call to snd_soc_card_free().
2950 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2951 const struct snd_soc_dapm_route *route, int num)
2955 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2956 for (i = 0; i < num; i++) {
2957 r = snd_soc_dapm_add_route(dapm, route);
2959 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2961 route->control ? route->control : "direct",
2967 mutex_unlock(&dapm->card->dapm_mutex);
2971 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2974 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2975 * @dapm: DAPM context
2976 * @route: audio routes
2977 * @num: number of routes
2979 * Removes routes from the DAPM context.
2981 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2982 const struct snd_soc_dapm_route *route, int num)
2986 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2987 for (i = 0; i < num; i++) {
2988 snd_soc_dapm_del_route(dapm, route);
2991 mutex_unlock(&dapm->card->dapm_mutex);
2995 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2997 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2998 const struct snd_soc_dapm_route *route)
3000 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3003 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3006 struct snd_soc_dapm_path *path;
3010 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3016 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3021 if (route->control || route->connected)
3022 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3023 route->source, route->sink);
3025 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3026 if (path->sink == sink) {
3033 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3034 route->source, route->sink);
3036 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3037 count, route->source, route->sink);
3043 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3044 * @dapm: DAPM context
3045 * @route: audio routes
3046 * @num: number of routes
3048 * Mark existing routes matching those specified in the passed array
3049 * as being weak, meaning that they are ignored for the purpose of
3050 * power decisions. The main intended use case is for sidetone paths
3051 * which couple audio between other independent paths if they are both
3052 * active in order to make the combination work better at the user
3053 * level but which aren't intended to be "used".
3055 * Note that CODEC drivers should not use this as sidetone type paths
3056 * can frequently also be used as bypass paths.
3058 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3059 const struct snd_soc_dapm_route *route, int num)
3064 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3065 for (i = 0; i < num; i++) {
3066 err = snd_soc_dapm_weak_route(dapm, route);
3071 mutex_unlock(&dapm->card->dapm_mutex);
3075 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3078 * snd_soc_dapm_new_widgets - add new dapm widgets
3079 * @card: card to be checked for new dapm widgets
3081 * Checks the codec for any new dapm widgets and creates them if found.
3083 * Returns 0 for success.
3085 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3087 struct snd_soc_dapm_widget *w;
3090 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3092 list_for_each_entry(w, &card->widgets, list)
3097 if (w->num_kcontrols) {
3098 w->kcontrols = kzalloc(w->num_kcontrols *
3099 sizeof(struct snd_kcontrol *),
3101 if (!w->kcontrols) {
3102 mutex_unlock(&card->dapm_mutex);
3108 case snd_soc_dapm_switch:
3109 case snd_soc_dapm_mixer:
3110 case snd_soc_dapm_mixer_named_ctl:
3113 case snd_soc_dapm_mux:
3114 case snd_soc_dapm_demux:
3117 case snd_soc_dapm_pga:
3118 case snd_soc_dapm_out_drv:
3121 case snd_soc_dapm_dai_link:
3122 dapm_new_dai_link(w);
3128 /* Read the initial power state from the device */
3130 soc_dapm_read(w->dapm, w->reg, &val);
3131 val = val >> w->shift;
3133 if (val == w->on_val)
3139 dapm_mark_dirty(w, "new widget");
3140 dapm_debugfs_add_widget(w);
3143 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3144 mutex_unlock(&card->dapm_mutex);
3147 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3150 * snd_soc_dapm_get_volsw - dapm mixer get callback
3151 * @kcontrol: mixer control
3152 * @ucontrol: control element information
3154 * Callback to get the value of a dapm mixer control.
3156 * Returns 0 for success.
3158 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3159 struct snd_ctl_elem_value *ucontrol)
3161 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3162 struct snd_soc_card *card = dapm->card;
3163 struct soc_mixer_control *mc =
3164 (struct soc_mixer_control *)kcontrol->private_value;
3166 unsigned int shift = mc->shift;
3168 unsigned int width = fls(max);
3169 unsigned int mask = (1 << fls(max)) - 1;
3170 unsigned int invert = mc->invert;
3171 unsigned int reg_val, val, rval = 0;
3174 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3175 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3176 ret = soc_dapm_read(dapm, reg, ®_val);
3177 val = (reg_val >> shift) & mask;
3179 if (ret == 0 && reg != mc->rreg)
3180 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3182 if (snd_soc_volsw_is_stereo(mc))
3183 rval = (reg_val >> mc->rshift) & mask;
3185 reg_val = dapm_kcontrol_get_value(kcontrol);
3186 val = reg_val & mask;
3188 if (snd_soc_volsw_is_stereo(mc))
3189 rval = (reg_val >> width) & mask;
3191 mutex_unlock(&card->dapm_mutex);
3197 ucontrol->value.integer.value[0] = max - val;
3199 ucontrol->value.integer.value[0] = val;
3201 if (snd_soc_volsw_is_stereo(mc)) {
3203 ucontrol->value.integer.value[1] = max - rval;
3205 ucontrol->value.integer.value[1] = rval;
3210 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3213 * snd_soc_dapm_put_volsw - dapm mixer set callback
3214 * @kcontrol: mixer control
3215 * @ucontrol: control element information
3217 * Callback to set the value of a dapm mixer control.
3219 * Returns 0 for success.
3221 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3222 struct snd_ctl_elem_value *ucontrol)
3224 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3225 struct snd_soc_card *card = dapm->card;
3226 struct soc_mixer_control *mc =
3227 (struct soc_mixer_control *)kcontrol->private_value;
3229 unsigned int shift = mc->shift;
3231 unsigned int width = fls(max);
3232 unsigned int mask = (1 << width) - 1;
3233 unsigned int invert = mc->invert;
3234 unsigned int val, rval = 0;
3235 int connect, rconnect = -1, change, reg_change = 0;
3236 struct snd_soc_dapm_update update = { NULL };
3239 val = (ucontrol->value.integer.value[0] & mask);
3245 if (snd_soc_volsw_is_stereo(mc)) {
3246 rval = (ucontrol->value.integer.value[1] & mask);
3252 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3254 /* This assumes field width < (bits in unsigned int / 2) */
3255 if (width > sizeof(unsigned int) * 8 / 2)
3257 "ASoC: control %s field width limit exceeded\n",
3259 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3261 if (reg != SND_SOC_NOPM) {
3263 rval = rval << mc->rshift;
3265 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3267 if (snd_soc_volsw_is_stereo(mc))
3268 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3273 if (change || reg_change) {
3275 if (snd_soc_volsw_is_stereo(mc)) {
3276 update.has_second_set = true;
3277 update.reg2 = mc->rreg;
3278 update.mask2 = mask << mc->rshift;
3281 update.kcontrol = kcontrol;
3283 update.mask = mask << shift;
3285 card->update = &update;
3287 change |= reg_change;
3289 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3292 card->update = NULL;
3295 mutex_unlock(&card->dapm_mutex);
3298 soc_dpcm_runtime_update(card);
3302 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3305 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3306 * @kcontrol: mixer control
3307 * @ucontrol: control element information
3309 * Callback to get the value of a dapm enumerated double mixer control.
3311 * Returns 0 for success.
3313 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3314 struct snd_ctl_elem_value *ucontrol)
3316 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3317 struct snd_soc_card *card = dapm->card;
3318 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3319 unsigned int reg_val, val;
3321 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3322 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3323 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3325 mutex_unlock(&card->dapm_mutex);
3329 reg_val = dapm_kcontrol_get_value(kcontrol);
3331 mutex_unlock(&card->dapm_mutex);
3333 val = (reg_val >> e->shift_l) & e->mask;
3334 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3335 if (e->shift_l != e->shift_r) {
3336 val = (reg_val >> e->shift_r) & e->mask;
3337 val = snd_soc_enum_val_to_item(e, val);
3338 ucontrol->value.enumerated.item[1] = val;
3343 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3346 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3347 * @kcontrol: mixer control
3348 * @ucontrol: control element information
3350 * Callback to set the value of a dapm enumerated double mixer control.
3352 * Returns 0 for success.
3354 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3355 struct snd_ctl_elem_value *ucontrol)
3357 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3358 struct snd_soc_card *card = dapm->card;
3359 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3360 unsigned int *item = ucontrol->value.enumerated.item;
3361 unsigned int val, change, reg_change = 0;
3363 struct snd_soc_dapm_update update = { NULL };
3366 if (item[0] >= e->items)
3369 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3370 mask = e->mask << e->shift_l;
3371 if (e->shift_l != e->shift_r) {
3372 if (item[1] > e->items)
3374 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3375 mask |= e->mask << e->shift_r;
3378 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3380 change = dapm_kcontrol_set_value(kcontrol, val);
3382 if (e->reg != SND_SOC_NOPM)
3383 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3385 if (change || reg_change) {
3387 update.kcontrol = kcontrol;
3388 update.reg = e->reg;
3391 card->update = &update;
3393 change |= reg_change;
3395 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3397 card->update = NULL;
3400 mutex_unlock(&card->dapm_mutex);
3403 soc_dpcm_runtime_update(card);
3407 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3410 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3412 * @kcontrol: mixer control
3413 * @uinfo: control element information
3415 * Callback to provide information about a pin switch control.
3417 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3418 struct snd_ctl_elem_info *uinfo)
3420 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3422 uinfo->value.integer.min = 0;
3423 uinfo->value.integer.max = 1;
3427 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3430 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3432 * @kcontrol: mixer control
3435 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3436 struct snd_ctl_elem_value *ucontrol)
3438 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3439 const char *pin = (const char *)kcontrol->private_value;
3441 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3443 ucontrol->value.integer.value[0] =
3444 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3446 mutex_unlock(&card->dapm_mutex);
3450 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3453 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3455 * @kcontrol: mixer control
3458 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3459 struct snd_ctl_elem_value *ucontrol)
3461 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3462 const char *pin = (const char *)kcontrol->private_value;
3465 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3466 ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3467 !!ucontrol->value.integer.value[0]);
3468 mutex_unlock(&card->dapm_mutex);
3470 snd_soc_dapm_sync(&card->dapm);
3473 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3475 struct snd_soc_dapm_widget *
3476 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3477 const struct snd_soc_dapm_widget *widget)
3479 struct snd_soc_dapm_widget *w;
3481 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3482 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3483 /* Do not nag about probe deferrals */
3485 int ret = PTR_ERR(w);
3487 if (ret != -EPROBE_DEFER)
3489 "ASoC: Failed to create DAPM control %s (%d)\n",
3495 "ASoC: Failed to create DAPM control %s\n",
3499 mutex_unlock(&dapm->card->dapm_mutex);
3502 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3504 struct snd_soc_dapm_widget *
3505 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3506 const struct snd_soc_dapm_widget *widget)
3508 enum snd_soc_dapm_direction dir;
3509 struct snd_soc_dapm_widget *w;
3513 if ((w = dapm_cnew_widget(widget)) == NULL)
3517 case snd_soc_dapm_regulator_supply:
3518 w->regulator = devm_regulator_get(dapm->dev, w->name);
3519 if (IS_ERR(w->regulator)) {
3520 ret = PTR_ERR(w->regulator);
3521 if (ret == -EPROBE_DEFER)
3522 return ERR_PTR(ret);
3523 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3528 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3529 ret = regulator_allow_bypass(w->regulator, true);
3531 dev_warn(w->dapm->dev,
3532 "ASoC: Failed to bypass %s: %d\n",
3536 case snd_soc_dapm_clock_supply:
3537 #ifdef CONFIG_CLKDEV_LOOKUP
3538 w->clk = devm_clk_get(dapm->dev, w->name);
3539 if (IS_ERR(w->clk)) {
3540 ret = PTR_ERR(w->clk);
3541 if (ret == -EPROBE_DEFER)
3542 return ERR_PTR(ret);
3543 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3555 prefix = soc_dapm_prefix(dapm);
3557 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3559 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3560 if (w->name == NULL) {
3566 case snd_soc_dapm_mic:
3567 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3568 w->power_check = dapm_generic_check_power;
3570 case snd_soc_dapm_input:
3571 if (!dapm->card->fully_routed)
3572 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3573 w->power_check = dapm_generic_check_power;
3575 case snd_soc_dapm_spk:
3576 case snd_soc_dapm_hp:
3577 w->is_ep = SND_SOC_DAPM_EP_SINK;
3578 w->power_check = dapm_generic_check_power;
3580 case snd_soc_dapm_output:
3581 if (!dapm->card->fully_routed)
3582 w->is_ep = SND_SOC_DAPM_EP_SINK;
3583 w->power_check = dapm_generic_check_power;
3585 case snd_soc_dapm_vmid:
3586 case snd_soc_dapm_siggen:
3587 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3588 w->power_check = dapm_always_on_check_power;
3590 case snd_soc_dapm_sink:
3591 w->is_ep = SND_SOC_DAPM_EP_SINK;
3592 w->power_check = dapm_always_on_check_power;
3595 case snd_soc_dapm_mux:
3596 case snd_soc_dapm_demux:
3597 case snd_soc_dapm_switch:
3598 case snd_soc_dapm_mixer:
3599 case snd_soc_dapm_mixer_named_ctl:
3600 case snd_soc_dapm_adc:
3601 case snd_soc_dapm_aif_out:
3602 case snd_soc_dapm_dac:
3603 case snd_soc_dapm_aif_in:
3604 case snd_soc_dapm_pga:
3605 case snd_soc_dapm_out_drv:
3606 case snd_soc_dapm_micbias:
3607 case snd_soc_dapm_line:
3608 case snd_soc_dapm_dai_link:
3609 case snd_soc_dapm_dai_out:
3610 case snd_soc_dapm_dai_in:
3611 w->power_check = dapm_generic_check_power;
3613 case snd_soc_dapm_supply:
3614 case snd_soc_dapm_regulator_supply:
3615 case snd_soc_dapm_clock_supply:
3616 case snd_soc_dapm_kcontrol:
3618 w->power_check = dapm_supply_check_power;
3621 w->power_check = dapm_always_on_check_power;
3626 INIT_LIST_HEAD(&w->list);
3627 INIT_LIST_HEAD(&w->dirty);
3628 list_add_tail(&w->list, &dapm->card->widgets);
3630 snd_soc_dapm_for_each_direction(dir) {
3631 INIT_LIST_HEAD(&w->edges[dir]);
3632 w->endpoints[dir] = -1;
3635 /* machine layer sets up unconnected pins and insertions */
3641 * snd_soc_dapm_new_controls - create new dapm controls
3642 * @dapm: DAPM context
3643 * @widget: widget array
3644 * @num: number of widgets
3646 * Creates new DAPM controls based upon the templates.
3648 * Returns 0 for success else error.
3650 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3651 const struct snd_soc_dapm_widget *widget,
3654 struct snd_soc_dapm_widget *w;
3658 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3659 for (i = 0; i < num; i++) {
3660 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3663 /* Do not nag about probe deferrals */
3664 if (ret == -EPROBE_DEFER)
3667 "ASoC: Failed to create DAPM control %s (%d)\n",
3673 "ASoC: Failed to create DAPM control %s\n",
3680 mutex_unlock(&dapm->card->dapm_mutex);
3683 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3685 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3686 struct snd_kcontrol *kcontrol, int event)
3688 struct snd_soc_dapm_path *source_p, *sink_p;
3689 struct snd_soc_dai *source, *sink;
3690 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
3691 struct snd_pcm_substream substream;
3692 struct snd_pcm_hw_params *params = NULL;
3693 struct snd_pcm_runtime *runtime = NULL;
3697 if (WARN_ON(!config) ||
3698 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3699 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3702 /* We only support a single source and sink, pick the first */
3703 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3704 struct snd_soc_dapm_path,
3705 list_node[SND_SOC_DAPM_DIR_OUT]);
3706 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3707 struct snd_soc_dapm_path,
3708 list_node[SND_SOC_DAPM_DIR_IN]);
3710 source = source_p->source->priv;
3711 sink = sink_p->sink->priv;
3713 /* Be a little careful as we don't want to overflow the mask array */
3714 if (config->formats) {
3715 fmt = ffs(config->formats) - 1;
3717 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3722 /* Currently very limited parameter selection */
3723 params = kzalloc(sizeof(*params), GFP_KERNEL);
3728 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3730 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3732 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3735 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3736 = config->channels_min;
3737 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3738 = config->channels_max;
3740 memset(&substream, 0, sizeof(substream));
3742 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3743 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3748 substream.runtime = runtime;
3751 case SND_SOC_DAPM_PRE_PMU:
3752 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3753 if (source->driver->ops && source->driver->ops->startup) {
3754 ret = source->driver->ops->startup(&substream, source);
3756 dev_err(source->dev,
3757 "ASoC: startup() failed: %d\n", ret);
3762 ret = soc_dai_hw_params(&substream, params, source);
3766 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3767 if (sink->driver->ops && sink->driver->ops->startup) {
3768 ret = sink->driver->ops->startup(&substream, sink);
3771 "ASoC: startup() failed: %d\n", ret);
3776 ret = soc_dai_hw_params(&substream, params, sink);
3781 case SND_SOC_DAPM_POST_PMU:
3782 ret = snd_soc_dai_digital_mute(sink, 0,
3783 SNDRV_PCM_STREAM_PLAYBACK);
3784 if (ret != 0 && ret != -ENOTSUPP)
3785 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3789 case SND_SOC_DAPM_PRE_PMD:
3790 ret = snd_soc_dai_digital_mute(sink, 1,
3791 SNDRV_PCM_STREAM_PLAYBACK);
3792 if (ret != 0 && ret != -ENOTSUPP)
3793 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3797 if (source->driver->ops && source->driver->ops->shutdown) {
3798 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3799 source->driver->ops->shutdown(&substream, source);
3803 if (sink->driver->ops && sink->driver->ops->shutdown) {
3804 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3805 sink->driver->ops->shutdown(&substream, sink);
3810 WARN(1, "Unknown event %d\n", event);
3820 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3821 struct snd_ctl_elem_value *ucontrol)
3823 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3825 ucontrol->value.enumerated.item[0] = w->params_select;
3830 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3831 struct snd_ctl_elem_value *ucontrol)
3833 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3835 /* Can't change the config when widget is already powered */
3839 if (ucontrol->value.enumerated.item[0] == w->params_select)
3842 if (ucontrol->value.enumerated.item[0] >= w->num_params)
3845 w->params_select = ucontrol->value.enumerated.item[0];
3850 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3851 const struct snd_soc_pcm_stream *params,
3852 unsigned int num_params,
3853 struct snd_soc_dapm_widget *source,
3854 struct snd_soc_dapm_widget *sink)
3856 struct snd_soc_dapm_widget template;
3857 struct snd_soc_dapm_widget *w;
3860 unsigned long private_value;
3861 const char **w_param_text;
3862 struct soc_enum w_param_enum[] = {
3863 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3865 struct snd_kcontrol_new kcontrol_dai_link[] = {
3866 SOC_ENUM_EXT(NULL, w_param_enum[0],
3867 snd_soc_dapm_dai_link_get,
3868 snd_soc_dapm_dai_link_put),
3870 const struct snd_soc_pcm_stream *config = params;
3872 w_param_text = devm_kcalloc(card->dev, num_params,
3873 sizeof(char *), GFP_KERNEL);
3877 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3878 source->name, sink->name);
3881 goto outfree_w_param;
3884 for (count = 0 ; count < num_params; count++) {
3885 if (!config->stream_name) {
3886 dev_warn(card->dapm.dev,
3887 "ASoC: anonymous config %d for dai link %s\n",
3889 w_param_text[count] =
3890 devm_kasprintf(card->dev, GFP_KERNEL,
3891 "Anonymous Configuration %d",
3893 if (!w_param_text[count]) {
3895 goto outfree_link_name;
3898 w_param_text[count] = devm_kmemdup(card->dev,
3899 config->stream_name,
3900 strlen(config->stream_name) + 1,
3902 if (!w_param_text[count]) {
3904 goto outfree_link_name;
3909 w_param_enum[0].items = num_params;
3910 w_param_enum[0].texts = w_param_text;
3912 memset(&template, 0, sizeof(template));
3913 template.reg = SND_SOC_NOPM;
3914 template.id = snd_soc_dapm_dai_link;
3915 template.name = link_name;
3916 template.event = snd_soc_dai_link_event;
3917 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3918 SND_SOC_DAPM_PRE_PMD;
3919 template.num_kcontrols = 1;
3920 /* duplicate w_param_enum on heap so that memory persists */
3922 (unsigned long) devm_kmemdup(card->dev,
3923 (void *)(kcontrol_dai_link[0].private_value),
3924 sizeof(struct soc_enum), GFP_KERNEL);
3925 if (!private_value) {
3926 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3929 goto outfree_link_name;
3931 kcontrol_dai_link[0].private_value = private_value;
3932 /* duplicate kcontrol_dai_link on heap so that memory persists */
3933 template.kcontrol_news =
3934 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3935 sizeof(struct snd_kcontrol_new),
3937 if (!template.kcontrol_news) {
3938 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3941 goto outfree_private_value;
3944 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3946 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
3949 /* Do not nag about probe deferrals */
3950 if (ret != -EPROBE_DEFER)
3952 "ASoC: Failed to create %s widget (%d)\n",
3954 goto outfree_kcontrol_news;
3957 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3960 goto outfree_kcontrol_news;
3964 w->num_params = num_params;
3966 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3969 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3972 devm_kfree(card->dev, w);
3973 outfree_kcontrol_news:
3974 devm_kfree(card->dev, (void *)template.kcontrol_news);
3975 outfree_private_value:
3976 devm_kfree(card->dev, (void *)private_value);
3978 devm_kfree(card->dev, link_name);
3980 for (count = 0 ; count < num_params; count++)
3981 devm_kfree(card->dev, (void *)w_param_text[count]);
3982 devm_kfree(card->dev, w_param_text);
3987 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3988 struct snd_soc_dai *dai)
3990 struct snd_soc_dapm_widget template;
3991 struct snd_soc_dapm_widget *w;
3993 WARN_ON(dapm->dev != dai->dev);
3995 memset(&template, 0, sizeof(template));
3996 template.reg = SND_SOC_NOPM;
3998 if (dai->driver->playback.stream_name) {
3999 template.id = snd_soc_dapm_dai_in;
4000 template.name = dai->driver->playback.stream_name;
4001 template.sname = dai->driver->playback.stream_name;
4003 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4006 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4008 int ret = PTR_ERR(w);
4010 /* Do not nag about probe deferrals */
4011 if (ret != -EPROBE_DEFER)
4013 "ASoC: Failed to create %s widget (%d)\n",
4014 dai->driver->playback.stream_name, ret);
4018 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4019 dai->driver->playback.stream_name);
4024 dai->playback_widget = w;
4027 if (dai->driver->capture.stream_name) {
4028 template.id = snd_soc_dapm_dai_out;
4029 template.name = dai->driver->capture.stream_name;
4030 template.sname = dai->driver->capture.stream_name;
4032 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4035 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4037 int ret = PTR_ERR(w);
4039 /* Do not nag about probe deferrals */
4040 if (ret != -EPROBE_DEFER)
4042 "ASoC: Failed to create %s widget (%d)\n",
4043 dai->driver->playback.stream_name, ret);
4047 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4048 dai->driver->capture.stream_name);
4053 dai->capture_widget = w;
4059 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4061 struct snd_soc_dapm_widget *dai_w, *w;
4062 struct snd_soc_dapm_widget *src, *sink;
4063 struct snd_soc_dai *dai;
4065 /* For each DAI widget... */
4066 list_for_each_entry(dai_w, &card->widgets, list) {
4067 switch (dai_w->id) {
4068 case snd_soc_dapm_dai_in:
4069 case snd_soc_dapm_dai_out:
4075 /* let users know there is no DAI to link */
4077 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4084 /* ...find all widgets with the same stream and link them */
4085 list_for_each_entry(w, &card->widgets, list) {
4086 if (w->dapm != dai_w->dapm)
4090 case snd_soc_dapm_dai_in:
4091 case snd_soc_dapm_dai_out:
4097 if (!w->sname || !strstr(w->sname, dai_w->sname))
4100 if (dai_w->id == snd_soc_dapm_dai_in) {
4107 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4108 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4115 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4116 struct snd_soc_pcm_runtime *rtd)
4118 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4119 struct snd_soc_dapm_widget *sink, *source;
4122 for (i = 0; i < rtd->num_codecs; i++) {
4123 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
4125 /* connect BE DAI playback if widgets are valid */
4126 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
4127 source = cpu_dai->playback_widget;
4128 sink = codec_dai->playback_widget;
4129 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4130 cpu_dai->component->name, source->name,
4131 codec_dai->component->name, sink->name);
4133 snd_soc_dapm_add_path(&card->dapm, source, sink,
4137 /* connect BE DAI capture if widgets are valid */
4138 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
4139 source = codec_dai->capture_widget;
4140 sink = cpu_dai->capture_widget;
4141 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4142 codec_dai->component->name, source->name,
4143 cpu_dai->component->name, sink->name);
4145 snd_soc_dapm_add_path(&card->dapm, source, sink,
4151 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4154 struct snd_soc_dapm_widget *w;
4157 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4158 w = dai->playback_widget;
4160 w = dai->capture_widget;
4163 dapm_mark_dirty(w, "stream event");
4165 if (w->id == snd_soc_dapm_dai_in) {
4166 ep = SND_SOC_DAPM_EP_SOURCE;
4167 dapm_widget_invalidate_input_paths(w);
4169 ep = SND_SOC_DAPM_EP_SINK;
4170 dapm_widget_invalidate_output_paths(w);
4174 case SND_SOC_DAPM_STREAM_START:
4178 case SND_SOC_DAPM_STREAM_STOP:
4182 case SND_SOC_DAPM_STREAM_SUSPEND:
4183 case SND_SOC_DAPM_STREAM_RESUME:
4184 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4185 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4191 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4193 struct snd_soc_pcm_runtime *rtd;
4195 /* for each BE DAI link... */
4196 list_for_each_entry(rtd, &card->rtd_list, list) {
4198 * dynamic FE links have no fixed DAI mapping.
4199 * CODEC<->CODEC links have no direct connection.
4201 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4204 dapm_connect_dai_link_widgets(card, rtd);
4208 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4213 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4214 for (i = 0; i < rtd->num_codecs; i++)
4215 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
4217 dapm_power_widgets(rtd->card, event);
4221 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4222 * @rtd: PCM runtime data
4223 * @stream: stream name
4224 * @event: stream event
4226 * Sends a stream event to the dapm core. The core then makes any
4227 * necessary widget power changes.
4229 * Returns 0 for success else error.
4231 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4234 struct snd_soc_card *card = rtd->card;
4236 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4237 soc_dapm_stream_event(rtd, stream, event);
4238 mutex_unlock(&card->dapm_mutex);
4242 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4243 * @dapm: DAPM context
4246 * Enables input/output pin and its parents or children widgets iff there is
4247 * a valid audio route and active audio stream.
4249 * Requires external locking.
4251 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4252 * do any widget power switching.
4254 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4257 return snd_soc_dapm_set_pin(dapm, pin, 1);
4259 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4262 * snd_soc_dapm_enable_pin - enable pin.
4263 * @dapm: DAPM context
4266 * Enables input/output pin and its parents or children widgets iff there is
4267 * a valid audio route and active audio stream.
4269 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4270 * do any widget power switching.
4272 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4276 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4278 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4280 mutex_unlock(&dapm->card->dapm_mutex);
4284 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4287 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4288 * @dapm: DAPM context
4291 * Enables input/output pin regardless of any other state. This is
4292 * intended for use with microphone bias supplies used in microphone
4295 * Requires external locking.
4297 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4298 * do any widget power switching.
4300 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4303 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4306 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4310 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4311 if (!w->connected) {
4313 * w->force does not affect the number of input or output paths,
4314 * so we only have to recheck if w->connected is changed
4316 dapm_widget_invalidate_input_paths(w);
4317 dapm_widget_invalidate_output_paths(w);
4321 dapm_mark_dirty(w, "force enable");
4325 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4328 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4329 * @dapm: DAPM context
4332 * Enables input/output pin regardless of any other state. This is
4333 * intended for use with microphone bias supplies used in microphone
4336 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4337 * do any widget power switching.
4339 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4344 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4346 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4348 mutex_unlock(&dapm->card->dapm_mutex);
4352 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4355 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4356 * @dapm: DAPM context
4359 * Disables input/output pin and its parents or children widgets.
4361 * Requires external locking.
4363 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4364 * do any widget power switching.
4366 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4369 return snd_soc_dapm_set_pin(dapm, pin, 0);
4371 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4374 * snd_soc_dapm_disable_pin - disable pin.
4375 * @dapm: DAPM context
4378 * Disables input/output pin and its parents or children widgets.
4380 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4381 * do any widget power switching.
4383 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4388 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4390 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4392 mutex_unlock(&dapm->card->dapm_mutex);
4396 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4399 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4400 * @dapm: DAPM context
4403 * Marks the specified pin as being not connected, disabling it along
4404 * any parent or child widgets. At present this is identical to
4405 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4406 * additional things such as disabling controls which only affect
4407 * paths through the pin.
4409 * Requires external locking.
4411 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4412 * do any widget power switching.
4414 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4417 return snd_soc_dapm_set_pin(dapm, pin, 0);
4419 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4422 * snd_soc_dapm_nc_pin - permanently disable pin.
4423 * @dapm: DAPM context
4426 * Marks the specified pin as being not connected, disabling it along
4427 * any parent or child widgets. At present this is identical to
4428 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4429 * additional things such as disabling controls which only affect
4430 * paths through the pin.
4432 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4433 * do any widget power switching.
4435 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4439 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4441 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4443 mutex_unlock(&dapm->card->dapm_mutex);
4447 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4450 * snd_soc_dapm_get_pin_status - get audio pin status
4451 * @dapm: DAPM context
4452 * @pin: audio signal pin endpoint (or start point)
4454 * Get audio pin status - connected or disconnected.
4456 * Returns 1 for connected otherwise 0.
4458 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4461 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4464 return w->connected;
4468 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4471 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4472 * @dapm: DAPM context
4473 * @pin: audio signal pin endpoint (or start point)
4475 * Mark the given endpoint or pin as ignoring suspend. When the
4476 * system is disabled a path between two endpoints flagged as ignoring
4477 * suspend will not be disabled. The path must already be enabled via
4478 * normal means at suspend time, it will not be turned on if it was not
4481 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4484 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4487 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4491 w->ignore_suspend = 1;
4495 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4498 * snd_soc_dapm_free - free dapm resources
4499 * @dapm: DAPM context
4501 * Free all dapm widgets and resources.
4503 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4505 dapm_debugfs_cleanup(dapm);
4506 dapm_free_widgets(dapm);
4507 list_del(&dapm->list);
4509 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4511 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4513 struct snd_soc_card *card = dapm->card;
4514 struct snd_soc_dapm_widget *w;
4515 LIST_HEAD(down_list);
4518 mutex_lock(&card->dapm_mutex);
4520 list_for_each_entry(w, &dapm->card->widgets, list) {
4521 if (w->dapm != dapm)
4524 dapm_seq_insert(w, &down_list, false);
4530 /* If there were no widgets to power down we're already in
4534 if (dapm->bias_level == SND_SOC_BIAS_ON)
4535 snd_soc_dapm_set_bias_level(dapm,
4536 SND_SOC_BIAS_PREPARE);
4537 dapm_seq_run(card, &down_list, 0, false);
4538 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4539 snd_soc_dapm_set_bias_level(dapm,
4540 SND_SOC_BIAS_STANDBY);
4543 mutex_unlock(&card->dapm_mutex);
4547 * snd_soc_dapm_shutdown - callback for system shutdown
4549 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4551 struct snd_soc_dapm_context *dapm;
4553 list_for_each_entry(dapm, &card->dapm_list, list) {
4554 if (dapm != &card->dapm) {
4555 soc_dapm_shutdown_dapm(dapm);
4556 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4557 snd_soc_dapm_set_bias_level(dapm,
4562 soc_dapm_shutdown_dapm(&card->dapm);
4563 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4564 snd_soc_dapm_set_bias_level(&card->dapm,
4568 /* Module information */
4569 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4570 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4571 MODULE_LICENSE("GPL");