1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
17 // o Delayed power down of audio subsystem to reduce pops between a quick
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/async.h>
24 #include <linux/delay.h>
26 #include <linux/bitops.h>
27 #include <linux/platform_device.h>
28 #include <linux/jiffies.h>
29 #include <linux/debugfs.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/pinctrl/consumer.h>
33 #include <linux/clk.h>
34 #include <linux/slab.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/initval.h>
41 #include <trace/events/asoc.h>
43 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
45 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
46 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
48 #define snd_soc_dapm_for_each_direction(dir) \
49 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
52 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
53 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
55 int (*connected)(struct snd_soc_dapm_widget *source,
56 struct snd_soc_dapm_widget *sink));
58 struct snd_soc_dapm_widget *
59 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
60 const struct snd_soc_dapm_widget *widget);
62 struct snd_soc_dapm_widget *
63 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
64 const struct snd_soc_dapm_widget *widget);
66 /* dapm power sequences - make this per codec in the future */
67 static int dapm_up_seq[] = {
68 [snd_soc_dapm_pre] = 0,
69 [snd_soc_dapm_regulator_supply] = 1,
70 [snd_soc_dapm_pinctrl] = 1,
71 [snd_soc_dapm_clock_supply] = 1,
72 [snd_soc_dapm_supply] = 2,
73 [snd_soc_dapm_micbias] = 3,
74 [snd_soc_dapm_vmid] = 3,
75 [snd_soc_dapm_dai_link] = 2,
76 [snd_soc_dapm_dai_in] = 4,
77 [snd_soc_dapm_dai_out] = 4,
78 [snd_soc_dapm_aif_in] = 4,
79 [snd_soc_dapm_aif_out] = 4,
80 [snd_soc_dapm_mic] = 5,
81 [snd_soc_dapm_siggen] = 5,
82 [snd_soc_dapm_input] = 5,
83 [snd_soc_dapm_output] = 5,
84 [snd_soc_dapm_mux] = 6,
85 [snd_soc_dapm_demux] = 6,
86 [snd_soc_dapm_dac] = 7,
87 [snd_soc_dapm_switch] = 8,
88 [snd_soc_dapm_mixer] = 8,
89 [snd_soc_dapm_mixer_named_ctl] = 8,
90 [snd_soc_dapm_pga] = 9,
91 [snd_soc_dapm_buffer] = 9,
92 [snd_soc_dapm_scheduler] = 9,
93 [snd_soc_dapm_effect] = 9,
94 [snd_soc_dapm_src] = 9,
95 [snd_soc_dapm_asrc] = 9,
96 [snd_soc_dapm_encoder] = 9,
97 [snd_soc_dapm_decoder] = 9,
98 [snd_soc_dapm_adc] = 10,
99 [snd_soc_dapm_out_drv] = 11,
100 [snd_soc_dapm_hp] = 11,
101 [snd_soc_dapm_spk] = 11,
102 [snd_soc_dapm_line] = 11,
103 [snd_soc_dapm_sink] = 11,
104 [snd_soc_dapm_kcontrol] = 12,
105 [snd_soc_dapm_post] = 13,
108 static int dapm_down_seq[] = {
109 [snd_soc_dapm_pre] = 0,
110 [snd_soc_dapm_kcontrol] = 1,
111 [snd_soc_dapm_adc] = 2,
112 [snd_soc_dapm_hp] = 3,
113 [snd_soc_dapm_spk] = 3,
114 [snd_soc_dapm_line] = 3,
115 [snd_soc_dapm_out_drv] = 3,
116 [snd_soc_dapm_sink] = 3,
117 [snd_soc_dapm_pga] = 4,
118 [snd_soc_dapm_buffer] = 4,
119 [snd_soc_dapm_scheduler] = 4,
120 [snd_soc_dapm_effect] = 4,
121 [snd_soc_dapm_src] = 4,
122 [snd_soc_dapm_asrc] = 4,
123 [snd_soc_dapm_encoder] = 4,
124 [snd_soc_dapm_decoder] = 4,
125 [snd_soc_dapm_switch] = 5,
126 [snd_soc_dapm_mixer_named_ctl] = 5,
127 [snd_soc_dapm_mixer] = 5,
128 [snd_soc_dapm_dac] = 6,
129 [snd_soc_dapm_mic] = 7,
130 [snd_soc_dapm_siggen] = 7,
131 [snd_soc_dapm_input] = 7,
132 [snd_soc_dapm_output] = 7,
133 [snd_soc_dapm_micbias] = 8,
134 [snd_soc_dapm_vmid] = 8,
135 [snd_soc_dapm_mux] = 9,
136 [snd_soc_dapm_demux] = 9,
137 [snd_soc_dapm_aif_in] = 10,
138 [snd_soc_dapm_aif_out] = 10,
139 [snd_soc_dapm_dai_in] = 10,
140 [snd_soc_dapm_dai_out] = 10,
141 [snd_soc_dapm_dai_link] = 11,
142 [snd_soc_dapm_supply] = 12,
143 [snd_soc_dapm_clock_supply] = 13,
144 [snd_soc_dapm_pinctrl] = 13,
145 [snd_soc_dapm_regulator_supply] = 13,
146 [snd_soc_dapm_post] = 14,
149 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
151 if (dapm->card && dapm->card->instantiated)
152 lockdep_assert_held(&dapm->card->dapm_mutex);
155 static void pop_wait(u32 pop_time)
158 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
161 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
169 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
174 vsnprintf(buf, PAGE_SIZE, fmt, args);
175 dev_info(dev, "%s", buf);
181 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
183 return !list_empty(&w->dirty);
186 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
188 dapm_assert_locked(w->dapm);
190 if (!dapm_dirty_widget(w)) {
191 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
193 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
198 * Common implementation for dapm_widget_invalidate_input_paths() and
199 * dapm_widget_invalidate_output_paths(). The function is inlined since the
200 * combined size of the two specialized functions is only marginally larger then
201 * the size of the generic function and at the same time the fast path of the
202 * specialized functions is significantly smaller than the generic function.
204 static __always_inline void dapm_widget_invalidate_paths(
205 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
207 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
208 struct snd_soc_dapm_widget *node;
209 struct snd_soc_dapm_path *p;
212 dapm_assert_locked(w->dapm);
214 if (w->endpoints[dir] == -1)
217 list_add_tail(&w->work_list, &list);
218 w->endpoints[dir] = -1;
220 list_for_each_entry(w, &list, work_list) {
221 snd_soc_dapm_widget_for_each_path(w, dir, p) {
222 if (p->is_supply || p->weak || !p->connect)
224 node = p->node[rdir];
225 if (node->endpoints[dir] != -1) {
226 node->endpoints[dir] = -1;
227 list_add_tail(&node->work_list, &list);
234 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
236 * @w: The widget for which to invalidate the cached number of input paths
238 * Resets the cached number of inputs for the specified widget and all widgets
239 * that can be reached via outcoming paths from the widget.
241 * This function must be called if the number of output paths for a widget might
242 * have changed. E.g. if the source state of a widget changes or a path is added
243 * or activated with the widget as the sink.
245 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
247 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
251 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
253 * @w: The widget for which to invalidate the cached number of output paths
255 * Resets the cached number of outputs for the specified widget and all widgets
256 * that can be reached via incoming paths from the widget.
258 * This function must be called if the number of output paths for a widget might
259 * have changed. E.g. if the sink state of a widget changes or a path is added
260 * or activated with the widget as the source.
262 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
264 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
268 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
269 * for the widgets connected to a path
270 * @p: The path to invalidate
272 * Resets the cached number of inputs for the sink of the path and the cached
273 * number of outputs for the source of the path.
275 * This function must be called when a path is added, removed or the connected
278 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
281 * Weak paths or supply paths do not influence the number of input or
282 * output paths of their neighbors.
284 if (p->weak || p->is_supply)
288 * The number of connected endpoints is the sum of the number of
289 * connected endpoints of all neighbors. If a node with 0 connected
290 * endpoints is either connected or disconnected that sum won't change,
291 * so there is no need to re-check the path.
293 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
294 dapm_widget_invalidate_input_paths(p->sink);
295 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
296 dapm_widget_invalidate_output_paths(p->source);
299 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
301 struct snd_soc_dapm_widget *w;
303 mutex_lock(&card->dapm_mutex);
305 list_for_each_entry(w, &card->widgets, list) {
307 dapm_mark_dirty(w, "Rechecking endpoints");
308 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
309 dapm_widget_invalidate_output_paths(w);
310 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
311 dapm_widget_invalidate_input_paths(w);
315 mutex_unlock(&card->dapm_mutex);
317 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
319 /* create a new dapm widget */
320 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
321 const struct snd_soc_dapm_widget *_widget)
323 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
326 struct dapm_kcontrol_data {
328 struct snd_soc_dapm_widget *widget;
329 struct list_head paths;
330 struct snd_soc_dapm_widget_list *wlist;
333 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
334 struct snd_kcontrol *kcontrol, const char *ctrl_name)
336 struct dapm_kcontrol_data *data;
337 struct soc_mixer_control *mc;
342 data = kzalloc(sizeof(*data), GFP_KERNEL);
346 INIT_LIST_HEAD(&data->paths);
348 switch (widget->id) {
349 case snd_soc_dapm_switch:
350 case snd_soc_dapm_mixer:
351 case snd_soc_dapm_mixer_named_ctl:
352 mc = (struct soc_mixer_control *)kcontrol->private_value;
354 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
355 dev_warn(widget->dapm->dev,
356 "ASoC: Unsupported stereo autodisable control '%s'\n",
359 if (mc->autodisable) {
360 struct snd_soc_dapm_widget template;
362 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
369 memset(&template, 0, sizeof(template));
370 template.reg = mc->reg;
371 template.mask = (1 << fls(mc->max)) - 1;
372 template.shift = mc->shift;
374 template.off_val = mc->max;
376 template.off_val = 0;
377 template.on_val = template.off_val;
378 template.id = snd_soc_dapm_kcontrol;
379 template.name = name;
381 data->value = template.on_val;
384 snd_soc_dapm_new_control_unlocked(widget->dapm,
387 if (IS_ERR(data->widget)) {
388 ret = PTR_ERR(data->widget);
397 case snd_soc_dapm_demux:
398 case snd_soc_dapm_mux:
399 e = (struct soc_enum *)kcontrol->private_value;
401 if (e->autodisable) {
402 struct snd_soc_dapm_widget template;
404 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
411 memset(&template, 0, sizeof(template));
412 template.reg = e->reg;
413 template.mask = e->mask;
414 template.shift = e->shift_l;
415 template.off_val = snd_soc_enum_item_to_val(e, 0);
416 template.on_val = template.off_val;
417 template.id = snd_soc_dapm_kcontrol;
418 template.name = name;
420 data->value = template.on_val;
422 data->widget = snd_soc_dapm_new_control_unlocked(
423 widget->dapm, &template);
425 if (IS_ERR(data->widget)) {
426 ret = PTR_ERR(data->widget);
434 snd_soc_dapm_add_path(widget->dapm, data->widget,
442 kcontrol->private_data = data;
451 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
453 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
455 list_del(&data->paths);
460 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
461 const struct snd_kcontrol *kcontrol)
463 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
468 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
469 struct snd_soc_dapm_widget *widget)
471 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
472 struct snd_soc_dapm_widget_list *new_wlist;
476 n = data->wlist->num_widgets + 1;
480 new_wlist = krealloc(data->wlist,
481 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
485 new_wlist->widgets[n - 1] = widget;
486 new_wlist->num_widgets = n;
488 data->wlist = new_wlist;
493 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
494 struct snd_soc_dapm_path *path)
496 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
498 list_add_tail(&path->list_kcontrol, &data->paths);
501 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
503 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
508 return data->widget->power;
511 static struct list_head *dapm_kcontrol_get_path_list(
512 const struct snd_kcontrol *kcontrol)
514 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
519 #define dapm_kcontrol_for_each_path(path, kcontrol) \
520 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
523 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
525 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
529 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
531 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
534 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
536 if (data->value == value)
540 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
541 case snd_soc_dapm_switch:
542 case snd_soc_dapm_mixer:
543 case snd_soc_dapm_mixer_named_ctl:
544 data->widget->on_val = value & data->widget->mask;
546 case snd_soc_dapm_demux:
547 case snd_soc_dapm_mux:
548 data->widget->on_val = value >> data->widget->shift;
551 data->widget->on_val = value;
562 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
564 * @kcontrol: The kcontrol
566 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
567 struct snd_kcontrol *kcontrol)
569 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
571 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
574 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
576 * @kcontrol: The kcontrol
578 * Note: This function must only be used on kcontrols that are known to have
579 * been registered for a CODEC. Otherwise the behaviour is undefined.
581 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
582 struct snd_kcontrol *kcontrol)
584 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
586 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
588 static void dapm_reset(struct snd_soc_card *card)
590 struct snd_soc_dapm_widget *w;
592 lockdep_assert_held(&card->dapm_mutex);
594 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
596 list_for_each_entry(w, &card->widgets, list) {
597 w->new_power = w->power;
598 w->power_checked = false;
602 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
604 if (!dapm->component)
606 return dapm->component->name_prefix;
609 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
612 if (!dapm->component)
614 return snd_soc_component_read(dapm->component, reg, value);
617 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
618 int reg, unsigned int mask, unsigned int value)
620 if (!dapm->component)
622 return snd_soc_component_update_bits(dapm->component, reg,
626 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
627 int reg, unsigned int mask, unsigned int value)
629 if (!dapm->component)
631 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
634 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
637 snd_soc_component_async_complete(dapm->component);
640 static struct snd_soc_dapm_widget *
641 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
643 struct snd_soc_dapm_widget *w = wcache->widget;
644 struct list_head *wlist;
649 wlist = &w->dapm->card->widgets;
651 list_for_each_entry_from(w, wlist, list) {
652 if (!strcmp(name, w->name))
663 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
664 struct snd_soc_dapm_widget *w)
670 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
671 * @dapm: The DAPM context for which to set the level
672 * @level: The level to set
674 * Forces the DAPM bias level to a specific state. It will call the bias level
675 * callback of DAPM context with the specified level. This will even happen if
676 * the context is already at the same level. Furthermore it will not go through
677 * the normal bias level sequencing, meaning any intermediate states between the
678 * current and the target state will not be entered.
680 * Note that the change in bias level is only temporary and the next time
681 * snd_soc_dapm_sync() is called the state will be set to the level as
682 * determined by the DAPM core. The function is mainly intended to be used to
683 * used during probe or resume from suspend to power up the device so
684 * initialization can be done, before the DAPM core takes over.
686 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
687 enum snd_soc_bias_level level)
691 if (dapm->set_bias_level)
692 ret = dapm->set_bias_level(dapm, level);
695 dapm->bias_level = level;
699 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
702 * snd_soc_dapm_set_bias_level - set the bias level for the system
703 * @dapm: DAPM context
704 * @level: level to configure
706 * Configure the bias (power) levels for the SoC audio device.
708 * Returns 0 for success else error.
710 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
711 enum snd_soc_bias_level level)
713 struct snd_soc_card *card = dapm->card;
716 trace_snd_soc_bias_level_start(card, level);
718 if (card && card->set_bias_level)
719 ret = card->set_bias_level(card, dapm, level);
723 if (!card || dapm != &card->dapm)
724 ret = snd_soc_dapm_force_bias_level(dapm, level);
729 if (card && card->set_bias_level_post)
730 ret = card->set_bias_level_post(card, dapm, level);
732 trace_snd_soc_bias_level_done(card, level);
737 /* connect mux widget to its interconnecting audio paths */
738 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
739 struct snd_soc_dapm_path *path, const char *control_name,
740 struct snd_soc_dapm_widget *w)
742 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
743 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
744 unsigned int val, item;
747 if (e->reg != SND_SOC_NOPM) {
748 soc_dapm_read(dapm, e->reg, &val);
749 val = (val >> e->shift_l) & e->mask;
750 item = snd_soc_enum_val_to_item(e, val);
752 /* since a virtual mux has no backing registers to
753 * decide which path to connect, it will try to match
754 * with the first enumeration. This is to ensure
755 * that the default mux choice (the first) will be
756 * correctly powered up during initialization.
761 i = match_string(e->texts, e->items, control_name);
765 path->name = e->texts[i];
766 path->connect = (i == item);
771 /* set up initial codec paths */
772 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
775 struct soc_mixer_control *mc = (struct soc_mixer_control *)
776 p->sink->kcontrol_news[i].private_value;
777 unsigned int reg = mc->reg;
778 unsigned int shift = mc->shift;
779 unsigned int max = mc->max;
780 unsigned int mask = (1 << fls(max)) - 1;
781 unsigned int invert = mc->invert;
784 if (reg != SND_SOC_NOPM) {
785 soc_dapm_read(p->sink->dapm, reg, &val);
787 * The nth_path argument allows this function to know
788 * which path of a kcontrol it is setting the initial
789 * status for. Ideally this would support any number
790 * of paths and channels. But since kcontrols only come
791 * in mono and stereo variants, we are limited to 2
794 * The following code assumes for stereo controls the
795 * first path is the left channel, and all remaining
796 * paths are the right channel.
798 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
800 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
801 val = (val >> mc->rshift) & mask;
803 val = (val >> shift) & mask;
809 /* since a virtual mixer has no backing registers to
810 * decide which path to connect, it will try to match
811 * with initial state. This is to ensure
812 * that the default mixer choice will be
813 * correctly powered up during initialization.
819 /* connect mixer widget to its interconnecting audio paths */
820 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
821 struct snd_soc_dapm_path *path, const char *control_name)
825 /* search for mixer kcontrol */
826 for (i = 0; i < path->sink->num_kcontrols; i++) {
827 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
828 path->name = path->sink->kcontrol_news[i].name;
829 dapm_set_mixer_path_status(path, i, nth_path++);
836 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
837 struct snd_soc_dapm_widget *kcontrolw,
838 const struct snd_kcontrol_new *kcontrol_new,
839 struct snd_kcontrol **kcontrol)
841 struct snd_soc_dapm_widget *w;
846 list_for_each_entry(w, &dapm->card->widgets, list) {
847 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
849 for (i = 0; i < w->num_kcontrols; i++) {
850 if (&w->kcontrol_news[i] == kcontrol_new) {
852 *kcontrol = w->kcontrols[i];
862 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
863 * create it. Either way, add the widget into the control's widget list
865 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
868 struct snd_soc_dapm_context *dapm = w->dapm;
869 struct snd_card *card = dapm->card->snd_card;
873 struct snd_kcontrol *kcontrol;
874 bool wname_in_long_name, kcname_in_long_name;
875 char *long_name = NULL;
879 prefix = soc_dapm_prefix(dapm);
881 prefix_len = strlen(prefix) + 1;
885 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
890 wname_in_long_name = false;
891 kcname_in_long_name = true;
894 case snd_soc_dapm_switch:
895 case snd_soc_dapm_mixer:
896 case snd_soc_dapm_pga:
897 case snd_soc_dapm_out_drv:
898 wname_in_long_name = true;
899 kcname_in_long_name = true;
901 case snd_soc_dapm_mixer_named_ctl:
902 wname_in_long_name = false;
903 kcname_in_long_name = true;
905 case snd_soc_dapm_demux:
906 case snd_soc_dapm_mux:
907 wname_in_long_name = true;
908 kcname_in_long_name = false;
915 if (wname_in_long_name && kcname_in_long_name) {
917 * The control will get a prefix from the control
918 * creation process but we're also using the same
919 * prefix for widgets so cut the prefix off the
920 * front of the widget name.
922 long_name = kasprintf(GFP_KERNEL, "%s %s",
923 w->name + prefix_len,
924 w->kcontrol_news[kci].name);
925 if (long_name == NULL)
929 } else if (wname_in_long_name) {
931 name = w->name + prefix_len;
934 name = w->kcontrol_news[kci].name;
937 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
944 kcontrol->private_free = dapm_kcontrol_free;
946 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
948 snd_ctl_free_one(kcontrol);
952 ret = snd_ctl_add(card, kcontrol);
955 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
961 ret = dapm_kcontrol_add_widget(kcontrol, w);
963 w->kcontrols[kci] = kcontrol;
971 /* create new dapm mixer control */
972 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
975 struct snd_soc_dapm_path *path;
976 struct dapm_kcontrol_data *data;
979 for (i = 0; i < w->num_kcontrols; i++) {
981 snd_soc_dapm_widget_for_each_source_path(w, path) {
982 /* mixer/mux paths name must match control name */
983 if (path->name != (char *)w->kcontrol_news[i].name)
986 if (!w->kcontrols[i]) {
987 ret = dapm_create_or_share_kcontrol(w, i);
992 dapm_kcontrol_add_path(w->kcontrols[i], path);
994 data = snd_kcontrol_chip(w->kcontrols[i]);
996 snd_soc_dapm_add_path(data->widget->dapm,
1006 /* create new dapm mux control */
1007 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1009 struct snd_soc_dapm_context *dapm = w->dapm;
1010 enum snd_soc_dapm_direction dir;
1011 struct snd_soc_dapm_path *path;
1016 case snd_soc_dapm_mux:
1017 dir = SND_SOC_DAPM_DIR_OUT;
1020 case snd_soc_dapm_demux:
1021 dir = SND_SOC_DAPM_DIR_IN;
1028 if (w->num_kcontrols != 1) {
1030 "ASoC: %s %s has incorrect number of controls\n", type,
1035 if (list_empty(&w->edges[dir])) {
1036 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1040 ret = dapm_create_or_share_kcontrol(w, 0);
1044 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1046 dapm_kcontrol_add_path(w->kcontrols[0], path);
1052 /* create new dapm volume control */
1053 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1057 for (i = 0; i < w->num_kcontrols; i++) {
1058 ret = dapm_create_or_share_kcontrol(w, i);
1066 /* create new dapm dai link control */
1067 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1070 struct snd_kcontrol *kcontrol;
1071 struct snd_soc_dapm_context *dapm = w->dapm;
1072 struct snd_card *card = dapm->card->snd_card;
1074 /* create control for links with > 1 config */
1075 if (w->num_params <= 1)
1079 for (i = 0; i < w->num_kcontrols; i++) {
1080 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1082 ret = snd_ctl_add(card, kcontrol);
1085 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1086 w->name, w->kcontrol_news[i].name, ret);
1089 kcontrol->private_data = w;
1090 w->kcontrols[i] = kcontrol;
1096 /* We implement power down on suspend by checking the power state of
1097 * the ALSA card - when we are suspending the ALSA state for the card
1100 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1102 int level = snd_power_get_state(widget->dapm->card->snd_card);
1105 case SNDRV_CTL_POWER_D3hot:
1106 case SNDRV_CTL_POWER_D3cold:
1107 if (widget->ignore_suspend)
1108 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1110 return widget->ignore_suspend;
1116 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1117 struct list_head *widgets)
1119 struct snd_soc_dapm_widget *w;
1120 struct list_head *it;
1121 unsigned int size = 0;
1124 list_for_each(it, widgets)
1127 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1131 list_for_each_entry(w, widgets, work_list)
1132 (*list)->widgets[i++] = w;
1134 (*list)->num_widgets = i;
1140 * Common implementation for is_connected_output_ep() and
1141 * is_connected_input_ep(). The function is inlined since the combined size of
1142 * the two specialized functions is only marginally larger then the size of the
1143 * generic function and at the same time the fast path of the specialized
1144 * functions is significantly smaller than the generic function.
1146 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1147 struct list_head *list, enum snd_soc_dapm_direction dir,
1148 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1149 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1150 enum snd_soc_dapm_direction)),
1151 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1152 enum snd_soc_dapm_direction))
1154 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1155 struct snd_soc_dapm_path *path;
1158 if (widget->endpoints[dir] >= 0)
1159 return widget->endpoints[dir];
1161 DAPM_UPDATE_STAT(widget, path_checks);
1163 /* do we need to add this widget to the list ? */
1165 list_add_tail(&widget->work_list, list);
1167 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1169 custom_stop_condition = NULL;
1172 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1173 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1174 return widget->endpoints[dir];
1177 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1178 DAPM_UPDATE_STAT(widget, neighbour_checks);
1180 if (path->weak || path->is_supply)
1186 trace_snd_soc_dapm_path(widget, dir, path);
1188 if (path->connect) {
1190 con += fn(path->node[dir], list, custom_stop_condition);
1195 widget->endpoints[dir] = con;
1201 * Recursively check for a completed path to an active or physically connected
1202 * output widget. Returns number of complete paths.
1204 * Optionally, can be supplied with a function acting as a stopping condition.
1205 * This function takes the dapm widget currently being examined and the walk
1206 * direction as an arguments, it should return true if widgets from that point
1207 * in the graph onwards should not be added to the widget list.
1209 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1210 struct list_head *list,
1211 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1212 enum snd_soc_dapm_direction))
1214 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1215 is_connected_output_ep, custom_stop_condition);
1219 * Recursively check for a completed path to an active or physically connected
1220 * input widget. Returns number of complete paths.
1222 * Optionally, can be supplied with a function acting as a stopping condition.
1223 * This function takes the dapm widget currently being examined and the walk
1224 * direction as an arguments, it should return true if the walk should be
1225 * stopped and false otherwise.
1227 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1228 struct list_head *list,
1229 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1230 enum snd_soc_dapm_direction))
1232 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1233 is_connected_input_ep, custom_stop_condition);
1237 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1238 * @dai: the soc DAI.
1239 * @stream: stream direction.
1240 * @list: list of active widgets for this stream.
1241 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1242 * walk based on custom logic.
1244 * Queries DAPM graph as to whether a valid audio stream path exists for
1245 * the initial stream specified by name. This takes into account
1246 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1248 * Optionally, can be supplied with a function acting as a stopping condition.
1249 * This function takes the dapm widget currently being examined and the walk
1250 * direction as an arguments, it should return true if the walk should be
1251 * stopped and false otherwise.
1253 * Returns the number of valid paths or negative error.
1255 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1256 struct snd_soc_dapm_widget_list **list,
1257 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1258 enum snd_soc_dapm_direction))
1260 struct snd_soc_card *card = dai->component->card;
1261 struct snd_soc_dapm_widget *w;
1266 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1269 * For is_connected_{output,input}_ep fully discover the graph we need
1270 * to reset the cached number of inputs and outputs.
1272 list_for_each_entry(w, &card->widgets, list) {
1273 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1274 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1277 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1278 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1279 custom_stop_condition);
1281 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1282 custom_stop_condition);
1284 /* Drop starting point */
1285 list_del(widgets.next);
1287 ret = dapm_widget_list_create(list, &widgets);
1291 trace_snd_soc_dapm_connected(paths, stream);
1292 mutex_unlock(&card->dapm_mutex);
1298 * Handler for regulator supply widget.
1300 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1301 struct snd_kcontrol *kcontrol, int event)
1305 soc_dapm_async_complete(w->dapm);
1307 if (SND_SOC_DAPM_EVENT_ON(event)) {
1308 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1309 ret = regulator_allow_bypass(w->regulator, false);
1311 dev_warn(w->dapm->dev,
1312 "ASoC: Failed to unbypass %s: %d\n",
1316 return regulator_enable(w->regulator);
1318 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1319 ret = regulator_allow_bypass(w->regulator, true);
1321 dev_warn(w->dapm->dev,
1322 "ASoC: Failed to bypass %s: %d\n",
1326 return regulator_disable_deferred(w->regulator, w->shift);
1329 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1332 * Handler for pinctrl widget.
1334 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1335 struct snd_kcontrol *kcontrol, int event)
1337 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1338 struct pinctrl *p = w->pinctrl;
1339 struct pinctrl_state *s;
1344 if (SND_SOC_DAPM_EVENT_ON(event))
1345 s = pinctrl_lookup_state(p, priv->active_state);
1347 s = pinctrl_lookup_state(p, priv->sleep_state);
1352 return pinctrl_select_state(p, s);
1354 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1357 * Handler for clock supply widget.
1359 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1360 struct snd_kcontrol *kcontrol, int event)
1365 soc_dapm_async_complete(w->dapm);
1367 #ifdef CONFIG_HAVE_CLK
1368 if (SND_SOC_DAPM_EVENT_ON(event)) {
1369 return clk_prepare_enable(w->clk);
1371 clk_disable_unprepare(w->clk);
1377 EXPORT_SYMBOL_GPL(dapm_clock_event);
1379 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1381 if (w->power_checked)
1382 return w->new_power;
1387 w->new_power = w->power_check(w);
1389 w->power_checked = true;
1391 return w->new_power;
1394 /* Generic check to see if a widget should be powered. */
1395 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1399 DAPM_UPDATE_STAT(w, power_checks);
1401 in = is_connected_input_ep(w, NULL, NULL);
1402 out = is_connected_output_ep(w, NULL, NULL);
1403 return out != 0 && in != 0;
1406 /* Check to see if a power supply is needed */
1407 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1409 struct snd_soc_dapm_path *path;
1411 DAPM_UPDATE_STAT(w, power_checks);
1413 /* Check if one of our outputs is connected */
1414 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1415 DAPM_UPDATE_STAT(w, neighbour_checks);
1420 if (path->connected &&
1421 !path->connected(path->source, path->sink))
1424 if (dapm_widget_power_check(path->sink))
1431 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1433 return w->connected;
1436 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1437 struct snd_soc_dapm_widget *b,
1445 sort = dapm_down_seq;
1447 if (sort[a->id] != sort[b->id])
1448 return sort[a->id] - sort[b->id];
1449 if (a->subseq != b->subseq) {
1451 return a->subseq - b->subseq;
1453 return b->subseq - a->subseq;
1455 if (a->reg != b->reg)
1456 return a->reg - b->reg;
1457 if (a->dapm != b->dapm)
1458 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1463 /* Insert a widget in order into a DAPM power sequence. */
1464 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1465 struct list_head *list,
1468 struct snd_soc_dapm_widget *w;
1470 list_for_each_entry(w, list, power_list)
1471 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1472 list_add_tail(&new_widget->power_list, &w->power_list);
1476 list_add_tail(&new_widget->power_list, list);
1479 static void dapm_seq_check_event(struct snd_soc_card *card,
1480 struct snd_soc_dapm_widget *w, int event)
1482 const char *ev_name;
1486 case SND_SOC_DAPM_PRE_PMU:
1487 ev_name = "PRE_PMU";
1490 case SND_SOC_DAPM_POST_PMU:
1491 ev_name = "POST_PMU";
1494 case SND_SOC_DAPM_PRE_PMD:
1495 ev_name = "PRE_PMD";
1498 case SND_SOC_DAPM_POST_PMD:
1499 ev_name = "POST_PMD";
1502 case SND_SOC_DAPM_WILL_PMU:
1503 ev_name = "WILL_PMU";
1506 case SND_SOC_DAPM_WILL_PMD:
1507 ev_name = "WILL_PMD";
1511 WARN(1, "Unknown event %d\n", event);
1515 if (w->new_power != power)
1518 if (w->event && (w->event_flags & event)) {
1519 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1521 soc_dapm_async_complete(w->dapm);
1522 trace_snd_soc_dapm_widget_event_start(w, event);
1523 ret = w->event(w, NULL, event);
1524 trace_snd_soc_dapm_widget_event_done(w, event);
1526 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1527 ev_name, w->name, ret);
1531 /* Apply the coalesced changes from a DAPM sequence */
1532 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1533 struct list_head *pending)
1535 struct snd_soc_dapm_context *dapm;
1536 struct snd_soc_dapm_widget *w;
1538 unsigned int value = 0;
1539 unsigned int mask = 0;
1541 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1545 list_for_each_entry(w, pending, power_list) {
1546 WARN_ON(reg != w->reg || dapm != w->dapm);
1547 w->power = w->new_power;
1549 mask |= w->mask << w->shift;
1551 value |= w->on_val << w->shift;
1553 value |= w->off_val << w->shift;
1555 pop_dbg(dapm->dev, card->pop_time,
1556 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1557 w->name, reg, value, mask);
1559 /* Check for events */
1560 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1561 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1565 /* Any widget will do, they should all be updating the
1569 pop_dbg(dapm->dev, card->pop_time,
1570 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1571 value, mask, reg, card->pop_time);
1572 pop_wait(card->pop_time);
1573 soc_dapm_update_bits(dapm, reg, mask, value);
1576 list_for_each_entry(w, pending, power_list) {
1577 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1578 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1582 /* Apply a DAPM power sequence.
1584 * We walk over a pre-sorted list of widgets to apply power to. In
1585 * order to minimise the number of writes to the device required
1586 * multiple widgets will be updated in a single write where possible.
1587 * Currently anything that requires more than a single write is not
1590 static void dapm_seq_run(struct snd_soc_card *card,
1591 struct list_head *list, int event, bool power_up)
1593 struct snd_soc_dapm_widget *w, *n;
1594 struct snd_soc_dapm_context *d;
1597 int cur_subseq = -1;
1598 int cur_reg = SND_SOC_NOPM;
1599 struct snd_soc_dapm_context *cur_dapm = NULL;
1606 sort = dapm_down_seq;
1608 list_for_each_entry_safe(w, n, list, power_list) {
1611 /* Do we need to apply any queued changes? */
1612 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1613 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1614 if (!list_empty(&pending))
1615 dapm_seq_run_coalesced(card, &pending);
1617 if (cur_dapm && cur_dapm->seq_notifier) {
1618 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1619 if (sort[i] == cur_sort)
1620 cur_dapm->seq_notifier(cur_dapm,
1625 if (cur_dapm && w->dapm != cur_dapm)
1626 soc_dapm_async_complete(cur_dapm);
1628 INIT_LIST_HEAD(&pending);
1630 cur_subseq = INT_MIN;
1631 cur_reg = SND_SOC_NOPM;
1636 case snd_soc_dapm_pre:
1638 list_for_each_entry_safe_continue(w, n, list,
1641 if (event == SND_SOC_DAPM_STREAM_START)
1643 NULL, SND_SOC_DAPM_PRE_PMU);
1644 else if (event == SND_SOC_DAPM_STREAM_STOP)
1646 NULL, SND_SOC_DAPM_PRE_PMD);
1649 case snd_soc_dapm_post:
1651 list_for_each_entry_safe_continue(w, n, list,
1654 if (event == SND_SOC_DAPM_STREAM_START)
1656 NULL, SND_SOC_DAPM_POST_PMU);
1657 else if (event == SND_SOC_DAPM_STREAM_STOP)
1659 NULL, SND_SOC_DAPM_POST_PMD);
1663 /* Queue it up for application */
1664 cur_sort = sort[w->id];
1665 cur_subseq = w->subseq;
1668 list_move(&w->power_list, &pending);
1673 dev_err(w->dapm->dev,
1674 "ASoC: Failed to apply widget power: %d\n", ret);
1677 if (!list_empty(&pending))
1678 dapm_seq_run_coalesced(card, &pending);
1680 if (cur_dapm && cur_dapm->seq_notifier) {
1681 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1682 if (sort[i] == cur_sort)
1683 cur_dapm->seq_notifier(cur_dapm,
1687 list_for_each_entry(d, &card->dapm_list, list) {
1688 soc_dapm_async_complete(d);
1692 static void dapm_widget_update(struct snd_soc_card *card)
1694 struct snd_soc_dapm_update *update = card->update;
1695 struct snd_soc_dapm_widget_list *wlist;
1696 struct snd_soc_dapm_widget *w = NULL;
1700 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1703 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1705 for (wi = 0; wi < wlist->num_widgets; wi++) {
1706 w = wlist->widgets[wi];
1708 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1709 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1711 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1719 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1722 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1725 if (update->has_second_set) {
1726 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1727 update->mask2, update->val2);
1729 dev_err(w->dapm->dev,
1730 "ASoC: %s DAPM update failed: %d\n",
1734 for (wi = 0; wi < wlist->num_widgets; wi++) {
1735 w = wlist->widgets[wi];
1737 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1738 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1740 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1746 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1747 * they're changing state.
1749 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1751 struct snd_soc_dapm_context *d = data;
1754 /* If we're off and we're not supposed to go into STANDBY */
1755 if (d->bias_level == SND_SOC_BIAS_OFF &&
1756 d->target_bias_level != SND_SOC_BIAS_OFF) {
1758 pm_runtime_get_sync(d->dev);
1760 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1763 "ASoC: Failed to turn on bias: %d\n", ret);
1766 /* Prepare for a transition to ON or away from ON */
1767 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1768 d->bias_level != SND_SOC_BIAS_ON) ||
1769 (d->target_bias_level != SND_SOC_BIAS_ON &&
1770 d->bias_level == SND_SOC_BIAS_ON)) {
1771 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1774 "ASoC: Failed to prepare bias: %d\n", ret);
1778 /* Async callback run prior to DAPM sequences - brings to their final
1781 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1783 struct snd_soc_dapm_context *d = data;
1786 /* If we just powered the last thing off drop to standby bias */
1787 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1788 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1789 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1790 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1792 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1796 /* If we're in standby and can support bias off then do that */
1797 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1798 d->target_bias_level == SND_SOC_BIAS_OFF) {
1799 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1801 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1805 pm_runtime_put(d->dev);
1808 /* If we just powered up then move to active bias */
1809 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1810 d->target_bias_level == SND_SOC_BIAS_ON) {
1811 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1813 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1818 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1819 bool power, bool connect)
1821 /* If a connection is being made or broken then that update
1822 * will have marked the peer dirty, otherwise the widgets are
1823 * not connected and this update has no impact. */
1827 /* If the peer is already in the state we're moving to then we
1828 * won't have an impact on it. */
1829 if (power != peer->power)
1830 dapm_mark_dirty(peer, "peer state change");
1833 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1834 struct list_head *up_list,
1835 struct list_head *down_list)
1837 struct snd_soc_dapm_path *path;
1839 if (w->power == power)
1842 trace_snd_soc_dapm_widget_power(w, power);
1844 /* If we changed our power state perhaps our neigbours changed
1847 snd_soc_dapm_widget_for_each_source_path(w, path)
1848 dapm_widget_set_peer_power(path->source, power, path->connect);
1850 /* Supplies can't affect their outputs, only their inputs */
1851 if (!w->is_supply) {
1852 snd_soc_dapm_widget_for_each_sink_path(w, path)
1853 dapm_widget_set_peer_power(path->sink, power,
1858 dapm_seq_insert(w, up_list, true);
1860 dapm_seq_insert(w, down_list, false);
1863 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1864 struct list_head *up_list,
1865 struct list_head *down_list)
1870 case snd_soc_dapm_pre:
1871 dapm_seq_insert(w, down_list, false);
1873 case snd_soc_dapm_post:
1874 dapm_seq_insert(w, up_list, true);
1878 power = dapm_widget_power_check(w);
1880 dapm_widget_set_power(w, power, up_list, down_list);
1885 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1887 if (dapm->idle_bias_off)
1890 switch (snd_power_get_state(dapm->card->snd_card)) {
1891 case SNDRV_CTL_POWER_D3hot:
1892 case SNDRV_CTL_POWER_D3cold:
1893 return dapm->suspend_bias_off;
1902 * Scan each dapm widget for complete audio path.
1903 * A complete path is a route that has valid endpoints i.e.:-
1905 * o DAC to output pin.
1906 * o Input pin to ADC.
1907 * o Input pin to Output pin (bypass, sidetone)
1908 * o DAC to ADC (loopback).
1910 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1912 struct snd_soc_dapm_widget *w;
1913 struct snd_soc_dapm_context *d;
1915 LIST_HEAD(down_list);
1916 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1917 enum snd_soc_bias_level bias;
1919 lockdep_assert_held(&card->dapm_mutex);
1921 trace_snd_soc_dapm_start(card);
1923 list_for_each_entry(d, &card->dapm_list, list) {
1924 if (dapm_idle_bias_off(d))
1925 d->target_bias_level = SND_SOC_BIAS_OFF;
1927 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1932 /* Check which widgets we need to power and store them in
1933 * lists indicating if they should be powered up or down. We
1934 * only check widgets that have been flagged as dirty but note
1935 * that new widgets may be added to the dirty list while we
1938 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1939 dapm_power_one_widget(w, &up_list, &down_list);
1942 list_for_each_entry(w, &card->widgets, list) {
1944 case snd_soc_dapm_pre:
1945 case snd_soc_dapm_post:
1946 /* These widgets always need to be powered */
1949 list_del_init(&w->dirty);
1956 /* Supplies and micbiases only bring the
1957 * context up to STANDBY as unless something
1958 * else is active and passing audio they
1959 * generally don't require full power. Signal
1960 * generators are virtual pins and have no
1961 * power impact themselves.
1964 case snd_soc_dapm_siggen:
1965 case snd_soc_dapm_vmid:
1967 case snd_soc_dapm_supply:
1968 case snd_soc_dapm_regulator_supply:
1969 case snd_soc_dapm_pinctrl:
1970 case snd_soc_dapm_clock_supply:
1971 case snd_soc_dapm_micbias:
1972 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1973 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1976 d->target_bias_level = SND_SOC_BIAS_ON;
1983 /* Force all contexts in the card to the same bias state if
1984 * they're not ground referenced.
1986 bias = SND_SOC_BIAS_OFF;
1987 list_for_each_entry(d, &card->dapm_list, list)
1988 if (d->target_bias_level > bias)
1989 bias = d->target_bias_level;
1990 list_for_each_entry(d, &card->dapm_list, list)
1991 if (!dapm_idle_bias_off(d))
1992 d->target_bias_level = bias;
1994 trace_snd_soc_dapm_walk_done(card);
1996 /* Run card bias changes at first */
1997 dapm_pre_sequence_async(&card->dapm, 0);
1998 /* Run other bias changes in parallel */
1999 list_for_each_entry(d, &card->dapm_list, list) {
2000 if (d != &card->dapm)
2001 async_schedule_domain(dapm_pre_sequence_async, d,
2004 async_synchronize_full_domain(&async_domain);
2006 list_for_each_entry(w, &down_list, power_list) {
2007 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2010 list_for_each_entry(w, &up_list, power_list) {
2011 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2014 /* Power down widgets first; try to avoid amplifying pops. */
2015 dapm_seq_run(card, &down_list, event, false);
2017 dapm_widget_update(card);
2020 dapm_seq_run(card, &up_list, event, true);
2022 /* Run all the bias changes in parallel */
2023 list_for_each_entry(d, &card->dapm_list, list) {
2024 if (d != &card->dapm)
2025 async_schedule_domain(dapm_post_sequence_async, d,
2028 async_synchronize_full_domain(&async_domain);
2029 /* Run card bias changes at last */
2030 dapm_post_sequence_async(&card->dapm, 0);
2032 /* do we need to notify any clients that DAPM event is complete */
2033 list_for_each_entry(d, &card->dapm_list, list) {
2034 if (d->stream_event)
2035 d->stream_event(d, event);
2038 pop_dbg(card->dev, card->pop_time,
2039 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2040 pop_wait(card->pop_time);
2042 trace_snd_soc_dapm_done(card);
2047 #ifdef CONFIG_DEBUG_FS
2048 static ssize_t dapm_widget_power_read_file(struct file *file,
2049 char __user *user_buf,
2050 size_t count, loff_t *ppos)
2052 struct snd_soc_dapm_widget *w = file->private_data;
2053 struct snd_soc_card *card = w->dapm->card;
2054 enum snd_soc_dapm_direction dir, rdir;
2058 struct snd_soc_dapm_path *p = NULL;
2060 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2064 mutex_lock(&card->dapm_mutex);
2066 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2071 in = is_connected_input_ep(w, NULL, NULL);
2072 out = is_connected_output_ep(w, NULL, NULL);
2075 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2076 w->name, w->power ? "On" : "Off",
2077 w->force ? " (forced)" : "", in, out);
2080 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2081 " - R%d(0x%x) mask 0x%x",
2082 w->reg, w->reg, w->mask << w->shift);
2084 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2087 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2089 w->active ? "active" : "inactive");
2091 snd_soc_dapm_for_each_direction(dir) {
2092 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2093 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2094 if (p->connected && !p->connected(p->source, p->sink))
2100 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2101 " %s \"%s\" \"%s\"\n",
2102 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2103 p->name ? p->name : "static",
2104 p->node[rdir]->name);
2108 mutex_unlock(&card->dapm_mutex);
2110 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2116 static const struct file_operations dapm_widget_power_fops = {
2117 .open = simple_open,
2118 .read = dapm_widget_power_read_file,
2119 .llseek = default_llseek,
2122 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2123 size_t count, loff_t *ppos)
2125 struct snd_soc_dapm_context *dapm = file->private_data;
2128 switch (dapm->bias_level) {
2129 case SND_SOC_BIAS_ON:
2132 case SND_SOC_BIAS_PREPARE:
2133 level = "Prepare\n";
2135 case SND_SOC_BIAS_STANDBY:
2136 level = "Standby\n";
2138 case SND_SOC_BIAS_OFF:
2142 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2143 level = "Unknown\n";
2147 return simple_read_from_buffer(user_buf, count, ppos, level,
2151 static const struct file_operations dapm_bias_fops = {
2152 .open = simple_open,
2153 .read = dapm_bias_read_file,
2154 .llseek = default_llseek,
2157 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2158 struct dentry *parent)
2162 if (!parent || IS_ERR(parent))
2165 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2167 if (IS_ERR(dapm->debugfs_dapm)) {
2169 "ASoC: Failed to create DAPM debugfs directory %ld\n",
2170 PTR_ERR(dapm->debugfs_dapm));
2174 d = debugfs_create_file("bias_level", 0444,
2175 dapm->debugfs_dapm, dapm,
2179 "ASoC: Failed to create bias level debugfs file: %ld\n",
2183 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2185 struct snd_soc_dapm_context *dapm = w->dapm;
2188 if (!dapm->debugfs_dapm || !w->name)
2191 d = debugfs_create_file(w->name, 0444,
2192 dapm->debugfs_dapm, w,
2193 &dapm_widget_power_fops);
2195 dev_warn(w->dapm->dev,
2196 "ASoC: Failed to create %s debugfs file: %ld\n",
2197 w->name, PTR_ERR(d));
2200 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2202 debugfs_remove_recursive(dapm->debugfs_dapm);
2206 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2207 struct dentry *parent)
2211 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2215 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2222 * soc_dapm_connect_path() - Connects or disconnects a path
2223 * @path: The path to update
2224 * @connect: The new connect state of the path. True if the path is connected,
2225 * false if it is disconnected.
2226 * @reason: The reason why the path changed (for debugging only)
2228 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2229 bool connect, const char *reason)
2231 if (path->connect == connect)
2234 path->connect = connect;
2235 dapm_mark_dirty(path->source, reason);
2236 dapm_mark_dirty(path->sink, reason);
2237 dapm_path_invalidate(path);
2240 /* test and update the power status of a mux widget */
2241 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2242 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2244 struct snd_soc_dapm_path *path;
2248 lockdep_assert_held(&card->dapm_mutex);
2250 /* find dapm widget path assoc with kcontrol */
2251 dapm_kcontrol_for_each_path(path, kcontrol) {
2253 /* we now need to match the string in the enum to the path */
2254 if (!(strcmp(path->name, e->texts[mux])))
2259 soc_dapm_connect_path(path, connect, "mux update");
2263 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2268 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2269 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2270 struct snd_soc_dapm_update *update)
2272 struct snd_soc_card *card = dapm->card;
2275 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2276 card->update = update;
2277 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2278 card->update = NULL;
2279 mutex_unlock(&card->dapm_mutex);
2281 soc_dpcm_runtime_update(card);
2284 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2286 /* test and update the power status of a mixer or switch widget */
2287 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2288 struct snd_kcontrol *kcontrol,
2289 int connect, int rconnect)
2291 struct snd_soc_dapm_path *path;
2294 lockdep_assert_held(&card->dapm_mutex);
2296 /* find dapm widget path assoc with kcontrol */
2297 dapm_kcontrol_for_each_path(path, kcontrol) {
2299 * Ideally this function should support any number of
2300 * paths and channels. But since kcontrols only come
2301 * in mono and stereo variants, we are limited to 2
2304 * The following code assumes for stereo controls the
2305 * first path (when 'found == 0') is the left channel,
2306 * and all remaining paths (when 'found == 1') are the
2309 * A stereo control is signified by a valid 'rconnect'
2310 * value, either 0 for unconnected, or >= 0 for connected.
2311 * This is chosen instead of using snd_soc_volsw_is_stereo,
2312 * so that the behavior of snd_soc_dapm_mixer_update_power
2313 * doesn't change even when the kcontrol passed in is
2316 * It passes 'connect' as the path connect status for
2317 * the left channel, and 'rconnect' for the right
2320 if (found && rconnect >= 0)
2321 soc_dapm_connect_path(path, rconnect, "mixer update");
2323 soc_dapm_connect_path(path, connect, "mixer update");
2328 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2333 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2334 struct snd_kcontrol *kcontrol, int connect,
2335 struct snd_soc_dapm_update *update)
2337 struct snd_soc_card *card = dapm->card;
2340 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2341 card->update = update;
2342 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2343 card->update = NULL;
2344 mutex_unlock(&card->dapm_mutex);
2346 soc_dpcm_runtime_update(card);
2349 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2351 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2354 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2355 struct snd_soc_dapm_widget *w;
2357 char *state = "not set";
2359 /* card won't be set for the dummy component, as a spot fix
2360 * we're checking for that case specifically here but in future
2361 * we will ensure that the dummy component looks like others.
2366 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2367 if (w->dapm != dapm)
2370 /* only display widgets that burn power */
2372 case snd_soc_dapm_hp:
2373 case snd_soc_dapm_mic:
2374 case snd_soc_dapm_spk:
2375 case snd_soc_dapm_line:
2376 case snd_soc_dapm_micbias:
2377 case snd_soc_dapm_dac:
2378 case snd_soc_dapm_adc:
2379 case snd_soc_dapm_pga:
2380 case snd_soc_dapm_out_drv:
2381 case snd_soc_dapm_mixer:
2382 case snd_soc_dapm_mixer_named_ctl:
2383 case snd_soc_dapm_supply:
2384 case snd_soc_dapm_regulator_supply:
2385 case snd_soc_dapm_pinctrl:
2386 case snd_soc_dapm_clock_supply:
2388 count += sprintf(buf + count, "%s: %s\n",
2389 w->name, w->power ? "On":"Off");
2396 switch (snd_soc_dapm_get_bias_level(dapm)) {
2397 case SND_SOC_BIAS_ON:
2400 case SND_SOC_BIAS_PREPARE:
2403 case SND_SOC_BIAS_STANDBY:
2406 case SND_SOC_BIAS_OFF:
2410 count += sprintf(buf + count, "PM State: %s\n", state);
2415 /* show dapm widget status in sys fs */
2416 static ssize_t dapm_widget_show(struct device *dev,
2417 struct device_attribute *attr, char *buf)
2419 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2422 mutex_lock(&rtd->card->dapm_mutex);
2424 for (i = 0; i < rtd->num_codecs; i++) {
2425 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2427 count += dapm_widget_show_component(cmpnt, buf + count);
2430 mutex_unlock(&rtd->card->dapm_mutex);
2435 static DEVICE_ATTR_RO(dapm_widget);
2437 struct attribute *soc_dapm_dev_attrs[] = {
2438 &dev_attr_dapm_widget.attr,
2442 static void dapm_free_path(struct snd_soc_dapm_path *path)
2444 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2445 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2446 list_del(&path->list_kcontrol);
2447 list_del(&path->list);
2451 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2453 struct snd_soc_dapm_path *p, *next_p;
2454 enum snd_soc_dapm_direction dir;
2457 list_del(&w->dirty);
2459 * remove source and sink paths associated to this widget.
2460 * While removing the path, remove reference to it from both
2461 * source and sink widgets so that path is removed only once.
2463 snd_soc_dapm_for_each_direction(dir) {
2464 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2468 kfree(w->kcontrols);
2469 kfree_const(w->name);
2473 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2475 dapm->path_sink_cache.widget = NULL;
2476 dapm->path_source_cache.widget = NULL;
2479 /* free all dapm widgets and resources */
2480 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2482 struct snd_soc_dapm_widget *w, *next_w;
2484 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2485 if (w->dapm != dapm)
2487 snd_soc_dapm_free_widget(w);
2489 snd_soc_dapm_reset_cache(dapm);
2492 static struct snd_soc_dapm_widget *dapm_find_widget(
2493 struct snd_soc_dapm_context *dapm, const char *pin,
2494 bool search_other_contexts)
2496 struct snd_soc_dapm_widget *w;
2497 struct snd_soc_dapm_widget *fallback = NULL;
2499 list_for_each_entry(w, &dapm->card->widgets, list) {
2500 if (!strcmp(w->name, pin)) {
2501 if (w->dapm == dapm)
2508 if (search_other_contexts)
2514 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2515 const char *pin, int status)
2517 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2519 dapm_assert_locked(dapm);
2522 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2526 if (w->connected != status) {
2527 dapm_mark_dirty(w, "pin configuration");
2528 dapm_widget_invalidate_input_paths(w);
2529 dapm_widget_invalidate_output_paths(w);
2532 w->connected = status;
2540 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2541 * @dapm: DAPM context
2543 * Walks all dapm audio paths and powers widgets according to their
2544 * stream or path usage.
2546 * Requires external locking.
2548 * Returns 0 for success.
2550 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2553 * Suppress early reports (eg, jacks syncing their state) to avoid
2554 * silly DAPM runs during card startup.
2556 if (!dapm->card || !dapm->card->instantiated)
2559 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2561 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2564 * snd_soc_dapm_sync - scan and power dapm paths
2565 * @dapm: DAPM context
2567 * Walks all dapm audio paths and powers widgets according to their
2568 * stream or path usage.
2570 * Returns 0 for success.
2572 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2576 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2577 ret = snd_soc_dapm_sync_unlocked(dapm);
2578 mutex_unlock(&dapm->card->dapm_mutex);
2581 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2584 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2585 * @w: The widget for which to update the flags
2587 * Some widgets have a dynamic category which depends on which neighbors they
2588 * are connected to. This function update the category for these widgets.
2590 * This function must be called whenever a path is added or removed to a widget.
2592 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2594 enum snd_soc_dapm_direction dir;
2595 struct snd_soc_dapm_path *p;
2599 case snd_soc_dapm_input:
2600 /* On a fully routed card an input is never a source */
2601 if (w->dapm->card->fully_routed)
2603 ep = SND_SOC_DAPM_EP_SOURCE;
2604 snd_soc_dapm_widget_for_each_source_path(w, p) {
2605 if (p->source->id == snd_soc_dapm_micbias ||
2606 p->source->id == snd_soc_dapm_mic ||
2607 p->source->id == snd_soc_dapm_line ||
2608 p->source->id == snd_soc_dapm_output) {
2614 case snd_soc_dapm_output:
2615 /* On a fully routed card a output is never a sink */
2616 if (w->dapm->card->fully_routed)
2618 ep = SND_SOC_DAPM_EP_SINK;
2619 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2620 if (p->sink->id == snd_soc_dapm_spk ||
2621 p->sink->id == snd_soc_dapm_hp ||
2622 p->sink->id == snd_soc_dapm_line ||
2623 p->sink->id == snd_soc_dapm_input) {
2629 case snd_soc_dapm_line:
2631 snd_soc_dapm_for_each_direction(dir) {
2632 if (!list_empty(&w->edges[dir]))
2633 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2643 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2644 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2645 const char *control)
2647 bool dynamic_source = false;
2648 bool dynamic_sink = false;
2653 switch (source->id) {
2654 case snd_soc_dapm_demux:
2655 dynamic_source = true;
2662 case snd_soc_dapm_mux:
2663 case snd_soc_dapm_switch:
2664 case snd_soc_dapm_mixer:
2665 case snd_soc_dapm_mixer_named_ctl:
2666 dynamic_sink = true;
2672 if (dynamic_source && dynamic_sink) {
2674 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2675 source->name, control, sink->name);
2677 } else if (!dynamic_source && !dynamic_sink) {
2679 "Control not supported for path %s -> [%s] -> %s\n",
2680 source->name, control, sink->name);
2687 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2688 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2689 const char *control,
2690 int (*connected)(struct snd_soc_dapm_widget *source,
2691 struct snd_soc_dapm_widget *sink))
2693 struct snd_soc_dapm_widget *widgets[2];
2694 enum snd_soc_dapm_direction dir;
2695 struct snd_soc_dapm_path *path;
2698 if (wsink->is_supply && !wsource->is_supply) {
2700 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2701 wsource->name, wsink->name);
2705 if (connected && !wsource->is_supply) {
2707 "connected() callback only supported for supply widgets (%s -> %s)\n",
2708 wsource->name, wsink->name);
2712 if (wsource->is_supply && control) {
2714 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2715 wsource->name, control, wsink->name);
2719 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2723 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2727 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2728 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2729 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2730 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2732 path->connected = connected;
2733 INIT_LIST_HEAD(&path->list);
2734 INIT_LIST_HEAD(&path->list_kcontrol);
2736 if (wsource->is_supply || wsink->is_supply)
2737 path->is_supply = 1;
2739 /* connect static paths */
2740 if (control == NULL) {
2743 switch (wsource->id) {
2744 case snd_soc_dapm_demux:
2745 ret = dapm_connect_mux(dapm, path, control, wsource);
2753 switch (wsink->id) {
2754 case snd_soc_dapm_mux:
2755 ret = dapm_connect_mux(dapm, path, control, wsink);
2759 case snd_soc_dapm_switch:
2760 case snd_soc_dapm_mixer:
2761 case snd_soc_dapm_mixer_named_ctl:
2762 ret = dapm_connect_mixer(dapm, path, control);
2771 list_add(&path->list, &dapm->card->paths);
2772 snd_soc_dapm_for_each_direction(dir)
2773 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2775 snd_soc_dapm_for_each_direction(dir) {
2776 dapm_update_widget_flags(widgets[dir]);
2777 dapm_mark_dirty(widgets[dir], "Route added");
2780 if (dapm->card->instantiated && path->connect)
2781 dapm_path_invalidate(path);
2789 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2790 const struct snd_soc_dapm_route *route)
2792 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2793 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2796 char prefixed_sink[80];
2797 char prefixed_source[80];
2801 prefix = soc_dapm_prefix(dapm);
2803 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2804 prefix, route->sink);
2805 sink = prefixed_sink;
2806 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2807 prefix, route->source);
2808 source = prefixed_source;
2811 source = route->source;
2814 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2815 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2817 if (wsink && wsource)
2821 * find src and dest widgets over all widgets but favor a widget from
2822 * current DAPM context
2824 list_for_each_entry(w, &dapm->card->widgets, list) {
2825 if (!wsink && !(strcmp(w->name, sink))) {
2827 if (w->dapm == dapm) {
2834 if (!wsource && !(strcmp(w->name, source))) {
2836 if (w->dapm == dapm) {
2843 /* use widget from another DAPM context if not found from this */
2849 if (wsource == NULL) {
2850 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2854 if (wsink == NULL) {
2855 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2861 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2862 dapm_wcache_update(&dapm->path_source_cache, wsource);
2864 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2871 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2872 source, route->control, sink);
2876 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2877 const struct snd_soc_dapm_route *route)
2879 struct snd_soc_dapm_widget *wsource, *wsink;
2880 struct snd_soc_dapm_path *path, *p;
2883 char prefixed_sink[80];
2884 char prefixed_source[80];
2887 if (route->control) {
2889 "ASoC: Removal of routes with controls not supported\n");
2893 prefix = soc_dapm_prefix(dapm);
2895 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2896 prefix, route->sink);
2897 sink = prefixed_sink;
2898 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2899 prefix, route->source);
2900 source = prefixed_source;
2903 source = route->source;
2907 list_for_each_entry(p, &dapm->card->paths, list) {
2908 if (strcmp(p->source->name, source) != 0)
2910 if (strcmp(p->sink->name, sink) != 0)
2917 wsource = path->source;
2920 dapm_mark_dirty(wsource, "Route removed");
2921 dapm_mark_dirty(wsink, "Route removed");
2923 dapm_path_invalidate(path);
2925 dapm_free_path(path);
2927 /* Update any path related flags */
2928 dapm_update_widget_flags(wsource);
2929 dapm_update_widget_flags(wsink);
2931 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2939 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2940 * @dapm: DAPM context
2941 * @route: audio routes
2942 * @num: number of routes
2944 * Connects 2 dapm widgets together via a named audio path. The sink is
2945 * the widget receiving the audio signal, whilst the source is the sender
2946 * of the audio signal.
2948 * Returns 0 for success else error. On error all resources can be freed
2949 * with a call to snd_soc_card_free().
2951 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2952 const struct snd_soc_dapm_route *route, int num)
2956 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2957 for (i = 0; i < num; i++) {
2958 r = snd_soc_dapm_add_route(dapm, route);
2960 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2962 route->control ? route->control : "direct",
2968 mutex_unlock(&dapm->card->dapm_mutex);
2972 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2975 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2976 * @dapm: DAPM context
2977 * @route: audio routes
2978 * @num: number of routes
2980 * Removes routes from the DAPM context.
2982 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2983 const struct snd_soc_dapm_route *route, int num)
2987 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2988 for (i = 0; i < num; i++) {
2989 snd_soc_dapm_del_route(dapm, route);
2992 mutex_unlock(&dapm->card->dapm_mutex);
2996 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2998 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2999 const struct snd_soc_dapm_route *route)
3001 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3004 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3007 struct snd_soc_dapm_path *path;
3011 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3017 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3022 if (route->control || route->connected)
3023 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3024 route->source, route->sink);
3026 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3027 if (path->sink == sink) {
3034 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3035 route->source, route->sink);
3037 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3038 count, route->source, route->sink);
3044 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3045 * @dapm: DAPM context
3046 * @route: audio routes
3047 * @num: number of routes
3049 * Mark existing routes matching those specified in the passed array
3050 * as being weak, meaning that they are ignored for the purpose of
3051 * power decisions. The main intended use case is for sidetone paths
3052 * which couple audio between other independent paths if they are both
3053 * active in order to make the combination work better at the user
3054 * level but which aren't intended to be "used".
3056 * Note that CODEC drivers should not use this as sidetone type paths
3057 * can frequently also be used as bypass paths.
3059 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3060 const struct snd_soc_dapm_route *route, int num)
3065 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3066 for (i = 0; i < num; i++) {
3067 err = snd_soc_dapm_weak_route(dapm, route);
3072 mutex_unlock(&dapm->card->dapm_mutex);
3076 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3079 * snd_soc_dapm_new_widgets - add new dapm widgets
3080 * @card: card to be checked for new dapm widgets
3082 * Checks the codec for any new dapm widgets and creates them if found.
3084 * Returns 0 for success.
3086 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3088 struct snd_soc_dapm_widget *w;
3091 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3093 list_for_each_entry(w, &card->widgets, list)
3098 if (w->num_kcontrols) {
3099 w->kcontrols = kcalloc(w->num_kcontrols,
3100 sizeof(struct snd_kcontrol *),
3102 if (!w->kcontrols) {
3103 mutex_unlock(&card->dapm_mutex);
3109 case snd_soc_dapm_switch:
3110 case snd_soc_dapm_mixer:
3111 case snd_soc_dapm_mixer_named_ctl:
3114 case snd_soc_dapm_mux:
3115 case snd_soc_dapm_demux:
3118 case snd_soc_dapm_pga:
3119 case snd_soc_dapm_out_drv:
3122 case snd_soc_dapm_dai_link:
3123 dapm_new_dai_link(w);
3129 /* Read the initial power state from the device */
3131 soc_dapm_read(w->dapm, w->reg, &val);
3132 val = val >> w->shift;
3134 if (val == w->on_val)
3140 dapm_mark_dirty(w, "new widget");
3141 dapm_debugfs_add_widget(w);
3144 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3145 mutex_unlock(&card->dapm_mutex);
3148 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3151 * snd_soc_dapm_get_volsw - dapm mixer get callback
3152 * @kcontrol: mixer control
3153 * @ucontrol: control element information
3155 * Callback to get the value of a dapm mixer control.
3157 * Returns 0 for success.
3159 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3160 struct snd_ctl_elem_value *ucontrol)
3162 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3163 struct snd_soc_card *card = dapm->card;
3164 struct soc_mixer_control *mc =
3165 (struct soc_mixer_control *)kcontrol->private_value;
3167 unsigned int shift = mc->shift;
3169 unsigned int width = fls(max);
3170 unsigned int mask = (1 << fls(max)) - 1;
3171 unsigned int invert = mc->invert;
3172 unsigned int reg_val, val, rval = 0;
3175 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3176 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3177 ret = soc_dapm_read(dapm, reg, ®_val);
3178 val = (reg_val >> shift) & mask;
3180 if (ret == 0 && reg != mc->rreg)
3181 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3183 if (snd_soc_volsw_is_stereo(mc))
3184 rval = (reg_val >> mc->rshift) & mask;
3186 reg_val = dapm_kcontrol_get_value(kcontrol);
3187 val = reg_val & mask;
3189 if (snd_soc_volsw_is_stereo(mc))
3190 rval = (reg_val >> width) & mask;
3192 mutex_unlock(&card->dapm_mutex);
3198 ucontrol->value.integer.value[0] = max - val;
3200 ucontrol->value.integer.value[0] = val;
3202 if (snd_soc_volsw_is_stereo(mc)) {
3204 ucontrol->value.integer.value[1] = max - rval;
3206 ucontrol->value.integer.value[1] = rval;
3211 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3214 * snd_soc_dapm_put_volsw - dapm mixer set callback
3215 * @kcontrol: mixer control
3216 * @ucontrol: control element information
3218 * Callback to set the value of a dapm mixer control.
3220 * Returns 0 for success.
3222 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3223 struct snd_ctl_elem_value *ucontrol)
3225 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3226 struct snd_soc_card *card = dapm->card;
3227 struct soc_mixer_control *mc =
3228 (struct soc_mixer_control *)kcontrol->private_value;
3230 unsigned int shift = mc->shift;
3232 unsigned int width = fls(max);
3233 unsigned int mask = (1 << width) - 1;
3234 unsigned int invert = mc->invert;
3235 unsigned int val, rval = 0;
3236 int connect, rconnect = -1, change, reg_change = 0;
3237 struct snd_soc_dapm_update update = {};
3240 val = (ucontrol->value.integer.value[0] & mask);
3246 if (snd_soc_volsw_is_stereo(mc)) {
3247 rval = (ucontrol->value.integer.value[1] & mask);
3253 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3255 /* This assumes field width < (bits in unsigned int / 2) */
3256 if (width > sizeof(unsigned int) * 8 / 2)
3258 "ASoC: control %s field width limit exceeded\n",
3260 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3262 if (reg != SND_SOC_NOPM) {
3264 rval = rval << mc->rshift;
3266 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3268 if (snd_soc_volsw_is_stereo(mc))
3269 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3274 if (change || reg_change) {
3276 if (snd_soc_volsw_is_stereo(mc)) {
3277 update.has_second_set = true;
3278 update.reg2 = mc->rreg;
3279 update.mask2 = mask << mc->rshift;
3282 update.kcontrol = kcontrol;
3284 update.mask = mask << shift;
3286 card->update = &update;
3288 change |= reg_change;
3290 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3293 card->update = NULL;
3296 mutex_unlock(&card->dapm_mutex);
3299 soc_dpcm_runtime_update(card);
3303 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3306 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3307 * @kcontrol: mixer control
3308 * @ucontrol: control element information
3310 * Callback to get the value of a dapm enumerated double mixer control.
3312 * Returns 0 for success.
3314 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3315 struct snd_ctl_elem_value *ucontrol)
3317 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3318 struct snd_soc_card *card = dapm->card;
3319 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3320 unsigned int reg_val, val;
3322 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3323 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3324 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3326 mutex_unlock(&card->dapm_mutex);
3330 reg_val = dapm_kcontrol_get_value(kcontrol);
3332 mutex_unlock(&card->dapm_mutex);
3334 val = (reg_val >> e->shift_l) & e->mask;
3335 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3336 if (e->shift_l != e->shift_r) {
3337 val = (reg_val >> e->shift_r) & e->mask;
3338 val = snd_soc_enum_val_to_item(e, val);
3339 ucontrol->value.enumerated.item[1] = val;
3344 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3347 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3348 * @kcontrol: mixer control
3349 * @ucontrol: control element information
3351 * Callback to set the value of a dapm enumerated double mixer control.
3353 * Returns 0 for success.
3355 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3356 struct snd_ctl_elem_value *ucontrol)
3358 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3359 struct snd_soc_card *card = dapm->card;
3360 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3361 unsigned int *item = ucontrol->value.enumerated.item;
3362 unsigned int val, change, reg_change = 0;
3364 struct snd_soc_dapm_update update = {};
3367 if (item[0] >= e->items)
3370 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3371 mask = e->mask << e->shift_l;
3372 if (e->shift_l != e->shift_r) {
3373 if (item[1] > e->items)
3375 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3376 mask |= e->mask << e->shift_r;
3379 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3381 change = dapm_kcontrol_set_value(kcontrol, val);
3383 if (e->reg != SND_SOC_NOPM)
3384 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3386 if (change || reg_change) {
3388 update.kcontrol = kcontrol;
3389 update.reg = e->reg;
3392 card->update = &update;
3394 change |= reg_change;
3396 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3398 card->update = NULL;
3401 mutex_unlock(&card->dapm_mutex);
3404 soc_dpcm_runtime_update(card);
3408 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3411 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3413 * @kcontrol: mixer control
3414 * @uinfo: control element information
3416 * Callback to provide information about a pin switch control.
3418 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3419 struct snd_ctl_elem_info *uinfo)
3421 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3423 uinfo->value.integer.min = 0;
3424 uinfo->value.integer.max = 1;
3428 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3431 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3433 * @kcontrol: mixer control
3436 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3437 struct snd_ctl_elem_value *ucontrol)
3439 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3440 const char *pin = (const char *)kcontrol->private_value;
3442 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3444 ucontrol->value.integer.value[0] =
3445 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3447 mutex_unlock(&card->dapm_mutex);
3451 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3454 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3456 * @kcontrol: mixer control
3459 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3460 struct snd_ctl_elem_value *ucontrol)
3462 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3463 const char *pin = (const char *)kcontrol->private_value;
3465 if (ucontrol->value.integer.value[0])
3466 snd_soc_dapm_enable_pin(&card->dapm, pin);
3468 snd_soc_dapm_disable_pin(&card->dapm, pin);
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_pinctrl:
3537 w->pinctrl = devm_pinctrl_get(dapm->dev);
3538 if (IS_ERR(w->pinctrl)) {
3539 ret = PTR_ERR(w->pinctrl);
3540 if (ret == -EPROBE_DEFER)
3541 return ERR_PTR(ret);
3542 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3547 case snd_soc_dapm_clock_supply:
3548 #ifdef CONFIG_CLKDEV_LOOKUP
3549 w->clk = devm_clk_get(dapm->dev, w->name);
3550 if (IS_ERR(w->clk)) {
3551 ret = PTR_ERR(w->clk);
3552 if (ret == -EPROBE_DEFER)
3553 return ERR_PTR(ret);
3554 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3566 prefix = soc_dapm_prefix(dapm);
3568 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3570 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3571 if (w->name == NULL) {
3577 case snd_soc_dapm_mic:
3578 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3579 w->power_check = dapm_generic_check_power;
3581 case snd_soc_dapm_input:
3582 if (!dapm->card->fully_routed)
3583 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3584 w->power_check = dapm_generic_check_power;
3586 case snd_soc_dapm_spk:
3587 case snd_soc_dapm_hp:
3588 w->is_ep = SND_SOC_DAPM_EP_SINK;
3589 w->power_check = dapm_generic_check_power;
3591 case snd_soc_dapm_output:
3592 if (!dapm->card->fully_routed)
3593 w->is_ep = SND_SOC_DAPM_EP_SINK;
3594 w->power_check = dapm_generic_check_power;
3596 case snd_soc_dapm_vmid:
3597 case snd_soc_dapm_siggen:
3598 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3599 w->power_check = dapm_always_on_check_power;
3601 case snd_soc_dapm_sink:
3602 w->is_ep = SND_SOC_DAPM_EP_SINK;
3603 w->power_check = dapm_always_on_check_power;
3606 case snd_soc_dapm_mux:
3607 case snd_soc_dapm_demux:
3608 case snd_soc_dapm_switch:
3609 case snd_soc_dapm_mixer:
3610 case snd_soc_dapm_mixer_named_ctl:
3611 case snd_soc_dapm_adc:
3612 case snd_soc_dapm_aif_out:
3613 case snd_soc_dapm_dac:
3614 case snd_soc_dapm_aif_in:
3615 case snd_soc_dapm_pga:
3616 case snd_soc_dapm_out_drv:
3617 case snd_soc_dapm_micbias:
3618 case snd_soc_dapm_line:
3619 case snd_soc_dapm_dai_link:
3620 case snd_soc_dapm_dai_out:
3621 case snd_soc_dapm_dai_in:
3622 w->power_check = dapm_generic_check_power;
3624 case snd_soc_dapm_supply:
3625 case snd_soc_dapm_regulator_supply:
3626 case snd_soc_dapm_pinctrl:
3627 case snd_soc_dapm_clock_supply:
3628 case snd_soc_dapm_kcontrol:
3630 w->power_check = dapm_supply_check_power;
3633 w->power_check = dapm_always_on_check_power;
3638 INIT_LIST_HEAD(&w->list);
3639 INIT_LIST_HEAD(&w->dirty);
3640 list_add_tail(&w->list, &dapm->card->widgets);
3642 snd_soc_dapm_for_each_direction(dir) {
3643 INIT_LIST_HEAD(&w->edges[dir]);
3644 w->endpoints[dir] = -1;
3647 /* machine layer sets up unconnected pins and insertions */
3653 * snd_soc_dapm_new_controls - create new dapm controls
3654 * @dapm: DAPM context
3655 * @widget: widget array
3656 * @num: number of widgets
3658 * Creates new DAPM controls based upon the templates.
3660 * Returns 0 for success else error.
3662 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3663 const struct snd_soc_dapm_widget *widget,
3666 struct snd_soc_dapm_widget *w;
3670 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3671 for (i = 0; i < num; i++) {
3672 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3675 /* Do not nag about probe deferrals */
3676 if (ret == -EPROBE_DEFER)
3679 "ASoC: Failed to create DAPM control %s (%d)\n",
3685 "ASoC: Failed to create DAPM control %s\n",
3692 mutex_unlock(&dapm->card->dapm_mutex);
3695 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3697 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3698 struct snd_kcontrol *kcontrol, int event)
3700 struct snd_soc_dapm_path *source_p, *sink_p;
3701 struct snd_soc_dai *source, *sink;
3702 struct snd_soc_pcm_runtime *rtd = w->priv;
3703 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
3704 struct snd_pcm_substream substream;
3705 struct snd_pcm_hw_params *params = NULL;
3706 struct snd_pcm_runtime *runtime = NULL;
3710 if (WARN_ON(!config) ||
3711 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3712 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3715 /* We only support a single source and sink, pick the first */
3716 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3717 struct snd_soc_dapm_path,
3718 list_node[SND_SOC_DAPM_DIR_OUT]);
3719 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3720 struct snd_soc_dapm_path,
3721 list_node[SND_SOC_DAPM_DIR_IN]);
3723 source = source_p->source->priv;
3724 sink = sink_p->sink->priv;
3726 /* Be a little careful as we don't want to overflow the mask array */
3727 if (config->formats) {
3728 fmt = ffs(config->formats) - 1;
3730 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3735 /* Currently very limited parameter selection */
3736 params = kzalloc(sizeof(*params), GFP_KERNEL);
3741 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3743 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3745 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3748 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3749 = config->channels_min;
3750 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3751 = config->channels_max;
3753 memset(&substream, 0, sizeof(substream));
3755 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3756 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3761 substream.runtime = runtime;
3762 substream.private_data = rtd;
3765 case SND_SOC_DAPM_PRE_PMU:
3766 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3767 if (source->driver->ops->startup) {
3768 ret = source->driver->ops->startup(&substream, source);
3770 dev_err(source->dev,
3771 "ASoC: startup() failed: %d\n", ret);
3776 ret = soc_dai_hw_params(&substream, params, source);
3780 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3781 if (sink->driver->ops->startup) {
3782 ret = sink->driver->ops->startup(&substream, sink);
3785 "ASoC: startup() failed: %d\n", ret);
3790 ret = soc_dai_hw_params(&substream, params, sink);
3795 case SND_SOC_DAPM_POST_PMU:
3796 ret = snd_soc_dai_digital_mute(sink, 0,
3797 SNDRV_PCM_STREAM_PLAYBACK);
3798 if (ret != 0 && ret != -ENOTSUPP)
3799 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3803 case SND_SOC_DAPM_PRE_PMD:
3804 ret = snd_soc_dai_digital_mute(sink, 1,
3805 SNDRV_PCM_STREAM_PLAYBACK);
3806 if (ret != 0 && ret != -ENOTSUPP)
3807 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3811 if (source->driver->ops->shutdown) {
3812 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3813 source->driver->ops->shutdown(&substream, source);
3817 if (sink->driver->ops->shutdown) {
3818 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3819 sink->driver->ops->shutdown(&substream, sink);
3824 WARN(1, "Unknown event %d\n", event);
3834 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3835 struct snd_ctl_elem_value *ucontrol)
3837 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3839 ucontrol->value.enumerated.item[0] = w->params_select;
3844 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3845 struct snd_ctl_elem_value *ucontrol)
3847 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3849 /* Can't change the config when widget is already powered */
3853 if (ucontrol->value.enumerated.item[0] == w->params_select)
3856 if (ucontrol->value.enumerated.item[0] >= w->num_params)
3859 w->params_select = ucontrol->value.enumerated.item[0];
3865 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3866 unsigned long *private_value,
3868 const char **w_param_text)
3872 devm_kfree(card->dev, (void *)*private_value);
3877 for (count = 0 ; count < num_params; count++)
3878 devm_kfree(card->dev, (void *)w_param_text[count]);
3879 devm_kfree(card->dev, w_param_text);
3882 static struct snd_kcontrol_new *
3883 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3885 const struct snd_soc_pcm_stream *params,
3886 int num_params, const char **w_param_text,
3887 unsigned long *private_value)
3889 struct soc_enum w_param_enum[] = {
3890 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3892 struct snd_kcontrol_new kcontrol_dai_link[] = {
3893 SOC_ENUM_EXT(NULL, w_param_enum[0],
3894 snd_soc_dapm_dai_link_get,
3895 snd_soc_dapm_dai_link_put),
3897 struct snd_kcontrol_new *kcontrol_news;
3898 const struct snd_soc_pcm_stream *config = params;
3901 for (count = 0 ; count < num_params; count++) {
3902 if (!config->stream_name) {
3903 dev_warn(card->dapm.dev,
3904 "ASoC: anonymous config %d for dai link %s\n",
3906 w_param_text[count] =
3907 devm_kasprintf(card->dev, GFP_KERNEL,
3908 "Anonymous Configuration %d",
3911 w_param_text[count] = devm_kmemdup(card->dev,
3912 config->stream_name,
3913 strlen(config->stream_name) + 1,
3916 if (!w_param_text[count])
3917 goto outfree_w_param;
3921 w_param_enum[0].items = num_params;
3922 w_param_enum[0].texts = w_param_text;
3925 (unsigned long) devm_kmemdup(card->dev,
3926 (void *)(kcontrol_dai_link[0].private_value),
3927 sizeof(struct soc_enum), GFP_KERNEL);
3928 if (!*private_value) {
3929 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3931 goto outfree_w_param;
3933 kcontrol_dai_link[0].private_value = *private_value;
3934 /* duplicate kcontrol_dai_link on heap so that memory persists */
3935 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3936 sizeof(struct snd_kcontrol_new),
3938 if (!kcontrol_news) {
3939 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3941 goto outfree_w_param;
3943 return kcontrol_news;
3946 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
3950 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3951 struct snd_soc_pcm_runtime *rtd,
3952 const struct snd_soc_pcm_stream *params,
3953 unsigned int num_params,
3954 struct snd_soc_dapm_widget *source,
3955 struct snd_soc_dapm_widget *sink)
3957 struct snd_soc_dapm_widget template;
3958 struct snd_soc_dapm_widget *w;
3959 const char **w_param_text;
3960 unsigned long private_value;
3964 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3965 source->name, sink->name);
3969 memset(&template, 0, sizeof(template));
3970 template.reg = SND_SOC_NOPM;
3971 template.id = snd_soc_dapm_dai_link;
3972 template.name = link_name;
3973 template.event = snd_soc_dai_link_event;
3974 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3975 SND_SOC_DAPM_PRE_PMD;
3976 template.kcontrol_news = NULL;
3978 /* allocate memory for control, only in case of multiple configs */
3979 if (num_params > 1) {
3980 w_param_text = devm_kcalloc(card->dev, num_params,
3981 sizeof(char *), GFP_KERNEL);
3982 if (!w_param_text) {
3987 template.num_kcontrols = 1;
3988 template.kcontrol_news =
3989 snd_soc_dapm_alloc_kcontrol(card,
3990 link_name, params, num_params,
3991 w_param_text, &private_value);
3992 if (!template.kcontrol_news) {
3997 w_param_text = NULL;
3999 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4001 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4004 /* Do not nag about probe deferrals */
4005 if (ret != -EPROBE_DEFER)
4007 "ASoC: Failed to create %s widget (%d)\n",
4009 goto outfree_kcontrol_news;
4012 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
4015 goto outfree_kcontrol_news;
4019 w->num_params = num_params;
4022 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
4025 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
4028 devm_kfree(card->dev, w);
4029 outfree_kcontrol_news:
4030 devm_kfree(card->dev, (void *)template.kcontrol_news);
4031 snd_soc_dapm_free_kcontrol(card, &private_value, num_params, w_param_text);
4033 devm_kfree(card->dev, link_name);
4037 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4038 struct snd_soc_dai *dai)
4040 struct snd_soc_dapm_widget template;
4041 struct snd_soc_dapm_widget *w;
4043 WARN_ON(dapm->dev != dai->dev);
4045 memset(&template, 0, sizeof(template));
4046 template.reg = SND_SOC_NOPM;
4048 if (dai->driver->playback.stream_name) {
4049 template.id = snd_soc_dapm_dai_in;
4050 template.name = dai->driver->playback.stream_name;
4051 template.sname = dai->driver->playback.stream_name;
4053 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4056 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4058 int ret = PTR_ERR(w);
4060 /* Do not nag about probe deferrals */
4061 if (ret != -EPROBE_DEFER)
4063 "ASoC: Failed to create %s widget (%d)\n",
4064 dai->driver->playback.stream_name, ret);
4068 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4069 dai->driver->playback.stream_name);
4074 dai->playback_widget = w;
4077 if (dai->driver->capture.stream_name) {
4078 template.id = snd_soc_dapm_dai_out;
4079 template.name = dai->driver->capture.stream_name;
4080 template.sname = dai->driver->capture.stream_name;
4082 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4085 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4087 int ret = PTR_ERR(w);
4089 /* Do not nag about probe deferrals */
4090 if (ret != -EPROBE_DEFER)
4092 "ASoC: Failed to create %s widget (%d)\n",
4093 dai->driver->playback.stream_name, ret);
4097 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4098 dai->driver->capture.stream_name);
4103 dai->capture_widget = w;
4109 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4111 struct snd_soc_dapm_widget *dai_w, *w;
4112 struct snd_soc_dapm_widget *src, *sink;
4113 struct snd_soc_dai *dai;
4115 /* For each DAI widget... */
4116 list_for_each_entry(dai_w, &card->widgets, list) {
4117 switch (dai_w->id) {
4118 case snd_soc_dapm_dai_in:
4119 case snd_soc_dapm_dai_out:
4125 /* let users know there is no DAI to link */
4127 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4134 /* ...find all widgets with the same stream and link them */
4135 list_for_each_entry(w, &card->widgets, list) {
4136 if (w->dapm != dai_w->dapm)
4140 case snd_soc_dapm_dai_in:
4141 case snd_soc_dapm_dai_out:
4147 if (!w->sname || !strstr(w->sname, dai_w->sname))
4150 if (dai_w->id == snd_soc_dapm_dai_in) {
4157 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4158 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4165 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4166 struct snd_soc_pcm_runtime *rtd)
4168 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4169 struct snd_soc_dapm_widget *sink, *source;
4172 for (i = 0; i < rtd->num_codecs; i++) {
4173 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
4175 /* connect BE DAI playback if widgets are valid */
4176 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
4177 source = cpu_dai->playback_widget;
4178 sink = codec_dai->playback_widget;
4179 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4180 cpu_dai->component->name, source->name,
4181 codec_dai->component->name, sink->name);
4183 snd_soc_dapm_add_path(&card->dapm, source, sink,
4187 /* connect BE DAI capture if widgets are valid */
4188 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
4189 source = codec_dai->capture_widget;
4190 sink = cpu_dai->capture_widget;
4191 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4192 codec_dai->component->name, source->name,
4193 cpu_dai->component->name, sink->name);
4195 snd_soc_dapm_add_path(&card->dapm, source, sink,
4201 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4204 struct snd_soc_dapm_widget *w;
4207 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4208 w = dai->playback_widget;
4210 w = dai->capture_widget;
4213 dapm_mark_dirty(w, "stream event");
4215 if (w->id == snd_soc_dapm_dai_in) {
4216 ep = SND_SOC_DAPM_EP_SOURCE;
4217 dapm_widget_invalidate_input_paths(w);
4219 ep = SND_SOC_DAPM_EP_SINK;
4220 dapm_widget_invalidate_output_paths(w);
4224 case SND_SOC_DAPM_STREAM_START:
4228 case SND_SOC_DAPM_STREAM_STOP:
4232 case SND_SOC_DAPM_STREAM_SUSPEND:
4233 case SND_SOC_DAPM_STREAM_RESUME:
4234 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4235 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4241 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4243 struct snd_soc_pcm_runtime *rtd;
4245 /* for each BE DAI link... */
4246 list_for_each_entry(rtd, &card->rtd_list, list) {
4248 * dynamic FE links have no fixed DAI mapping.
4249 * CODEC<->CODEC links have no direct connection.
4251 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4254 dapm_connect_dai_link_widgets(card, rtd);
4258 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4263 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4264 for (i = 0; i < rtd->num_codecs; i++)
4265 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
4267 dapm_power_widgets(rtd->card, event);
4271 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4272 * @rtd: PCM runtime data
4273 * @stream: stream name
4274 * @event: stream event
4276 * Sends a stream event to the dapm core. The core then makes any
4277 * necessary widget power changes.
4279 * Returns 0 for success else error.
4281 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4284 struct snd_soc_card *card = rtd->card;
4286 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4287 soc_dapm_stream_event(rtd, stream, event);
4288 mutex_unlock(&card->dapm_mutex);
4292 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4293 * @dapm: DAPM context
4296 * Enables input/output pin and its parents or children widgets iff there is
4297 * a valid audio route and active audio stream.
4299 * Requires external locking.
4301 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4302 * do any widget power switching.
4304 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4307 return snd_soc_dapm_set_pin(dapm, pin, 1);
4309 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4312 * snd_soc_dapm_enable_pin - enable pin.
4313 * @dapm: DAPM context
4316 * Enables input/output pin and its parents or children widgets iff there is
4317 * a valid audio route and active audio stream.
4319 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4320 * do any widget power switching.
4322 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4326 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4328 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4330 mutex_unlock(&dapm->card->dapm_mutex);
4334 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4337 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4338 * @dapm: DAPM context
4341 * Enables input/output pin regardless of any other state. This is
4342 * intended for use with microphone bias supplies used in microphone
4345 * Requires external locking.
4347 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4348 * do any widget power switching.
4350 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4353 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4356 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4360 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4361 if (!w->connected) {
4363 * w->force does not affect the number of input or output paths,
4364 * so we only have to recheck if w->connected is changed
4366 dapm_widget_invalidate_input_paths(w);
4367 dapm_widget_invalidate_output_paths(w);
4371 dapm_mark_dirty(w, "force enable");
4375 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4378 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4379 * @dapm: DAPM context
4382 * Enables input/output pin regardless of any other state. This is
4383 * intended for use with microphone bias supplies used in microphone
4386 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4387 * do any widget power switching.
4389 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4394 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4396 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4398 mutex_unlock(&dapm->card->dapm_mutex);
4402 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4405 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4406 * @dapm: DAPM context
4409 * Disables input/output pin and its parents or children widgets.
4411 * Requires external locking.
4413 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4414 * do any widget power switching.
4416 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4419 return snd_soc_dapm_set_pin(dapm, pin, 0);
4421 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4424 * snd_soc_dapm_disable_pin - disable pin.
4425 * @dapm: DAPM context
4428 * Disables input/output pin and its parents or children widgets.
4430 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4431 * do any widget power switching.
4433 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4438 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4440 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4442 mutex_unlock(&dapm->card->dapm_mutex);
4446 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4449 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4450 * @dapm: DAPM context
4453 * Marks the specified pin as being not connected, disabling it along
4454 * any parent or child widgets. At present this is identical to
4455 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4456 * additional things such as disabling controls which only affect
4457 * paths through the pin.
4459 * Requires external locking.
4461 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4462 * do any widget power switching.
4464 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4467 return snd_soc_dapm_set_pin(dapm, pin, 0);
4469 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4472 * snd_soc_dapm_nc_pin - permanently disable pin.
4473 * @dapm: DAPM context
4476 * Marks the specified pin as being not connected, disabling it along
4477 * any parent or child widgets. At present this is identical to
4478 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4479 * additional things such as disabling controls which only affect
4480 * paths through the pin.
4482 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4483 * do any widget power switching.
4485 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4489 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4491 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4493 mutex_unlock(&dapm->card->dapm_mutex);
4497 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4500 * snd_soc_dapm_get_pin_status - get audio pin status
4501 * @dapm: DAPM context
4502 * @pin: audio signal pin endpoint (or start point)
4504 * Get audio pin status - connected or disconnected.
4506 * Returns 1 for connected otherwise 0.
4508 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4511 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4514 return w->connected;
4518 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4521 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4522 * @dapm: DAPM context
4523 * @pin: audio signal pin endpoint (or start point)
4525 * Mark the given endpoint or pin as ignoring suspend. When the
4526 * system is disabled a path between two endpoints flagged as ignoring
4527 * suspend will not be disabled. The path must already be enabled via
4528 * normal means at suspend time, it will not be turned on if it was not
4531 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4534 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4537 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4541 w->ignore_suspend = 1;
4545 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4548 * snd_soc_dapm_free - free dapm resources
4549 * @dapm: DAPM context
4551 * Free all dapm widgets and resources.
4553 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4555 dapm_debugfs_cleanup(dapm);
4556 dapm_free_widgets(dapm);
4557 list_del(&dapm->list);
4559 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4561 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4563 struct snd_soc_card *card = dapm->card;
4564 struct snd_soc_dapm_widget *w;
4565 LIST_HEAD(down_list);
4568 mutex_lock(&card->dapm_mutex);
4570 list_for_each_entry(w, &dapm->card->widgets, list) {
4571 if (w->dapm != dapm)
4574 dapm_seq_insert(w, &down_list, false);
4580 /* If there were no widgets to power down we're already in
4584 if (dapm->bias_level == SND_SOC_BIAS_ON)
4585 snd_soc_dapm_set_bias_level(dapm,
4586 SND_SOC_BIAS_PREPARE);
4587 dapm_seq_run(card, &down_list, 0, false);
4588 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4589 snd_soc_dapm_set_bias_level(dapm,
4590 SND_SOC_BIAS_STANDBY);
4593 mutex_unlock(&card->dapm_mutex);
4597 * snd_soc_dapm_shutdown - callback for system shutdown
4599 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4601 struct snd_soc_dapm_context *dapm;
4603 list_for_each_entry(dapm, &card->dapm_list, list) {
4604 if (dapm != &card->dapm) {
4605 soc_dapm_shutdown_dapm(dapm);
4606 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4607 snd_soc_dapm_set_bias_level(dapm,
4612 soc_dapm_shutdown_dapm(&card->dapm);
4613 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4614 snd_soc_dapm_set_bias_level(&card->dapm,
4618 /* Module information */
4619 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4620 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4621 MODULE_LICENSE("GPL");