1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
17 // o Delayed power down of audio subsystem to reduce pops between a quick
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/async.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/platform_device.h>
27 #include <linux/jiffies.h>
28 #include <linux/debugfs.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/clk.h>
33 #include <linux/slab.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/initval.h>
40 #include <trace/events/asoc.h>
42 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
44 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
45 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
47 #define snd_soc_dapm_for_each_direction(dir) \
48 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
51 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
52 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
54 int (*connected)(struct snd_soc_dapm_widget *source,
55 struct snd_soc_dapm_widget *sink));
57 struct snd_soc_dapm_widget *
58 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
59 const struct snd_soc_dapm_widget *widget);
61 struct snd_soc_dapm_widget *
62 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
63 const struct snd_soc_dapm_widget *widget);
65 /* dapm power sequences - make this per codec in the future */
66 static int dapm_up_seq[] = {
67 [snd_soc_dapm_pre] = 0,
68 [snd_soc_dapm_regulator_supply] = 1,
69 [snd_soc_dapm_pinctrl] = 1,
70 [snd_soc_dapm_clock_supply] = 1,
71 [snd_soc_dapm_supply] = 2,
72 [snd_soc_dapm_micbias] = 3,
73 [snd_soc_dapm_vmid] = 3,
74 [snd_soc_dapm_dai_link] = 2,
75 [snd_soc_dapm_dai_in] = 4,
76 [snd_soc_dapm_dai_out] = 4,
77 [snd_soc_dapm_aif_in] = 4,
78 [snd_soc_dapm_aif_out] = 4,
79 [snd_soc_dapm_mic] = 5,
80 [snd_soc_dapm_siggen] = 5,
81 [snd_soc_dapm_input] = 5,
82 [snd_soc_dapm_output] = 5,
83 [snd_soc_dapm_mux] = 6,
84 [snd_soc_dapm_demux] = 6,
85 [snd_soc_dapm_dac] = 7,
86 [snd_soc_dapm_switch] = 8,
87 [snd_soc_dapm_mixer] = 8,
88 [snd_soc_dapm_mixer_named_ctl] = 8,
89 [snd_soc_dapm_pga] = 9,
90 [snd_soc_dapm_buffer] = 9,
91 [snd_soc_dapm_scheduler] = 9,
92 [snd_soc_dapm_effect] = 9,
93 [snd_soc_dapm_src] = 9,
94 [snd_soc_dapm_asrc] = 9,
95 [snd_soc_dapm_encoder] = 9,
96 [snd_soc_dapm_decoder] = 9,
97 [snd_soc_dapm_adc] = 10,
98 [snd_soc_dapm_out_drv] = 11,
99 [snd_soc_dapm_hp] = 11,
100 [snd_soc_dapm_spk] = 11,
101 [snd_soc_dapm_line] = 11,
102 [snd_soc_dapm_sink] = 11,
103 [snd_soc_dapm_kcontrol] = 12,
104 [snd_soc_dapm_post] = 13,
107 static int dapm_down_seq[] = {
108 [snd_soc_dapm_pre] = 0,
109 [snd_soc_dapm_kcontrol] = 1,
110 [snd_soc_dapm_adc] = 2,
111 [snd_soc_dapm_hp] = 3,
112 [snd_soc_dapm_spk] = 3,
113 [snd_soc_dapm_line] = 3,
114 [snd_soc_dapm_out_drv] = 3,
115 [snd_soc_dapm_sink] = 3,
116 [snd_soc_dapm_pga] = 4,
117 [snd_soc_dapm_buffer] = 4,
118 [snd_soc_dapm_scheduler] = 4,
119 [snd_soc_dapm_effect] = 4,
120 [snd_soc_dapm_src] = 4,
121 [snd_soc_dapm_asrc] = 4,
122 [snd_soc_dapm_encoder] = 4,
123 [snd_soc_dapm_decoder] = 4,
124 [snd_soc_dapm_switch] = 5,
125 [snd_soc_dapm_mixer_named_ctl] = 5,
126 [snd_soc_dapm_mixer] = 5,
127 [snd_soc_dapm_dac] = 6,
128 [snd_soc_dapm_mic] = 7,
129 [snd_soc_dapm_siggen] = 7,
130 [snd_soc_dapm_input] = 7,
131 [snd_soc_dapm_output] = 7,
132 [snd_soc_dapm_micbias] = 8,
133 [snd_soc_dapm_vmid] = 8,
134 [snd_soc_dapm_mux] = 9,
135 [snd_soc_dapm_demux] = 9,
136 [snd_soc_dapm_aif_in] = 10,
137 [snd_soc_dapm_aif_out] = 10,
138 [snd_soc_dapm_dai_in] = 10,
139 [snd_soc_dapm_dai_out] = 10,
140 [snd_soc_dapm_dai_link] = 11,
141 [snd_soc_dapm_supply] = 12,
142 [snd_soc_dapm_clock_supply] = 13,
143 [snd_soc_dapm_pinctrl] = 13,
144 [snd_soc_dapm_regulator_supply] = 13,
145 [snd_soc_dapm_post] = 14,
148 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
150 if (dapm->card && dapm->card->instantiated)
151 lockdep_assert_held(&dapm->card->dapm_mutex);
154 static void pop_wait(u32 pop_time)
157 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
160 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
168 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
173 vsnprintf(buf, PAGE_SIZE, fmt, args);
174 dev_info(dev, "%s", buf);
180 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
182 return !list_empty(&w->dirty);
185 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
187 dapm_assert_locked(w->dapm);
189 if (!dapm_dirty_widget(w)) {
190 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
192 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
197 * Common implementation for dapm_widget_invalidate_input_paths() and
198 * dapm_widget_invalidate_output_paths(). The function is inlined since the
199 * combined size of the two specialized functions is only marginally larger then
200 * the size of the generic function and at the same time the fast path of the
201 * specialized functions is significantly smaller than the generic function.
203 static __always_inline void dapm_widget_invalidate_paths(
204 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
206 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
207 struct snd_soc_dapm_widget *node;
208 struct snd_soc_dapm_path *p;
211 dapm_assert_locked(w->dapm);
213 if (w->endpoints[dir] == -1)
216 list_add_tail(&w->work_list, &list);
217 w->endpoints[dir] = -1;
219 list_for_each_entry(w, &list, work_list) {
220 snd_soc_dapm_widget_for_each_path(w, dir, p) {
221 if (p->is_supply || p->weak || !p->connect)
223 node = p->node[rdir];
224 if (node->endpoints[dir] != -1) {
225 node->endpoints[dir] = -1;
226 list_add_tail(&node->work_list, &list);
233 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
235 * @w: The widget for which to invalidate the cached number of input paths
237 * Resets the cached number of inputs for the specified widget and all widgets
238 * that can be reached via outcoming paths from the widget.
240 * This function must be called if the number of output paths for a widget might
241 * have changed. E.g. if the source state of a widget changes or a path is added
242 * or activated with the widget as the sink.
244 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
246 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
250 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
252 * @w: The widget for which to invalidate the cached number of output paths
254 * Resets the cached number of outputs for the specified widget and all widgets
255 * that can be reached via incoming paths from the widget.
257 * This function must be called if the number of output paths for a widget might
258 * have changed. E.g. if the sink state of a widget changes or a path is added
259 * or activated with the widget as the source.
261 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
263 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
267 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
268 * for the widgets connected to a path
269 * @p: The path to invalidate
271 * Resets the cached number of inputs for the sink of the path and the cached
272 * number of outputs for the source of the path.
274 * This function must be called when a path is added, removed or the connected
277 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
280 * Weak paths or supply paths do not influence the number of input or
281 * output paths of their neighbors.
283 if (p->weak || p->is_supply)
287 * The number of connected endpoints is the sum of the number of
288 * connected endpoints of all neighbors. If a node with 0 connected
289 * endpoints is either connected or disconnected that sum won't change,
290 * so there is no need to re-check the path.
292 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
293 dapm_widget_invalidate_input_paths(p->sink);
294 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
295 dapm_widget_invalidate_output_paths(p->source);
298 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
300 struct snd_soc_dapm_widget *w;
302 mutex_lock(&card->dapm_mutex);
304 list_for_each_entry(w, &card->widgets, list) {
306 dapm_mark_dirty(w, "Rechecking endpoints");
307 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
308 dapm_widget_invalidate_output_paths(w);
309 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
310 dapm_widget_invalidate_input_paths(w);
314 mutex_unlock(&card->dapm_mutex);
316 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
318 /* create a new dapm widget */
319 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
320 const struct snd_soc_dapm_widget *_widget)
322 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
325 struct dapm_kcontrol_data {
327 struct snd_soc_dapm_widget *widget;
328 struct list_head paths;
329 struct snd_soc_dapm_widget_list *wlist;
332 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
333 struct snd_kcontrol *kcontrol, const char *ctrl_name)
335 struct dapm_kcontrol_data *data;
336 struct soc_mixer_control *mc;
341 data = kzalloc(sizeof(*data), GFP_KERNEL);
345 INIT_LIST_HEAD(&data->paths);
347 switch (widget->id) {
348 case snd_soc_dapm_switch:
349 case snd_soc_dapm_mixer:
350 case snd_soc_dapm_mixer_named_ctl:
351 mc = (struct soc_mixer_control *)kcontrol->private_value;
353 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
354 dev_warn(widget->dapm->dev,
355 "ASoC: Unsupported stereo autodisable control '%s'\n",
358 if (mc->autodisable) {
359 struct snd_soc_dapm_widget template;
361 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
368 memset(&template, 0, sizeof(template));
369 template.reg = mc->reg;
370 template.mask = (1 << fls(mc->max)) - 1;
371 template.shift = mc->shift;
373 template.off_val = mc->max;
375 template.off_val = 0;
376 template.on_val = template.off_val;
377 template.id = snd_soc_dapm_kcontrol;
378 template.name = name;
380 data->value = template.on_val;
383 snd_soc_dapm_new_control_unlocked(widget->dapm,
386 if (IS_ERR(data->widget)) {
387 ret = PTR_ERR(data->widget);
392 case snd_soc_dapm_demux:
393 case snd_soc_dapm_mux:
394 e = (struct soc_enum *)kcontrol->private_value;
396 if (e->autodisable) {
397 struct snd_soc_dapm_widget template;
399 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
406 memset(&template, 0, sizeof(template));
407 template.reg = e->reg;
408 template.mask = e->mask << e->shift_l;
409 template.shift = e->shift_l;
410 template.off_val = snd_soc_enum_item_to_val(e, 0);
411 template.on_val = template.off_val;
412 template.id = snd_soc_dapm_kcontrol;
413 template.name = name;
415 data->value = template.on_val;
417 data->widget = snd_soc_dapm_new_control_unlocked(
418 widget->dapm, &template);
420 if (IS_ERR(data->widget)) {
421 ret = PTR_ERR(data->widget);
425 snd_soc_dapm_add_path(widget->dapm, data->widget,
433 kcontrol->private_data = data;
442 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
444 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
446 list_del(&data->paths);
451 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
452 const struct snd_kcontrol *kcontrol)
454 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
459 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
460 struct snd_soc_dapm_widget *widget)
462 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
463 struct snd_soc_dapm_widget_list *new_wlist;
467 n = data->wlist->num_widgets + 1;
471 new_wlist = krealloc(data->wlist,
472 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
476 new_wlist->widgets[n - 1] = widget;
477 new_wlist->num_widgets = n;
479 data->wlist = new_wlist;
484 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
485 struct snd_soc_dapm_path *path)
487 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
489 list_add_tail(&path->list_kcontrol, &data->paths);
492 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
494 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
499 return data->widget->power;
502 static struct list_head *dapm_kcontrol_get_path_list(
503 const struct snd_kcontrol *kcontrol)
505 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
510 #define dapm_kcontrol_for_each_path(path, kcontrol) \
511 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
514 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
516 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
520 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
522 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
525 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
527 if (data->value == value)
531 data->widget->on_val = value;
539 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
541 * @kcontrol: The kcontrol
543 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
544 struct snd_kcontrol *kcontrol)
546 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
548 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
551 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
553 * @kcontrol: The kcontrol
555 * Note: This function must only be used on kcontrols that are known to have
556 * been registered for a CODEC. Otherwise the behaviour is undefined.
558 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
559 struct snd_kcontrol *kcontrol)
561 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
563 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
565 static void dapm_reset(struct snd_soc_card *card)
567 struct snd_soc_dapm_widget *w;
569 lockdep_assert_held(&card->dapm_mutex);
571 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
573 list_for_each_entry(w, &card->widgets, list) {
574 w->new_power = w->power;
575 w->power_checked = false;
579 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
581 if (!dapm->component)
583 return dapm->component->name_prefix;
586 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
589 if (!dapm->component)
591 return snd_soc_component_read(dapm->component, reg, value);
594 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
595 int reg, unsigned int mask, unsigned int value)
597 if (!dapm->component)
599 return snd_soc_component_update_bits(dapm->component, reg,
603 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
604 int reg, unsigned int mask, unsigned int value)
606 if (!dapm->component)
608 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
611 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
614 snd_soc_component_async_complete(dapm->component);
617 static struct snd_soc_dapm_widget *
618 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
620 struct snd_soc_dapm_widget *w = wcache->widget;
621 struct list_head *wlist;
626 wlist = &w->dapm->card->widgets;
628 list_for_each_entry_from(w, wlist, list) {
629 if (!strcmp(name, w->name))
640 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
641 struct snd_soc_dapm_widget *w)
647 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
648 * @dapm: The DAPM context for which to set the level
649 * @level: The level to set
651 * Forces the DAPM bias level to a specific state. It will call the bias level
652 * callback of DAPM context with the specified level. This will even happen if
653 * the context is already at the same level. Furthermore it will not go through
654 * the normal bias level sequencing, meaning any intermediate states between the
655 * current and the target state will not be entered.
657 * Note that the change in bias level is only temporary and the next time
658 * snd_soc_dapm_sync() is called the state will be set to the level as
659 * determined by the DAPM core. The function is mainly intended to be used to
660 * used during probe or resume from suspend to power up the device so
661 * initialization can be done, before the DAPM core takes over.
663 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
664 enum snd_soc_bias_level level)
668 if (dapm->set_bias_level)
669 ret = dapm->set_bias_level(dapm, level);
672 dapm->bias_level = level;
676 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
679 * snd_soc_dapm_set_bias_level - set the bias level for the system
680 * @dapm: DAPM context
681 * @level: level to configure
683 * Configure the bias (power) levels for the SoC audio device.
685 * Returns 0 for success else error.
687 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
688 enum snd_soc_bias_level level)
690 struct snd_soc_card *card = dapm->card;
693 trace_snd_soc_bias_level_start(card, level);
695 if (card && card->set_bias_level)
696 ret = card->set_bias_level(card, dapm, level);
700 if (!card || dapm != &card->dapm)
701 ret = snd_soc_dapm_force_bias_level(dapm, level);
706 if (card && card->set_bias_level_post)
707 ret = card->set_bias_level_post(card, dapm, level);
709 trace_snd_soc_bias_level_done(card, level);
714 /* connect mux widget to its interconnecting audio paths */
715 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
716 struct snd_soc_dapm_path *path, const char *control_name,
717 struct snd_soc_dapm_widget *w)
719 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
720 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
721 unsigned int val, item;
724 if (e->reg != SND_SOC_NOPM) {
725 soc_dapm_read(dapm, e->reg, &val);
726 val = (val >> e->shift_l) & e->mask;
727 item = snd_soc_enum_val_to_item(e, val);
729 /* since a virtual mux has no backing registers to
730 * decide which path to connect, it will try to match
731 * with the first enumeration. This is to ensure
732 * that the default mux choice (the first) will be
733 * correctly powered up during initialization.
738 i = match_string(e->texts, e->items, control_name);
742 path->name = e->texts[i];
743 path->connect = (i == item);
748 /* set up initial codec paths */
749 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
752 struct soc_mixer_control *mc = (struct soc_mixer_control *)
753 p->sink->kcontrol_news[i].private_value;
754 unsigned int reg = mc->reg;
755 unsigned int shift = mc->shift;
756 unsigned int max = mc->max;
757 unsigned int mask = (1 << fls(max)) - 1;
758 unsigned int invert = mc->invert;
761 if (reg != SND_SOC_NOPM) {
762 soc_dapm_read(p->sink->dapm, reg, &val);
764 * The nth_path argument allows this function to know
765 * which path of a kcontrol it is setting the initial
766 * status for. Ideally this would support any number
767 * of paths and channels. But since kcontrols only come
768 * in mono and stereo variants, we are limited to 2
771 * The following code assumes for stereo controls the
772 * first path is the left channel, and all remaining
773 * paths are the right channel.
775 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
777 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
778 val = (val >> mc->rshift) & mask;
780 val = (val >> shift) & mask;
790 /* connect mixer widget to its interconnecting audio paths */
791 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
792 struct snd_soc_dapm_path *path, const char *control_name)
796 /* search for mixer kcontrol */
797 for (i = 0; i < path->sink->num_kcontrols; i++) {
798 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
799 path->name = path->sink->kcontrol_news[i].name;
800 dapm_set_mixer_path_status(path, i, nth_path++);
807 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
808 struct snd_soc_dapm_widget *kcontrolw,
809 const struct snd_kcontrol_new *kcontrol_new,
810 struct snd_kcontrol **kcontrol)
812 struct snd_soc_dapm_widget *w;
817 list_for_each_entry(w, &dapm->card->widgets, list) {
818 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
820 for (i = 0; i < w->num_kcontrols; i++) {
821 if (&w->kcontrol_news[i] == kcontrol_new) {
823 *kcontrol = w->kcontrols[i];
833 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
834 * create it. Either way, add the widget into the control's widget list
836 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
839 struct snd_soc_dapm_context *dapm = w->dapm;
840 struct snd_card *card = dapm->card->snd_card;
844 struct snd_kcontrol *kcontrol;
845 bool wname_in_long_name, kcname_in_long_name;
846 char *long_name = NULL;
850 prefix = soc_dapm_prefix(dapm);
852 prefix_len = strlen(prefix) + 1;
856 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
861 wname_in_long_name = false;
862 kcname_in_long_name = true;
865 case snd_soc_dapm_switch:
866 case snd_soc_dapm_mixer:
867 case snd_soc_dapm_pga:
868 case snd_soc_dapm_out_drv:
869 wname_in_long_name = true;
870 kcname_in_long_name = true;
872 case snd_soc_dapm_mixer_named_ctl:
873 wname_in_long_name = false;
874 kcname_in_long_name = true;
876 case snd_soc_dapm_demux:
877 case snd_soc_dapm_mux:
878 wname_in_long_name = true;
879 kcname_in_long_name = false;
886 if (wname_in_long_name && kcname_in_long_name) {
888 * The control will get a prefix from the control
889 * creation process but we're also using the same
890 * prefix for widgets so cut the prefix off the
891 * front of the widget name.
893 long_name = kasprintf(GFP_KERNEL, "%s %s",
894 w->name + prefix_len,
895 w->kcontrol_news[kci].name);
896 if (long_name == NULL)
900 } else if (wname_in_long_name) {
902 name = w->name + prefix_len;
905 name = w->kcontrol_news[kci].name;
908 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
915 kcontrol->private_free = dapm_kcontrol_free;
917 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
919 snd_ctl_free_one(kcontrol);
923 ret = snd_ctl_add(card, kcontrol);
926 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
932 ret = dapm_kcontrol_add_widget(kcontrol, w);
934 w->kcontrols[kci] = kcontrol;
942 /* create new dapm mixer control */
943 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
946 struct snd_soc_dapm_path *path;
947 struct dapm_kcontrol_data *data;
950 for (i = 0; i < w->num_kcontrols; i++) {
952 snd_soc_dapm_widget_for_each_source_path(w, path) {
953 /* mixer/mux paths name must match control name */
954 if (path->name != (char *)w->kcontrol_news[i].name)
957 if (!w->kcontrols[i]) {
958 ret = dapm_create_or_share_kcontrol(w, i);
963 dapm_kcontrol_add_path(w->kcontrols[i], path);
965 data = snd_kcontrol_chip(w->kcontrols[i]);
967 snd_soc_dapm_add_path(data->widget->dapm,
977 /* create new dapm mux control */
978 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
980 struct snd_soc_dapm_context *dapm = w->dapm;
981 enum snd_soc_dapm_direction dir;
982 struct snd_soc_dapm_path *path;
987 case snd_soc_dapm_mux:
988 dir = SND_SOC_DAPM_DIR_OUT;
991 case snd_soc_dapm_demux:
992 dir = SND_SOC_DAPM_DIR_IN;
999 if (w->num_kcontrols != 1) {
1001 "ASoC: %s %s has incorrect number of controls\n", type,
1006 if (list_empty(&w->edges[dir])) {
1007 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1011 ret = dapm_create_or_share_kcontrol(w, 0);
1015 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1017 dapm_kcontrol_add_path(w->kcontrols[0], path);
1023 /* create new dapm volume control */
1024 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1028 for (i = 0; i < w->num_kcontrols; i++) {
1029 ret = dapm_create_or_share_kcontrol(w, i);
1037 /* create new dapm dai link control */
1038 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1041 struct snd_kcontrol *kcontrol;
1042 struct snd_soc_dapm_context *dapm = w->dapm;
1043 struct snd_card *card = dapm->card->snd_card;
1044 struct snd_soc_pcm_runtime *rtd = w->priv;
1046 /* create control for links with > 1 config */
1047 if (rtd->dai_link->num_params <= 1)
1051 for (i = 0; i < w->num_kcontrols; i++) {
1052 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1054 ret = snd_ctl_add(card, kcontrol);
1057 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1058 w->name, w->kcontrol_news[i].name, ret);
1061 kcontrol->private_data = w;
1062 w->kcontrols[i] = kcontrol;
1068 /* We implement power down on suspend by checking the power state of
1069 * the ALSA card - when we are suspending the ALSA state for the card
1072 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1074 int level = snd_power_get_state(widget->dapm->card->snd_card);
1077 case SNDRV_CTL_POWER_D3hot:
1078 case SNDRV_CTL_POWER_D3cold:
1079 if (widget->ignore_suspend)
1080 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1082 return widget->ignore_suspend;
1088 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1089 struct list_head *widgets)
1091 struct snd_soc_dapm_widget *w;
1092 struct list_head *it;
1093 unsigned int size = 0;
1096 list_for_each(it, widgets)
1099 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1103 list_for_each_entry(w, widgets, work_list)
1104 (*list)->widgets[i++] = w;
1106 (*list)->num_widgets = i;
1112 * Common implementation for is_connected_output_ep() and
1113 * is_connected_input_ep(). The function is inlined since the combined size of
1114 * the two specialized functions is only marginally larger then the size of the
1115 * generic function and at the same time the fast path of the specialized
1116 * functions is significantly smaller than the generic function.
1118 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1119 struct list_head *list, enum snd_soc_dapm_direction dir,
1120 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1121 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1122 enum snd_soc_dapm_direction)),
1123 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1124 enum snd_soc_dapm_direction))
1126 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1127 struct snd_soc_dapm_path *path;
1130 if (widget->endpoints[dir] >= 0)
1131 return widget->endpoints[dir];
1133 DAPM_UPDATE_STAT(widget, path_checks);
1135 /* do we need to add this widget to the list ? */
1137 list_add_tail(&widget->work_list, list);
1139 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1140 widget->endpoints[dir] = 1;
1141 return widget->endpoints[dir];
1144 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1145 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1146 return widget->endpoints[dir];
1149 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1150 DAPM_UPDATE_STAT(widget, neighbour_checks);
1152 if (path->weak || path->is_supply)
1158 trace_snd_soc_dapm_path(widget, dir, path);
1160 if (path->connect) {
1162 con += fn(path->node[dir], list, custom_stop_condition);
1167 widget->endpoints[dir] = con;
1173 * Recursively check for a completed path to an active or physically connected
1174 * output widget. Returns number of complete paths.
1176 * Optionally, can be supplied with a function acting as a stopping condition.
1177 * This function takes the dapm widget currently being examined and the walk
1178 * direction as an arguments, it should return true if the walk should be
1179 * stopped and false otherwise.
1181 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1182 struct list_head *list,
1183 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1184 enum snd_soc_dapm_direction))
1186 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1187 is_connected_output_ep, custom_stop_condition);
1191 * Recursively check for a completed path to an active or physically connected
1192 * input widget. Returns number of complete paths.
1194 * Optionally, can be supplied with a function acting as a stopping condition.
1195 * This function takes the dapm widget currently being examined and the walk
1196 * direction as an arguments, it should return true if the walk should be
1197 * stopped and false otherwise.
1199 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1200 struct list_head *list,
1201 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1202 enum snd_soc_dapm_direction))
1204 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1205 is_connected_input_ep, custom_stop_condition);
1209 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1210 * @dai: the soc DAI.
1211 * @stream: stream direction.
1212 * @list: list of active widgets for this stream.
1213 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1214 * walk based on custom logic.
1216 * Queries DAPM graph as to whether a valid audio stream path exists for
1217 * the initial stream specified by name. This takes into account
1218 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1220 * Optionally, can be supplied with a function acting as a stopping condition.
1221 * This function takes the dapm widget currently being examined and the walk
1222 * direction as an arguments, it should return true if the walk should be
1223 * stopped and false otherwise.
1225 * Returns the number of valid paths or negative error.
1227 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1228 struct snd_soc_dapm_widget_list **list,
1229 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1230 enum snd_soc_dapm_direction))
1232 struct snd_soc_card *card = dai->component->card;
1233 struct snd_soc_dapm_widget *w;
1238 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1241 * For is_connected_{output,input}_ep fully discover the graph we need
1242 * to reset the cached number of inputs and outputs.
1244 list_for_each_entry(w, &card->widgets, list) {
1245 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1246 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1249 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1250 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1251 custom_stop_condition);
1253 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1254 custom_stop_condition);
1256 /* Drop starting point */
1257 list_del(widgets.next);
1259 ret = dapm_widget_list_create(list, &widgets);
1263 trace_snd_soc_dapm_connected(paths, stream);
1264 mutex_unlock(&card->dapm_mutex);
1270 * Handler for regulator supply widget.
1272 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1273 struct snd_kcontrol *kcontrol, int event)
1277 soc_dapm_async_complete(w->dapm);
1279 if (SND_SOC_DAPM_EVENT_ON(event)) {
1280 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1281 ret = regulator_allow_bypass(w->regulator, false);
1283 dev_warn(w->dapm->dev,
1284 "ASoC: Failed to unbypass %s: %d\n",
1288 return regulator_enable(w->regulator);
1290 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1291 ret = regulator_allow_bypass(w->regulator, true);
1293 dev_warn(w->dapm->dev,
1294 "ASoC: Failed to bypass %s: %d\n",
1298 return regulator_disable_deferred(w->regulator, w->shift);
1301 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1304 * Handler for pinctrl widget.
1306 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1307 struct snd_kcontrol *kcontrol, int event)
1309 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1310 struct pinctrl *p = w->pinctrl;
1311 struct pinctrl_state *s;
1316 if (SND_SOC_DAPM_EVENT_ON(event))
1317 s = pinctrl_lookup_state(p, priv->active_state);
1319 s = pinctrl_lookup_state(p, priv->sleep_state);
1324 return pinctrl_select_state(p, s);
1326 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1329 * Handler for clock supply widget.
1331 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1332 struct snd_kcontrol *kcontrol, int event)
1337 soc_dapm_async_complete(w->dapm);
1339 if (SND_SOC_DAPM_EVENT_ON(event)) {
1340 return clk_prepare_enable(w->clk);
1342 clk_disable_unprepare(w->clk);
1348 EXPORT_SYMBOL_GPL(dapm_clock_event);
1350 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1352 if (w->power_checked)
1353 return w->new_power;
1358 w->new_power = w->power_check(w);
1360 w->power_checked = true;
1362 return w->new_power;
1365 /* Generic check to see if a widget should be powered. */
1366 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1370 DAPM_UPDATE_STAT(w, power_checks);
1372 in = is_connected_input_ep(w, NULL, NULL);
1373 out = is_connected_output_ep(w, NULL, NULL);
1374 return out != 0 && in != 0;
1377 /* Check to see if a power supply is needed */
1378 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1380 struct snd_soc_dapm_path *path;
1382 DAPM_UPDATE_STAT(w, power_checks);
1384 /* Check if one of our outputs is connected */
1385 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1386 DAPM_UPDATE_STAT(w, neighbour_checks);
1391 if (path->connected &&
1392 !path->connected(path->source, path->sink))
1395 if (dapm_widget_power_check(path->sink))
1402 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1404 return w->connected;
1407 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1408 struct snd_soc_dapm_widget *b,
1416 sort = dapm_down_seq;
1418 if (sort[a->id] != sort[b->id])
1419 return sort[a->id] - sort[b->id];
1420 if (a->subseq != b->subseq) {
1422 return a->subseq - b->subseq;
1424 return b->subseq - a->subseq;
1426 if (a->reg != b->reg)
1427 return a->reg - b->reg;
1428 if (a->dapm != b->dapm)
1429 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1434 /* Insert a widget in order into a DAPM power sequence. */
1435 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1436 struct list_head *list,
1439 struct snd_soc_dapm_widget *w;
1441 list_for_each_entry(w, list, power_list)
1442 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1443 list_add_tail(&new_widget->power_list, &w->power_list);
1447 list_add_tail(&new_widget->power_list, list);
1450 static void dapm_seq_check_event(struct snd_soc_card *card,
1451 struct snd_soc_dapm_widget *w, int event)
1453 const char *ev_name;
1457 case SND_SOC_DAPM_PRE_PMU:
1458 ev_name = "PRE_PMU";
1461 case SND_SOC_DAPM_POST_PMU:
1462 ev_name = "POST_PMU";
1465 case SND_SOC_DAPM_PRE_PMD:
1466 ev_name = "PRE_PMD";
1469 case SND_SOC_DAPM_POST_PMD:
1470 ev_name = "POST_PMD";
1473 case SND_SOC_DAPM_WILL_PMU:
1474 ev_name = "WILL_PMU";
1477 case SND_SOC_DAPM_WILL_PMD:
1478 ev_name = "WILL_PMD";
1482 WARN(1, "Unknown event %d\n", event);
1486 if (w->new_power != power)
1489 if (w->event && (w->event_flags & event)) {
1490 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1492 soc_dapm_async_complete(w->dapm);
1493 trace_snd_soc_dapm_widget_event_start(w, event);
1494 ret = w->event(w, NULL, event);
1495 trace_snd_soc_dapm_widget_event_done(w, event);
1497 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1498 ev_name, w->name, ret);
1502 /* Apply the coalesced changes from a DAPM sequence */
1503 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1504 struct list_head *pending)
1506 struct snd_soc_dapm_context *dapm;
1507 struct snd_soc_dapm_widget *w;
1509 unsigned int value = 0;
1510 unsigned int mask = 0;
1512 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1516 list_for_each_entry(w, pending, power_list) {
1517 WARN_ON(reg != w->reg || dapm != w->dapm);
1518 w->power = w->new_power;
1520 mask |= w->mask << w->shift;
1522 value |= w->on_val << w->shift;
1524 value |= w->off_val << w->shift;
1526 pop_dbg(dapm->dev, card->pop_time,
1527 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1528 w->name, reg, value, mask);
1530 /* Check for events */
1531 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1532 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1536 /* Any widget will do, they should all be updating the
1540 pop_dbg(dapm->dev, card->pop_time,
1541 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1542 value, mask, reg, card->pop_time);
1543 pop_wait(card->pop_time);
1544 soc_dapm_update_bits(dapm, reg, mask, value);
1547 list_for_each_entry(w, pending, power_list) {
1548 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1549 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1553 /* Apply a DAPM power sequence.
1555 * We walk over a pre-sorted list of widgets to apply power to. In
1556 * order to minimise the number of writes to the device required
1557 * multiple widgets will be updated in a single write where possible.
1558 * Currently anything that requires more than a single write is not
1561 static void dapm_seq_run(struct snd_soc_card *card,
1562 struct list_head *list, int event, bool power_up)
1564 struct snd_soc_dapm_widget *w, *n;
1565 struct snd_soc_dapm_context *d;
1568 int cur_subseq = -1;
1569 int cur_reg = SND_SOC_NOPM;
1570 struct snd_soc_dapm_context *cur_dapm = NULL;
1577 sort = dapm_down_seq;
1579 list_for_each_entry_safe(w, n, list, power_list) {
1582 /* Do we need to apply any queued changes? */
1583 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1584 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1585 if (!list_empty(&pending))
1586 dapm_seq_run_coalesced(card, &pending);
1588 if (cur_dapm && cur_dapm->seq_notifier) {
1589 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1590 if (sort[i] == cur_sort)
1591 cur_dapm->seq_notifier(cur_dapm,
1596 if (cur_dapm && w->dapm != cur_dapm)
1597 soc_dapm_async_complete(cur_dapm);
1599 INIT_LIST_HEAD(&pending);
1601 cur_subseq = INT_MIN;
1602 cur_reg = SND_SOC_NOPM;
1607 case snd_soc_dapm_pre:
1609 list_for_each_entry_safe_continue(w, n, list,
1612 if (event == SND_SOC_DAPM_STREAM_START)
1614 NULL, SND_SOC_DAPM_PRE_PMU);
1615 else if (event == SND_SOC_DAPM_STREAM_STOP)
1617 NULL, SND_SOC_DAPM_PRE_PMD);
1620 case snd_soc_dapm_post:
1622 list_for_each_entry_safe_continue(w, n, list,
1625 if (event == SND_SOC_DAPM_STREAM_START)
1627 NULL, SND_SOC_DAPM_POST_PMU);
1628 else if (event == SND_SOC_DAPM_STREAM_STOP)
1630 NULL, SND_SOC_DAPM_POST_PMD);
1634 /* Queue it up for application */
1635 cur_sort = sort[w->id];
1636 cur_subseq = w->subseq;
1639 list_move(&w->power_list, &pending);
1644 dev_err(w->dapm->dev,
1645 "ASoC: Failed to apply widget power: %d\n", ret);
1648 if (!list_empty(&pending))
1649 dapm_seq_run_coalesced(card, &pending);
1651 if (cur_dapm && cur_dapm->seq_notifier) {
1652 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1653 if (sort[i] == cur_sort)
1654 cur_dapm->seq_notifier(cur_dapm,
1658 list_for_each_entry(d, &card->dapm_list, list) {
1659 soc_dapm_async_complete(d);
1663 static void dapm_widget_update(struct snd_soc_card *card)
1665 struct snd_soc_dapm_update *update = card->update;
1666 struct snd_soc_dapm_widget_list *wlist;
1667 struct snd_soc_dapm_widget *w = NULL;
1671 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1674 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1676 for (wi = 0; wi < wlist->num_widgets; wi++) {
1677 w = wlist->widgets[wi];
1679 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1680 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1682 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1690 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1693 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1696 if (update->has_second_set) {
1697 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1698 update->mask2, update->val2);
1700 dev_err(w->dapm->dev,
1701 "ASoC: %s DAPM update failed: %d\n",
1705 for (wi = 0; wi < wlist->num_widgets; wi++) {
1706 w = wlist->widgets[wi];
1708 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1709 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1711 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1717 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1718 * they're changing state.
1720 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1722 struct snd_soc_dapm_context *d = data;
1725 /* If we're off and we're not supposed to go into STANDBY */
1726 if (d->bias_level == SND_SOC_BIAS_OFF &&
1727 d->target_bias_level != SND_SOC_BIAS_OFF) {
1729 pm_runtime_get_sync(d->dev);
1731 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1734 "ASoC: Failed to turn on bias: %d\n", ret);
1737 /* Prepare for a transition to ON or away from ON */
1738 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1739 d->bias_level != SND_SOC_BIAS_ON) ||
1740 (d->target_bias_level != SND_SOC_BIAS_ON &&
1741 d->bias_level == SND_SOC_BIAS_ON)) {
1742 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1745 "ASoC: Failed to prepare bias: %d\n", ret);
1749 /* Async callback run prior to DAPM sequences - brings to their final
1752 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1754 struct snd_soc_dapm_context *d = data;
1757 /* If we just powered the last thing off drop to standby bias */
1758 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1759 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1760 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1761 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1763 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1767 /* If we're in standby and can support bias off then do that */
1768 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1769 d->target_bias_level == SND_SOC_BIAS_OFF) {
1770 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1772 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1776 pm_runtime_put(d->dev);
1779 /* If we just powered up then move to active bias */
1780 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1781 d->target_bias_level == SND_SOC_BIAS_ON) {
1782 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1784 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1789 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1790 bool power, bool connect)
1792 /* If a connection is being made or broken then that update
1793 * will have marked the peer dirty, otherwise the widgets are
1794 * not connected and this update has no impact. */
1798 /* If the peer is already in the state we're moving to then we
1799 * won't have an impact on it. */
1800 if (power != peer->power)
1801 dapm_mark_dirty(peer, "peer state change");
1804 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1805 struct list_head *up_list,
1806 struct list_head *down_list)
1808 struct snd_soc_dapm_path *path;
1810 if (w->power == power)
1813 trace_snd_soc_dapm_widget_power(w, power);
1815 /* If we changed our power state perhaps our neigbours changed
1818 snd_soc_dapm_widget_for_each_source_path(w, path)
1819 dapm_widget_set_peer_power(path->source, power, path->connect);
1821 /* Supplies can't affect their outputs, only their inputs */
1822 if (!w->is_supply) {
1823 snd_soc_dapm_widget_for_each_sink_path(w, path)
1824 dapm_widget_set_peer_power(path->sink, power,
1829 dapm_seq_insert(w, up_list, true);
1831 dapm_seq_insert(w, down_list, false);
1834 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1835 struct list_head *up_list,
1836 struct list_head *down_list)
1841 case snd_soc_dapm_pre:
1842 dapm_seq_insert(w, down_list, false);
1844 case snd_soc_dapm_post:
1845 dapm_seq_insert(w, up_list, true);
1849 power = dapm_widget_power_check(w);
1851 dapm_widget_set_power(w, power, up_list, down_list);
1856 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1858 if (dapm->idle_bias_off)
1861 switch (snd_power_get_state(dapm->card->snd_card)) {
1862 case SNDRV_CTL_POWER_D3hot:
1863 case SNDRV_CTL_POWER_D3cold:
1864 return dapm->suspend_bias_off;
1873 * Scan each dapm widget for complete audio path.
1874 * A complete path is a route that has valid endpoints i.e.:-
1876 * o DAC to output pin.
1877 * o Input pin to ADC.
1878 * o Input pin to Output pin (bypass, sidetone)
1879 * o DAC to ADC (loopback).
1881 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1883 struct snd_soc_dapm_widget *w;
1884 struct snd_soc_dapm_context *d;
1886 LIST_HEAD(down_list);
1887 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1888 enum snd_soc_bias_level bias;
1890 lockdep_assert_held(&card->dapm_mutex);
1892 trace_snd_soc_dapm_start(card);
1894 list_for_each_entry(d, &card->dapm_list, list) {
1895 if (dapm_idle_bias_off(d))
1896 d->target_bias_level = SND_SOC_BIAS_OFF;
1898 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1903 /* Check which widgets we need to power and store them in
1904 * lists indicating if they should be powered up or down. We
1905 * only check widgets that have been flagged as dirty but note
1906 * that new widgets may be added to the dirty list while we
1909 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1910 dapm_power_one_widget(w, &up_list, &down_list);
1913 list_for_each_entry(w, &card->widgets, list) {
1915 case snd_soc_dapm_pre:
1916 case snd_soc_dapm_post:
1917 /* These widgets always need to be powered */
1920 list_del_init(&w->dirty);
1927 /* Supplies and micbiases only bring the
1928 * context up to STANDBY as unless something
1929 * else is active and passing audio they
1930 * generally don't require full power. Signal
1931 * generators are virtual pins and have no
1932 * power impact themselves.
1935 case snd_soc_dapm_siggen:
1936 case snd_soc_dapm_vmid:
1938 case snd_soc_dapm_supply:
1939 case snd_soc_dapm_regulator_supply:
1940 case snd_soc_dapm_pinctrl:
1941 case snd_soc_dapm_clock_supply:
1942 case snd_soc_dapm_micbias:
1943 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1944 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1947 d->target_bias_level = SND_SOC_BIAS_ON;
1954 /* Force all contexts in the card to the same bias state if
1955 * they're not ground referenced.
1957 bias = SND_SOC_BIAS_OFF;
1958 list_for_each_entry(d, &card->dapm_list, list)
1959 if (d->target_bias_level > bias)
1960 bias = d->target_bias_level;
1961 list_for_each_entry(d, &card->dapm_list, list)
1962 if (!dapm_idle_bias_off(d))
1963 d->target_bias_level = bias;
1965 trace_snd_soc_dapm_walk_done(card);
1967 /* Run card bias changes at first */
1968 dapm_pre_sequence_async(&card->dapm, 0);
1969 /* Run other bias changes in parallel */
1970 list_for_each_entry(d, &card->dapm_list, list) {
1971 if (d != &card->dapm && d->bias_level != d->target_bias_level)
1972 async_schedule_domain(dapm_pre_sequence_async, d,
1975 async_synchronize_full_domain(&async_domain);
1977 list_for_each_entry(w, &down_list, power_list) {
1978 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1981 list_for_each_entry(w, &up_list, power_list) {
1982 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1985 /* Power down widgets first; try to avoid amplifying pops. */
1986 dapm_seq_run(card, &down_list, event, false);
1988 dapm_widget_update(card);
1991 dapm_seq_run(card, &up_list, event, true);
1993 /* Run all the bias changes in parallel */
1994 list_for_each_entry(d, &card->dapm_list, list) {
1995 if (d != &card->dapm && d->bias_level != d->target_bias_level)
1996 async_schedule_domain(dapm_post_sequence_async, d,
1999 async_synchronize_full_domain(&async_domain);
2000 /* Run card bias changes at last */
2001 dapm_post_sequence_async(&card->dapm, 0);
2003 /* do we need to notify any clients that DAPM event is complete */
2004 list_for_each_entry(d, &card->dapm_list, list) {
2005 if (d->stream_event)
2006 d->stream_event(d, event);
2009 pop_dbg(card->dev, card->pop_time,
2010 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2011 pop_wait(card->pop_time);
2013 trace_snd_soc_dapm_done(card);
2018 #ifdef CONFIG_DEBUG_FS
2019 static ssize_t dapm_widget_power_read_file(struct file *file,
2020 char __user *user_buf,
2021 size_t count, loff_t *ppos)
2023 struct snd_soc_dapm_widget *w = file->private_data;
2024 struct snd_soc_card *card = w->dapm->card;
2025 enum snd_soc_dapm_direction dir, rdir;
2029 struct snd_soc_dapm_path *p = NULL;
2031 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2035 mutex_lock(&card->dapm_mutex);
2037 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2042 in = is_connected_input_ep(w, NULL, NULL);
2043 out = is_connected_output_ep(w, NULL, NULL);
2046 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2047 w->name, w->power ? "On" : "Off",
2048 w->force ? " (forced)" : "", in, out);
2051 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2052 " - R%d(0x%x) mask 0x%x",
2053 w->reg, w->reg, w->mask << w->shift);
2055 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2058 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2060 w->active ? "active" : "inactive");
2062 snd_soc_dapm_for_each_direction(dir) {
2063 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2064 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2065 if (p->connected && !p->connected(p->source, p->sink))
2071 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2072 " %s \"%s\" \"%s\"\n",
2073 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2074 p->name ? p->name : "static",
2075 p->node[rdir]->name);
2079 mutex_unlock(&card->dapm_mutex);
2081 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2087 static const struct file_operations dapm_widget_power_fops = {
2088 .open = simple_open,
2089 .read = dapm_widget_power_read_file,
2090 .llseek = default_llseek,
2093 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2094 size_t count, loff_t *ppos)
2096 struct snd_soc_dapm_context *dapm = file->private_data;
2099 switch (dapm->bias_level) {
2100 case SND_SOC_BIAS_ON:
2103 case SND_SOC_BIAS_PREPARE:
2104 level = "Prepare\n";
2106 case SND_SOC_BIAS_STANDBY:
2107 level = "Standby\n";
2109 case SND_SOC_BIAS_OFF:
2113 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2114 level = "Unknown\n";
2118 return simple_read_from_buffer(user_buf, count, ppos, level,
2122 static const struct file_operations dapm_bias_fops = {
2123 .open = simple_open,
2124 .read = dapm_bias_read_file,
2125 .llseek = default_llseek,
2128 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2129 struct dentry *parent)
2136 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2138 if (!dapm->debugfs_dapm) {
2140 "ASoC: Failed to create DAPM debugfs directory\n");
2144 d = debugfs_create_file("bias_level", 0444,
2145 dapm->debugfs_dapm, dapm,
2149 "ASoC: Failed to create bias level debugfs file\n");
2152 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2154 struct snd_soc_dapm_context *dapm = w->dapm;
2157 if (!dapm->debugfs_dapm || !w->name)
2160 d = debugfs_create_file(w->name, 0444,
2161 dapm->debugfs_dapm, w,
2162 &dapm_widget_power_fops);
2164 dev_warn(w->dapm->dev,
2165 "ASoC: Failed to create %s debugfs file\n",
2169 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2171 debugfs_remove_recursive(dapm->debugfs_dapm);
2175 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2176 struct dentry *parent)
2180 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2184 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2191 * soc_dapm_connect_path() - Connects or disconnects a path
2192 * @path: The path to update
2193 * @connect: The new connect state of the path. True if the path is connected,
2194 * false if it is disconnected.
2195 * @reason: The reason why the path changed (for debugging only)
2197 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2198 bool connect, const char *reason)
2200 if (path->connect == connect)
2203 path->connect = connect;
2204 dapm_mark_dirty(path->source, reason);
2205 dapm_mark_dirty(path->sink, reason);
2206 dapm_path_invalidate(path);
2209 /* test and update the power status of a mux widget */
2210 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2211 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2213 struct snd_soc_dapm_path *path;
2217 lockdep_assert_held(&card->dapm_mutex);
2219 /* find dapm widget path assoc with kcontrol */
2220 dapm_kcontrol_for_each_path(path, kcontrol) {
2222 /* we now need to match the string in the enum to the path */
2223 if (!(strcmp(path->name, e->texts[mux])))
2228 soc_dapm_connect_path(path, connect, "mux update");
2232 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2237 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2238 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2239 struct snd_soc_dapm_update *update)
2241 struct snd_soc_card *card = dapm->card;
2244 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2245 card->update = update;
2246 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2247 card->update = NULL;
2248 mutex_unlock(&card->dapm_mutex);
2250 soc_dpcm_runtime_update(card);
2253 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2255 /* test and update the power status of a mixer or switch widget */
2256 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2257 struct snd_kcontrol *kcontrol,
2258 int connect, int rconnect)
2260 struct snd_soc_dapm_path *path;
2263 lockdep_assert_held(&card->dapm_mutex);
2265 /* find dapm widget path assoc with kcontrol */
2266 dapm_kcontrol_for_each_path(path, kcontrol) {
2268 * Ideally this function should support any number of
2269 * paths and channels. But since kcontrols only come
2270 * in mono and stereo variants, we are limited to 2
2273 * The following code assumes for stereo controls the
2274 * first path (when 'found == 0') is the left channel,
2275 * and all remaining paths (when 'found == 1') are the
2278 * A stereo control is signified by a valid 'rconnect'
2279 * value, either 0 for unconnected, or >= 0 for connected.
2280 * This is chosen instead of using snd_soc_volsw_is_stereo,
2281 * so that the behavior of snd_soc_dapm_mixer_update_power
2282 * doesn't change even when the kcontrol passed in is
2285 * It passes 'connect' as the path connect status for
2286 * the left channel, and 'rconnect' for the right
2289 if (found && rconnect >= 0)
2290 soc_dapm_connect_path(path, rconnect, "mixer update");
2292 soc_dapm_connect_path(path, connect, "mixer update");
2297 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2302 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2303 struct snd_kcontrol *kcontrol, int connect,
2304 struct snd_soc_dapm_update *update)
2306 struct snd_soc_card *card = dapm->card;
2309 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2310 card->update = update;
2311 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2312 card->update = NULL;
2313 mutex_unlock(&card->dapm_mutex);
2315 soc_dpcm_runtime_update(card);
2318 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2320 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2323 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2324 struct snd_soc_dapm_widget *w;
2326 char *state = "not set";
2328 /* card won't be set for the dummy component, as a spot fix
2329 * we're checking for that case specifically here but in future
2330 * we will ensure that the dummy component looks like others.
2335 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2336 if (w->dapm != dapm)
2339 /* only display widgets that burn power */
2341 case snd_soc_dapm_hp:
2342 case snd_soc_dapm_mic:
2343 case snd_soc_dapm_spk:
2344 case snd_soc_dapm_line:
2345 case snd_soc_dapm_micbias:
2346 case snd_soc_dapm_dac:
2347 case snd_soc_dapm_adc:
2348 case snd_soc_dapm_pga:
2349 case snd_soc_dapm_out_drv:
2350 case snd_soc_dapm_mixer:
2351 case snd_soc_dapm_mixer_named_ctl:
2352 case snd_soc_dapm_supply:
2353 case snd_soc_dapm_regulator_supply:
2354 case snd_soc_dapm_pinctrl:
2355 case snd_soc_dapm_clock_supply:
2357 count += sprintf(buf + count, "%s: %s\n",
2358 w->name, w->power ? "On":"Off");
2365 switch (snd_soc_dapm_get_bias_level(dapm)) {
2366 case SND_SOC_BIAS_ON:
2369 case SND_SOC_BIAS_PREPARE:
2372 case SND_SOC_BIAS_STANDBY:
2375 case SND_SOC_BIAS_OFF:
2379 count += sprintf(buf + count, "PM State: %s\n", state);
2384 /* show dapm widget status in sys fs */
2385 static ssize_t dapm_widget_show(struct device *dev,
2386 struct device_attribute *attr, char *buf)
2388 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2389 struct snd_soc_dai *codec_dai;
2392 mutex_lock(&rtd->card->dapm_mutex);
2394 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2395 struct snd_soc_component *cmpnt = codec_dai->component;
2397 count += dapm_widget_show_component(cmpnt, buf + count);
2400 mutex_unlock(&rtd->card->dapm_mutex);
2405 static DEVICE_ATTR_RO(dapm_widget);
2407 struct attribute *soc_dapm_dev_attrs[] = {
2408 &dev_attr_dapm_widget.attr,
2412 static void dapm_free_path(struct snd_soc_dapm_path *path)
2414 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2415 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2416 list_del(&path->list_kcontrol);
2417 list_del(&path->list);
2421 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2423 struct snd_soc_dapm_path *p, *next_p;
2424 enum snd_soc_dapm_direction dir;
2428 * remove source and sink paths associated to this widget.
2429 * While removing the path, remove reference to it from both
2430 * source and sink widgets so that path is removed only once.
2432 snd_soc_dapm_for_each_direction(dir) {
2433 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2437 kfree(w->kcontrols);
2438 kfree_const(w->name);
2442 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2444 dapm->path_sink_cache.widget = NULL;
2445 dapm->path_source_cache.widget = NULL;
2448 /* free all dapm widgets and resources */
2449 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2451 struct snd_soc_dapm_widget *w, *next_w;
2453 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2454 if (w->dapm != dapm)
2456 snd_soc_dapm_free_widget(w);
2458 snd_soc_dapm_reset_cache(dapm);
2461 static struct snd_soc_dapm_widget *dapm_find_widget(
2462 struct snd_soc_dapm_context *dapm, const char *pin,
2463 bool search_other_contexts)
2465 struct snd_soc_dapm_widget *w;
2466 struct snd_soc_dapm_widget *fallback = NULL;
2468 list_for_each_entry(w, &dapm->card->widgets, list) {
2469 if (!strcmp(w->name, pin)) {
2470 if (w->dapm == dapm)
2477 if (search_other_contexts)
2483 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2484 const char *pin, int status)
2486 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2488 dapm_assert_locked(dapm);
2491 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2495 if (w->connected != status) {
2496 dapm_mark_dirty(w, "pin configuration");
2497 dapm_widget_invalidate_input_paths(w);
2498 dapm_widget_invalidate_output_paths(w);
2501 w->connected = status;
2509 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2510 * @dapm: DAPM context
2512 * Walks all dapm audio paths and powers widgets according to their
2513 * stream or path usage.
2515 * Requires external locking.
2517 * Returns 0 for success.
2519 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2522 * Suppress early reports (eg, jacks syncing their state) to avoid
2523 * silly DAPM runs during card startup.
2525 if (!dapm->card || !dapm->card->instantiated)
2528 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2530 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2533 * snd_soc_dapm_sync - scan and power dapm paths
2534 * @dapm: DAPM context
2536 * Walks all dapm audio paths and powers widgets according to their
2537 * stream or path usage.
2539 * Returns 0 for success.
2541 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2545 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2546 ret = snd_soc_dapm_sync_unlocked(dapm);
2547 mutex_unlock(&dapm->card->dapm_mutex);
2550 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2553 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2554 * @w: The widget for which to update the flags
2556 * Some widgets have a dynamic category which depends on which neighbors they
2557 * are connected to. This function update the category for these widgets.
2559 * This function must be called whenever a path is added or removed to a widget.
2561 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2563 enum snd_soc_dapm_direction dir;
2564 struct snd_soc_dapm_path *p;
2568 case snd_soc_dapm_input:
2569 /* On a fully routed card an input is never a source */
2570 if (w->dapm->card->fully_routed)
2572 ep = SND_SOC_DAPM_EP_SOURCE;
2573 snd_soc_dapm_widget_for_each_source_path(w, p) {
2574 if (p->source->id == snd_soc_dapm_micbias ||
2575 p->source->id == snd_soc_dapm_mic ||
2576 p->source->id == snd_soc_dapm_line ||
2577 p->source->id == snd_soc_dapm_output) {
2583 case snd_soc_dapm_output:
2584 /* On a fully routed card a output is never a sink */
2585 if (w->dapm->card->fully_routed)
2587 ep = SND_SOC_DAPM_EP_SINK;
2588 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2589 if (p->sink->id == snd_soc_dapm_spk ||
2590 p->sink->id == snd_soc_dapm_hp ||
2591 p->sink->id == snd_soc_dapm_line ||
2592 p->sink->id == snd_soc_dapm_input) {
2598 case snd_soc_dapm_line:
2600 snd_soc_dapm_for_each_direction(dir) {
2601 if (!list_empty(&w->edges[dir]))
2602 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2612 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2613 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2614 const char *control)
2616 bool dynamic_source = false;
2617 bool dynamic_sink = false;
2622 switch (source->id) {
2623 case snd_soc_dapm_demux:
2624 dynamic_source = true;
2631 case snd_soc_dapm_mux:
2632 case snd_soc_dapm_switch:
2633 case snd_soc_dapm_mixer:
2634 case snd_soc_dapm_mixer_named_ctl:
2635 dynamic_sink = true;
2641 if (dynamic_source && dynamic_sink) {
2643 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2644 source->name, control, sink->name);
2646 } else if (!dynamic_source && !dynamic_sink) {
2648 "Control not supported for path %s -> [%s] -> %s\n",
2649 source->name, control, sink->name);
2656 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2657 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2658 const char *control,
2659 int (*connected)(struct snd_soc_dapm_widget *source,
2660 struct snd_soc_dapm_widget *sink))
2662 struct snd_soc_dapm_widget *widgets[2];
2663 enum snd_soc_dapm_direction dir;
2664 struct snd_soc_dapm_path *path;
2667 if (wsink->is_supply && !wsource->is_supply) {
2669 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2670 wsource->name, wsink->name);
2674 if (connected && !wsource->is_supply) {
2676 "connected() callback only supported for supply widgets (%s -> %s)\n",
2677 wsource->name, wsink->name);
2681 if (wsource->is_supply && control) {
2683 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2684 wsource->name, control, wsink->name);
2688 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2692 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2696 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2697 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2698 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2699 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2701 path->connected = connected;
2702 INIT_LIST_HEAD(&path->list);
2703 INIT_LIST_HEAD(&path->list_kcontrol);
2705 if (wsource->is_supply || wsink->is_supply)
2706 path->is_supply = 1;
2708 /* connect static paths */
2709 if (control == NULL) {
2712 switch (wsource->id) {
2713 case snd_soc_dapm_demux:
2714 ret = dapm_connect_mux(dapm, path, control, wsource);
2722 switch (wsink->id) {
2723 case snd_soc_dapm_mux:
2724 ret = dapm_connect_mux(dapm, path, control, wsink);
2728 case snd_soc_dapm_switch:
2729 case snd_soc_dapm_mixer:
2730 case snd_soc_dapm_mixer_named_ctl:
2731 ret = dapm_connect_mixer(dapm, path, control);
2740 list_add(&path->list, &dapm->card->paths);
2741 snd_soc_dapm_for_each_direction(dir)
2742 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2744 snd_soc_dapm_for_each_direction(dir) {
2745 dapm_update_widget_flags(widgets[dir]);
2746 dapm_mark_dirty(widgets[dir], "Route added");
2749 if (dapm->card->instantiated && path->connect)
2750 dapm_path_invalidate(path);
2758 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2759 const struct snd_soc_dapm_route *route)
2761 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2762 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2765 char prefixed_sink[80];
2766 char prefixed_source[80];
2770 prefix = soc_dapm_prefix(dapm);
2772 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2773 prefix, route->sink);
2774 sink = prefixed_sink;
2775 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2776 prefix, route->source);
2777 source = prefixed_source;
2780 source = route->source;
2783 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2784 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2786 if (wsink && wsource)
2790 * find src and dest widgets over all widgets but favor a widget from
2791 * current DAPM context
2793 list_for_each_entry(w, &dapm->card->widgets, list) {
2794 if (!wsink && !(strcmp(w->name, sink))) {
2796 if (w->dapm == dapm) {
2803 if (!wsource && !(strcmp(w->name, source))) {
2805 if (w->dapm == dapm) {
2812 /* use widget from another DAPM context if not found from this */
2818 if (wsource == NULL) {
2819 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2823 if (wsink == NULL) {
2824 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2830 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2831 dapm_wcache_update(&dapm->path_source_cache, wsource);
2833 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2840 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2841 source, route->control, sink);
2845 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2846 const struct snd_soc_dapm_route *route)
2848 struct snd_soc_dapm_widget *wsource, *wsink;
2849 struct snd_soc_dapm_path *path, *p;
2852 char prefixed_sink[80];
2853 char prefixed_source[80];
2856 if (route->control) {
2858 "ASoC: Removal of routes with controls not supported\n");
2862 prefix = soc_dapm_prefix(dapm);
2864 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2865 prefix, route->sink);
2866 sink = prefixed_sink;
2867 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2868 prefix, route->source);
2869 source = prefixed_source;
2872 source = route->source;
2876 list_for_each_entry(p, &dapm->card->paths, list) {
2877 if (strcmp(p->source->name, source) != 0)
2879 if (strcmp(p->sink->name, sink) != 0)
2886 wsource = path->source;
2889 dapm_mark_dirty(wsource, "Route removed");
2890 dapm_mark_dirty(wsink, "Route removed");
2892 dapm_path_invalidate(path);
2894 dapm_free_path(path);
2896 /* Update any path related flags */
2897 dapm_update_widget_flags(wsource);
2898 dapm_update_widget_flags(wsink);
2900 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2908 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2909 * @dapm: DAPM context
2910 * @route: audio routes
2911 * @num: number of routes
2913 * Connects 2 dapm widgets together via a named audio path. The sink is
2914 * the widget receiving the audio signal, whilst the source is the sender
2915 * of the audio signal.
2917 * Returns 0 for success else error. On error all resources can be freed
2918 * with a call to snd_soc_card_free().
2920 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2921 const struct snd_soc_dapm_route *route, int num)
2925 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2926 for (i = 0; i < num; i++) {
2927 r = snd_soc_dapm_add_route(dapm, route);
2929 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2931 route->control ? route->control : "direct",
2937 mutex_unlock(&dapm->card->dapm_mutex);
2941 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2944 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2945 * @dapm: DAPM context
2946 * @route: audio routes
2947 * @num: number of routes
2949 * Removes routes from the DAPM context.
2951 int snd_soc_dapm_del_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 snd_soc_dapm_del_route(dapm, route);
2961 mutex_unlock(&dapm->card->dapm_mutex);
2965 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2967 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2968 const struct snd_soc_dapm_route *route)
2970 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2973 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2976 struct snd_soc_dapm_path *path;
2980 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2986 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2991 if (route->control || route->connected)
2992 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2993 route->source, route->sink);
2995 snd_soc_dapm_widget_for_each_sink_path(source, path) {
2996 if (path->sink == sink) {
3003 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3004 route->source, route->sink);
3006 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3007 count, route->source, route->sink);
3013 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3014 * @dapm: DAPM context
3015 * @route: audio routes
3016 * @num: number of routes
3018 * Mark existing routes matching those specified in the passed array
3019 * as being weak, meaning that they are ignored for the purpose of
3020 * power decisions. The main intended use case is for sidetone paths
3021 * which couple audio between other independent paths if they are both
3022 * active in order to make the combination work better at the user
3023 * level but which aren't intended to be "used".
3025 * Note that CODEC drivers should not use this as sidetone type paths
3026 * can frequently also be used as bypass paths.
3028 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3029 const struct snd_soc_dapm_route *route, int num)
3034 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3035 for (i = 0; i < num; i++) {
3036 err = snd_soc_dapm_weak_route(dapm, route);
3041 mutex_unlock(&dapm->card->dapm_mutex);
3045 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3048 * snd_soc_dapm_new_widgets - add new dapm widgets
3049 * @card: card to be checked for new dapm widgets
3051 * Checks the codec for any new dapm widgets and creates them if found.
3053 * Returns 0 for success.
3055 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3057 struct snd_soc_dapm_widget *w;
3060 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3062 list_for_each_entry(w, &card->widgets, list)
3067 if (w->num_kcontrols) {
3068 w->kcontrols = kcalloc(w->num_kcontrols,
3069 sizeof(struct snd_kcontrol *),
3071 if (!w->kcontrols) {
3072 mutex_unlock(&card->dapm_mutex);
3078 case snd_soc_dapm_switch:
3079 case snd_soc_dapm_mixer:
3080 case snd_soc_dapm_mixer_named_ctl:
3083 case snd_soc_dapm_mux:
3084 case snd_soc_dapm_demux:
3087 case snd_soc_dapm_pga:
3088 case snd_soc_dapm_out_drv:
3091 case snd_soc_dapm_dai_link:
3092 dapm_new_dai_link(w);
3098 /* Read the initial power state from the device */
3100 soc_dapm_read(w->dapm, w->reg, &val);
3101 val = val >> w->shift;
3103 if (val == w->on_val)
3109 dapm_mark_dirty(w, "new widget");
3110 dapm_debugfs_add_widget(w);
3113 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3114 mutex_unlock(&card->dapm_mutex);
3117 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3120 * snd_soc_dapm_get_volsw - dapm mixer get callback
3121 * @kcontrol: mixer control
3122 * @ucontrol: control element information
3124 * Callback to get the value of a dapm mixer control.
3126 * Returns 0 for success.
3128 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3129 struct snd_ctl_elem_value *ucontrol)
3131 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3132 struct snd_soc_card *card = dapm->card;
3133 struct soc_mixer_control *mc =
3134 (struct soc_mixer_control *)kcontrol->private_value;
3136 unsigned int shift = mc->shift;
3138 unsigned int width = fls(max);
3139 unsigned int mask = (1 << fls(max)) - 1;
3140 unsigned int invert = mc->invert;
3141 unsigned int reg_val, val, rval = 0;
3144 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3145 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3146 ret = soc_dapm_read(dapm, reg, ®_val);
3147 val = (reg_val >> shift) & mask;
3149 if (ret == 0 && reg != mc->rreg)
3150 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3152 if (snd_soc_volsw_is_stereo(mc))
3153 rval = (reg_val >> mc->rshift) & mask;
3155 reg_val = dapm_kcontrol_get_value(kcontrol);
3156 val = reg_val & mask;
3158 if (snd_soc_volsw_is_stereo(mc))
3159 rval = (reg_val >> width) & mask;
3161 mutex_unlock(&card->dapm_mutex);
3167 ucontrol->value.integer.value[0] = max - val;
3169 ucontrol->value.integer.value[0] = val;
3171 if (snd_soc_volsw_is_stereo(mc)) {
3173 ucontrol->value.integer.value[1] = max - rval;
3175 ucontrol->value.integer.value[1] = rval;
3180 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3183 * snd_soc_dapm_put_volsw - dapm mixer set callback
3184 * @kcontrol: mixer control
3185 * @ucontrol: control element information
3187 * Callback to set the value of a dapm mixer control.
3189 * Returns 0 for success.
3191 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3192 struct snd_ctl_elem_value *ucontrol)
3194 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3195 struct snd_soc_card *card = dapm->card;
3196 struct soc_mixer_control *mc =
3197 (struct soc_mixer_control *)kcontrol->private_value;
3199 unsigned int shift = mc->shift;
3201 unsigned int width = fls(max);
3202 unsigned int mask = (1 << width) - 1;
3203 unsigned int invert = mc->invert;
3204 unsigned int val, rval = 0;
3205 int connect, rconnect = -1, change, reg_change = 0;
3206 struct snd_soc_dapm_update update = {};
3209 val = (ucontrol->value.integer.value[0] & mask);
3215 if (snd_soc_volsw_is_stereo(mc)) {
3216 rval = (ucontrol->value.integer.value[1] & mask);
3222 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3224 /* This assumes field width < (bits in unsigned int / 2) */
3225 if (width > sizeof(unsigned int) * 8 / 2)
3227 "ASoC: control %s field width limit exceeded\n",
3229 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3231 if (reg != SND_SOC_NOPM) {
3233 rval = rval << mc->rshift;
3235 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3237 if (snd_soc_volsw_is_stereo(mc))
3238 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3243 if (change || reg_change) {
3245 if (snd_soc_volsw_is_stereo(mc)) {
3246 update.has_second_set = true;
3247 update.reg2 = mc->rreg;
3248 update.mask2 = mask << mc->rshift;
3251 update.kcontrol = kcontrol;
3253 update.mask = mask << shift;
3255 card->update = &update;
3257 change |= reg_change;
3259 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3262 card->update = NULL;
3265 mutex_unlock(&card->dapm_mutex);
3268 soc_dpcm_runtime_update(card);
3272 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3275 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3276 * @kcontrol: mixer control
3277 * @ucontrol: control element information
3279 * Callback to get the value of a dapm enumerated double mixer control.
3281 * Returns 0 for success.
3283 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3284 struct snd_ctl_elem_value *ucontrol)
3286 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3287 struct snd_soc_card *card = dapm->card;
3288 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3289 unsigned int reg_val, val;
3291 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3292 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3293 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3295 mutex_unlock(&card->dapm_mutex);
3299 reg_val = dapm_kcontrol_get_value(kcontrol);
3301 mutex_unlock(&card->dapm_mutex);
3303 val = (reg_val >> e->shift_l) & e->mask;
3304 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3305 if (e->shift_l != e->shift_r) {
3306 val = (reg_val >> e->shift_r) & e->mask;
3307 val = snd_soc_enum_val_to_item(e, val);
3308 ucontrol->value.enumerated.item[1] = val;
3313 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3316 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3317 * @kcontrol: mixer control
3318 * @ucontrol: control element information
3320 * Callback to set the value of a dapm enumerated double mixer control.
3322 * Returns 0 for success.
3324 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3325 struct snd_ctl_elem_value *ucontrol)
3327 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3328 struct snd_soc_card *card = dapm->card;
3329 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3330 unsigned int *item = ucontrol->value.enumerated.item;
3331 unsigned int val, change, reg_change = 0;
3333 struct snd_soc_dapm_update update = {};
3336 if (item[0] >= e->items)
3339 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3340 mask = e->mask << e->shift_l;
3341 if (e->shift_l != e->shift_r) {
3342 if (item[1] > e->items)
3344 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3345 mask |= e->mask << e->shift_r;
3348 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3350 change = dapm_kcontrol_set_value(kcontrol, val);
3352 if (e->reg != SND_SOC_NOPM)
3353 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3355 if (change || reg_change) {
3357 update.kcontrol = kcontrol;
3358 update.reg = e->reg;
3361 card->update = &update;
3363 change |= reg_change;
3365 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3367 card->update = NULL;
3370 mutex_unlock(&card->dapm_mutex);
3373 soc_dpcm_runtime_update(card);
3377 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3380 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3382 * @kcontrol: mixer control
3383 * @uinfo: control element information
3385 * Callback to provide information about a pin switch control.
3387 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3388 struct snd_ctl_elem_info *uinfo)
3390 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3392 uinfo->value.integer.min = 0;
3393 uinfo->value.integer.max = 1;
3397 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3400 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3402 * @kcontrol: mixer control
3405 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3406 struct snd_ctl_elem_value *ucontrol)
3408 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3409 const char *pin = (const char *)kcontrol->private_value;
3411 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3413 ucontrol->value.integer.value[0] =
3414 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3416 mutex_unlock(&card->dapm_mutex);
3420 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3423 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3425 * @kcontrol: mixer control
3428 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3429 struct snd_ctl_elem_value *ucontrol)
3431 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3432 const char *pin = (const char *)kcontrol->private_value;
3434 if (ucontrol->value.integer.value[0])
3435 snd_soc_dapm_enable_pin(&card->dapm, pin);
3437 snd_soc_dapm_disable_pin(&card->dapm, pin);
3439 snd_soc_dapm_sync(&card->dapm);
3442 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3444 struct snd_soc_dapm_widget *
3445 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3446 const struct snd_soc_dapm_widget *widget)
3448 enum snd_soc_dapm_direction dir;
3449 struct snd_soc_dapm_widget *w;
3453 if ((w = dapm_cnew_widget(widget)) == NULL)
3454 return ERR_PTR(-ENOMEM);
3457 case snd_soc_dapm_regulator_supply:
3458 w->regulator = devm_regulator_get(dapm->dev, w->name);
3459 if (IS_ERR(w->regulator)) {
3460 ret = PTR_ERR(w->regulator);
3461 goto request_failed;
3464 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3465 ret = regulator_allow_bypass(w->regulator, true);
3468 "ASoC: Failed to bypass %s: %d\n",
3472 case snd_soc_dapm_pinctrl:
3473 w->pinctrl = devm_pinctrl_get(dapm->dev);
3474 if (IS_ERR(w->pinctrl)) {
3475 ret = PTR_ERR(w->pinctrl);
3476 goto request_failed;
3479 case snd_soc_dapm_clock_supply:
3480 w->clk = devm_clk_get(dapm->dev, w->name);
3481 if (IS_ERR(w->clk)) {
3482 ret = PTR_ERR(w->clk);
3483 goto request_failed;
3490 prefix = soc_dapm_prefix(dapm);
3492 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3494 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3495 if (w->name == NULL) {
3497 return ERR_PTR(-ENOMEM);
3501 case snd_soc_dapm_mic:
3502 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3503 w->power_check = dapm_generic_check_power;
3505 case snd_soc_dapm_input:
3506 if (!dapm->card->fully_routed)
3507 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3508 w->power_check = dapm_generic_check_power;
3510 case snd_soc_dapm_spk:
3511 case snd_soc_dapm_hp:
3512 w->is_ep = SND_SOC_DAPM_EP_SINK;
3513 w->power_check = dapm_generic_check_power;
3515 case snd_soc_dapm_output:
3516 if (!dapm->card->fully_routed)
3517 w->is_ep = SND_SOC_DAPM_EP_SINK;
3518 w->power_check = dapm_generic_check_power;
3520 case snd_soc_dapm_vmid:
3521 case snd_soc_dapm_siggen:
3522 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3523 w->power_check = dapm_always_on_check_power;
3525 case snd_soc_dapm_sink:
3526 w->is_ep = SND_SOC_DAPM_EP_SINK;
3527 w->power_check = dapm_always_on_check_power;
3530 case snd_soc_dapm_mux:
3531 case snd_soc_dapm_demux:
3532 case snd_soc_dapm_switch:
3533 case snd_soc_dapm_mixer:
3534 case snd_soc_dapm_mixer_named_ctl:
3535 case snd_soc_dapm_adc:
3536 case snd_soc_dapm_aif_out:
3537 case snd_soc_dapm_dac:
3538 case snd_soc_dapm_aif_in:
3539 case snd_soc_dapm_pga:
3540 case snd_soc_dapm_out_drv:
3541 case snd_soc_dapm_micbias:
3542 case snd_soc_dapm_line:
3543 case snd_soc_dapm_dai_link:
3544 case snd_soc_dapm_dai_out:
3545 case snd_soc_dapm_dai_in:
3546 w->power_check = dapm_generic_check_power;
3548 case snd_soc_dapm_supply:
3549 case snd_soc_dapm_regulator_supply:
3550 case snd_soc_dapm_pinctrl:
3551 case snd_soc_dapm_clock_supply:
3552 case snd_soc_dapm_kcontrol:
3554 w->power_check = dapm_supply_check_power;
3557 w->power_check = dapm_always_on_check_power;
3562 INIT_LIST_HEAD(&w->list);
3563 INIT_LIST_HEAD(&w->dirty);
3564 list_add_tail(&w->list, &dapm->card->widgets);
3566 snd_soc_dapm_for_each_direction(dir) {
3567 INIT_LIST_HEAD(&w->edges[dir]);
3568 w->endpoints[dir] = -1;
3571 /* machine layer sets up unconnected pins and insertions */
3576 if (ret != -EPROBE_DEFER)
3577 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3580 return ERR_PTR(ret);
3584 * snd_soc_dapm_new_control - create new dapm control
3585 * @dapm: DAPM context
3586 * @widget: widget template
3588 * Creates new DAPM control based upon a template.
3590 * Returns a widget pointer on success or an error pointer on failure
3592 struct snd_soc_dapm_widget *
3593 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3594 const struct snd_soc_dapm_widget *widget)
3596 struct snd_soc_dapm_widget *w;
3598 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3599 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3600 mutex_unlock(&dapm->card->dapm_mutex);
3604 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3607 * snd_soc_dapm_new_controls - create new dapm controls
3608 * @dapm: DAPM context
3609 * @widget: widget array
3610 * @num: number of widgets
3612 * Creates new DAPM controls based upon the templates.
3614 * Returns 0 for success else error.
3616 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3617 const struct snd_soc_dapm_widget *widget,
3620 struct snd_soc_dapm_widget *w;
3624 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3625 for (i = 0; i < num; i++) {
3626 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3633 mutex_unlock(&dapm->card->dapm_mutex);
3636 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3638 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3639 struct snd_kcontrol *kcontrol, int event)
3641 struct snd_soc_dapm_path *path;
3642 struct snd_soc_dai *source, *sink;
3643 struct snd_soc_pcm_runtime *rtd = w->priv;
3644 const struct snd_soc_pcm_stream *config;
3645 struct snd_pcm_substream substream;
3646 struct snd_pcm_hw_params *params = NULL;
3647 struct snd_pcm_runtime *runtime = NULL;
3651 config = rtd->dai_link->params + rtd->params_select;
3653 if (WARN_ON(!config) ||
3654 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3655 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3658 /* Be a little careful as we don't want to overflow the mask array */
3659 if (config->formats) {
3660 fmt = ffs(config->formats) - 1;
3662 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3667 /* Currently very limited parameter selection */
3668 params = kzalloc(sizeof(*params), GFP_KERNEL);
3673 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3675 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3677 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3680 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3681 = config->channels_min;
3682 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3683 = config->channels_max;
3685 memset(&substream, 0, sizeof(substream));
3687 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3688 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3693 substream.runtime = runtime;
3694 substream.private_data = rtd;
3697 case SND_SOC_DAPM_PRE_PMU:
3698 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3699 snd_soc_dapm_widget_for_each_source_path(w, path) {
3700 source = path->source->priv;
3702 if (source->driver->ops->startup) {
3703 ret = source->driver->ops->startup(&substream,
3706 dev_err(source->dev,
3707 "ASoC: startup() failed: %d\n",
3713 ret = soc_dai_hw_params(&substream, params, source);
3718 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3719 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3720 sink = path->sink->priv;
3722 if (sink->driver->ops->startup) {
3723 ret = sink->driver->ops->startup(&substream,
3727 "ASoC: startup() failed: %d\n",
3733 ret = soc_dai_hw_params(&substream, params, sink);
3739 case SND_SOC_DAPM_POST_PMU:
3740 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3741 sink = path->sink->priv;
3743 ret = snd_soc_dai_digital_mute(sink, 0,
3744 SNDRV_PCM_STREAM_PLAYBACK);
3745 if (ret != 0 && ret != -ENOTSUPP)
3747 "ASoC: Failed to unmute: %d\n", ret);
3752 case SND_SOC_DAPM_PRE_PMD:
3753 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3754 sink = path->sink->priv;
3756 ret = snd_soc_dai_digital_mute(sink, 1,
3757 SNDRV_PCM_STREAM_PLAYBACK);
3758 if (ret != 0 && ret != -ENOTSUPP)
3760 "ASoC: Failed to mute: %d\n", ret);
3764 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3765 snd_soc_dapm_widget_for_each_source_path(w, path) {
3766 source = path->source->priv;
3768 if (source->driver->ops->hw_free)
3769 source->driver->ops->hw_free(&substream,
3773 if (source->driver->ops->shutdown)
3774 source->driver->ops->shutdown(&substream,
3778 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3779 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3780 sink = path->sink->priv;
3782 if (sink->driver->ops->hw_free)
3783 sink->driver->ops->hw_free(&substream, sink);
3786 if (sink->driver->ops->shutdown)
3787 sink->driver->ops->shutdown(&substream, sink);
3792 WARN(1, "Unknown event %d\n", event);
3802 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3803 struct snd_ctl_elem_value *ucontrol)
3805 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3806 struct snd_soc_pcm_runtime *rtd = w->priv;
3808 ucontrol->value.enumerated.item[0] = rtd->params_select;
3813 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3814 struct snd_ctl_elem_value *ucontrol)
3816 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3817 struct snd_soc_pcm_runtime *rtd = w->priv;
3819 /* Can't change the config when widget is already powered */
3823 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
3826 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
3829 rtd->params_select = ucontrol->value.enumerated.item[0];
3835 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3836 unsigned long *private_value,
3838 const char **w_param_text)
3842 devm_kfree(card->dev, (void *)*private_value);
3843 for (count = 0 ; count < num_params; count++)
3844 devm_kfree(card->dev, (void *)w_param_text[count]);
3845 devm_kfree(card->dev, w_param_text);
3848 static struct snd_kcontrol_new *
3849 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3851 const struct snd_soc_pcm_stream *params,
3852 int num_params, const char **w_param_text,
3853 unsigned long *private_value)
3855 struct soc_enum w_param_enum[] = {
3856 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3858 struct snd_kcontrol_new kcontrol_dai_link[] = {
3859 SOC_ENUM_EXT(NULL, w_param_enum[0],
3860 snd_soc_dapm_dai_link_get,
3861 snd_soc_dapm_dai_link_put),
3863 struct snd_kcontrol_new *kcontrol_news;
3864 const struct snd_soc_pcm_stream *config = params;
3867 for (count = 0 ; count < num_params; count++) {
3868 if (!config->stream_name) {
3869 dev_warn(card->dapm.dev,
3870 "ASoC: anonymous config %d for dai link %s\n",
3872 w_param_text[count] =
3873 devm_kasprintf(card->dev, GFP_KERNEL,
3874 "Anonymous Configuration %d",
3877 w_param_text[count] = devm_kmemdup(card->dev,
3878 config->stream_name,
3879 strlen(config->stream_name) + 1,
3882 if (!w_param_text[count])
3883 goto outfree_w_param;
3887 w_param_enum[0].items = num_params;
3888 w_param_enum[0].texts = w_param_text;
3891 (unsigned long) devm_kmemdup(card->dev,
3892 (void *)(kcontrol_dai_link[0].private_value),
3893 sizeof(struct soc_enum), GFP_KERNEL);
3894 if (!*private_value) {
3895 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3897 goto outfree_w_param;
3899 kcontrol_dai_link[0].private_value = *private_value;
3900 /* duplicate kcontrol_dai_link on heap so that memory persists */
3901 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3902 sizeof(struct snd_kcontrol_new),
3904 if (!kcontrol_news) {
3905 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3907 goto outfree_w_param;
3909 return kcontrol_news;
3912 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
3916 static struct snd_soc_dapm_widget *
3917 snd_soc_dapm_new_dai(struct snd_soc_card *card, struct snd_soc_pcm_runtime *rtd,
3918 struct snd_soc_dapm_widget *source,
3919 struct snd_soc_dapm_widget *sink)
3921 struct snd_soc_dapm_widget template;
3922 struct snd_soc_dapm_widget *w;
3923 const char **w_param_text;
3924 unsigned long private_value;
3928 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3929 source->name, sink->name);
3931 return ERR_PTR(-ENOMEM);
3933 memset(&template, 0, sizeof(template));
3934 template.reg = SND_SOC_NOPM;
3935 template.id = snd_soc_dapm_dai_link;
3936 template.name = link_name;
3937 template.event = snd_soc_dai_link_event;
3938 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3939 SND_SOC_DAPM_PRE_PMD;
3940 template.kcontrol_news = NULL;
3942 /* allocate memory for control, only in case of multiple configs */
3943 if (rtd->dai_link->num_params > 1) {
3944 w_param_text = devm_kcalloc(card->dev,
3945 rtd->dai_link->num_params,
3946 sizeof(char *), GFP_KERNEL);
3947 if (!w_param_text) {
3952 template.num_kcontrols = 1;
3953 template.kcontrol_news =
3954 snd_soc_dapm_alloc_kcontrol(card,
3956 rtd->dai_link->params,
3957 rtd->dai_link->num_params,
3958 w_param_text, &private_value);
3959 if (!template.kcontrol_news) {
3964 w_param_text = NULL;
3966 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3968 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
3971 goto outfree_kcontrol_news;
3978 outfree_kcontrol_news:
3979 devm_kfree(card->dev, (void *)template.kcontrol_news);
3980 snd_soc_dapm_free_kcontrol(card, &private_value,
3981 rtd->dai_link->num_params, w_param_text);
3983 devm_kfree(card->dev, link_name);
3984 return ERR_PTR(ret);
3987 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3988 struct snd_soc_dai *dai)
3990 struct snd_soc_dapm_widget template;
3991 struct snd_soc_dapm_widget *w;
3993 WARN_ON(dapm->dev != dai->dev);
3995 memset(&template, 0, sizeof(template));
3996 template.reg = SND_SOC_NOPM;
3998 if (dai->driver->playback.stream_name) {
3999 template.id = snd_soc_dapm_dai_in;
4000 template.name = dai->driver->playback.stream_name;
4001 template.sname = dai->driver->playback.stream_name;
4003 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4006 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4011 dai->playback_widget = w;
4014 if (dai->driver->capture.stream_name) {
4015 template.id = snd_soc_dapm_dai_out;
4016 template.name = dai->driver->capture.stream_name;
4017 template.sname = dai->driver->capture.stream_name;
4019 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4022 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4027 dai->capture_widget = w;
4033 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4035 struct snd_soc_dapm_widget *dai_w, *w;
4036 struct snd_soc_dapm_widget *src, *sink;
4037 struct snd_soc_dai *dai;
4039 /* For each DAI widget... */
4040 list_for_each_entry(dai_w, &card->widgets, list) {
4041 switch (dai_w->id) {
4042 case snd_soc_dapm_dai_in:
4043 case snd_soc_dapm_dai_out:
4049 /* let users know there is no DAI to link */
4051 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4058 /* ...find all widgets with the same stream and link them */
4059 list_for_each_entry(w, &card->widgets, list) {
4060 if (w->dapm != dai_w->dapm)
4064 case snd_soc_dapm_dai_in:
4065 case snd_soc_dapm_dai_out:
4071 if (!w->sname || !strstr(w->sname, dai_w->sname))
4074 if (dai_w->id == snd_soc_dapm_dai_in) {
4081 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4082 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4089 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4090 struct snd_soc_pcm_runtime *rtd)
4092 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4093 struct snd_soc_dai *codec_dai;
4094 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4095 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
4098 if (rtd->dai_link->params) {
4099 playback_cpu = cpu_dai->capture_widget;
4100 capture_cpu = cpu_dai->playback_widget;
4102 playback = cpu_dai->playback_widget;
4103 capture = cpu_dai->capture_widget;
4104 playback_cpu = playback;
4105 capture_cpu = capture;
4108 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4110 /* connect BE DAI playback if widgets are valid */
4111 codec = codec_dai->playback_widget;
4113 if (playback_cpu && codec) {
4115 playback = snd_soc_dapm_new_dai(card, rtd,
4118 if (IS_ERR(playback)) {
4120 "ASoC: Failed to create DAI %s: %ld\n",
4126 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4127 playback, NULL, NULL);
4130 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4131 cpu_dai->component->name, playback_cpu->name,
4132 codec_dai->component->name, codec->name);
4134 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4139 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4140 /* connect BE DAI capture if widgets are valid */
4141 codec = codec_dai->capture_widget;
4143 if (codec && capture_cpu) {
4145 capture = snd_soc_dapm_new_dai(card, rtd,
4148 if (IS_ERR(capture)) {
4150 "ASoC: Failed to create DAI %s: %ld\n",
4156 snd_soc_dapm_add_path(&card->dapm, capture,
4157 capture_cpu, NULL, NULL);
4160 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4161 codec_dai->component->name, codec->name,
4162 cpu_dai->component->name, capture_cpu->name);
4164 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4170 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4173 struct snd_soc_dapm_widget *w;
4176 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4177 w = dai->playback_widget;
4179 w = dai->capture_widget;
4182 dapm_mark_dirty(w, "stream event");
4184 if (w->id == snd_soc_dapm_dai_in) {
4185 ep = SND_SOC_DAPM_EP_SOURCE;
4186 dapm_widget_invalidate_input_paths(w);
4188 ep = SND_SOC_DAPM_EP_SINK;
4189 dapm_widget_invalidate_output_paths(w);
4193 case SND_SOC_DAPM_STREAM_START:
4197 case SND_SOC_DAPM_STREAM_STOP:
4201 case SND_SOC_DAPM_STREAM_SUSPEND:
4202 case SND_SOC_DAPM_STREAM_RESUME:
4203 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4204 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4210 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4212 struct snd_soc_pcm_runtime *rtd;
4214 /* for each BE DAI link... */
4215 for_each_card_rtds(card, rtd) {
4217 * dynamic FE links have no fixed DAI mapping.
4218 * CODEC<->CODEC links have no direct connection.
4220 if (rtd->dai_link->dynamic)
4223 dapm_connect_dai_link_widgets(card, rtd);
4227 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4230 struct snd_soc_dai *codec_dai;
4233 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4234 for_each_rtd_codec_dai(rtd, i, codec_dai)
4235 soc_dapm_dai_stream_event(codec_dai, stream, event);
4237 dapm_power_widgets(rtd->card, event);
4241 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4242 * @rtd: PCM runtime data
4243 * @stream: stream name
4244 * @event: stream event
4246 * Sends a stream event to the dapm core. The core then makes any
4247 * necessary widget power changes.
4249 * Returns 0 for success else error.
4251 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4254 struct snd_soc_card *card = rtd->card;
4256 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4257 soc_dapm_stream_event(rtd, stream, event);
4258 mutex_unlock(&card->dapm_mutex);
4262 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4263 * @dapm: DAPM context
4266 * Enables input/output pin and its parents or children widgets iff there is
4267 * a valid audio route and active audio stream.
4269 * Requires external locking.
4271 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4272 * do any widget power switching.
4274 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4277 return snd_soc_dapm_set_pin(dapm, pin, 1);
4279 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4282 * snd_soc_dapm_enable_pin - enable pin.
4283 * @dapm: DAPM context
4286 * Enables input/output pin and its parents or children widgets iff there is
4287 * a valid audio route and active audio stream.
4289 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4290 * do any widget power switching.
4292 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4296 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4298 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4300 mutex_unlock(&dapm->card->dapm_mutex);
4304 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4307 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4308 * @dapm: DAPM context
4311 * Enables input/output pin regardless of any other state. This is
4312 * intended for use with microphone bias supplies used in microphone
4315 * Requires external locking.
4317 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4318 * do any widget power switching.
4320 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4323 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4326 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4330 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4331 if (!w->connected) {
4333 * w->force does not affect the number of input or output paths,
4334 * so we only have to recheck if w->connected is changed
4336 dapm_widget_invalidate_input_paths(w);
4337 dapm_widget_invalidate_output_paths(w);
4341 dapm_mark_dirty(w, "force enable");
4345 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4348 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4349 * @dapm: DAPM context
4352 * Enables input/output pin regardless of any other state. This is
4353 * intended for use with microphone bias supplies used in microphone
4356 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4357 * do any widget power switching.
4359 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4364 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4366 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4368 mutex_unlock(&dapm->card->dapm_mutex);
4372 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4375 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4376 * @dapm: DAPM context
4379 * Disables input/output pin and its parents or children widgets.
4381 * Requires external locking.
4383 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4384 * do any widget power switching.
4386 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4389 return snd_soc_dapm_set_pin(dapm, pin, 0);
4391 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4394 * snd_soc_dapm_disable_pin - disable pin.
4395 * @dapm: DAPM context
4398 * Disables input/output pin and its parents or children widgets.
4400 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4401 * do any widget power switching.
4403 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4408 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4410 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4412 mutex_unlock(&dapm->card->dapm_mutex);
4416 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4419 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4420 * @dapm: DAPM context
4423 * Marks the specified pin as being not connected, disabling it along
4424 * any parent or child widgets. At present this is identical to
4425 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4426 * additional things such as disabling controls which only affect
4427 * paths through the pin.
4429 * Requires external locking.
4431 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4432 * do any widget power switching.
4434 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4437 return snd_soc_dapm_set_pin(dapm, pin, 0);
4439 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4442 * snd_soc_dapm_nc_pin - permanently disable pin.
4443 * @dapm: DAPM context
4446 * Marks the specified pin as being not connected, disabling it along
4447 * any parent or child widgets. At present this is identical to
4448 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4449 * additional things such as disabling controls which only affect
4450 * paths through the pin.
4452 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4453 * do any widget power switching.
4455 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4459 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4461 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4463 mutex_unlock(&dapm->card->dapm_mutex);
4467 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4470 * snd_soc_dapm_get_pin_status - get audio pin status
4471 * @dapm: DAPM context
4472 * @pin: audio signal pin endpoint (or start point)
4474 * Get audio pin status - connected or disconnected.
4476 * Returns 1 for connected otherwise 0.
4478 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4481 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4484 return w->connected;
4488 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4491 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4492 * @dapm: DAPM context
4493 * @pin: audio signal pin endpoint (or start point)
4495 * Mark the given endpoint or pin as ignoring suspend. When the
4496 * system is disabled a path between two endpoints flagged as ignoring
4497 * suspend will not be disabled. The path must already be enabled via
4498 * normal means at suspend time, it will not be turned on if it was not
4501 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4504 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4507 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4511 w->ignore_suspend = 1;
4515 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4518 * snd_soc_dapm_free - free dapm resources
4519 * @dapm: DAPM context
4521 * Free all dapm widgets and resources.
4523 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4525 dapm_debugfs_cleanup(dapm);
4526 dapm_free_widgets(dapm);
4527 list_del(&dapm->list);
4529 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4531 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4533 struct snd_soc_card *card = dapm->card;
4534 struct snd_soc_dapm_widget *w;
4535 LIST_HEAD(down_list);
4538 mutex_lock(&card->dapm_mutex);
4540 list_for_each_entry(w, &dapm->card->widgets, list) {
4541 if (w->dapm != dapm)
4544 dapm_seq_insert(w, &down_list, false);
4550 /* If there were no widgets to power down we're already in
4554 if (dapm->bias_level == SND_SOC_BIAS_ON)
4555 snd_soc_dapm_set_bias_level(dapm,
4556 SND_SOC_BIAS_PREPARE);
4557 dapm_seq_run(card, &down_list, 0, false);
4558 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4559 snd_soc_dapm_set_bias_level(dapm,
4560 SND_SOC_BIAS_STANDBY);
4563 mutex_unlock(&card->dapm_mutex);
4567 * snd_soc_dapm_shutdown - callback for system shutdown
4569 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4571 struct snd_soc_dapm_context *dapm;
4573 list_for_each_entry(dapm, &card->dapm_list, list) {
4574 if (dapm != &card->dapm) {
4575 soc_dapm_shutdown_dapm(dapm);
4576 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4577 snd_soc_dapm_set_bias_level(dapm,
4582 soc_dapm_shutdown_dapm(&card->dapm);
4583 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4584 snd_soc_dapm_set_bias_level(&card->dapm,
4588 /* Module information */
4589 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4590 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4591 MODULE_LICENSE("GPL");