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] = 1,
68 [snd_soc_dapm_regulator_supply] = 2,
69 [snd_soc_dapm_pinctrl] = 2,
70 [snd_soc_dapm_clock_supply] = 2,
71 [snd_soc_dapm_supply] = 3,
72 [snd_soc_dapm_micbias] = 4,
73 [snd_soc_dapm_vmid] = 4,
74 [snd_soc_dapm_dai_link] = 3,
75 [snd_soc_dapm_dai_in] = 5,
76 [snd_soc_dapm_dai_out] = 5,
77 [snd_soc_dapm_aif_in] = 5,
78 [snd_soc_dapm_aif_out] = 5,
79 [snd_soc_dapm_mic] = 6,
80 [snd_soc_dapm_siggen] = 6,
81 [snd_soc_dapm_input] = 6,
82 [snd_soc_dapm_output] = 6,
83 [snd_soc_dapm_mux] = 7,
84 [snd_soc_dapm_demux] = 7,
85 [snd_soc_dapm_dac] = 8,
86 [snd_soc_dapm_switch] = 9,
87 [snd_soc_dapm_mixer] = 9,
88 [snd_soc_dapm_mixer_named_ctl] = 9,
89 [snd_soc_dapm_pga] = 10,
90 [snd_soc_dapm_buffer] = 10,
91 [snd_soc_dapm_scheduler] = 10,
92 [snd_soc_dapm_effect] = 10,
93 [snd_soc_dapm_src] = 10,
94 [snd_soc_dapm_asrc] = 10,
95 [snd_soc_dapm_encoder] = 10,
96 [snd_soc_dapm_decoder] = 10,
97 [snd_soc_dapm_adc] = 11,
98 [snd_soc_dapm_out_drv] = 12,
99 [snd_soc_dapm_hp] = 12,
100 [snd_soc_dapm_spk] = 12,
101 [snd_soc_dapm_line] = 12,
102 [snd_soc_dapm_sink] = 12,
103 [snd_soc_dapm_kcontrol] = 13,
104 [snd_soc_dapm_post] = 14,
107 static int dapm_down_seq[] = {
108 [snd_soc_dapm_pre] = 1,
109 [snd_soc_dapm_kcontrol] = 2,
110 [snd_soc_dapm_adc] = 3,
111 [snd_soc_dapm_hp] = 4,
112 [snd_soc_dapm_spk] = 4,
113 [snd_soc_dapm_line] = 4,
114 [snd_soc_dapm_out_drv] = 4,
115 [snd_soc_dapm_sink] = 4,
116 [snd_soc_dapm_pga] = 5,
117 [snd_soc_dapm_buffer] = 5,
118 [snd_soc_dapm_scheduler] = 5,
119 [snd_soc_dapm_effect] = 5,
120 [snd_soc_dapm_src] = 5,
121 [snd_soc_dapm_asrc] = 5,
122 [snd_soc_dapm_encoder] = 5,
123 [snd_soc_dapm_decoder] = 5,
124 [snd_soc_dapm_switch] = 6,
125 [snd_soc_dapm_mixer_named_ctl] = 6,
126 [snd_soc_dapm_mixer] = 6,
127 [snd_soc_dapm_dac] = 7,
128 [snd_soc_dapm_mic] = 8,
129 [snd_soc_dapm_siggen] = 8,
130 [snd_soc_dapm_input] = 8,
131 [snd_soc_dapm_output] = 8,
132 [snd_soc_dapm_micbias] = 9,
133 [snd_soc_dapm_vmid] = 9,
134 [snd_soc_dapm_mux] = 10,
135 [snd_soc_dapm_demux] = 10,
136 [snd_soc_dapm_aif_in] = 11,
137 [snd_soc_dapm_aif_out] = 11,
138 [snd_soc_dapm_dai_in] = 11,
139 [snd_soc_dapm_dai_out] = 11,
140 [snd_soc_dapm_dai_link] = 12,
141 [snd_soc_dapm_supply] = 13,
142 [snd_soc_dapm_clock_supply] = 14,
143 [snd_soc_dapm_pinctrl] = 14,
144 [snd_soc_dapm_regulator_supply] = 14,
145 [snd_soc_dapm_post] = 15,
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));
161 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
169 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
174 vsnprintf(buf, PAGE_SIZE, fmt, args);
175 dev_info(dev, "%s", buf);
181 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
183 return !list_empty(&w->dirty);
186 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
188 dapm_assert_locked(w->dapm);
190 if (!dapm_dirty_widget(w)) {
191 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
193 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
198 * Common implementation for dapm_widget_invalidate_input_paths() and
199 * dapm_widget_invalidate_output_paths(). The function is inlined since the
200 * combined size of the two specialized functions is only marginally larger then
201 * the size of the generic function and at the same time the fast path of the
202 * specialized functions is significantly smaller than the generic function.
204 static __always_inline void dapm_widget_invalidate_paths(
205 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
207 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
208 struct snd_soc_dapm_widget *node;
209 struct snd_soc_dapm_path *p;
212 dapm_assert_locked(w->dapm);
214 if (w->endpoints[dir] == -1)
217 list_add_tail(&w->work_list, &list);
218 w->endpoints[dir] = -1;
220 list_for_each_entry(w, &list, work_list) {
221 snd_soc_dapm_widget_for_each_path(w, dir, p) {
222 if (p->is_supply || p->weak || !p->connect)
224 node = p->node[rdir];
225 if (node->endpoints[dir] != -1) {
226 node->endpoints[dir] = -1;
227 list_add_tail(&node->work_list, &list);
234 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
236 * @w: The widget for which to invalidate the cached number of input paths
238 * Resets the cached number of inputs for the specified widget and all widgets
239 * that can be reached via outcoming paths from the widget.
241 * This function must be called if the number of output paths for a widget might
242 * have changed. E.g. if the source state of a widget changes or a path is added
243 * or activated with the widget as the sink.
245 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
247 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
251 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
253 * @w: The widget for which to invalidate the cached number of output paths
255 * Resets the cached number of outputs for the specified widget and all widgets
256 * that can be reached via incoming paths from the widget.
258 * This function must be called if the number of output paths for a widget might
259 * have changed. E.g. if the sink state of a widget changes or a path is added
260 * or activated with the widget as the source.
262 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
264 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
268 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
269 * for the widgets connected to a path
270 * @p: The path to invalidate
272 * Resets the cached number of inputs for the sink of the path and the cached
273 * number of outputs for the source of the path.
275 * This function must be called when a path is added, removed or the connected
278 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
281 * Weak paths or supply paths do not influence the number of input or
282 * output paths of their neighbors.
284 if (p->weak || p->is_supply)
288 * The number of connected endpoints is the sum of the number of
289 * connected endpoints of all neighbors. If a node with 0 connected
290 * endpoints is either connected or disconnected that sum won't change,
291 * so there is no need to re-check the path.
293 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
294 dapm_widget_invalidate_input_paths(p->sink);
295 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
296 dapm_widget_invalidate_output_paths(p->source);
299 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
301 struct snd_soc_dapm_widget *w;
303 mutex_lock(&card->dapm_mutex);
305 list_for_each_entry(w, &card->widgets, list) {
307 dapm_mark_dirty(w, "Rechecking endpoints");
308 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
309 dapm_widget_invalidate_output_paths(w);
310 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
311 dapm_widget_invalidate_input_paths(w);
315 mutex_unlock(&card->dapm_mutex);
317 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
319 /* create a new dapm widget */
320 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
321 const struct snd_soc_dapm_widget *_widget)
323 struct snd_soc_dapm_widget *w;
325 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
330 * w->name is duplicated in caller, but w->sname isn't.
331 * Duplicate it here if defined
333 if (_widget->sname) {
334 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
343 struct dapm_kcontrol_data {
345 struct snd_soc_dapm_widget *widget;
346 struct list_head paths;
347 struct snd_soc_dapm_widget_list *wlist;
350 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
351 struct snd_kcontrol *kcontrol, const char *ctrl_name)
353 struct dapm_kcontrol_data *data;
354 struct soc_mixer_control *mc;
359 data = kzalloc(sizeof(*data), GFP_KERNEL);
363 INIT_LIST_HEAD(&data->paths);
365 switch (widget->id) {
366 case snd_soc_dapm_switch:
367 case snd_soc_dapm_mixer:
368 case snd_soc_dapm_mixer_named_ctl:
369 mc = (struct soc_mixer_control *)kcontrol->private_value;
371 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
372 dev_warn(widget->dapm->dev,
373 "ASoC: Unsupported stereo autodisable control '%s'\n",
376 if (mc->autodisable) {
377 struct snd_soc_dapm_widget template;
379 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
386 memset(&template, 0, sizeof(template));
387 template.reg = mc->reg;
388 template.mask = (1 << fls(mc->max)) - 1;
389 template.shift = mc->shift;
391 template.off_val = mc->max;
393 template.off_val = 0;
394 template.on_val = template.off_val;
395 template.id = snd_soc_dapm_kcontrol;
396 template.name = name;
398 data->value = template.on_val;
401 snd_soc_dapm_new_control_unlocked(widget->dapm,
404 if (IS_ERR(data->widget)) {
405 ret = PTR_ERR(data->widget);
410 case snd_soc_dapm_demux:
411 case snd_soc_dapm_mux:
412 e = (struct soc_enum *)kcontrol->private_value;
414 if (e->autodisable) {
415 struct snd_soc_dapm_widget template;
417 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
424 memset(&template, 0, sizeof(template));
425 template.reg = e->reg;
426 template.mask = e->mask << e->shift_l;
427 template.shift = e->shift_l;
428 template.off_val = snd_soc_enum_item_to_val(e, 0);
429 template.on_val = template.off_val;
430 template.id = snd_soc_dapm_kcontrol;
431 template.name = name;
433 data->value = template.on_val;
435 data->widget = snd_soc_dapm_new_control_unlocked(
436 widget->dapm, &template);
438 if (IS_ERR(data->widget)) {
439 ret = PTR_ERR(data->widget);
443 snd_soc_dapm_add_path(widget->dapm, data->widget,
451 kcontrol->private_data = data;
460 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
462 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
464 list_del(&data->paths);
469 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
470 const struct snd_kcontrol *kcontrol)
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
477 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
478 struct snd_soc_dapm_widget *widget)
480 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
481 struct snd_soc_dapm_widget_list *new_wlist;
485 n = data->wlist->num_widgets + 1;
489 new_wlist = krealloc(data->wlist,
490 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
494 new_wlist->widgets[n - 1] = widget;
495 new_wlist->num_widgets = n;
497 data->wlist = new_wlist;
502 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
503 struct snd_soc_dapm_path *path)
505 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
507 list_add_tail(&path->list_kcontrol, &data->paths);
510 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
512 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
517 return data->widget->power;
520 static struct list_head *dapm_kcontrol_get_path_list(
521 const struct snd_kcontrol *kcontrol)
523 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
528 #define dapm_kcontrol_for_each_path(path, kcontrol) \
529 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
532 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
534 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
538 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
540 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
543 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
545 if (data->value == value)
549 data->widget->on_val = value;
557 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
559 * @kcontrol: The kcontrol
561 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
562 struct snd_kcontrol *kcontrol)
564 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
566 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
569 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
571 * @kcontrol: The kcontrol
573 * Note: This function must only be used on kcontrols that are known to have
574 * been registered for a CODEC. Otherwise the behaviour is undefined.
576 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
577 struct snd_kcontrol *kcontrol)
579 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
581 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
583 static void dapm_reset(struct snd_soc_card *card)
585 struct snd_soc_dapm_widget *w;
587 lockdep_assert_held(&card->dapm_mutex);
589 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
591 list_for_each_entry(w, &card->widgets, list) {
592 w->new_power = w->power;
593 w->power_checked = false;
597 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
599 if (!dapm->component)
601 return dapm->component->name_prefix;
604 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
607 if (!dapm->component)
609 return snd_soc_component_read(dapm->component, reg, value);
612 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
613 int reg, unsigned int mask, unsigned int value)
615 if (!dapm->component)
617 return snd_soc_component_update_bits(dapm->component, reg,
621 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
622 int reg, unsigned int mask, unsigned int value)
624 if (!dapm->component)
626 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
629 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
632 snd_soc_component_async_complete(dapm->component);
635 static struct snd_soc_dapm_widget *
636 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
638 struct snd_soc_dapm_widget *w = wcache->widget;
639 struct list_head *wlist;
644 wlist = &w->dapm->card->widgets;
646 list_for_each_entry_from(w, wlist, list) {
647 if (!strcmp(name, w->name))
658 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
659 struct snd_soc_dapm_widget *w)
665 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
666 * @dapm: The DAPM context for which to set the level
667 * @level: The level to set
669 * Forces the DAPM bias level to a specific state. It will call the bias level
670 * callback of DAPM context with the specified level. This will even happen if
671 * the context is already at the same level. Furthermore it will not go through
672 * the normal bias level sequencing, meaning any intermediate states between the
673 * current and the target state will not be entered.
675 * Note that the change in bias level is only temporary and the next time
676 * snd_soc_dapm_sync() is called the state will be set to the level as
677 * determined by the DAPM core. The function is mainly intended to be used to
678 * used during probe or resume from suspend to power up the device so
679 * initialization can be done, before the DAPM core takes over.
681 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
682 enum snd_soc_bias_level level)
686 if (dapm->set_bias_level)
687 ret = dapm->set_bias_level(dapm, level);
690 dapm->bias_level = level;
694 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
697 * snd_soc_dapm_set_bias_level - set the bias level for the system
698 * @dapm: DAPM context
699 * @level: level to configure
701 * Configure the bias (power) levels for the SoC audio device.
703 * Returns 0 for success else error.
705 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
706 enum snd_soc_bias_level level)
708 struct snd_soc_card *card = dapm->card;
711 trace_snd_soc_bias_level_start(card, level);
713 if (card && card->set_bias_level)
714 ret = card->set_bias_level(card, dapm, level);
718 if (!card || dapm != &card->dapm)
719 ret = snd_soc_dapm_force_bias_level(dapm, level);
724 if (card && card->set_bias_level_post)
725 ret = card->set_bias_level_post(card, dapm, level);
727 trace_snd_soc_bias_level_done(card, level);
732 /* connect mux widget to its interconnecting audio paths */
733 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
734 struct snd_soc_dapm_path *path, const char *control_name,
735 struct snd_soc_dapm_widget *w)
737 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
738 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
739 unsigned int val, item;
742 if (e->reg != SND_SOC_NOPM) {
743 soc_dapm_read(dapm, e->reg, &val);
744 val = (val >> e->shift_l) & e->mask;
745 item = snd_soc_enum_val_to_item(e, val);
747 /* since a virtual mux has no backing registers to
748 * decide which path to connect, it will try to match
749 * with the first enumeration. This is to ensure
750 * that the default mux choice (the first) will be
751 * correctly powered up during initialization.
756 i = match_string(e->texts, e->items, control_name);
760 path->name = e->texts[i];
761 path->connect = (i == item);
766 /* set up initial codec paths */
767 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
770 struct soc_mixer_control *mc = (struct soc_mixer_control *)
771 p->sink->kcontrol_news[i].private_value;
772 unsigned int reg = mc->reg;
773 unsigned int shift = mc->shift;
774 unsigned int max = mc->max;
775 unsigned int mask = (1 << fls(max)) - 1;
776 unsigned int invert = mc->invert;
779 if (reg != SND_SOC_NOPM) {
780 soc_dapm_read(p->sink->dapm, reg, &val);
782 * The nth_path argument allows this function to know
783 * which path of a kcontrol it is setting the initial
784 * status for. Ideally this would support any number
785 * of paths and channels. But since kcontrols only come
786 * in mono and stereo variants, we are limited to 2
789 * The following code assumes for stereo controls the
790 * first path is the left channel, and all remaining
791 * paths are the right channel.
793 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
795 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
796 val = (val >> mc->rshift) & mask;
798 val = (val >> shift) & mask;
808 /* connect mixer widget to its interconnecting audio paths */
809 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
810 struct snd_soc_dapm_path *path, const char *control_name)
814 /* search for mixer kcontrol */
815 for (i = 0; i < path->sink->num_kcontrols; i++) {
816 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
817 path->name = path->sink->kcontrol_news[i].name;
818 dapm_set_mixer_path_status(path, i, nth_path++);
825 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
826 struct snd_soc_dapm_widget *kcontrolw,
827 const struct snd_kcontrol_new *kcontrol_new,
828 struct snd_kcontrol **kcontrol)
830 struct snd_soc_dapm_widget *w;
835 list_for_each_entry(w, &dapm->card->widgets, list) {
836 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
838 for (i = 0; i < w->num_kcontrols; i++) {
839 if (&w->kcontrol_news[i] == kcontrol_new) {
841 *kcontrol = w->kcontrols[i];
851 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
852 * create it. Either way, add the widget into the control's widget list
854 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
857 struct snd_soc_dapm_context *dapm = w->dapm;
858 struct snd_card *card = dapm->card->snd_card;
862 struct snd_kcontrol *kcontrol;
863 bool wname_in_long_name, kcname_in_long_name;
864 char *long_name = NULL;
868 prefix = soc_dapm_prefix(dapm);
870 prefix_len = strlen(prefix) + 1;
874 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
879 wname_in_long_name = false;
880 kcname_in_long_name = true;
883 case snd_soc_dapm_switch:
884 case snd_soc_dapm_mixer:
885 case snd_soc_dapm_pga:
886 case snd_soc_dapm_effect:
887 case snd_soc_dapm_out_drv:
888 wname_in_long_name = true;
889 kcname_in_long_name = true;
891 case snd_soc_dapm_mixer_named_ctl:
892 wname_in_long_name = false;
893 kcname_in_long_name = true;
895 case snd_soc_dapm_demux:
896 case snd_soc_dapm_mux:
897 wname_in_long_name = true;
898 kcname_in_long_name = false;
905 if (wname_in_long_name && kcname_in_long_name) {
907 * The control will get a prefix from the control
908 * creation process but we're also using the same
909 * prefix for widgets so cut the prefix off the
910 * front of the widget name.
912 long_name = kasprintf(GFP_KERNEL, "%s %s",
913 w->name + prefix_len,
914 w->kcontrol_news[kci].name);
915 if (long_name == NULL)
919 } else if (wname_in_long_name) {
921 name = w->name + prefix_len;
924 name = w->kcontrol_news[kci].name;
927 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
934 kcontrol->private_free = dapm_kcontrol_free;
936 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
938 snd_ctl_free_one(kcontrol);
942 ret = snd_ctl_add(card, kcontrol);
945 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
951 ret = dapm_kcontrol_add_widget(kcontrol, w);
953 w->kcontrols[kci] = kcontrol;
961 /* create new dapm mixer control */
962 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
965 struct snd_soc_dapm_path *path;
966 struct dapm_kcontrol_data *data;
969 for (i = 0; i < w->num_kcontrols; i++) {
971 snd_soc_dapm_widget_for_each_source_path(w, path) {
972 /* mixer/mux paths name must match control name */
973 if (path->name != (char *)w->kcontrol_news[i].name)
976 if (!w->kcontrols[i]) {
977 ret = dapm_create_or_share_kcontrol(w, i);
982 dapm_kcontrol_add_path(w->kcontrols[i], path);
984 data = snd_kcontrol_chip(w->kcontrols[i]);
986 snd_soc_dapm_add_path(data->widget->dapm,
996 /* create new dapm mux control */
997 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
999 struct snd_soc_dapm_context *dapm = w->dapm;
1000 enum snd_soc_dapm_direction dir;
1001 struct snd_soc_dapm_path *path;
1006 case snd_soc_dapm_mux:
1007 dir = SND_SOC_DAPM_DIR_OUT;
1010 case snd_soc_dapm_demux:
1011 dir = SND_SOC_DAPM_DIR_IN;
1018 if (w->num_kcontrols != 1) {
1020 "ASoC: %s %s has incorrect number of controls\n", type,
1025 if (list_empty(&w->edges[dir])) {
1026 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1030 ret = dapm_create_or_share_kcontrol(w, 0);
1034 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1036 dapm_kcontrol_add_path(w->kcontrols[0], path);
1042 /* create new dapm volume control */
1043 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1047 for (i = 0; i < w->num_kcontrols; i++) {
1048 ret = dapm_create_or_share_kcontrol(w, i);
1056 /* create new dapm dai link control */
1057 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1060 struct snd_kcontrol *kcontrol;
1061 struct snd_soc_dapm_context *dapm = w->dapm;
1062 struct snd_card *card = dapm->card->snd_card;
1063 struct snd_soc_pcm_runtime *rtd = w->priv;
1065 /* create control for links with > 1 config */
1066 if (rtd->dai_link->num_params <= 1)
1070 for (i = 0; i < w->num_kcontrols; i++) {
1071 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1073 ret = snd_ctl_add(card, kcontrol);
1076 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1077 w->name, w->kcontrol_news[i].name, ret);
1080 kcontrol->private_data = w;
1081 w->kcontrols[i] = kcontrol;
1087 /* We implement power down on suspend by checking the power state of
1088 * the ALSA card - when we are suspending the ALSA state for the card
1091 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1093 int level = snd_power_get_state(widget->dapm->card->snd_card);
1096 case SNDRV_CTL_POWER_D3hot:
1097 case SNDRV_CTL_POWER_D3cold:
1098 if (widget->ignore_suspend)
1099 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1101 return widget->ignore_suspend;
1107 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1108 struct list_head *widgets)
1110 struct snd_soc_dapm_widget *w;
1111 struct list_head *it;
1112 unsigned int size = 0;
1115 list_for_each(it, widgets)
1118 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1122 list_for_each_entry(w, widgets, work_list)
1123 (*list)->widgets[i++] = w;
1125 (*list)->num_widgets = i;
1131 * Common implementation for is_connected_output_ep() and
1132 * is_connected_input_ep(). The function is inlined since the combined size of
1133 * the two specialized functions is only marginally larger then the size of the
1134 * generic function and at the same time the fast path of the specialized
1135 * functions is significantly smaller than the generic function.
1137 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1138 struct list_head *list, enum snd_soc_dapm_direction dir,
1139 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1140 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1141 enum snd_soc_dapm_direction)),
1142 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1143 enum snd_soc_dapm_direction))
1145 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1146 struct snd_soc_dapm_path *path;
1149 if (widget->endpoints[dir] >= 0)
1150 return widget->endpoints[dir];
1152 DAPM_UPDATE_STAT(widget, path_checks);
1154 /* do we need to add this widget to the list ? */
1156 list_add_tail(&widget->work_list, list);
1158 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1159 widget->endpoints[dir] = 1;
1160 return widget->endpoints[dir];
1163 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1164 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1165 return widget->endpoints[dir];
1168 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1169 DAPM_UPDATE_STAT(widget, neighbour_checks);
1171 if (path->weak || path->is_supply)
1177 trace_snd_soc_dapm_path(widget, dir, path);
1179 if (path->connect) {
1181 con += fn(path->node[dir], list, custom_stop_condition);
1186 widget->endpoints[dir] = con;
1192 * Recursively check for a completed path to an active or physically connected
1193 * output widget. Returns number of complete paths.
1195 * Optionally, can be supplied with a function acting as a stopping condition.
1196 * This function takes the dapm widget currently being examined and the walk
1197 * direction as an arguments, it should return true if the walk should be
1198 * stopped and false otherwise.
1200 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1201 struct list_head *list,
1202 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1203 enum snd_soc_dapm_direction))
1205 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1206 is_connected_output_ep, custom_stop_condition);
1210 * Recursively check for a completed path to an active or physically connected
1211 * input widget. Returns number of complete paths.
1213 * Optionally, can be supplied with a function acting as a stopping condition.
1214 * This function takes the dapm widget currently being examined and the walk
1215 * direction as an arguments, it should return true if the walk should be
1216 * stopped and false otherwise.
1218 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1219 struct list_head *list,
1220 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1221 enum snd_soc_dapm_direction))
1223 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1224 is_connected_input_ep, custom_stop_condition);
1228 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1229 * @dai: the soc DAI.
1230 * @stream: stream direction.
1231 * @list: list of active widgets for this stream.
1232 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1233 * walk based on custom logic.
1235 * Queries DAPM graph as to whether a valid audio stream path exists for
1236 * the initial stream specified by name. This takes into account
1237 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1239 * Optionally, can be supplied with a function acting as a stopping condition.
1240 * This function takes the dapm widget currently being examined and the walk
1241 * direction as an arguments, it should return true if the walk should be
1242 * stopped and false otherwise.
1244 * Returns the number of valid paths or negative error.
1246 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1247 struct snd_soc_dapm_widget_list **list,
1248 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1249 enum snd_soc_dapm_direction))
1251 struct snd_soc_card *card = dai->component->card;
1252 struct snd_soc_dapm_widget *w;
1257 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1260 * For is_connected_{output,input}_ep fully discover the graph we need
1261 * to reset the cached number of inputs and outputs.
1263 list_for_each_entry(w, &card->widgets, list) {
1264 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1265 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1268 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1269 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1270 custom_stop_condition);
1272 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1273 custom_stop_condition);
1275 /* Drop starting point */
1276 list_del(widgets.next);
1278 ret = dapm_widget_list_create(list, &widgets);
1282 trace_snd_soc_dapm_connected(paths, stream);
1283 mutex_unlock(&card->dapm_mutex);
1289 * Handler for regulator supply widget.
1291 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1292 struct snd_kcontrol *kcontrol, int event)
1296 soc_dapm_async_complete(w->dapm);
1298 if (SND_SOC_DAPM_EVENT_ON(event)) {
1299 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1300 ret = regulator_allow_bypass(w->regulator, false);
1302 dev_warn(w->dapm->dev,
1303 "ASoC: Failed to unbypass %s: %d\n",
1307 return regulator_enable(w->regulator);
1309 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1310 ret = regulator_allow_bypass(w->regulator, true);
1312 dev_warn(w->dapm->dev,
1313 "ASoC: Failed to bypass %s: %d\n",
1317 return regulator_disable_deferred(w->regulator, w->shift);
1320 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1323 * Handler for pinctrl widget.
1325 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1326 struct snd_kcontrol *kcontrol, int event)
1328 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1329 struct pinctrl *p = w->pinctrl;
1330 struct pinctrl_state *s;
1335 if (SND_SOC_DAPM_EVENT_ON(event))
1336 s = pinctrl_lookup_state(p, priv->active_state);
1338 s = pinctrl_lookup_state(p, priv->sleep_state);
1343 return pinctrl_select_state(p, s);
1345 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1348 * Handler for clock supply widget.
1350 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1351 struct snd_kcontrol *kcontrol, int event)
1356 soc_dapm_async_complete(w->dapm);
1358 if (SND_SOC_DAPM_EVENT_ON(event)) {
1359 return clk_prepare_enable(w->clk);
1361 clk_disable_unprepare(w->clk);
1367 EXPORT_SYMBOL_GPL(dapm_clock_event);
1369 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1371 if (w->power_checked)
1372 return w->new_power;
1377 w->new_power = w->power_check(w);
1379 w->power_checked = true;
1381 return w->new_power;
1384 /* Generic check to see if a widget should be powered. */
1385 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1389 DAPM_UPDATE_STAT(w, power_checks);
1391 in = is_connected_input_ep(w, NULL, NULL);
1392 out = is_connected_output_ep(w, NULL, NULL);
1393 return out != 0 && in != 0;
1396 /* Check to see if a power supply is needed */
1397 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1399 struct snd_soc_dapm_path *path;
1401 DAPM_UPDATE_STAT(w, power_checks);
1403 /* Check if one of our outputs is connected */
1404 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1405 DAPM_UPDATE_STAT(w, neighbour_checks);
1410 if (path->connected &&
1411 !path->connected(path->source, path->sink))
1414 if (dapm_widget_power_check(path->sink))
1421 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1423 return w->connected;
1426 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1427 struct snd_soc_dapm_widget *b,
1432 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1433 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1438 sort = dapm_down_seq;
1440 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1441 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1443 if (sort[a->id] != sort[b->id])
1444 return sort[a->id] - sort[b->id];
1445 if (a->subseq != b->subseq) {
1447 return a->subseq - b->subseq;
1449 return b->subseq - a->subseq;
1451 if (a->reg != b->reg)
1452 return a->reg - b->reg;
1453 if (a->dapm != b->dapm)
1454 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1459 /* Insert a widget in order into a DAPM power sequence. */
1460 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1461 struct list_head *list,
1464 struct snd_soc_dapm_widget *w;
1466 list_for_each_entry(w, list, power_list)
1467 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1468 list_add_tail(&new_widget->power_list, &w->power_list);
1472 list_add_tail(&new_widget->power_list, list);
1475 static void dapm_seq_check_event(struct snd_soc_card *card,
1476 struct snd_soc_dapm_widget *w, int event)
1478 const char *ev_name;
1482 case SND_SOC_DAPM_PRE_PMU:
1483 ev_name = "PRE_PMU";
1486 case SND_SOC_DAPM_POST_PMU:
1487 ev_name = "POST_PMU";
1490 case SND_SOC_DAPM_PRE_PMD:
1491 ev_name = "PRE_PMD";
1494 case SND_SOC_DAPM_POST_PMD:
1495 ev_name = "POST_PMD";
1498 case SND_SOC_DAPM_WILL_PMU:
1499 ev_name = "WILL_PMU";
1502 case SND_SOC_DAPM_WILL_PMD:
1503 ev_name = "WILL_PMD";
1507 WARN(1, "Unknown event %d\n", event);
1511 if (w->new_power != power)
1514 if (w->event && (w->event_flags & event)) {
1515 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1517 soc_dapm_async_complete(w->dapm);
1518 trace_snd_soc_dapm_widget_event_start(w, event);
1519 ret = w->event(w, NULL, event);
1520 trace_snd_soc_dapm_widget_event_done(w, event);
1522 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1523 ev_name, w->name, ret);
1527 /* Apply the coalesced changes from a DAPM sequence */
1528 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1529 struct list_head *pending)
1531 struct snd_soc_dapm_context *dapm;
1532 struct snd_soc_dapm_widget *w;
1534 unsigned int value = 0;
1535 unsigned int mask = 0;
1537 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1541 list_for_each_entry(w, pending, power_list) {
1542 WARN_ON(reg != w->reg || dapm != w->dapm);
1543 w->power = w->new_power;
1545 mask |= w->mask << w->shift;
1547 value |= w->on_val << w->shift;
1549 value |= w->off_val << w->shift;
1551 pop_dbg(dapm->dev, card->pop_time,
1552 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1553 w->name, reg, value, mask);
1555 /* Check for events */
1556 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1557 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1561 /* Any widget will do, they should all be updating the
1565 pop_dbg(dapm->dev, card->pop_time,
1566 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1567 value, mask, reg, card->pop_time);
1568 pop_wait(card->pop_time);
1569 soc_dapm_update_bits(dapm, reg, mask, value);
1572 list_for_each_entry(w, pending, power_list) {
1573 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1574 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1578 /* Apply a DAPM power sequence.
1580 * We walk over a pre-sorted list of widgets to apply power to. In
1581 * order to minimise the number of writes to the device required
1582 * multiple widgets will be updated in a single write where possible.
1583 * Currently anything that requires more than a single write is not
1586 static void dapm_seq_run(struct snd_soc_card *card,
1587 struct list_head *list, int event, bool power_up)
1589 struct snd_soc_dapm_widget *w, *n;
1590 struct snd_soc_dapm_context *d;
1593 int cur_subseq = -1;
1594 int cur_reg = SND_SOC_NOPM;
1595 struct snd_soc_dapm_context *cur_dapm = NULL;
1602 sort = dapm_down_seq;
1604 list_for_each_entry_safe(w, n, list, power_list) {
1607 /* Do we need to apply any queued changes? */
1608 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1609 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1610 if (!list_empty(&pending))
1611 dapm_seq_run_coalesced(card, &pending);
1613 if (cur_dapm && cur_dapm->seq_notifier) {
1614 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1615 if (sort[i] == cur_sort)
1616 cur_dapm->seq_notifier(cur_dapm,
1621 if (cur_dapm && w->dapm != cur_dapm)
1622 soc_dapm_async_complete(cur_dapm);
1624 INIT_LIST_HEAD(&pending);
1626 cur_subseq = INT_MIN;
1627 cur_reg = SND_SOC_NOPM;
1632 case snd_soc_dapm_pre:
1634 list_for_each_entry_safe_continue(w, n, list,
1637 if (event == SND_SOC_DAPM_STREAM_START)
1639 NULL, SND_SOC_DAPM_PRE_PMU);
1640 else if (event == SND_SOC_DAPM_STREAM_STOP)
1642 NULL, SND_SOC_DAPM_PRE_PMD);
1645 case snd_soc_dapm_post:
1647 list_for_each_entry_safe_continue(w, n, list,
1650 if (event == SND_SOC_DAPM_STREAM_START)
1652 NULL, SND_SOC_DAPM_POST_PMU);
1653 else if (event == SND_SOC_DAPM_STREAM_STOP)
1655 NULL, SND_SOC_DAPM_POST_PMD);
1659 /* Queue it up for application */
1660 cur_sort = sort[w->id];
1661 cur_subseq = w->subseq;
1664 list_move(&w->power_list, &pending);
1669 dev_err(w->dapm->dev,
1670 "ASoC: Failed to apply widget power: %d\n", ret);
1673 if (!list_empty(&pending))
1674 dapm_seq_run_coalesced(card, &pending);
1676 if (cur_dapm && cur_dapm->seq_notifier) {
1677 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1678 if (sort[i] == cur_sort)
1679 cur_dapm->seq_notifier(cur_dapm,
1683 list_for_each_entry(d, &card->dapm_list, list) {
1684 soc_dapm_async_complete(d);
1688 static void dapm_widget_update(struct snd_soc_card *card)
1690 struct snd_soc_dapm_update *update = card->update;
1691 struct snd_soc_dapm_widget_list *wlist;
1692 struct snd_soc_dapm_widget *w = NULL;
1696 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1699 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1701 for (wi = 0; wi < wlist->num_widgets; wi++) {
1702 w = wlist->widgets[wi];
1704 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1705 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1707 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1715 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1718 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1721 if (update->has_second_set) {
1722 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1723 update->mask2, update->val2);
1725 dev_err(w->dapm->dev,
1726 "ASoC: %s DAPM update failed: %d\n",
1730 for (wi = 0; wi < wlist->num_widgets; wi++) {
1731 w = wlist->widgets[wi];
1733 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1734 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1736 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1742 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1743 * they're changing state.
1745 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1747 struct snd_soc_dapm_context *d = data;
1750 /* If we're off and we're not supposed to go into STANDBY */
1751 if (d->bias_level == SND_SOC_BIAS_OFF &&
1752 d->target_bias_level != SND_SOC_BIAS_OFF) {
1754 pm_runtime_get_sync(d->dev);
1756 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1759 "ASoC: Failed to turn on bias: %d\n", ret);
1762 /* Prepare for a transition to ON or away from ON */
1763 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1764 d->bias_level != SND_SOC_BIAS_ON) ||
1765 (d->target_bias_level != SND_SOC_BIAS_ON &&
1766 d->bias_level == SND_SOC_BIAS_ON)) {
1767 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1770 "ASoC: Failed to prepare bias: %d\n", ret);
1774 /* Async callback run prior to DAPM sequences - brings to their final
1777 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1779 struct snd_soc_dapm_context *d = data;
1782 /* If we just powered the last thing off drop to standby bias */
1783 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1784 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1785 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1786 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1788 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1792 /* If we're in standby and can support bias off then do that */
1793 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1794 d->target_bias_level == SND_SOC_BIAS_OFF) {
1795 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1797 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1801 pm_runtime_put(d->dev);
1804 /* If we just powered up then move to active bias */
1805 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1806 d->target_bias_level == SND_SOC_BIAS_ON) {
1807 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1809 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1814 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1815 bool power, bool connect)
1817 /* If a connection is being made or broken then that update
1818 * will have marked the peer dirty, otherwise the widgets are
1819 * not connected and this update has no impact. */
1823 /* If the peer is already in the state we're moving to then we
1824 * won't have an impact on it. */
1825 if (power != peer->power)
1826 dapm_mark_dirty(peer, "peer state change");
1829 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1830 struct list_head *up_list,
1831 struct list_head *down_list)
1833 struct snd_soc_dapm_path *path;
1835 if (w->power == power)
1838 trace_snd_soc_dapm_widget_power(w, power);
1840 /* If we changed our power state perhaps our neigbours changed
1843 snd_soc_dapm_widget_for_each_source_path(w, path)
1844 dapm_widget_set_peer_power(path->source, power, path->connect);
1846 /* Supplies can't affect their outputs, only their inputs */
1847 if (!w->is_supply) {
1848 snd_soc_dapm_widget_for_each_sink_path(w, path)
1849 dapm_widget_set_peer_power(path->sink, power,
1854 dapm_seq_insert(w, up_list, true);
1856 dapm_seq_insert(w, down_list, false);
1859 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1860 struct list_head *up_list,
1861 struct list_head *down_list)
1866 case snd_soc_dapm_pre:
1867 dapm_seq_insert(w, down_list, false);
1869 case snd_soc_dapm_post:
1870 dapm_seq_insert(w, up_list, true);
1874 power = dapm_widget_power_check(w);
1876 dapm_widget_set_power(w, power, up_list, down_list);
1881 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1883 if (dapm->idle_bias_off)
1886 switch (snd_power_get_state(dapm->card->snd_card)) {
1887 case SNDRV_CTL_POWER_D3hot:
1888 case SNDRV_CTL_POWER_D3cold:
1889 return dapm->suspend_bias_off;
1898 * Scan each dapm widget for complete audio path.
1899 * A complete path is a route that has valid endpoints i.e.:-
1901 * o DAC to output pin.
1902 * o Input pin to ADC.
1903 * o Input pin to Output pin (bypass, sidetone)
1904 * o DAC to ADC (loopback).
1906 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1908 struct snd_soc_dapm_widget *w;
1909 struct snd_soc_dapm_context *d;
1911 LIST_HEAD(down_list);
1912 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1913 enum snd_soc_bias_level bias;
1915 lockdep_assert_held(&card->dapm_mutex);
1917 trace_snd_soc_dapm_start(card);
1919 list_for_each_entry(d, &card->dapm_list, list) {
1920 if (dapm_idle_bias_off(d))
1921 d->target_bias_level = SND_SOC_BIAS_OFF;
1923 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1928 /* Check which widgets we need to power and store them in
1929 * lists indicating if they should be powered up or down. We
1930 * only check widgets that have been flagged as dirty but note
1931 * that new widgets may be added to the dirty list while we
1934 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1935 dapm_power_one_widget(w, &up_list, &down_list);
1938 list_for_each_entry(w, &card->widgets, list) {
1940 case snd_soc_dapm_pre:
1941 case snd_soc_dapm_post:
1942 /* These widgets always need to be powered */
1945 list_del_init(&w->dirty);
1952 /* Supplies and micbiases only bring the
1953 * context up to STANDBY as unless something
1954 * else is active and passing audio they
1955 * generally don't require full power. Signal
1956 * generators are virtual pins and have no
1957 * power impact themselves.
1960 case snd_soc_dapm_siggen:
1961 case snd_soc_dapm_vmid:
1963 case snd_soc_dapm_supply:
1964 case snd_soc_dapm_regulator_supply:
1965 case snd_soc_dapm_pinctrl:
1966 case snd_soc_dapm_clock_supply:
1967 case snd_soc_dapm_micbias:
1968 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1969 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1972 d->target_bias_level = SND_SOC_BIAS_ON;
1979 /* Force all contexts in the card to the same bias state if
1980 * they're not ground referenced.
1982 bias = SND_SOC_BIAS_OFF;
1983 list_for_each_entry(d, &card->dapm_list, list)
1984 if (d->target_bias_level > bias)
1985 bias = d->target_bias_level;
1986 list_for_each_entry(d, &card->dapm_list, list)
1987 if (!dapm_idle_bias_off(d))
1988 d->target_bias_level = bias;
1990 trace_snd_soc_dapm_walk_done(card);
1992 /* Run card bias changes at first */
1993 dapm_pre_sequence_async(&card->dapm, 0);
1994 /* Run other bias changes in parallel */
1995 list_for_each_entry(d, &card->dapm_list, list) {
1996 if (d != &card->dapm && d->bias_level != d->target_bias_level)
1997 async_schedule_domain(dapm_pre_sequence_async, d,
2000 async_synchronize_full_domain(&async_domain);
2002 list_for_each_entry(w, &down_list, power_list) {
2003 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2006 list_for_each_entry(w, &up_list, power_list) {
2007 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2010 /* Power down widgets first; try to avoid amplifying pops. */
2011 dapm_seq_run(card, &down_list, event, false);
2013 dapm_widget_update(card);
2016 dapm_seq_run(card, &up_list, event, true);
2018 /* Run all the bias changes in parallel */
2019 list_for_each_entry(d, &card->dapm_list, list) {
2020 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2021 async_schedule_domain(dapm_post_sequence_async, d,
2024 async_synchronize_full_domain(&async_domain);
2025 /* Run card bias changes at last */
2026 dapm_post_sequence_async(&card->dapm, 0);
2028 /* do we need to notify any clients that DAPM event is complete */
2029 list_for_each_entry(d, &card->dapm_list, list) {
2030 if (d->stream_event)
2031 d->stream_event(d, event);
2034 pop_dbg(card->dev, card->pop_time,
2035 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2036 pop_wait(card->pop_time);
2038 trace_snd_soc_dapm_done(card);
2043 #ifdef CONFIG_DEBUG_FS
2044 static ssize_t dapm_widget_power_read_file(struct file *file,
2045 char __user *user_buf,
2046 size_t count, loff_t *ppos)
2048 struct snd_soc_dapm_widget *w = file->private_data;
2049 struct snd_soc_card *card = w->dapm->card;
2050 enum snd_soc_dapm_direction dir, rdir;
2054 struct snd_soc_dapm_path *p = NULL;
2056 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2060 mutex_lock(&card->dapm_mutex);
2062 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2067 in = is_connected_input_ep(w, NULL, NULL);
2068 out = is_connected_output_ep(w, NULL, NULL);
2071 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2072 w->name, w->power ? "On" : "Off",
2073 w->force ? " (forced)" : "", in, out);
2076 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2077 " - R%d(0x%x) mask 0x%x",
2078 w->reg, w->reg, w->mask << w->shift);
2080 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2083 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2085 w->active ? "active" : "inactive");
2087 snd_soc_dapm_for_each_direction(dir) {
2088 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2089 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2090 if (p->connected && !p->connected(p->source, p->sink))
2096 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2097 " %s \"%s\" \"%s\"\n",
2098 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2099 p->name ? p->name : "static",
2100 p->node[rdir]->name);
2104 mutex_unlock(&card->dapm_mutex);
2106 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2112 static const struct file_operations dapm_widget_power_fops = {
2113 .open = simple_open,
2114 .read = dapm_widget_power_read_file,
2115 .llseek = default_llseek,
2118 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2119 size_t count, loff_t *ppos)
2121 struct snd_soc_dapm_context *dapm = file->private_data;
2124 switch (dapm->bias_level) {
2125 case SND_SOC_BIAS_ON:
2128 case SND_SOC_BIAS_PREPARE:
2129 level = "Prepare\n";
2131 case SND_SOC_BIAS_STANDBY:
2132 level = "Standby\n";
2134 case SND_SOC_BIAS_OFF:
2138 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2139 level = "Unknown\n";
2143 return simple_read_from_buffer(user_buf, count, ppos, level,
2147 static const struct file_operations dapm_bias_fops = {
2148 .open = simple_open,
2149 .read = dapm_bias_read_file,
2150 .llseek = default_llseek,
2153 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2154 struct dentry *parent)
2161 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2163 if (!dapm->debugfs_dapm) {
2165 "ASoC: Failed to create DAPM debugfs directory\n");
2169 d = debugfs_create_file("bias_level", 0444,
2170 dapm->debugfs_dapm, dapm,
2174 "ASoC: Failed to create bias level debugfs file\n");
2177 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2179 struct snd_soc_dapm_context *dapm = w->dapm;
2182 if (!dapm->debugfs_dapm || !w->name)
2185 d = debugfs_create_file(w->name, 0444,
2186 dapm->debugfs_dapm, w,
2187 &dapm_widget_power_fops);
2189 dev_warn(w->dapm->dev,
2190 "ASoC: Failed to create %s debugfs file\n",
2194 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2196 if (!dapm->debugfs_dapm)
2198 debugfs_remove_recursive(dapm->debugfs_dapm);
2199 dapm->debugfs_dapm = NULL;
2203 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2204 struct dentry *parent)
2208 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2212 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2219 * soc_dapm_connect_path() - Connects or disconnects a path
2220 * @path: The path to update
2221 * @connect: The new connect state of the path. True if the path is connected,
2222 * false if it is disconnected.
2223 * @reason: The reason why the path changed (for debugging only)
2225 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2226 bool connect, const char *reason)
2228 if (path->connect == connect)
2231 path->connect = connect;
2232 dapm_mark_dirty(path->source, reason);
2233 dapm_mark_dirty(path->sink, reason);
2234 dapm_path_invalidate(path);
2237 /* test and update the power status of a mux widget */
2238 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2239 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2241 struct snd_soc_dapm_path *path;
2245 lockdep_assert_held(&card->dapm_mutex);
2247 /* find dapm widget path assoc with kcontrol */
2248 dapm_kcontrol_for_each_path(path, kcontrol) {
2250 /* we now need to match the string in the enum to the path */
2251 if (!(strcmp(path->name, e->texts[mux])))
2256 soc_dapm_connect_path(path, connect, "mux update");
2260 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2265 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2266 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2267 struct snd_soc_dapm_update *update)
2269 struct snd_soc_card *card = dapm->card;
2272 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2273 card->update = update;
2274 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2275 card->update = NULL;
2276 mutex_unlock(&card->dapm_mutex);
2278 soc_dpcm_runtime_update(card);
2281 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2283 /* test and update the power status of a mixer or switch widget */
2284 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2285 struct snd_kcontrol *kcontrol,
2286 int connect, int rconnect)
2288 struct snd_soc_dapm_path *path;
2291 lockdep_assert_held(&card->dapm_mutex);
2293 /* find dapm widget path assoc with kcontrol */
2294 dapm_kcontrol_for_each_path(path, kcontrol) {
2296 * Ideally this function should support any number of
2297 * paths and channels. But since kcontrols only come
2298 * in mono and stereo variants, we are limited to 2
2301 * The following code assumes for stereo controls the
2302 * first path (when 'found == 0') is the left channel,
2303 * and all remaining paths (when 'found == 1') are the
2306 * A stereo control is signified by a valid 'rconnect'
2307 * value, either 0 for unconnected, or >= 0 for connected.
2308 * This is chosen instead of using snd_soc_volsw_is_stereo,
2309 * so that the behavior of snd_soc_dapm_mixer_update_power
2310 * doesn't change even when the kcontrol passed in is
2313 * It passes 'connect' as the path connect status for
2314 * the left channel, and 'rconnect' for the right
2317 if (found && rconnect >= 0)
2318 soc_dapm_connect_path(path, rconnect, "mixer update");
2320 soc_dapm_connect_path(path, connect, "mixer update");
2325 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2330 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2331 struct snd_kcontrol *kcontrol, int connect,
2332 struct snd_soc_dapm_update *update)
2334 struct snd_soc_card *card = dapm->card;
2337 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2338 card->update = update;
2339 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2340 card->update = NULL;
2341 mutex_unlock(&card->dapm_mutex);
2343 soc_dpcm_runtime_update(card);
2346 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2348 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2351 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2352 struct snd_soc_dapm_widget *w;
2354 char *state = "not set";
2356 /* card won't be set for the dummy component, as a spot fix
2357 * we're checking for that case specifically here but in future
2358 * we will ensure that the dummy component looks like others.
2363 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2364 if (w->dapm != dapm)
2367 /* only display widgets that burn power */
2369 case snd_soc_dapm_hp:
2370 case snd_soc_dapm_mic:
2371 case snd_soc_dapm_spk:
2372 case snd_soc_dapm_line:
2373 case snd_soc_dapm_micbias:
2374 case snd_soc_dapm_dac:
2375 case snd_soc_dapm_adc:
2376 case snd_soc_dapm_pga:
2377 case snd_soc_dapm_effect:
2378 case snd_soc_dapm_out_drv:
2379 case snd_soc_dapm_mixer:
2380 case snd_soc_dapm_mixer_named_ctl:
2381 case snd_soc_dapm_supply:
2382 case snd_soc_dapm_regulator_supply:
2383 case snd_soc_dapm_pinctrl:
2384 case snd_soc_dapm_clock_supply:
2386 count += sprintf(buf + count, "%s: %s\n",
2387 w->name, w->power ? "On":"Off");
2394 switch (snd_soc_dapm_get_bias_level(dapm)) {
2395 case SND_SOC_BIAS_ON:
2398 case SND_SOC_BIAS_PREPARE:
2401 case SND_SOC_BIAS_STANDBY:
2404 case SND_SOC_BIAS_OFF:
2408 count += sprintf(buf + count, "PM State: %s\n", state);
2413 /* show dapm widget status in sys fs */
2414 static ssize_t dapm_widget_show(struct device *dev,
2415 struct device_attribute *attr, char *buf)
2417 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2418 struct snd_soc_dai *codec_dai;
2421 mutex_lock(&rtd->card->dapm_mutex);
2423 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2424 struct snd_soc_component *cmpnt = codec_dai->component;
2426 count += dapm_widget_show_component(cmpnt, buf + count);
2429 mutex_unlock(&rtd->card->dapm_mutex);
2434 static DEVICE_ATTR_RO(dapm_widget);
2436 struct attribute *soc_dapm_dev_attrs[] = {
2437 &dev_attr_dapm_widget.attr,
2441 static void dapm_free_path(struct snd_soc_dapm_path *path)
2443 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2444 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2445 list_del(&path->list_kcontrol);
2446 list_del(&path->list);
2450 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2452 struct snd_soc_dapm_path *p, *next_p;
2453 enum snd_soc_dapm_direction dir;
2457 * remove source and sink paths associated to this widget.
2458 * While removing the path, remove reference to it from both
2459 * source and sink widgets so that path is removed only once.
2461 snd_soc_dapm_for_each_direction(dir) {
2462 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2466 kfree(w->kcontrols);
2467 kfree_const(w->name);
2468 kfree_const(w->sname);
2472 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2474 dapm->path_sink_cache.widget = NULL;
2475 dapm->path_source_cache.widget = NULL;
2478 /* free all dapm widgets and resources */
2479 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2481 struct snd_soc_dapm_widget *w, *next_w;
2483 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2484 if (w->dapm != dapm)
2486 snd_soc_dapm_free_widget(w);
2488 snd_soc_dapm_reset_cache(dapm);
2491 static struct snd_soc_dapm_widget *dapm_find_widget(
2492 struct snd_soc_dapm_context *dapm, const char *pin,
2493 bool search_other_contexts)
2495 struct snd_soc_dapm_widget *w;
2496 struct snd_soc_dapm_widget *fallback = NULL;
2498 list_for_each_entry(w, &dapm->card->widgets, list) {
2499 if (!strcmp(w->name, pin)) {
2500 if (w->dapm == dapm)
2507 if (search_other_contexts)
2513 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2514 const char *pin, int status)
2516 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2518 dapm_assert_locked(dapm);
2521 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2525 if (w->connected != status) {
2526 dapm_mark_dirty(w, "pin configuration");
2527 dapm_widget_invalidate_input_paths(w);
2528 dapm_widget_invalidate_output_paths(w);
2531 w->connected = status;
2539 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2540 * @dapm: DAPM context
2542 * Walks all dapm audio paths and powers widgets according to their
2543 * stream or path usage.
2545 * Requires external locking.
2547 * Returns 0 for success.
2549 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2552 * Suppress early reports (eg, jacks syncing their state) to avoid
2553 * silly DAPM runs during card startup.
2555 if (!dapm->card || !dapm->card->instantiated)
2558 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2560 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2563 * snd_soc_dapm_sync - scan and power dapm paths
2564 * @dapm: DAPM context
2566 * Walks all dapm audio paths and powers widgets according to their
2567 * stream or path usage.
2569 * Returns 0 for success.
2571 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2575 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2576 ret = snd_soc_dapm_sync_unlocked(dapm);
2577 mutex_unlock(&dapm->card->dapm_mutex);
2580 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2582 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2583 struct snd_soc_dapm_widget *w,
2587 case snd_soc_dapm_aif_out:
2588 case snd_soc_dapm_aif_in:
2594 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2595 w->channel < channels ? "Connecting" : "Disconnecting",
2596 p->source->name, p->sink->name);
2598 if (w->channel < channels)
2599 soc_dapm_connect_path(p, true, "dai update");
2601 soc_dapm_connect_path(p, false, "dai update");
2606 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2607 struct snd_pcm_hw_params *params,
2608 struct snd_soc_dai *dai)
2610 int dir = substream->stream;
2611 int channels = params_channels(params);
2612 struct snd_soc_dapm_path *p;
2613 struct snd_soc_dapm_widget *w;
2616 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
2617 w = dai->playback_widget;
2619 w = dai->capture_widget;
2624 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2625 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2627 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2628 ret = dapm_update_dai_chan(p, p->sink, channels);
2633 snd_soc_dapm_widget_for_each_source_path(w, p) {
2634 ret = dapm_update_dai_chan(p, p->source, channels);
2642 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2643 struct snd_pcm_hw_params *params,
2644 struct snd_soc_dai *dai)
2646 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2649 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2650 ret = dapm_update_dai_unlocked(substream, params, dai);
2651 mutex_unlock(&rtd->card->dapm_mutex);
2655 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2658 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2659 * @w: The widget for which to update the flags
2661 * Some widgets have a dynamic category which depends on which neighbors they
2662 * are connected to. This function update the category for these widgets.
2664 * This function must be called whenever a path is added or removed to a widget.
2666 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2668 enum snd_soc_dapm_direction dir;
2669 struct snd_soc_dapm_path *p;
2673 case snd_soc_dapm_input:
2674 /* On a fully routed card an input is never a source */
2675 if (w->dapm->card->fully_routed)
2677 ep = SND_SOC_DAPM_EP_SOURCE;
2678 snd_soc_dapm_widget_for_each_source_path(w, p) {
2679 if (p->source->id == snd_soc_dapm_micbias ||
2680 p->source->id == snd_soc_dapm_mic ||
2681 p->source->id == snd_soc_dapm_line ||
2682 p->source->id == snd_soc_dapm_output) {
2688 case snd_soc_dapm_output:
2689 /* On a fully routed card a output is never a sink */
2690 if (w->dapm->card->fully_routed)
2692 ep = SND_SOC_DAPM_EP_SINK;
2693 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2694 if (p->sink->id == snd_soc_dapm_spk ||
2695 p->sink->id == snd_soc_dapm_hp ||
2696 p->sink->id == snd_soc_dapm_line ||
2697 p->sink->id == snd_soc_dapm_input) {
2703 case snd_soc_dapm_line:
2705 snd_soc_dapm_for_each_direction(dir) {
2706 if (!list_empty(&w->edges[dir]))
2707 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2717 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2718 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2719 const char *control)
2721 bool dynamic_source = false;
2722 bool dynamic_sink = false;
2727 switch (source->id) {
2728 case snd_soc_dapm_demux:
2729 dynamic_source = true;
2736 case snd_soc_dapm_mux:
2737 case snd_soc_dapm_switch:
2738 case snd_soc_dapm_mixer:
2739 case snd_soc_dapm_mixer_named_ctl:
2740 dynamic_sink = true;
2746 if (dynamic_source && dynamic_sink) {
2748 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2749 source->name, control, sink->name);
2751 } else if (!dynamic_source && !dynamic_sink) {
2753 "Control not supported for path %s -> [%s] -> %s\n",
2754 source->name, control, sink->name);
2761 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2762 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2763 const char *control,
2764 int (*connected)(struct snd_soc_dapm_widget *source,
2765 struct snd_soc_dapm_widget *sink))
2767 struct snd_soc_dapm_widget *widgets[2];
2768 enum snd_soc_dapm_direction dir;
2769 struct snd_soc_dapm_path *path;
2772 if (wsink->is_supply && !wsource->is_supply) {
2774 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2775 wsource->name, wsink->name);
2779 if (connected && !wsource->is_supply) {
2781 "connected() callback only supported for supply widgets (%s -> %s)\n",
2782 wsource->name, wsink->name);
2786 if (wsource->is_supply && control) {
2788 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2789 wsource->name, control, wsink->name);
2793 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2797 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2801 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2802 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2803 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2804 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2806 path->connected = connected;
2807 INIT_LIST_HEAD(&path->list);
2808 INIT_LIST_HEAD(&path->list_kcontrol);
2810 if (wsource->is_supply || wsink->is_supply)
2811 path->is_supply = 1;
2813 /* connect static paths */
2814 if (control == NULL) {
2817 switch (wsource->id) {
2818 case snd_soc_dapm_demux:
2819 ret = dapm_connect_mux(dapm, path, control, wsource);
2827 switch (wsink->id) {
2828 case snd_soc_dapm_mux:
2829 ret = dapm_connect_mux(dapm, path, control, wsink);
2833 case snd_soc_dapm_switch:
2834 case snd_soc_dapm_mixer:
2835 case snd_soc_dapm_mixer_named_ctl:
2836 ret = dapm_connect_mixer(dapm, path, control);
2845 list_add(&path->list, &dapm->card->paths);
2846 snd_soc_dapm_for_each_direction(dir)
2847 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2849 snd_soc_dapm_for_each_direction(dir) {
2850 dapm_update_widget_flags(widgets[dir]);
2851 dapm_mark_dirty(widgets[dir], "Route added");
2854 if (dapm->card->instantiated && path->connect)
2855 dapm_path_invalidate(path);
2863 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2864 const struct snd_soc_dapm_route *route)
2866 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2867 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2870 char prefixed_sink[80];
2871 char prefixed_source[80];
2873 unsigned int sink_ref = 0;
2874 unsigned int source_ref = 0;
2877 prefix = soc_dapm_prefix(dapm);
2879 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2880 prefix, route->sink);
2881 sink = prefixed_sink;
2882 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2883 prefix, route->source);
2884 source = prefixed_source;
2887 source = route->source;
2890 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2891 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2893 if (wsink && wsource)
2897 * find src and dest widgets over all widgets but favor a widget from
2898 * current DAPM context
2900 list_for_each_entry(w, &dapm->card->widgets, list) {
2901 if (!wsink && !(strcmp(w->name, sink))) {
2903 if (w->dapm == dapm) {
2911 "ASoC: sink widget %s overwritten\n",
2915 if (!wsource && !(strcmp(w->name, source))) {
2917 if (w->dapm == dapm) {
2925 "ASoC: source widget %s overwritten\n",
2929 /* use widget from another DAPM context if not found from this */
2935 if (wsource == NULL) {
2936 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2940 if (wsink == NULL) {
2941 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2947 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2948 dapm_wcache_update(&dapm->path_source_cache, wsource);
2950 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2957 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2958 source, route->control, sink);
2962 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2963 const struct snd_soc_dapm_route *route)
2965 struct snd_soc_dapm_widget *wsource, *wsink;
2966 struct snd_soc_dapm_path *path, *p;
2969 char prefixed_sink[80];
2970 char prefixed_source[80];
2973 if (route->control) {
2975 "ASoC: Removal of routes with controls not supported\n");
2979 prefix = soc_dapm_prefix(dapm);
2981 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2982 prefix, route->sink);
2983 sink = prefixed_sink;
2984 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2985 prefix, route->source);
2986 source = prefixed_source;
2989 source = route->source;
2993 list_for_each_entry(p, &dapm->card->paths, list) {
2994 if (strcmp(p->source->name, source) != 0)
2996 if (strcmp(p->sink->name, sink) != 0)
3003 wsource = path->source;
3006 dapm_mark_dirty(wsource, "Route removed");
3007 dapm_mark_dirty(wsink, "Route removed");
3009 dapm_path_invalidate(path);
3011 dapm_free_path(path);
3013 /* Update any path related flags */
3014 dapm_update_widget_flags(wsource);
3015 dapm_update_widget_flags(wsink);
3017 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3025 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3026 * @dapm: DAPM context
3027 * @route: audio routes
3028 * @num: number of routes
3030 * Connects 2 dapm widgets together via a named audio path. The sink is
3031 * the widget receiving the audio signal, whilst the source is the sender
3032 * of the audio signal.
3034 * Returns 0 for success else error. On error all resources can be freed
3035 * with a call to snd_soc_card_free().
3037 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3038 const struct snd_soc_dapm_route *route, int num)
3042 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3043 for (i = 0; i < num; i++) {
3044 r = snd_soc_dapm_add_route(dapm, route);
3046 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3048 route->control ? route->control : "direct",
3054 mutex_unlock(&dapm->card->dapm_mutex);
3058 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3061 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3062 * @dapm: DAPM context
3063 * @route: audio routes
3064 * @num: number of routes
3066 * Removes routes from the DAPM context.
3068 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3069 const struct snd_soc_dapm_route *route, int num)
3073 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3074 for (i = 0; i < num; i++) {
3075 snd_soc_dapm_del_route(dapm, route);
3078 mutex_unlock(&dapm->card->dapm_mutex);
3082 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3084 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3085 const struct snd_soc_dapm_route *route)
3087 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3090 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3093 struct snd_soc_dapm_path *path;
3097 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3103 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3108 if (route->control || route->connected)
3109 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3110 route->source, route->sink);
3112 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3113 if (path->sink == sink) {
3120 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3121 route->source, route->sink);
3123 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3124 count, route->source, route->sink);
3130 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3131 * @dapm: DAPM context
3132 * @route: audio routes
3133 * @num: number of routes
3135 * Mark existing routes matching those specified in the passed array
3136 * as being weak, meaning that they are ignored for the purpose of
3137 * power decisions. The main intended use case is for sidetone paths
3138 * which couple audio between other independent paths if they are both
3139 * active in order to make the combination work better at the user
3140 * level but which aren't intended to be "used".
3142 * Note that CODEC drivers should not use this as sidetone type paths
3143 * can frequently also be used as bypass paths.
3145 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3146 const struct snd_soc_dapm_route *route, int num)
3151 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3152 for (i = 0; i < num; i++) {
3153 err = snd_soc_dapm_weak_route(dapm, route);
3158 mutex_unlock(&dapm->card->dapm_mutex);
3162 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3165 * snd_soc_dapm_new_widgets - add new dapm widgets
3166 * @card: card to be checked for new dapm widgets
3168 * Checks the codec for any new dapm widgets and creates them if found.
3170 * Returns 0 for success.
3172 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3174 struct snd_soc_dapm_widget *w;
3177 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3179 list_for_each_entry(w, &card->widgets, list)
3184 if (w->num_kcontrols) {
3185 w->kcontrols = kcalloc(w->num_kcontrols,
3186 sizeof(struct snd_kcontrol *),
3188 if (!w->kcontrols) {
3189 mutex_unlock(&card->dapm_mutex);
3195 case snd_soc_dapm_switch:
3196 case snd_soc_dapm_mixer:
3197 case snd_soc_dapm_mixer_named_ctl:
3200 case snd_soc_dapm_mux:
3201 case snd_soc_dapm_demux:
3204 case snd_soc_dapm_pga:
3205 case snd_soc_dapm_effect:
3206 case snd_soc_dapm_out_drv:
3209 case snd_soc_dapm_dai_link:
3210 dapm_new_dai_link(w);
3216 /* Read the initial power state from the device */
3218 soc_dapm_read(w->dapm, w->reg, &val);
3219 val = val >> w->shift;
3221 if (val == w->on_val)
3227 dapm_mark_dirty(w, "new widget");
3228 dapm_debugfs_add_widget(w);
3231 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3232 mutex_unlock(&card->dapm_mutex);
3235 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3238 * snd_soc_dapm_get_volsw - dapm mixer get callback
3239 * @kcontrol: mixer control
3240 * @ucontrol: control element information
3242 * Callback to get the value of a dapm mixer control.
3244 * Returns 0 for success.
3246 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_value *ucontrol)
3249 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3250 struct snd_soc_card *card = dapm->card;
3251 struct soc_mixer_control *mc =
3252 (struct soc_mixer_control *)kcontrol->private_value;
3254 unsigned int shift = mc->shift;
3256 unsigned int width = fls(max);
3257 unsigned int mask = (1 << fls(max)) - 1;
3258 unsigned int invert = mc->invert;
3259 unsigned int reg_val, val, rval = 0;
3262 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3263 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3264 ret = soc_dapm_read(dapm, reg, ®_val);
3265 val = (reg_val >> shift) & mask;
3267 if (ret == 0 && reg != mc->rreg)
3268 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3270 if (snd_soc_volsw_is_stereo(mc))
3271 rval = (reg_val >> mc->rshift) & mask;
3273 reg_val = dapm_kcontrol_get_value(kcontrol);
3274 val = reg_val & mask;
3276 if (snd_soc_volsw_is_stereo(mc))
3277 rval = (reg_val >> width) & mask;
3279 mutex_unlock(&card->dapm_mutex);
3285 ucontrol->value.integer.value[0] = max - val;
3287 ucontrol->value.integer.value[0] = val;
3289 if (snd_soc_volsw_is_stereo(mc)) {
3291 ucontrol->value.integer.value[1] = max - rval;
3293 ucontrol->value.integer.value[1] = rval;
3298 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3301 * snd_soc_dapm_put_volsw - dapm mixer set callback
3302 * @kcontrol: mixer control
3303 * @ucontrol: control element information
3305 * Callback to set the value of a dapm mixer control.
3307 * Returns 0 for success.
3309 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3310 struct snd_ctl_elem_value *ucontrol)
3312 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3313 struct snd_soc_card *card = dapm->card;
3314 struct soc_mixer_control *mc =
3315 (struct soc_mixer_control *)kcontrol->private_value;
3317 unsigned int shift = mc->shift;
3319 unsigned int width = fls(max);
3320 unsigned int mask = (1 << width) - 1;
3321 unsigned int invert = mc->invert;
3322 unsigned int val, rval = 0;
3323 int connect, rconnect = -1, change, reg_change = 0;
3324 struct snd_soc_dapm_update update = {};
3327 val = (ucontrol->value.integer.value[0] & mask);
3333 if (snd_soc_volsw_is_stereo(mc)) {
3334 rval = (ucontrol->value.integer.value[1] & mask);
3340 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3342 /* This assumes field width < (bits in unsigned int / 2) */
3343 if (width > sizeof(unsigned int) * 8 / 2)
3345 "ASoC: control %s field width limit exceeded\n",
3347 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3349 if (reg != SND_SOC_NOPM) {
3351 rval = rval << mc->rshift;
3353 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3355 if (snd_soc_volsw_is_stereo(mc))
3356 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3361 if (change || reg_change) {
3363 if (snd_soc_volsw_is_stereo(mc)) {
3364 update.has_second_set = true;
3365 update.reg2 = mc->rreg;
3366 update.mask2 = mask << mc->rshift;
3369 update.kcontrol = kcontrol;
3371 update.mask = mask << shift;
3373 card->update = &update;
3375 change |= reg_change;
3377 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3380 card->update = NULL;
3383 mutex_unlock(&card->dapm_mutex);
3386 soc_dpcm_runtime_update(card);
3390 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3393 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3394 * @kcontrol: mixer control
3395 * @ucontrol: control element information
3397 * Callback to get the value of a dapm enumerated double mixer control.
3399 * Returns 0 for success.
3401 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3402 struct snd_ctl_elem_value *ucontrol)
3404 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3405 struct snd_soc_card *card = dapm->card;
3406 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3407 unsigned int reg_val, val;
3409 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3410 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3411 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3413 mutex_unlock(&card->dapm_mutex);
3417 reg_val = dapm_kcontrol_get_value(kcontrol);
3419 mutex_unlock(&card->dapm_mutex);
3421 val = (reg_val >> e->shift_l) & e->mask;
3422 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3423 if (e->shift_l != e->shift_r) {
3424 val = (reg_val >> e->shift_r) & e->mask;
3425 val = snd_soc_enum_val_to_item(e, val);
3426 ucontrol->value.enumerated.item[1] = val;
3431 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3434 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3435 * @kcontrol: mixer control
3436 * @ucontrol: control element information
3438 * Callback to set the value of a dapm enumerated double mixer control.
3440 * Returns 0 for success.
3442 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3443 struct snd_ctl_elem_value *ucontrol)
3445 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3446 struct snd_soc_card *card = dapm->card;
3447 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3448 unsigned int *item = ucontrol->value.enumerated.item;
3449 unsigned int val, change, reg_change = 0;
3451 struct snd_soc_dapm_update update = {};
3454 if (item[0] >= e->items)
3457 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3458 mask = e->mask << e->shift_l;
3459 if (e->shift_l != e->shift_r) {
3460 if (item[1] > e->items)
3462 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3463 mask |= e->mask << e->shift_r;
3466 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3468 change = dapm_kcontrol_set_value(kcontrol, val);
3470 if (e->reg != SND_SOC_NOPM)
3471 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3473 if (change || reg_change) {
3475 update.kcontrol = kcontrol;
3476 update.reg = e->reg;
3479 card->update = &update;
3481 change |= reg_change;
3483 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3485 card->update = NULL;
3488 mutex_unlock(&card->dapm_mutex);
3491 soc_dpcm_runtime_update(card);
3495 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3498 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3500 * @kcontrol: mixer control
3501 * @uinfo: control element information
3503 * Callback to provide information about a pin switch control.
3505 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3506 struct snd_ctl_elem_info *uinfo)
3508 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3510 uinfo->value.integer.min = 0;
3511 uinfo->value.integer.max = 1;
3515 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3518 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3520 * @kcontrol: mixer control
3523 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3524 struct snd_ctl_elem_value *ucontrol)
3526 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3527 const char *pin = (const char *)kcontrol->private_value;
3529 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3531 ucontrol->value.integer.value[0] =
3532 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3534 mutex_unlock(&card->dapm_mutex);
3538 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3541 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3543 * @kcontrol: mixer control
3546 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3547 struct snd_ctl_elem_value *ucontrol)
3549 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3550 const char *pin = (const char *)kcontrol->private_value;
3552 if (ucontrol->value.integer.value[0])
3553 snd_soc_dapm_enable_pin(&card->dapm, pin);
3555 snd_soc_dapm_disable_pin(&card->dapm, pin);
3557 snd_soc_dapm_sync(&card->dapm);
3560 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3562 struct snd_soc_dapm_widget *
3563 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3564 const struct snd_soc_dapm_widget *widget)
3566 enum snd_soc_dapm_direction dir;
3567 struct snd_soc_dapm_widget *w;
3571 if ((w = dapm_cnew_widget(widget)) == NULL)
3572 return ERR_PTR(-ENOMEM);
3575 case snd_soc_dapm_regulator_supply:
3576 w->regulator = devm_regulator_get(dapm->dev, w->name);
3577 if (IS_ERR(w->regulator)) {
3578 ret = PTR_ERR(w->regulator);
3579 goto request_failed;
3582 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3583 ret = regulator_allow_bypass(w->regulator, true);
3586 "ASoC: Failed to bypass %s: %d\n",
3590 case snd_soc_dapm_pinctrl:
3591 w->pinctrl = devm_pinctrl_get(dapm->dev);
3592 if (IS_ERR(w->pinctrl)) {
3593 ret = PTR_ERR(w->pinctrl);
3594 goto request_failed;
3597 case snd_soc_dapm_clock_supply:
3598 w->clk = devm_clk_get(dapm->dev, w->name);
3599 if (IS_ERR(w->clk)) {
3600 ret = PTR_ERR(w->clk);
3601 goto request_failed;
3608 prefix = soc_dapm_prefix(dapm);
3610 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3612 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3613 if (w->name == NULL) {
3614 kfree_const(w->sname);
3616 return ERR_PTR(-ENOMEM);
3620 case snd_soc_dapm_mic:
3621 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3622 w->power_check = dapm_generic_check_power;
3624 case snd_soc_dapm_input:
3625 if (!dapm->card->fully_routed)
3626 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3627 w->power_check = dapm_generic_check_power;
3629 case snd_soc_dapm_spk:
3630 case snd_soc_dapm_hp:
3631 w->is_ep = SND_SOC_DAPM_EP_SINK;
3632 w->power_check = dapm_generic_check_power;
3634 case snd_soc_dapm_output:
3635 if (!dapm->card->fully_routed)
3636 w->is_ep = SND_SOC_DAPM_EP_SINK;
3637 w->power_check = dapm_generic_check_power;
3639 case snd_soc_dapm_vmid:
3640 case snd_soc_dapm_siggen:
3641 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3642 w->power_check = dapm_always_on_check_power;
3644 case snd_soc_dapm_sink:
3645 w->is_ep = SND_SOC_DAPM_EP_SINK;
3646 w->power_check = dapm_always_on_check_power;
3649 case snd_soc_dapm_mux:
3650 case snd_soc_dapm_demux:
3651 case snd_soc_dapm_switch:
3652 case snd_soc_dapm_mixer:
3653 case snd_soc_dapm_mixer_named_ctl:
3654 case snd_soc_dapm_adc:
3655 case snd_soc_dapm_aif_out:
3656 case snd_soc_dapm_dac:
3657 case snd_soc_dapm_aif_in:
3658 case snd_soc_dapm_pga:
3659 case snd_soc_dapm_buffer:
3660 case snd_soc_dapm_scheduler:
3661 case snd_soc_dapm_effect:
3662 case snd_soc_dapm_src:
3663 case snd_soc_dapm_asrc:
3664 case snd_soc_dapm_encoder:
3665 case snd_soc_dapm_decoder:
3666 case snd_soc_dapm_out_drv:
3667 case snd_soc_dapm_micbias:
3668 case snd_soc_dapm_line:
3669 case snd_soc_dapm_dai_link:
3670 case snd_soc_dapm_dai_out:
3671 case snd_soc_dapm_dai_in:
3672 w->power_check = dapm_generic_check_power;
3674 case snd_soc_dapm_supply:
3675 case snd_soc_dapm_regulator_supply:
3676 case snd_soc_dapm_pinctrl:
3677 case snd_soc_dapm_clock_supply:
3678 case snd_soc_dapm_kcontrol:
3680 w->power_check = dapm_supply_check_power;
3683 w->power_check = dapm_always_on_check_power;
3688 INIT_LIST_HEAD(&w->list);
3689 INIT_LIST_HEAD(&w->dirty);
3690 list_add_tail(&w->list, &dapm->card->widgets);
3692 snd_soc_dapm_for_each_direction(dir) {
3693 INIT_LIST_HEAD(&w->edges[dir]);
3694 w->endpoints[dir] = -1;
3697 /* machine layer sets up unconnected pins and insertions */
3702 if (ret != -EPROBE_DEFER)
3703 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3706 return ERR_PTR(ret);
3710 * snd_soc_dapm_new_control - create new dapm control
3711 * @dapm: DAPM context
3712 * @widget: widget template
3714 * Creates new DAPM control based upon a template.
3716 * Returns a widget pointer on success or an error pointer on failure
3718 struct snd_soc_dapm_widget *
3719 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3720 const struct snd_soc_dapm_widget *widget)
3722 struct snd_soc_dapm_widget *w;
3724 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3725 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3726 mutex_unlock(&dapm->card->dapm_mutex);
3730 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3733 * snd_soc_dapm_new_controls - create new dapm controls
3734 * @dapm: DAPM context
3735 * @widget: widget array
3736 * @num: number of widgets
3738 * Creates new DAPM controls based upon the templates.
3740 * Returns 0 for success else error.
3742 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3743 const struct snd_soc_dapm_widget *widget,
3746 struct snd_soc_dapm_widget *w;
3750 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3751 for (i = 0; i < num; i++) {
3752 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3759 mutex_unlock(&dapm->card->dapm_mutex);
3762 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3764 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3765 struct snd_kcontrol *kcontrol, int event)
3767 struct snd_soc_dapm_path *path;
3768 struct snd_soc_dai *source, *sink;
3769 struct snd_soc_pcm_runtime *rtd = w->priv;
3770 const struct snd_soc_pcm_stream *config;
3771 struct snd_pcm_substream substream;
3772 struct snd_pcm_hw_params *params = NULL;
3773 struct snd_pcm_runtime *runtime = NULL;
3777 config = rtd->dai_link->params + rtd->params_select;
3779 if (WARN_ON(!config) ||
3780 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3781 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3784 /* Be a little careful as we don't want to overflow the mask array */
3785 if (config->formats) {
3786 fmt = ffs(config->formats) - 1;
3788 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3793 /* Currently very limited parameter selection */
3794 params = kzalloc(sizeof(*params), GFP_KERNEL);
3799 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3801 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3803 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3806 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3807 = config->channels_min;
3808 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3809 = config->channels_max;
3811 memset(&substream, 0, sizeof(substream));
3813 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3814 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3819 substream.runtime = runtime;
3820 substream.private_data = rtd;
3823 case SND_SOC_DAPM_PRE_PMU:
3824 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3825 snd_soc_dapm_widget_for_each_source_path(w, path) {
3826 source = path->source->priv;
3828 if (source->driver->ops->startup) {
3829 ret = source->driver->ops->startup(&substream,
3832 dev_err(source->dev,
3833 "ASoC: startup() failed: %d\n",
3839 ret = soc_dai_hw_params(&substream, params, source);
3843 dapm_update_dai_unlocked(&substream, params, source);
3846 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3847 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3848 sink = path->sink->priv;
3850 if (sink->driver->ops->startup) {
3851 ret = sink->driver->ops->startup(&substream,
3855 "ASoC: startup() failed: %d\n",
3861 ret = soc_dai_hw_params(&substream, params, sink);
3865 dapm_update_dai_unlocked(&substream, params, sink);
3869 case SND_SOC_DAPM_POST_PMU:
3870 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3871 sink = path->sink->priv;
3873 ret = snd_soc_dai_digital_mute(sink, 0,
3874 SNDRV_PCM_STREAM_PLAYBACK);
3875 if (ret != 0 && ret != -ENOTSUPP)
3877 "ASoC: Failed to unmute: %d\n", ret);
3882 case SND_SOC_DAPM_PRE_PMD:
3883 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3884 sink = path->sink->priv;
3886 ret = snd_soc_dai_digital_mute(sink, 1,
3887 SNDRV_PCM_STREAM_PLAYBACK);
3888 if (ret != 0 && ret != -ENOTSUPP)
3890 "ASoC: Failed to mute: %d\n", ret);
3894 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3895 snd_soc_dapm_widget_for_each_source_path(w, path) {
3896 source = path->source->priv;
3898 if (source->driver->ops->hw_free)
3899 source->driver->ops->hw_free(&substream,
3903 if (source->driver->ops->shutdown)
3904 source->driver->ops->shutdown(&substream,
3908 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3909 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3910 sink = path->sink->priv;
3912 if (sink->driver->ops->hw_free)
3913 sink->driver->ops->hw_free(&substream, sink);
3916 if (sink->driver->ops->shutdown)
3917 sink->driver->ops->shutdown(&substream, sink);
3922 WARN(1, "Unknown event %d\n", event);
3932 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3933 struct snd_ctl_elem_value *ucontrol)
3935 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3936 struct snd_soc_pcm_runtime *rtd = w->priv;
3938 ucontrol->value.enumerated.item[0] = rtd->params_select;
3943 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3944 struct snd_ctl_elem_value *ucontrol)
3946 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3947 struct snd_soc_pcm_runtime *rtd = w->priv;
3949 /* Can't change the config when widget is already powered */
3953 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
3956 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
3959 rtd->params_select = ucontrol->value.enumerated.item[0];
3965 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3966 unsigned long *private_value,
3968 const char **w_param_text)
3972 devm_kfree(card->dev, (void *)*private_value);
3977 for (count = 0 ; count < num_params; count++)
3978 devm_kfree(card->dev, (void *)w_param_text[count]);
3979 devm_kfree(card->dev, w_param_text);
3982 static struct snd_kcontrol_new *
3983 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3985 const struct snd_soc_pcm_stream *params,
3986 int num_params, const char **w_param_text,
3987 unsigned long *private_value)
3989 struct soc_enum w_param_enum[] = {
3990 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3992 struct snd_kcontrol_new kcontrol_dai_link[] = {
3993 SOC_ENUM_EXT(NULL, w_param_enum[0],
3994 snd_soc_dapm_dai_link_get,
3995 snd_soc_dapm_dai_link_put),
3997 struct snd_kcontrol_new *kcontrol_news;
3998 const struct snd_soc_pcm_stream *config = params;
4001 for (count = 0 ; count < num_params; count++) {
4002 if (!config->stream_name) {
4003 dev_warn(card->dapm.dev,
4004 "ASoC: anonymous config %d for dai link %s\n",
4006 w_param_text[count] =
4007 devm_kasprintf(card->dev, GFP_KERNEL,
4008 "Anonymous Configuration %d",
4011 w_param_text[count] = devm_kmemdup(card->dev,
4012 config->stream_name,
4013 strlen(config->stream_name) + 1,
4016 if (!w_param_text[count])
4017 goto outfree_w_param;
4021 w_param_enum[0].items = num_params;
4022 w_param_enum[0].texts = w_param_text;
4025 (unsigned long) devm_kmemdup(card->dev,
4026 (void *)(kcontrol_dai_link[0].private_value),
4027 sizeof(struct soc_enum), GFP_KERNEL);
4028 if (!*private_value) {
4029 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4031 goto outfree_w_param;
4033 kcontrol_dai_link[0].private_value = *private_value;
4034 /* duplicate kcontrol_dai_link on heap so that memory persists */
4035 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4036 sizeof(struct snd_kcontrol_new),
4038 if (!kcontrol_news) {
4039 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4041 goto outfree_w_param;
4043 return kcontrol_news;
4046 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4050 static struct snd_soc_dapm_widget *
4051 snd_soc_dapm_new_dai(struct snd_soc_card *card, struct snd_soc_pcm_runtime *rtd,
4052 struct snd_soc_dapm_widget *source,
4053 struct snd_soc_dapm_widget *sink)
4055 struct snd_soc_dapm_widget template;
4056 struct snd_soc_dapm_widget *w;
4057 const char **w_param_text;
4058 unsigned long private_value = 0;
4062 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4063 source->name, sink->name);
4065 return ERR_PTR(-ENOMEM);
4067 memset(&template, 0, sizeof(template));
4068 template.reg = SND_SOC_NOPM;
4069 template.id = snd_soc_dapm_dai_link;
4070 template.name = link_name;
4071 template.event = snd_soc_dai_link_event;
4072 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4073 SND_SOC_DAPM_PRE_PMD;
4074 template.kcontrol_news = NULL;
4076 /* allocate memory for control, only in case of multiple configs */
4077 if (rtd->dai_link->num_params > 1) {
4078 w_param_text = devm_kcalloc(card->dev,
4079 rtd->dai_link->num_params,
4080 sizeof(char *), GFP_KERNEL);
4081 if (!w_param_text) {
4086 template.num_kcontrols = 1;
4087 template.kcontrol_news =
4088 snd_soc_dapm_alloc_kcontrol(card,
4090 rtd->dai_link->params,
4091 rtd->dai_link->num_params,
4092 w_param_text, &private_value);
4093 if (!template.kcontrol_news) {
4098 w_param_text = NULL;
4100 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4102 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4105 goto outfree_kcontrol_news;
4112 outfree_kcontrol_news:
4113 devm_kfree(card->dev, (void *)template.kcontrol_news);
4114 snd_soc_dapm_free_kcontrol(card, &private_value,
4115 rtd->dai_link->num_params, w_param_text);
4117 devm_kfree(card->dev, link_name);
4118 return ERR_PTR(ret);
4121 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4122 struct snd_soc_dai *dai)
4124 struct snd_soc_dapm_widget template;
4125 struct snd_soc_dapm_widget *w;
4127 WARN_ON(dapm->dev != dai->dev);
4129 memset(&template, 0, sizeof(template));
4130 template.reg = SND_SOC_NOPM;
4132 if (dai->driver->playback.stream_name) {
4133 template.id = snd_soc_dapm_dai_in;
4134 template.name = dai->driver->playback.stream_name;
4135 template.sname = dai->driver->playback.stream_name;
4137 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4140 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4145 dai->playback_widget = w;
4148 if (dai->driver->capture.stream_name) {
4149 template.id = snd_soc_dapm_dai_out;
4150 template.name = dai->driver->capture.stream_name;
4151 template.sname = dai->driver->capture.stream_name;
4153 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4156 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4161 dai->capture_widget = w;
4167 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4169 struct snd_soc_dapm_widget *dai_w, *w;
4170 struct snd_soc_dapm_widget *src, *sink;
4171 struct snd_soc_dai *dai;
4173 /* For each DAI widget... */
4174 list_for_each_entry(dai_w, &card->widgets, list) {
4175 switch (dai_w->id) {
4176 case snd_soc_dapm_dai_in:
4177 case snd_soc_dapm_dai_out:
4183 /* let users know there is no DAI to link */
4185 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4192 /* ...find all widgets with the same stream and link them */
4193 list_for_each_entry(w, &card->widgets, list) {
4194 if (w->dapm != dai_w->dapm)
4198 case snd_soc_dapm_dai_in:
4199 case snd_soc_dapm_dai_out:
4205 if (!w->sname || !strstr(w->sname, dai_w->sname))
4208 if (dai_w->id == snd_soc_dapm_dai_in) {
4215 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4216 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4223 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4224 struct snd_soc_pcm_runtime *rtd)
4226 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4227 struct snd_soc_dai *codec_dai;
4228 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4229 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
4232 if (rtd->dai_link->params) {
4233 playback_cpu = cpu_dai->capture_widget;
4234 capture_cpu = cpu_dai->playback_widget;
4236 playback = cpu_dai->playback_widget;
4237 capture = cpu_dai->capture_widget;
4238 playback_cpu = playback;
4239 capture_cpu = capture;
4242 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4244 /* connect BE DAI playback if widgets are valid */
4245 codec = codec_dai->playback_widget;
4247 if (playback_cpu && codec) {
4249 playback = snd_soc_dapm_new_dai(card, rtd,
4252 if (IS_ERR(playback)) {
4254 "ASoC: Failed to create DAI %s: %ld\n",
4260 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4261 playback, NULL, NULL);
4264 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4265 cpu_dai->component->name, playback_cpu->name,
4266 codec_dai->component->name, codec->name);
4268 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4273 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4274 /* connect BE DAI capture if widgets are valid */
4275 codec = codec_dai->capture_widget;
4277 if (codec && capture_cpu) {
4279 capture = snd_soc_dapm_new_dai(card, rtd,
4282 if (IS_ERR(capture)) {
4284 "ASoC: Failed to create DAI %s: %ld\n",
4290 snd_soc_dapm_add_path(&card->dapm, capture,
4291 capture_cpu, NULL, NULL);
4294 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4295 codec_dai->component->name, codec->name,
4296 cpu_dai->component->name, capture_cpu->name);
4298 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4304 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4307 struct snd_soc_dapm_widget *w;
4310 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4311 w = dai->playback_widget;
4313 w = dai->capture_widget;
4316 dapm_mark_dirty(w, "stream event");
4318 if (w->id == snd_soc_dapm_dai_in) {
4319 ep = SND_SOC_DAPM_EP_SOURCE;
4320 dapm_widget_invalidate_input_paths(w);
4322 ep = SND_SOC_DAPM_EP_SINK;
4323 dapm_widget_invalidate_output_paths(w);
4327 case SND_SOC_DAPM_STREAM_START:
4331 case SND_SOC_DAPM_STREAM_STOP:
4335 case SND_SOC_DAPM_STREAM_SUSPEND:
4336 case SND_SOC_DAPM_STREAM_RESUME:
4337 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4338 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4344 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4346 struct snd_soc_pcm_runtime *rtd;
4348 /* for each BE DAI link... */
4349 for_each_card_rtds(card, rtd) {
4351 * dynamic FE links have no fixed DAI mapping.
4352 * CODEC<->CODEC links have no direct connection.
4354 if (rtd->dai_link->dynamic)
4357 dapm_connect_dai_link_widgets(card, rtd);
4361 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4364 struct snd_soc_dai *codec_dai;
4367 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4368 for_each_rtd_codec_dai(rtd, i, codec_dai)
4369 soc_dapm_dai_stream_event(codec_dai, stream, event);
4371 dapm_power_widgets(rtd->card, event);
4375 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4376 * @rtd: PCM runtime data
4377 * @stream: stream name
4378 * @event: stream event
4380 * Sends a stream event to the dapm core. The core then makes any
4381 * necessary widget power changes.
4383 * Returns 0 for success else error.
4385 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4388 struct snd_soc_card *card = rtd->card;
4390 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4391 soc_dapm_stream_event(rtd, stream, event);
4392 mutex_unlock(&card->dapm_mutex);
4396 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4397 * @dapm: DAPM context
4400 * Enables input/output pin and its parents or children widgets iff there is
4401 * a valid audio route and active audio stream.
4403 * Requires external locking.
4405 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4406 * do any widget power switching.
4408 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4411 return snd_soc_dapm_set_pin(dapm, pin, 1);
4413 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4416 * snd_soc_dapm_enable_pin - enable pin.
4417 * @dapm: DAPM context
4420 * Enables input/output pin and its parents or children widgets iff there is
4421 * a valid audio route and active audio stream.
4423 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4424 * do any widget power switching.
4426 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4430 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4432 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4434 mutex_unlock(&dapm->card->dapm_mutex);
4438 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4441 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4442 * @dapm: DAPM context
4445 * Enables input/output pin regardless of any other state. This is
4446 * intended for use with microphone bias supplies used in microphone
4449 * Requires external locking.
4451 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4452 * do any widget power switching.
4454 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4457 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4460 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4464 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4465 if (!w->connected) {
4467 * w->force does not affect the number of input or output paths,
4468 * so we only have to recheck if w->connected is changed
4470 dapm_widget_invalidate_input_paths(w);
4471 dapm_widget_invalidate_output_paths(w);
4475 dapm_mark_dirty(w, "force enable");
4479 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4482 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4483 * @dapm: DAPM context
4486 * Enables input/output pin regardless of any other state. This is
4487 * intended for use with microphone bias supplies used in microphone
4490 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4491 * do any widget power switching.
4493 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4498 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4500 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4502 mutex_unlock(&dapm->card->dapm_mutex);
4506 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4509 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4510 * @dapm: DAPM context
4513 * Disables input/output pin and its parents or children widgets.
4515 * Requires external locking.
4517 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4518 * do any widget power switching.
4520 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4523 return snd_soc_dapm_set_pin(dapm, pin, 0);
4525 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4528 * snd_soc_dapm_disable_pin - disable pin.
4529 * @dapm: DAPM context
4532 * Disables input/output pin and its parents or children widgets.
4534 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4535 * do any widget power switching.
4537 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4542 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4544 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4546 mutex_unlock(&dapm->card->dapm_mutex);
4550 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4553 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4554 * @dapm: DAPM context
4557 * Marks the specified pin as being not connected, disabling it along
4558 * any parent or child widgets. At present this is identical to
4559 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4560 * additional things such as disabling controls which only affect
4561 * paths through the pin.
4563 * Requires external locking.
4565 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4566 * do any widget power switching.
4568 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4571 return snd_soc_dapm_set_pin(dapm, pin, 0);
4573 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4576 * snd_soc_dapm_nc_pin - permanently disable pin.
4577 * @dapm: DAPM context
4580 * Marks the specified pin as being not connected, disabling it along
4581 * any parent or child widgets. At present this is identical to
4582 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4583 * additional things such as disabling controls which only affect
4584 * paths through the pin.
4586 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4587 * do any widget power switching.
4589 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4593 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4595 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4597 mutex_unlock(&dapm->card->dapm_mutex);
4601 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4604 * snd_soc_dapm_get_pin_status - get audio pin status
4605 * @dapm: DAPM context
4606 * @pin: audio signal pin endpoint (or start point)
4608 * Get audio pin status - connected or disconnected.
4610 * Returns 1 for connected otherwise 0.
4612 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4615 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4618 return w->connected;
4622 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4625 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4626 * @dapm: DAPM context
4627 * @pin: audio signal pin endpoint (or start point)
4629 * Mark the given endpoint or pin as ignoring suspend. When the
4630 * system is disabled a path between two endpoints flagged as ignoring
4631 * suspend will not be disabled. The path must already be enabled via
4632 * normal means at suspend time, it will not be turned on if it was not
4635 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4638 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4641 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4645 w->ignore_suspend = 1;
4649 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4652 * snd_soc_dapm_free - free dapm resources
4653 * @dapm: DAPM context
4655 * Free all dapm widgets and resources.
4657 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4659 dapm_debugfs_cleanup(dapm);
4660 dapm_free_widgets(dapm);
4661 list_del(&dapm->list);
4663 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4665 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4667 struct snd_soc_card *card = dapm->card;
4668 struct snd_soc_dapm_widget *w;
4669 LIST_HEAD(down_list);
4672 mutex_lock(&card->dapm_mutex);
4674 list_for_each_entry(w, &dapm->card->widgets, list) {
4675 if (w->dapm != dapm)
4678 dapm_seq_insert(w, &down_list, false);
4684 /* If there were no widgets to power down we're already in
4688 if (dapm->bias_level == SND_SOC_BIAS_ON)
4689 snd_soc_dapm_set_bias_level(dapm,
4690 SND_SOC_BIAS_PREPARE);
4691 dapm_seq_run(card, &down_list, 0, false);
4692 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4693 snd_soc_dapm_set_bias_level(dapm,
4694 SND_SOC_BIAS_STANDBY);
4697 mutex_unlock(&card->dapm_mutex);
4701 * snd_soc_dapm_shutdown - callback for system shutdown
4703 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4705 struct snd_soc_dapm_context *dapm;
4707 list_for_each_entry(dapm, &card->dapm_list, list) {
4708 if (dapm != &card->dapm) {
4709 soc_dapm_shutdown_dapm(dapm);
4710 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4711 snd_soc_dapm_set_bias_level(dapm,
4716 soc_dapm_shutdown_dapm(&card->dapm);
4717 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4718 snd_soc_dapm_set_bias_level(&card->dapm,
4722 /* Module information */
4723 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4724 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4725 MODULE_LICENSE("GPL");