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_out_drv:
887 wname_in_long_name = true;
888 kcname_in_long_name = true;
890 case snd_soc_dapm_mixer_named_ctl:
891 wname_in_long_name = false;
892 kcname_in_long_name = true;
894 case snd_soc_dapm_demux:
895 case snd_soc_dapm_mux:
896 wname_in_long_name = true;
897 kcname_in_long_name = false;
904 if (wname_in_long_name && kcname_in_long_name) {
906 * The control will get a prefix from the control
907 * creation process but we're also using the same
908 * prefix for widgets so cut the prefix off the
909 * front of the widget name.
911 long_name = kasprintf(GFP_KERNEL, "%s %s",
912 w->name + prefix_len,
913 w->kcontrol_news[kci].name);
914 if (long_name == NULL)
918 } else if (wname_in_long_name) {
920 name = w->name + prefix_len;
923 name = w->kcontrol_news[kci].name;
926 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
933 kcontrol->private_free = dapm_kcontrol_free;
935 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
937 snd_ctl_free_one(kcontrol);
941 ret = snd_ctl_add(card, kcontrol);
944 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
950 ret = dapm_kcontrol_add_widget(kcontrol, w);
952 w->kcontrols[kci] = kcontrol;
960 /* create new dapm mixer control */
961 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
964 struct snd_soc_dapm_path *path;
965 struct dapm_kcontrol_data *data;
968 for (i = 0; i < w->num_kcontrols; i++) {
970 snd_soc_dapm_widget_for_each_source_path(w, path) {
971 /* mixer/mux paths name must match control name */
972 if (path->name != (char *)w->kcontrol_news[i].name)
975 if (!w->kcontrols[i]) {
976 ret = dapm_create_or_share_kcontrol(w, i);
981 dapm_kcontrol_add_path(w->kcontrols[i], path);
983 data = snd_kcontrol_chip(w->kcontrols[i]);
985 snd_soc_dapm_add_path(data->widget->dapm,
995 /* create new dapm mux control */
996 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
998 struct snd_soc_dapm_context *dapm = w->dapm;
999 enum snd_soc_dapm_direction dir;
1000 struct snd_soc_dapm_path *path;
1005 case snd_soc_dapm_mux:
1006 dir = SND_SOC_DAPM_DIR_OUT;
1009 case snd_soc_dapm_demux:
1010 dir = SND_SOC_DAPM_DIR_IN;
1017 if (w->num_kcontrols != 1) {
1019 "ASoC: %s %s has incorrect number of controls\n", type,
1024 if (list_empty(&w->edges[dir])) {
1025 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1029 ret = dapm_create_or_share_kcontrol(w, 0);
1033 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1035 dapm_kcontrol_add_path(w->kcontrols[0], path);
1041 /* create new dapm volume control */
1042 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1046 for (i = 0; i < w->num_kcontrols; i++) {
1047 ret = dapm_create_or_share_kcontrol(w, i);
1055 /* create new dapm dai link control */
1056 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1059 struct snd_kcontrol *kcontrol;
1060 struct snd_soc_dapm_context *dapm = w->dapm;
1061 struct snd_card *card = dapm->card->snd_card;
1062 struct snd_soc_pcm_runtime *rtd = w->priv;
1064 /* create control for links with > 1 config */
1065 if (rtd->dai_link->num_params <= 1)
1069 for (i = 0; i < w->num_kcontrols; i++) {
1070 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1072 ret = snd_ctl_add(card, kcontrol);
1075 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1076 w->name, w->kcontrol_news[i].name, ret);
1079 kcontrol->private_data = w;
1080 w->kcontrols[i] = kcontrol;
1086 /* We implement power down on suspend by checking the power state of
1087 * the ALSA card - when we are suspending the ALSA state for the card
1090 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1092 int level = snd_power_get_state(widget->dapm->card->snd_card);
1095 case SNDRV_CTL_POWER_D3hot:
1096 case SNDRV_CTL_POWER_D3cold:
1097 if (widget->ignore_suspend)
1098 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1100 return widget->ignore_suspend;
1106 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1107 struct list_head *widgets)
1109 struct snd_soc_dapm_widget *w;
1110 struct list_head *it;
1111 unsigned int size = 0;
1114 list_for_each(it, widgets)
1117 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1121 list_for_each_entry(w, widgets, work_list)
1122 (*list)->widgets[i++] = w;
1124 (*list)->num_widgets = i;
1130 * Common implementation for is_connected_output_ep() and
1131 * is_connected_input_ep(). The function is inlined since the combined size of
1132 * the two specialized functions is only marginally larger then the size of the
1133 * generic function and at the same time the fast path of the specialized
1134 * functions is significantly smaller than the generic function.
1136 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1137 struct list_head *list, enum snd_soc_dapm_direction dir,
1138 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1139 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1140 enum snd_soc_dapm_direction)),
1141 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1142 enum snd_soc_dapm_direction))
1144 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1145 struct snd_soc_dapm_path *path;
1148 if (widget->endpoints[dir] >= 0)
1149 return widget->endpoints[dir];
1151 DAPM_UPDATE_STAT(widget, path_checks);
1153 /* do we need to add this widget to the list ? */
1155 list_add_tail(&widget->work_list, list);
1157 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1158 widget->endpoints[dir] = 1;
1159 return widget->endpoints[dir];
1162 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1163 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1164 return widget->endpoints[dir];
1167 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1168 DAPM_UPDATE_STAT(widget, neighbour_checks);
1170 if (path->weak || path->is_supply)
1176 trace_snd_soc_dapm_path(widget, dir, path);
1178 if (path->connect) {
1180 con += fn(path->node[dir], list, custom_stop_condition);
1185 widget->endpoints[dir] = con;
1191 * Recursively check for a completed path to an active or physically connected
1192 * output widget. Returns number of complete paths.
1194 * Optionally, can be supplied with a function acting as a stopping condition.
1195 * This function takes the dapm widget currently being examined and the walk
1196 * direction as an arguments, it should return true if the walk should be
1197 * stopped and false otherwise.
1199 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1200 struct list_head *list,
1201 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1202 enum snd_soc_dapm_direction))
1204 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1205 is_connected_output_ep, custom_stop_condition);
1209 * Recursively check for a completed path to an active or physically connected
1210 * input widget. Returns number of complete paths.
1212 * Optionally, can be supplied with a function acting as a stopping condition.
1213 * This function takes the dapm widget currently being examined and the walk
1214 * direction as an arguments, it should return true if the walk should be
1215 * stopped and false otherwise.
1217 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1218 struct list_head *list,
1219 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1220 enum snd_soc_dapm_direction))
1222 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1223 is_connected_input_ep, custom_stop_condition);
1227 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1228 * @dai: the soc DAI.
1229 * @stream: stream direction.
1230 * @list: list of active widgets for this stream.
1231 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1232 * walk based on custom logic.
1234 * Queries DAPM graph as to whether a valid audio stream path exists for
1235 * the initial stream specified by name. This takes into account
1236 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1238 * Optionally, can be supplied with a function acting as a stopping condition.
1239 * This function takes the dapm widget currently being examined and the walk
1240 * direction as an arguments, it should return true if the walk should be
1241 * stopped and false otherwise.
1243 * Returns the number of valid paths or negative error.
1245 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1246 struct snd_soc_dapm_widget_list **list,
1247 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1248 enum snd_soc_dapm_direction))
1250 struct snd_soc_card *card = dai->component->card;
1251 struct snd_soc_dapm_widget *w;
1256 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1259 * For is_connected_{output,input}_ep fully discover the graph we need
1260 * to reset the cached number of inputs and outputs.
1262 list_for_each_entry(w, &card->widgets, list) {
1263 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1264 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1267 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1268 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1269 custom_stop_condition);
1271 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1272 custom_stop_condition);
1274 /* Drop starting point */
1275 list_del(widgets.next);
1277 ret = dapm_widget_list_create(list, &widgets);
1281 trace_snd_soc_dapm_connected(paths, stream);
1282 mutex_unlock(&card->dapm_mutex);
1288 * Handler for regulator supply widget.
1290 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1291 struct snd_kcontrol *kcontrol, int event)
1295 soc_dapm_async_complete(w->dapm);
1297 if (SND_SOC_DAPM_EVENT_ON(event)) {
1298 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1299 ret = regulator_allow_bypass(w->regulator, false);
1301 dev_warn(w->dapm->dev,
1302 "ASoC: Failed to unbypass %s: %d\n",
1306 return regulator_enable(w->regulator);
1308 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1309 ret = regulator_allow_bypass(w->regulator, true);
1311 dev_warn(w->dapm->dev,
1312 "ASoC: Failed to bypass %s: %d\n",
1316 return regulator_disable_deferred(w->regulator, w->shift);
1319 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1322 * Handler for pinctrl widget.
1324 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1325 struct snd_kcontrol *kcontrol, int event)
1327 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1328 struct pinctrl *p = w->pinctrl;
1329 struct pinctrl_state *s;
1334 if (SND_SOC_DAPM_EVENT_ON(event))
1335 s = pinctrl_lookup_state(p, priv->active_state);
1337 s = pinctrl_lookup_state(p, priv->sleep_state);
1342 return pinctrl_select_state(p, s);
1344 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1347 * Handler for clock supply widget.
1349 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1350 struct snd_kcontrol *kcontrol, int event)
1355 soc_dapm_async_complete(w->dapm);
1357 if (SND_SOC_DAPM_EVENT_ON(event)) {
1358 return clk_prepare_enable(w->clk);
1360 clk_disable_unprepare(w->clk);
1366 EXPORT_SYMBOL_GPL(dapm_clock_event);
1368 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1370 if (w->power_checked)
1371 return w->new_power;
1376 w->new_power = w->power_check(w);
1378 w->power_checked = true;
1380 return w->new_power;
1383 /* Generic check to see if a widget should be powered. */
1384 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1388 DAPM_UPDATE_STAT(w, power_checks);
1390 in = is_connected_input_ep(w, NULL, NULL);
1391 out = is_connected_output_ep(w, NULL, NULL);
1392 return out != 0 && in != 0;
1395 /* Check to see if a power supply is needed */
1396 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1398 struct snd_soc_dapm_path *path;
1400 DAPM_UPDATE_STAT(w, power_checks);
1402 /* Check if one of our outputs is connected */
1403 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1404 DAPM_UPDATE_STAT(w, neighbour_checks);
1409 if (path->connected &&
1410 !path->connected(path->source, path->sink))
1413 if (dapm_widget_power_check(path->sink))
1420 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1422 return w->connected;
1425 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1426 struct snd_soc_dapm_widget *b,
1431 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1432 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1437 sort = dapm_down_seq;
1439 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1440 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1442 if (sort[a->id] != sort[b->id])
1443 return sort[a->id] - sort[b->id];
1444 if (a->subseq != b->subseq) {
1446 return a->subseq - b->subseq;
1448 return b->subseq - a->subseq;
1450 if (a->reg != b->reg)
1451 return a->reg - b->reg;
1452 if (a->dapm != b->dapm)
1453 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1458 /* Insert a widget in order into a DAPM power sequence. */
1459 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1460 struct list_head *list,
1463 struct snd_soc_dapm_widget *w;
1465 list_for_each_entry(w, list, power_list)
1466 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1467 list_add_tail(&new_widget->power_list, &w->power_list);
1471 list_add_tail(&new_widget->power_list, list);
1474 static void dapm_seq_check_event(struct snd_soc_card *card,
1475 struct snd_soc_dapm_widget *w, int event)
1477 const char *ev_name;
1481 case SND_SOC_DAPM_PRE_PMU:
1482 ev_name = "PRE_PMU";
1485 case SND_SOC_DAPM_POST_PMU:
1486 ev_name = "POST_PMU";
1489 case SND_SOC_DAPM_PRE_PMD:
1490 ev_name = "PRE_PMD";
1493 case SND_SOC_DAPM_POST_PMD:
1494 ev_name = "POST_PMD";
1497 case SND_SOC_DAPM_WILL_PMU:
1498 ev_name = "WILL_PMU";
1501 case SND_SOC_DAPM_WILL_PMD:
1502 ev_name = "WILL_PMD";
1506 WARN(1, "Unknown event %d\n", event);
1510 if (w->new_power != power)
1513 if (w->event && (w->event_flags & event)) {
1514 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1516 soc_dapm_async_complete(w->dapm);
1517 trace_snd_soc_dapm_widget_event_start(w, event);
1518 ret = w->event(w, NULL, event);
1519 trace_snd_soc_dapm_widget_event_done(w, event);
1521 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1522 ev_name, w->name, ret);
1526 /* Apply the coalesced changes from a DAPM sequence */
1527 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1528 struct list_head *pending)
1530 struct snd_soc_dapm_context *dapm;
1531 struct snd_soc_dapm_widget *w;
1533 unsigned int value = 0;
1534 unsigned int mask = 0;
1536 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1540 list_for_each_entry(w, pending, power_list) {
1541 WARN_ON(reg != w->reg || dapm != w->dapm);
1542 w->power = w->new_power;
1544 mask |= w->mask << w->shift;
1546 value |= w->on_val << w->shift;
1548 value |= w->off_val << w->shift;
1550 pop_dbg(dapm->dev, card->pop_time,
1551 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1552 w->name, reg, value, mask);
1554 /* Check for events */
1555 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1556 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1560 /* Any widget will do, they should all be updating the
1564 pop_dbg(dapm->dev, card->pop_time,
1565 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1566 value, mask, reg, card->pop_time);
1567 pop_wait(card->pop_time);
1568 soc_dapm_update_bits(dapm, reg, mask, value);
1571 list_for_each_entry(w, pending, power_list) {
1572 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1573 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1577 /* Apply a DAPM power sequence.
1579 * We walk over a pre-sorted list of widgets to apply power to. In
1580 * order to minimise the number of writes to the device required
1581 * multiple widgets will be updated in a single write where possible.
1582 * Currently anything that requires more than a single write is not
1585 static void dapm_seq_run(struct snd_soc_card *card,
1586 struct list_head *list, int event, bool power_up)
1588 struct snd_soc_dapm_widget *w, *n;
1589 struct snd_soc_dapm_context *d;
1592 int cur_subseq = -1;
1593 int cur_reg = SND_SOC_NOPM;
1594 struct snd_soc_dapm_context *cur_dapm = NULL;
1601 sort = dapm_down_seq;
1603 list_for_each_entry_safe(w, n, list, power_list) {
1606 /* Do we need to apply any queued changes? */
1607 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1608 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1609 if (!list_empty(&pending))
1610 dapm_seq_run_coalesced(card, &pending);
1612 if (cur_dapm && cur_dapm->seq_notifier) {
1613 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1614 if (sort[i] == cur_sort)
1615 cur_dapm->seq_notifier(cur_dapm,
1620 if (cur_dapm && w->dapm != cur_dapm)
1621 soc_dapm_async_complete(cur_dapm);
1623 INIT_LIST_HEAD(&pending);
1625 cur_subseq = INT_MIN;
1626 cur_reg = SND_SOC_NOPM;
1631 case snd_soc_dapm_pre:
1633 list_for_each_entry_safe_continue(w, n, list,
1636 if (event == SND_SOC_DAPM_STREAM_START)
1638 NULL, SND_SOC_DAPM_PRE_PMU);
1639 else if (event == SND_SOC_DAPM_STREAM_STOP)
1641 NULL, SND_SOC_DAPM_PRE_PMD);
1644 case snd_soc_dapm_post:
1646 list_for_each_entry_safe_continue(w, n, list,
1649 if (event == SND_SOC_DAPM_STREAM_START)
1651 NULL, SND_SOC_DAPM_POST_PMU);
1652 else if (event == SND_SOC_DAPM_STREAM_STOP)
1654 NULL, SND_SOC_DAPM_POST_PMD);
1658 /* Queue it up for application */
1659 cur_sort = sort[w->id];
1660 cur_subseq = w->subseq;
1663 list_move(&w->power_list, &pending);
1668 dev_err(w->dapm->dev,
1669 "ASoC: Failed to apply widget power: %d\n", ret);
1672 if (!list_empty(&pending))
1673 dapm_seq_run_coalesced(card, &pending);
1675 if (cur_dapm && cur_dapm->seq_notifier) {
1676 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1677 if (sort[i] == cur_sort)
1678 cur_dapm->seq_notifier(cur_dapm,
1682 list_for_each_entry(d, &card->dapm_list, list) {
1683 soc_dapm_async_complete(d);
1687 static void dapm_widget_update(struct snd_soc_card *card)
1689 struct snd_soc_dapm_update *update = card->update;
1690 struct snd_soc_dapm_widget_list *wlist;
1691 struct snd_soc_dapm_widget *w = NULL;
1695 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1698 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1700 for (wi = 0; wi < wlist->num_widgets; wi++) {
1701 w = wlist->widgets[wi];
1703 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1704 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1706 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1714 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1717 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1720 if (update->has_second_set) {
1721 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1722 update->mask2, update->val2);
1724 dev_err(w->dapm->dev,
1725 "ASoC: %s DAPM update failed: %d\n",
1729 for (wi = 0; wi < wlist->num_widgets; wi++) {
1730 w = wlist->widgets[wi];
1732 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1733 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1735 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1741 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1742 * they're changing state.
1744 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1746 struct snd_soc_dapm_context *d = data;
1749 /* If we're off and we're not supposed to go into STANDBY */
1750 if (d->bias_level == SND_SOC_BIAS_OFF &&
1751 d->target_bias_level != SND_SOC_BIAS_OFF) {
1753 pm_runtime_get_sync(d->dev);
1755 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1758 "ASoC: Failed to turn on bias: %d\n", ret);
1761 /* Prepare for a transition to ON or away from ON */
1762 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1763 d->bias_level != SND_SOC_BIAS_ON) ||
1764 (d->target_bias_level != SND_SOC_BIAS_ON &&
1765 d->bias_level == SND_SOC_BIAS_ON)) {
1766 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1769 "ASoC: Failed to prepare bias: %d\n", ret);
1773 /* Async callback run prior to DAPM sequences - brings to their final
1776 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1778 struct snd_soc_dapm_context *d = data;
1781 /* If we just powered the last thing off drop to standby bias */
1782 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1783 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1784 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1785 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1787 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1791 /* If we're in standby and can support bias off then do that */
1792 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1793 d->target_bias_level == SND_SOC_BIAS_OFF) {
1794 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1796 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1800 pm_runtime_put(d->dev);
1803 /* If we just powered up then move to active bias */
1804 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1805 d->target_bias_level == SND_SOC_BIAS_ON) {
1806 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1808 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1813 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1814 bool power, bool connect)
1816 /* If a connection is being made or broken then that update
1817 * will have marked the peer dirty, otherwise the widgets are
1818 * not connected and this update has no impact. */
1822 /* If the peer is already in the state we're moving to then we
1823 * won't have an impact on it. */
1824 if (power != peer->power)
1825 dapm_mark_dirty(peer, "peer state change");
1828 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1829 struct list_head *up_list,
1830 struct list_head *down_list)
1832 struct snd_soc_dapm_path *path;
1834 if (w->power == power)
1837 trace_snd_soc_dapm_widget_power(w, power);
1839 /* If we changed our power state perhaps our neigbours changed
1842 snd_soc_dapm_widget_for_each_source_path(w, path)
1843 dapm_widget_set_peer_power(path->source, power, path->connect);
1845 /* Supplies can't affect their outputs, only their inputs */
1846 if (!w->is_supply) {
1847 snd_soc_dapm_widget_for_each_sink_path(w, path)
1848 dapm_widget_set_peer_power(path->sink, power,
1853 dapm_seq_insert(w, up_list, true);
1855 dapm_seq_insert(w, down_list, false);
1858 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1859 struct list_head *up_list,
1860 struct list_head *down_list)
1865 case snd_soc_dapm_pre:
1866 dapm_seq_insert(w, down_list, false);
1868 case snd_soc_dapm_post:
1869 dapm_seq_insert(w, up_list, true);
1873 power = dapm_widget_power_check(w);
1875 dapm_widget_set_power(w, power, up_list, down_list);
1880 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1882 if (dapm->idle_bias_off)
1885 switch (snd_power_get_state(dapm->card->snd_card)) {
1886 case SNDRV_CTL_POWER_D3hot:
1887 case SNDRV_CTL_POWER_D3cold:
1888 return dapm->suspend_bias_off;
1897 * Scan each dapm widget for complete audio path.
1898 * A complete path is a route that has valid endpoints i.e.:-
1900 * o DAC to output pin.
1901 * o Input pin to ADC.
1902 * o Input pin to Output pin (bypass, sidetone)
1903 * o DAC to ADC (loopback).
1905 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1907 struct snd_soc_dapm_widget *w;
1908 struct snd_soc_dapm_context *d;
1910 LIST_HEAD(down_list);
1911 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1912 enum snd_soc_bias_level bias;
1914 lockdep_assert_held(&card->dapm_mutex);
1916 trace_snd_soc_dapm_start(card);
1918 list_for_each_entry(d, &card->dapm_list, list) {
1919 if (dapm_idle_bias_off(d))
1920 d->target_bias_level = SND_SOC_BIAS_OFF;
1922 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1927 /* Check which widgets we need to power and store them in
1928 * lists indicating if they should be powered up or down. We
1929 * only check widgets that have been flagged as dirty but note
1930 * that new widgets may be added to the dirty list while we
1933 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1934 dapm_power_one_widget(w, &up_list, &down_list);
1937 list_for_each_entry(w, &card->widgets, list) {
1939 case snd_soc_dapm_pre:
1940 case snd_soc_dapm_post:
1941 /* These widgets always need to be powered */
1944 list_del_init(&w->dirty);
1951 /* Supplies and micbiases only bring the
1952 * context up to STANDBY as unless something
1953 * else is active and passing audio they
1954 * generally don't require full power. Signal
1955 * generators are virtual pins and have no
1956 * power impact themselves.
1959 case snd_soc_dapm_siggen:
1960 case snd_soc_dapm_vmid:
1962 case snd_soc_dapm_supply:
1963 case snd_soc_dapm_regulator_supply:
1964 case snd_soc_dapm_pinctrl:
1965 case snd_soc_dapm_clock_supply:
1966 case snd_soc_dapm_micbias:
1967 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1968 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1971 d->target_bias_level = SND_SOC_BIAS_ON;
1978 /* Force all contexts in the card to the same bias state if
1979 * they're not ground referenced.
1981 bias = SND_SOC_BIAS_OFF;
1982 list_for_each_entry(d, &card->dapm_list, list)
1983 if (d->target_bias_level > bias)
1984 bias = d->target_bias_level;
1985 list_for_each_entry(d, &card->dapm_list, list)
1986 if (!dapm_idle_bias_off(d))
1987 d->target_bias_level = bias;
1989 trace_snd_soc_dapm_walk_done(card);
1991 /* Run card bias changes at first */
1992 dapm_pre_sequence_async(&card->dapm, 0);
1993 /* Run other bias changes in parallel */
1994 list_for_each_entry(d, &card->dapm_list, list) {
1995 if (d != &card->dapm && d->bias_level != d->target_bias_level)
1996 async_schedule_domain(dapm_pre_sequence_async, d,
1999 async_synchronize_full_domain(&async_domain);
2001 list_for_each_entry(w, &down_list, power_list) {
2002 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2005 list_for_each_entry(w, &up_list, power_list) {
2006 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2009 /* Power down widgets first; try to avoid amplifying pops. */
2010 dapm_seq_run(card, &down_list, event, false);
2012 dapm_widget_update(card);
2015 dapm_seq_run(card, &up_list, event, true);
2017 /* Run all the bias changes in parallel */
2018 list_for_each_entry(d, &card->dapm_list, list) {
2019 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2020 async_schedule_domain(dapm_post_sequence_async, d,
2023 async_synchronize_full_domain(&async_domain);
2024 /* Run card bias changes at last */
2025 dapm_post_sequence_async(&card->dapm, 0);
2027 /* do we need to notify any clients that DAPM event is complete */
2028 list_for_each_entry(d, &card->dapm_list, list) {
2029 if (d->stream_event)
2030 d->stream_event(d, event);
2033 pop_dbg(card->dev, card->pop_time,
2034 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2035 pop_wait(card->pop_time);
2037 trace_snd_soc_dapm_done(card);
2042 #ifdef CONFIG_DEBUG_FS
2043 static ssize_t dapm_widget_power_read_file(struct file *file,
2044 char __user *user_buf,
2045 size_t count, loff_t *ppos)
2047 struct snd_soc_dapm_widget *w = file->private_data;
2048 struct snd_soc_card *card = w->dapm->card;
2049 enum snd_soc_dapm_direction dir, rdir;
2053 struct snd_soc_dapm_path *p = NULL;
2055 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2059 mutex_lock(&card->dapm_mutex);
2061 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2066 in = is_connected_input_ep(w, NULL, NULL);
2067 out = is_connected_output_ep(w, NULL, NULL);
2070 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2071 w->name, w->power ? "On" : "Off",
2072 w->force ? " (forced)" : "", in, out);
2075 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2076 " - R%d(0x%x) mask 0x%x",
2077 w->reg, w->reg, w->mask << w->shift);
2079 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2082 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2084 w->active ? "active" : "inactive");
2086 snd_soc_dapm_for_each_direction(dir) {
2087 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2088 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2089 if (p->connected && !p->connected(p->source, p->sink))
2095 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2096 " %s \"%s\" \"%s\"\n",
2097 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2098 p->name ? p->name : "static",
2099 p->node[rdir]->name);
2103 mutex_unlock(&card->dapm_mutex);
2105 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2111 static const struct file_operations dapm_widget_power_fops = {
2112 .open = simple_open,
2113 .read = dapm_widget_power_read_file,
2114 .llseek = default_llseek,
2117 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2118 size_t count, loff_t *ppos)
2120 struct snd_soc_dapm_context *dapm = file->private_data;
2123 switch (dapm->bias_level) {
2124 case SND_SOC_BIAS_ON:
2127 case SND_SOC_BIAS_PREPARE:
2128 level = "Prepare\n";
2130 case SND_SOC_BIAS_STANDBY:
2131 level = "Standby\n";
2133 case SND_SOC_BIAS_OFF:
2137 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2138 level = "Unknown\n";
2142 return simple_read_from_buffer(user_buf, count, ppos, level,
2146 static const struct file_operations dapm_bias_fops = {
2147 .open = simple_open,
2148 .read = dapm_bias_read_file,
2149 .llseek = default_llseek,
2152 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2153 struct dentry *parent)
2160 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2162 if (!dapm->debugfs_dapm) {
2164 "ASoC: Failed to create DAPM debugfs directory\n");
2168 d = debugfs_create_file("bias_level", 0444,
2169 dapm->debugfs_dapm, dapm,
2173 "ASoC: Failed to create bias level debugfs file\n");
2176 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2178 struct snd_soc_dapm_context *dapm = w->dapm;
2181 if (!dapm->debugfs_dapm || !w->name)
2184 d = debugfs_create_file(w->name, 0444,
2185 dapm->debugfs_dapm, w,
2186 &dapm_widget_power_fops);
2188 dev_warn(w->dapm->dev,
2189 "ASoC: Failed to create %s debugfs file\n",
2193 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2195 debugfs_remove_recursive(dapm->debugfs_dapm);
2199 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2200 struct dentry *parent)
2204 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2208 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2215 * soc_dapm_connect_path() - Connects or disconnects a path
2216 * @path: The path to update
2217 * @connect: The new connect state of the path. True if the path is connected,
2218 * false if it is disconnected.
2219 * @reason: The reason why the path changed (for debugging only)
2221 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2222 bool connect, const char *reason)
2224 if (path->connect == connect)
2227 path->connect = connect;
2228 dapm_mark_dirty(path->source, reason);
2229 dapm_mark_dirty(path->sink, reason);
2230 dapm_path_invalidate(path);
2233 /* test and update the power status of a mux widget */
2234 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2235 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2237 struct snd_soc_dapm_path *path;
2241 lockdep_assert_held(&card->dapm_mutex);
2243 /* find dapm widget path assoc with kcontrol */
2244 dapm_kcontrol_for_each_path(path, kcontrol) {
2246 /* we now need to match the string in the enum to the path */
2247 if (!(strcmp(path->name, e->texts[mux])))
2252 soc_dapm_connect_path(path, connect, "mux update");
2256 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2261 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2262 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2263 struct snd_soc_dapm_update *update)
2265 struct snd_soc_card *card = dapm->card;
2268 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2269 card->update = update;
2270 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2271 card->update = NULL;
2272 mutex_unlock(&card->dapm_mutex);
2274 soc_dpcm_runtime_update(card);
2277 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2279 /* test and update the power status of a mixer or switch widget */
2280 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2281 struct snd_kcontrol *kcontrol,
2282 int connect, int rconnect)
2284 struct snd_soc_dapm_path *path;
2287 lockdep_assert_held(&card->dapm_mutex);
2289 /* find dapm widget path assoc with kcontrol */
2290 dapm_kcontrol_for_each_path(path, kcontrol) {
2292 * Ideally this function should support any number of
2293 * paths and channels. But since kcontrols only come
2294 * in mono and stereo variants, we are limited to 2
2297 * The following code assumes for stereo controls the
2298 * first path (when 'found == 0') is the left channel,
2299 * and all remaining paths (when 'found == 1') are the
2302 * A stereo control is signified by a valid 'rconnect'
2303 * value, either 0 for unconnected, or >= 0 for connected.
2304 * This is chosen instead of using snd_soc_volsw_is_stereo,
2305 * so that the behavior of snd_soc_dapm_mixer_update_power
2306 * doesn't change even when the kcontrol passed in is
2309 * It passes 'connect' as the path connect status for
2310 * the left channel, and 'rconnect' for the right
2313 if (found && rconnect >= 0)
2314 soc_dapm_connect_path(path, rconnect, "mixer update");
2316 soc_dapm_connect_path(path, connect, "mixer update");
2321 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2326 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2327 struct snd_kcontrol *kcontrol, int connect,
2328 struct snd_soc_dapm_update *update)
2330 struct snd_soc_card *card = dapm->card;
2333 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2334 card->update = update;
2335 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2336 card->update = NULL;
2337 mutex_unlock(&card->dapm_mutex);
2339 soc_dpcm_runtime_update(card);
2342 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2344 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2347 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2348 struct snd_soc_dapm_widget *w;
2350 char *state = "not set";
2352 /* card won't be set for the dummy component, as a spot fix
2353 * we're checking for that case specifically here but in future
2354 * we will ensure that the dummy component looks like others.
2359 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2360 if (w->dapm != dapm)
2363 /* only display widgets that burn power */
2365 case snd_soc_dapm_hp:
2366 case snd_soc_dapm_mic:
2367 case snd_soc_dapm_spk:
2368 case snd_soc_dapm_line:
2369 case snd_soc_dapm_micbias:
2370 case snd_soc_dapm_dac:
2371 case snd_soc_dapm_adc:
2372 case snd_soc_dapm_pga:
2373 case snd_soc_dapm_out_drv:
2374 case snd_soc_dapm_mixer:
2375 case snd_soc_dapm_mixer_named_ctl:
2376 case snd_soc_dapm_supply:
2377 case snd_soc_dapm_regulator_supply:
2378 case snd_soc_dapm_pinctrl:
2379 case snd_soc_dapm_clock_supply:
2381 count += sprintf(buf + count, "%s: %s\n",
2382 w->name, w->power ? "On":"Off");
2389 switch (snd_soc_dapm_get_bias_level(dapm)) {
2390 case SND_SOC_BIAS_ON:
2393 case SND_SOC_BIAS_PREPARE:
2396 case SND_SOC_BIAS_STANDBY:
2399 case SND_SOC_BIAS_OFF:
2403 count += sprintf(buf + count, "PM State: %s\n", state);
2408 /* show dapm widget status in sys fs */
2409 static ssize_t dapm_widget_show(struct device *dev,
2410 struct device_attribute *attr, char *buf)
2412 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2413 struct snd_soc_dai *codec_dai;
2416 mutex_lock(&rtd->card->dapm_mutex);
2418 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2419 struct snd_soc_component *cmpnt = codec_dai->component;
2421 count += dapm_widget_show_component(cmpnt, buf + count);
2424 mutex_unlock(&rtd->card->dapm_mutex);
2429 static DEVICE_ATTR_RO(dapm_widget);
2431 struct attribute *soc_dapm_dev_attrs[] = {
2432 &dev_attr_dapm_widget.attr,
2436 static void dapm_free_path(struct snd_soc_dapm_path *path)
2438 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2439 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2440 list_del(&path->list_kcontrol);
2441 list_del(&path->list);
2445 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2447 struct snd_soc_dapm_path *p, *next_p;
2448 enum snd_soc_dapm_direction dir;
2452 * remove source and sink paths associated to this widget.
2453 * While removing the path, remove reference to it from both
2454 * source and sink widgets so that path is removed only once.
2456 snd_soc_dapm_for_each_direction(dir) {
2457 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2461 kfree(w->kcontrols);
2462 kfree_const(w->name);
2463 kfree_const(w->sname);
2467 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2469 dapm->path_sink_cache.widget = NULL;
2470 dapm->path_source_cache.widget = NULL;
2473 /* free all dapm widgets and resources */
2474 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2476 struct snd_soc_dapm_widget *w, *next_w;
2478 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2479 if (w->dapm != dapm)
2481 snd_soc_dapm_free_widget(w);
2483 snd_soc_dapm_reset_cache(dapm);
2486 static struct snd_soc_dapm_widget *dapm_find_widget(
2487 struct snd_soc_dapm_context *dapm, const char *pin,
2488 bool search_other_contexts)
2490 struct snd_soc_dapm_widget *w;
2491 struct snd_soc_dapm_widget *fallback = NULL;
2493 list_for_each_entry(w, &dapm->card->widgets, list) {
2494 if (!strcmp(w->name, pin)) {
2495 if (w->dapm == dapm)
2502 if (search_other_contexts)
2508 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2509 const char *pin, int status)
2511 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2513 dapm_assert_locked(dapm);
2516 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2520 if (w->connected != status) {
2521 dapm_mark_dirty(w, "pin configuration");
2522 dapm_widget_invalidate_input_paths(w);
2523 dapm_widget_invalidate_output_paths(w);
2526 w->connected = status;
2534 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2535 * @dapm: DAPM context
2537 * Walks all dapm audio paths and powers widgets according to their
2538 * stream or path usage.
2540 * Requires external locking.
2542 * Returns 0 for success.
2544 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2547 * Suppress early reports (eg, jacks syncing their state) to avoid
2548 * silly DAPM runs during card startup.
2550 if (!dapm->card || !dapm->card->instantiated)
2553 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2555 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2558 * snd_soc_dapm_sync - scan and power dapm paths
2559 * @dapm: DAPM context
2561 * Walks all dapm audio paths and powers widgets according to their
2562 * stream or path usage.
2564 * Returns 0 for success.
2566 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2570 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2571 ret = snd_soc_dapm_sync_unlocked(dapm);
2572 mutex_unlock(&dapm->card->dapm_mutex);
2575 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2577 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2578 struct snd_soc_dapm_widget *w,
2582 case snd_soc_dapm_aif_out:
2583 case snd_soc_dapm_aif_in:
2589 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2590 w->channel < channels ? "Connecting" : "Disconnecting",
2591 p->source->name, p->sink->name);
2593 if (w->channel < channels)
2594 soc_dapm_connect_path(p, true, "dai update");
2596 soc_dapm_connect_path(p, false, "dai update");
2601 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2602 struct snd_pcm_hw_params *params,
2603 struct snd_soc_dai *dai)
2605 int dir = substream->stream;
2606 int channels = params_channels(params);
2607 struct snd_soc_dapm_path *p;
2608 struct snd_soc_dapm_widget *w;
2611 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
2612 w = dai->playback_widget;
2614 w = dai->capture_widget;
2619 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2620 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2622 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2623 ret = dapm_update_dai_chan(p, p->sink, channels);
2628 snd_soc_dapm_widget_for_each_source_path(w, p) {
2629 ret = dapm_update_dai_chan(p, p->source, channels);
2637 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2638 struct snd_pcm_hw_params *params,
2639 struct snd_soc_dai *dai)
2641 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2644 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2645 ret = dapm_update_dai_unlocked(substream, params, dai);
2646 mutex_unlock(&rtd->card->dapm_mutex);
2650 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2653 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2654 * @w: The widget for which to update the flags
2656 * Some widgets have a dynamic category which depends on which neighbors they
2657 * are connected to. This function update the category for these widgets.
2659 * This function must be called whenever a path is added or removed to a widget.
2661 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2663 enum snd_soc_dapm_direction dir;
2664 struct snd_soc_dapm_path *p;
2668 case snd_soc_dapm_input:
2669 /* On a fully routed card an input is never a source */
2670 if (w->dapm->card->fully_routed)
2672 ep = SND_SOC_DAPM_EP_SOURCE;
2673 snd_soc_dapm_widget_for_each_source_path(w, p) {
2674 if (p->source->id == snd_soc_dapm_micbias ||
2675 p->source->id == snd_soc_dapm_mic ||
2676 p->source->id == snd_soc_dapm_line ||
2677 p->source->id == snd_soc_dapm_output) {
2683 case snd_soc_dapm_output:
2684 /* On a fully routed card a output is never a sink */
2685 if (w->dapm->card->fully_routed)
2687 ep = SND_SOC_DAPM_EP_SINK;
2688 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2689 if (p->sink->id == snd_soc_dapm_spk ||
2690 p->sink->id == snd_soc_dapm_hp ||
2691 p->sink->id == snd_soc_dapm_line ||
2692 p->sink->id == snd_soc_dapm_input) {
2698 case snd_soc_dapm_line:
2700 snd_soc_dapm_for_each_direction(dir) {
2701 if (!list_empty(&w->edges[dir]))
2702 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2712 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2713 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2714 const char *control)
2716 bool dynamic_source = false;
2717 bool dynamic_sink = false;
2722 switch (source->id) {
2723 case snd_soc_dapm_demux:
2724 dynamic_source = true;
2731 case snd_soc_dapm_mux:
2732 case snd_soc_dapm_switch:
2733 case snd_soc_dapm_mixer:
2734 case snd_soc_dapm_mixer_named_ctl:
2735 dynamic_sink = true;
2741 if (dynamic_source && dynamic_sink) {
2743 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2744 source->name, control, sink->name);
2746 } else if (!dynamic_source && !dynamic_sink) {
2748 "Control not supported for path %s -> [%s] -> %s\n",
2749 source->name, control, sink->name);
2756 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2757 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2758 const char *control,
2759 int (*connected)(struct snd_soc_dapm_widget *source,
2760 struct snd_soc_dapm_widget *sink))
2762 struct snd_soc_dapm_widget *widgets[2];
2763 enum snd_soc_dapm_direction dir;
2764 struct snd_soc_dapm_path *path;
2767 if (wsink->is_supply && !wsource->is_supply) {
2769 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2770 wsource->name, wsink->name);
2774 if (connected && !wsource->is_supply) {
2776 "connected() callback only supported for supply widgets (%s -> %s)\n",
2777 wsource->name, wsink->name);
2781 if (wsource->is_supply && control) {
2783 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2784 wsource->name, control, wsink->name);
2788 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2792 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2796 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2797 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2798 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2799 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2801 path->connected = connected;
2802 INIT_LIST_HEAD(&path->list);
2803 INIT_LIST_HEAD(&path->list_kcontrol);
2805 if (wsource->is_supply || wsink->is_supply)
2806 path->is_supply = 1;
2808 /* connect static paths */
2809 if (control == NULL) {
2812 switch (wsource->id) {
2813 case snd_soc_dapm_demux:
2814 ret = dapm_connect_mux(dapm, path, control, wsource);
2822 switch (wsink->id) {
2823 case snd_soc_dapm_mux:
2824 ret = dapm_connect_mux(dapm, path, control, wsink);
2828 case snd_soc_dapm_switch:
2829 case snd_soc_dapm_mixer:
2830 case snd_soc_dapm_mixer_named_ctl:
2831 ret = dapm_connect_mixer(dapm, path, control);
2840 list_add(&path->list, &dapm->card->paths);
2841 snd_soc_dapm_for_each_direction(dir)
2842 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2844 snd_soc_dapm_for_each_direction(dir) {
2845 dapm_update_widget_flags(widgets[dir]);
2846 dapm_mark_dirty(widgets[dir], "Route added");
2849 if (dapm->card->instantiated && path->connect)
2850 dapm_path_invalidate(path);
2858 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2859 const struct snd_soc_dapm_route *route)
2861 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2862 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2865 char prefixed_sink[80];
2866 char prefixed_source[80];
2868 unsigned int sink_ref = 0;
2869 unsigned int source_ref = 0;
2872 prefix = soc_dapm_prefix(dapm);
2874 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2875 prefix, route->sink);
2876 sink = prefixed_sink;
2877 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2878 prefix, route->source);
2879 source = prefixed_source;
2882 source = route->source;
2885 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2886 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2888 if (wsink && wsource)
2892 * find src and dest widgets over all widgets but favor a widget from
2893 * current DAPM context
2895 list_for_each_entry(w, &dapm->card->widgets, list) {
2896 if (!wsink && !(strcmp(w->name, sink))) {
2898 if (w->dapm == dapm) {
2906 "ASoC: sink widget %s overwritten\n",
2910 if (!wsource && !(strcmp(w->name, source))) {
2912 if (w->dapm == dapm) {
2920 "ASoC: source widget %s overwritten\n",
2924 /* use widget from another DAPM context if not found from this */
2930 if (wsource == NULL) {
2931 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2935 if (wsink == NULL) {
2936 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2942 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2943 dapm_wcache_update(&dapm->path_source_cache, wsource);
2945 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2952 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2953 source, route->control, sink);
2957 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2958 const struct snd_soc_dapm_route *route)
2960 struct snd_soc_dapm_widget *wsource, *wsink;
2961 struct snd_soc_dapm_path *path, *p;
2964 char prefixed_sink[80];
2965 char prefixed_source[80];
2968 if (route->control) {
2970 "ASoC: Removal of routes with controls not supported\n");
2974 prefix = soc_dapm_prefix(dapm);
2976 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2977 prefix, route->sink);
2978 sink = prefixed_sink;
2979 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2980 prefix, route->source);
2981 source = prefixed_source;
2984 source = route->source;
2988 list_for_each_entry(p, &dapm->card->paths, list) {
2989 if (strcmp(p->source->name, source) != 0)
2991 if (strcmp(p->sink->name, sink) != 0)
2998 wsource = path->source;
3001 dapm_mark_dirty(wsource, "Route removed");
3002 dapm_mark_dirty(wsink, "Route removed");
3004 dapm_path_invalidate(path);
3006 dapm_free_path(path);
3008 /* Update any path related flags */
3009 dapm_update_widget_flags(wsource);
3010 dapm_update_widget_flags(wsink);
3012 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3020 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3021 * @dapm: DAPM context
3022 * @route: audio routes
3023 * @num: number of routes
3025 * Connects 2 dapm widgets together via a named audio path. The sink is
3026 * the widget receiving the audio signal, whilst the source is the sender
3027 * of the audio signal.
3029 * Returns 0 for success else error. On error all resources can be freed
3030 * with a call to snd_soc_card_free().
3032 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3033 const struct snd_soc_dapm_route *route, int num)
3037 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3038 for (i = 0; i < num; i++) {
3039 r = snd_soc_dapm_add_route(dapm, route);
3041 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3043 route->control ? route->control : "direct",
3049 mutex_unlock(&dapm->card->dapm_mutex);
3053 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3056 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3057 * @dapm: DAPM context
3058 * @route: audio routes
3059 * @num: number of routes
3061 * Removes routes from the DAPM context.
3063 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3064 const struct snd_soc_dapm_route *route, int num)
3068 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3069 for (i = 0; i < num; i++) {
3070 snd_soc_dapm_del_route(dapm, route);
3073 mutex_unlock(&dapm->card->dapm_mutex);
3077 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3079 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3080 const struct snd_soc_dapm_route *route)
3082 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3085 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3088 struct snd_soc_dapm_path *path;
3092 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3098 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3103 if (route->control || route->connected)
3104 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3105 route->source, route->sink);
3107 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3108 if (path->sink == sink) {
3115 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3116 route->source, route->sink);
3118 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3119 count, route->source, route->sink);
3125 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3126 * @dapm: DAPM context
3127 * @route: audio routes
3128 * @num: number of routes
3130 * Mark existing routes matching those specified in the passed array
3131 * as being weak, meaning that they are ignored for the purpose of
3132 * power decisions. The main intended use case is for sidetone paths
3133 * which couple audio between other independent paths if they are both
3134 * active in order to make the combination work better at the user
3135 * level but which aren't intended to be "used".
3137 * Note that CODEC drivers should not use this as sidetone type paths
3138 * can frequently also be used as bypass paths.
3140 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3141 const struct snd_soc_dapm_route *route, int num)
3146 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3147 for (i = 0; i < num; i++) {
3148 err = snd_soc_dapm_weak_route(dapm, route);
3153 mutex_unlock(&dapm->card->dapm_mutex);
3157 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3160 * snd_soc_dapm_new_widgets - add new dapm widgets
3161 * @card: card to be checked for new dapm widgets
3163 * Checks the codec for any new dapm widgets and creates them if found.
3165 * Returns 0 for success.
3167 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3169 struct snd_soc_dapm_widget *w;
3172 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3174 list_for_each_entry(w, &card->widgets, list)
3179 if (w->num_kcontrols) {
3180 w->kcontrols = kcalloc(w->num_kcontrols,
3181 sizeof(struct snd_kcontrol *),
3183 if (!w->kcontrols) {
3184 mutex_unlock(&card->dapm_mutex);
3190 case snd_soc_dapm_switch:
3191 case snd_soc_dapm_mixer:
3192 case snd_soc_dapm_mixer_named_ctl:
3195 case snd_soc_dapm_mux:
3196 case snd_soc_dapm_demux:
3199 case snd_soc_dapm_pga:
3200 case snd_soc_dapm_out_drv:
3203 case snd_soc_dapm_dai_link:
3204 dapm_new_dai_link(w);
3210 /* Read the initial power state from the device */
3212 soc_dapm_read(w->dapm, w->reg, &val);
3213 val = val >> w->shift;
3215 if (val == w->on_val)
3221 dapm_mark_dirty(w, "new widget");
3222 dapm_debugfs_add_widget(w);
3225 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3226 mutex_unlock(&card->dapm_mutex);
3229 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3232 * snd_soc_dapm_get_volsw - dapm mixer get callback
3233 * @kcontrol: mixer control
3234 * @ucontrol: control element information
3236 * Callback to get the value of a dapm mixer control.
3238 * Returns 0 for success.
3240 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3241 struct snd_ctl_elem_value *ucontrol)
3243 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3244 struct snd_soc_card *card = dapm->card;
3245 struct soc_mixer_control *mc =
3246 (struct soc_mixer_control *)kcontrol->private_value;
3248 unsigned int shift = mc->shift;
3250 unsigned int width = fls(max);
3251 unsigned int mask = (1 << fls(max)) - 1;
3252 unsigned int invert = mc->invert;
3253 unsigned int reg_val, val, rval = 0;
3256 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3257 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3258 ret = soc_dapm_read(dapm, reg, ®_val);
3259 val = (reg_val >> shift) & mask;
3261 if (ret == 0 && reg != mc->rreg)
3262 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3264 if (snd_soc_volsw_is_stereo(mc))
3265 rval = (reg_val >> mc->rshift) & mask;
3267 reg_val = dapm_kcontrol_get_value(kcontrol);
3268 val = reg_val & mask;
3270 if (snd_soc_volsw_is_stereo(mc))
3271 rval = (reg_val >> width) & mask;
3273 mutex_unlock(&card->dapm_mutex);
3279 ucontrol->value.integer.value[0] = max - val;
3281 ucontrol->value.integer.value[0] = val;
3283 if (snd_soc_volsw_is_stereo(mc)) {
3285 ucontrol->value.integer.value[1] = max - rval;
3287 ucontrol->value.integer.value[1] = rval;
3292 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3295 * snd_soc_dapm_put_volsw - dapm mixer set callback
3296 * @kcontrol: mixer control
3297 * @ucontrol: control element information
3299 * Callback to set the value of a dapm mixer control.
3301 * Returns 0 for success.
3303 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3304 struct snd_ctl_elem_value *ucontrol)
3306 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3307 struct snd_soc_card *card = dapm->card;
3308 struct soc_mixer_control *mc =
3309 (struct soc_mixer_control *)kcontrol->private_value;
3311 unsigned int shift = mc->shift;
3313 unsigned int width = fls(max);
3314 unsigned int mask = (1 << width) - 1;
3315 unsigned int invert = mc->invert;
3316 unsigned int val, rval = 0;
3317 int connect, rconnect = -1, change, reg_change = 0;
3318 struct snd_soc_dapm_update update = {};
3321 val = (ucontrol->value.integer.value[0] & mask);
3327 if (snd_soc_volsw_is_stereo(mc)) {
3328 rval = (ucontrol->value.integer.value[1] & mask);
3334 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3336 /* This assumes field width < (bits in unsigned int / 2) */
3337 if (width > sizeof(unsigned int) * 8 / 2)
3339 "ASoC: control %s field width limit exceeded\n",
3341 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3343 if (reg != SND_SOC_NOPM) {
3345 rval = rval << mc->rshift;
3347 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3349 if (snd_soc_volsw_is_stereo(mc))
3350 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3355 if (change || reg_change) {
3357 if (snd_soc_volsw_is_stereo(mc)) {
3358 update.has_second_set = true;
3359 update.reg2 = mc->rreg;
3360 update.mask2 = mask << mc->rshift;
3363 update.kcontrol = kcontrol;
3365 update.mask = mask << shift;
3367 card->update = &update;
3369 change |= reg_change;
3371 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3374 card->update = NULL;
3377 mutex_unlock(&card->dapm_mutex);
3380 soc_dpcm_runtime_update(card);
3384 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3387 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3388 * @kcontrol: mixer control
3389 * @ucontrol: control element information
3391 * Callback to get the value of a dapm enumerated double mixer control.
3393 * Returns 0 for success.
3395 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3396 struct snd_ctl_elem_value *ucontrol)
3398 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3399 struct snd_soc_card *card = dapm->card;
3400 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3401 unsigned int reg_val, val;
3403 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3404 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3405 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3407 mutex_unlock(&card->dapm_mutex);
3411 reg_val = dapm_kcontrol_get_value(kcontrol);
3413 mutex_unlock(&card->dapm_mutex);
3415 val = (reg_val >> e->shift_l) & e->mask;
3416 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3417 if (e->shift_l != e->shift_r) {
3418 val = (reg_val >> e->shift_r) & e->mask;
3419 val = snd_soc_enum_val_to_item(e, val);
3420 ucontrol->value.enumerated.item[1] = val;
3425 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3428 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3429 * @kcontrol: mixer control
3430 * @ucontrol: control element information
3432 * Callback to set the value of a dapm enumerated double mixer control.
3434 * Returns 0 for success.
3436 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3437 struct snd_ctl_elem_value *ucontrol)
3439 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3440 struct snd_soc_card *card = dapm->card;
3441 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3442 unsigned int *item = ucontrol->value.enumerated.item;
3443 unsigned int val, change, reg_change = 0;
3445 struct snd_soc_dapm_update update = {};
3448 if (item[0] >= e->items)
3451 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3452 mask = e->mask << e->shift_l;
3453 if (e->shift_l != e->shift_r) {
3454 if (item[1] > e->items)
3456 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3457 mask |= e->mask << e->shift_r;
3460 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3462 change = dapm_kcontrol_set_value(kcontrol, val);
3464 if (e->reg != SND_SOC_NOPM)
3465 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3467 if (change || reg_change) {
3469 update.kcontrol = kcontrol;
3470 update.reg = e->reg;
3473 card->update = &update;
3475 change |= reg_change;
3477 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3479 card->update = NULL;
3482 mutex_unlock(&card->dapm_mutex);
3485 soc_dpcm_runtime_update(card);
3489 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3492 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3494 * @kcontrol: mixer control
3495 * @uinfo: control element information
3497 * Callback to provide information about a pin switch control.
3499 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3500 struct snd_ctl_elem_info *uinfo)
3502 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3504 uinfo->value.integer.min = 0;
3505 uinfo->value.integer.max = 1;
3509 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3512 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3514 * @kcontrol: mixer control
3517 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3518 struct snd_ctl_elem_value *ucontrol)
3520 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3521 const char *pin = (const char *)kcontrol->private_value;
3523 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3525 ucontrol->value.integer.value[0] =
3526 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3528 mutex_unlock(&card->dapm_mutex);
3532 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3535 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3537 * @kcontrol: mixer control
3540 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3541 struct snd_ctl_elem_value *ucontrol)
3543 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3544 const char *pin = (const char *)kcontrol->private_value;
3546 if (ucontrol->value.integer.value[0])
3547 snd_soc_dapm_enable_pin(&card->dapm, pin);
3549 snd_soc_dapm_disable_pin(&card->dapm, pin);
3551 snd_soc_dapm_sync(&card->dapm);
3554 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3556 struct snd_soc_dapm_widget *
3557 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3558 const struct snd_soc_dapm_widget *widget)
3560 enum snd_soc_dapm_direction dir;
3561 struct snd_soc_dapm_widget *w;
3565 if ((w = dapm_cnew_widget(widget)) == NULL)
3566 return ERR_PTR(-ENOMEM);
3569 case snd_soc_dapm_regulator_supply:
3570 w->regulator = devm_regulator_get(dapm->dev, w->name);
3571 if (IS_ERR(w->regulator)) {
3572 ret = PTR_ERR(w->regulator);
3573 goto request_failed;
3576 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3577 ret = regulator_allow_bypass(w->regulator, true);
3580 "ASoC: Failed to bypass %s: %d\n",
3584 case snd_soc_dapm_pinctrl:
3585 w->pinctrl = devm_pinctrl_get(dapm->dev);
3586 if (IS_ERR(w->pinctrl)) {
3587 ret = PTR_ERR(w->pinctrl);
3588 goto request_failed;
3591 case snd_soc_dapm_clock_supply:
3592 w->clk = devm_clk_get(dapm->dev, w->name);
3593 if (IS_ERR(w->clk)) {
3594 ret = PTR_ERR(w->clk);
3595 goto request_failed;
3602 prefix = soc_dapm_prefix(dapm);
3604 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3606 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3607 if (w->name == NULL) {
3608 kfree_const(w->sname);
3610 return ERR_PTR(-ENOMEM);
3614 case snd_soc_dapm_mic:
3615 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3616 w->power_check = dapm_generic_check_power;
3618 case snd_soc_dapm_input:
3619 if (!dapm->card->fully_routed)
3620 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3621 w->power_check = dapm_generic_check_power;
3623 case snd_soc_dapm_spk:
3624 case snd_soc_dapm_hp:
3625 w->is_ep = SND_SOC_DAPM_EP_SINK;
3626 w->power_check = dapm_generic_check_power;
3628 case snd_soc_dapm_output:
3629 if (!dapm->card->fully_routed)
3630 w->is_ep = SND_SOC_DAPM_EP_SINK;
3631 w->power_check = dapm_generic_check_power;
3633 case snd_soc_dapm_vmid:
3634 case snd_soc_dapm_siggen:
3635 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3636 w->power_check = dapm_always_on_check_power;
3638 case snd_soc_dapm_sink:
3639 w->is_ep = SND_SOC_DAPM_EP_SINK;
3640 w->power_check = dapm_always_on_check_power;
3643 case snd_soc_dapm_mux:
3644 case snd_soc_dapm_demux:
3645 case snd_soc_dapm_switch:
3646 case snd_soc_dapm_mixer:
3647 case snd_soc_dapm_mixer_named_ctl:
3648 case snd_soc_dapm_adc:
3649 case snd_soc_dapm_aif_out:
3650 case snd_soc_dapm_dac:
3651 case snd_soc_dapm_aif_in:
3652 case snd_soc_dapm_pga:
3653 case snd_soc_dapm_out_drv:
3654 case snd_soc_dapm_micbias:
3655 case snd_soc_dapm_line:
3656 case snd_soc_dapm_dai_link:
3657 case snd_soc_dapm_dai_out:
3658 case snd_soc_dapm_dai_in:
3659 w->power_check = dapm_generic_check_power;
3661 case snd_soc_dapm_supply:
3662 case snd_soc_dapm_regulator_supply:
3663 case snd_soc_dapm_pinctrl:
3664 case snd_soc_dapm_clock_supply:
3665 case snd_soc_dapm_kcontrol:
3667 w->power_check = dapm_supply_check_power;
3670 w->power_check = dapm_always_on_check_power;
3675 INIT_LIST_HEAD(&w->list);
3676 INIT_LIST_HEAD(&w->dirty);
3677 list_add_tail(&w->list, &dapm->card->widgets);
3679 snd_soc_dapm_for_each_direction(dir) {
3680 INIT_LIST_HEAD(&w->edges[dir]);
3681 w->endpoints[dir] = -1;
3684 /* machine layer sets up unconnected pins and insertions */
3689 if (ret != -EPROBE_DEFER)
3690 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3693 return ERR_PTR(ret);
3697 * snd_soc_dapm_new_control - create new dapm control
3698 * @dapm: DAPM context
3699 * @widget: widget template
3701 * Creates new DAPM control based upon a template.
3703 * Returns a widget pointer on success or an error pointer on failure
3705 struct snd_soc_dapm_widget *
3706 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3707 const struct snd_soc_dapm_widget *widget)
3709 struct snd_soc_dapm_widget *w;
3711 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3712 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3713 mutex_unlock(&dapm->card->dapm_mutex);
3717 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3720 * snd_soc_dapm_new_controls - create new dapm controls
3721 * @dapm: DAPM context
3722 * @widget: widget array
3723 * @num: number of widgets
3725 * Creates new DAPM controls based upon the templates.
3727 * Returns 0 for success else error.
3729 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3730 const struct snd_soc_dapm_widget *widget,
3733 struct snd_soc_dapm_widget *w;
3737 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3738 for (i = 0; i < num; i++) {
3739 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3746 mutex_unlock(&dapm->card->dapm_mutex);
3749 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3751 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3752 struct snd_kcontrol *kcontrol, int event)
3754 struct snd_soc_dapm_path *path;
3755 struct snd_soc_dai *source, *sink;
3756 struct snd_soc_pcm_runtime *rtd = w->priv;
3757 const struct snd_soc_pcm_stream *config;
3758 struct snd_pcm_substream substream;
3759 struct snd_pcm_hw_params *params = NULL;
3760 struct snd_pcm_runtime *runtime = NULL;
3764 config = rtd->dai_link->params + rtd->params_select;
3766 if (WARN_ON(!config) ||
3767 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3768 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3771 /* Be a little careful as we don't want to overflow the mask array */
3772 if (config->formats) {
3773 fmt = ffs(config->formats) - 1;
3775 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3780 /* Currently very limited parameter selection */
3781 params = kzalloc(sizeof(*params), GFP_KERNEL);
3786 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3788 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3790 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3793 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3794 = config->channels_min;
3795 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3796 = config->channels_max;
3798 memset(&substream, 0, sizeof(substream));
3800 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3801 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3806 substream.runtime = runtime;
3807 substream.private_data = rtd;
3810 case SND_SOC_DAPM_PRE_PMU:
3811 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3812 snd_soc_dapm_widget_for_each_source_path(w, path) {
3813 source = path->source->priv;
3815 if (source->driver->ops->startup) {
3816 ret = source->driver->ops->startup(&substream,
3819 dev_err(source->dev,
3820 "ASoC: startup() failed: %d\n",
3826 ret = soc_dai_hw_params(&substream, params, source);
3830 dapm_update_dai_unlocked(&substream, params, source);
3833 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3834 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3835 sink = path->sink->priv;
3837 if (sink->driver->ops->startup) {
3838 ret = sink->driver->ops->startup(&substream,
3842 "ASoC: startup() failed: %d\n",
3848 ret = soc_dai_hw_params(&substream, params, sink);
3852 dapm_update_dai_unlocked(&substream, params, sink);
3856 case SND_SOC_DAPM_POST_PMU:
3857 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3858 sink = path->sink->priv;
3860 ret = snd_soc_dai_digital_mute(sink, 0,
3861 SNDRV_PCM_STREAM_PLAYBACK);
3862 if (ret != 0 && ret != -ENOTSUPP)
3864 "ASoC: Failed to unmute: %d\n", ret);
3869 case SND_SOC_DAPM_PRE_PMD:
3870 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3871 sink = path->sink->priv;
3873 ret = snd_soc_dai_digital_mute(sink, 1,
3874 SNDRV_PCM_STREAM_PLAYBACK);
3875 if (ret != 0 && ret != -ENOTSUPP)
3877 "ASoC: Failed to mute: %d\n", ret);
3881 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3882 snd_soc_dapm_widget_for_each_source_path(w, path) {
3883 source = path->source->priv;
3885 if (source->driver->ops->hw_free)
3886 source->driver->ops->hw_free(&substream,
3890 if (source->driver->ops->shutdown)
3891 source->driver->ops->shutdown(&substream,
3895 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3896 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3897 sink = path->sink->priv;
3899 if (sink->driver->ops->hw_free)
3900 sink->driver->ops->hw_free(&substream, sink);
3903 if (sink->driver->ops->shutdown)
3904 sink->driver->ops->shutdown(&substream, sink);
3909 WARN(1, "Unknown event %d\n", event);
3919 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3920 struct snd_ctl_elem_value *ucontrol)
3922 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3923 struct snd_soc_pcm_runtime *rtd = w->priv;
3925 ucontrol->value.enumerated.item[0] = rtd->params_select;
3930 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3931 struct snd_ctl_elem_value *ucontrol)
3933 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3934 struct snd_soc_pcm_runtime *rtd = w->priv;
3936 /* Can't change the config when widget is already powered */
3940 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
3943 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
3946 rtd->params_select = ucontrol->value.enumerated.item[0];
3952 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3953 unsigned long *private_value,
3955 const char **w_param_text)
3959 devm_kfree(card->dev, (void *)*private_value);
3960 for (count = 0 ; count < num_params; count++)
3961 devm_kfree(card->dev, (void *)w_param_text[count]);
3962 devm_kfree(card->dev, w_param_text);
3965 static struct snd_kcontrol_new *
3966 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3968 const struct snd_soc_pcm_stream *params,
3969 int num_params, const char **w_param_text,
3970 unsigned long *private_value)
3972 struct soc_enum w_param_enum[] = {
3973 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3975 struct snd_kcontrol_new kcontrol_dai_link[] = {
3976 SOC_ENUM_EXT(NULL, w_param_enum[0],
3977 snd_soc_dapm_dai_link_get,
3978 snd_soc_dapm_dai_link_put),
3980 struct snd_kcontrol_new *kcontrol_news;
3981 const struct snd_soc_pcm_stream *config = params;
3984 for (count = 0 ; count < num_params; count++) {
3985 if (!config->stream_name) {
3986 dev_warn(card->dapm.dev,
3987 "ASoC: anonymous config %d for dai link %s\n",
3989 w_param_text[count] =
3990 devm_kasprintf(card->dev, GFP_KERNEL,
3991 "Anonymous Configuration %d",
3994 w_param_text[count] = devm_kmemdup(card->dev,
3995 config->stream_name,
3996 strlen(config->stream_name) + 1,
3999 if (!w_param_text[count])
4000 goto outfree_w_param;
4004 w_param_enum[0].items = num_params;
4005 w_param_enum[0].texts = w_param_text;
4008 (unsigned long) devm_kmemdup(card->dev,
4009 (void *)(kcontrol_dai_link[0].private_value),
4010 sizeof(struct soc_enum), GFP_KERNEL);
4011 if (!*private_value) {
4012 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4014 goto outfree_w_param;
4016 kcontrol_dai_link[0].private_value = *private_value;
4017 /* duplicate kcontrol_dai_link on heap so that memory persists */
4018 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4019 sizeof(struct snd_kcontrol_new),
4021 if (!kcontrol_news) {
4022 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4024 goto outfree_w_param;
4026 return kcontrol_news;
4029 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4033 static struct snd_soc_dapm_widget *
4034 snd_soc_dapm_new_dai(struct snd_soc_card *card, struct snd_soc_pcm_runtime *rtd,
4035 struct snd_soc_dapm_widget *source,
4036 struct snd_soc_dapm_widget *sink)
4038 struct snd_soc_dapm_widget template;
4039 struct snd_soc_dapm_widget *w;
4040 const char **w_param_text;
4041 unsigned long private_value;
4045 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4046 source->name, sink->name);
4048 return ERR_PTR(-ENOMEM);
4050 memset(&template, 0, sizeof(template));
4051 template.reg = SND_SOC_NOPM;
4052 template.id = snd_soc_dapm_dai_link;
4053 template.name = link_name;
4054 template.event = snd_soc_dai_link_event;
4055 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4056 SND_SOC_DAPM_PRE_PMD;
4057 template.kcontrol_news = NULL;
4059 /* allocate memory for control, only in case of multiple configs */
4060 if (rtd->dai_link->num_params > 1) {
4061 w_param_text = devm_kcalloc(card->dev,
4062 rtd->dai_link->num_params,
4063 sizeof(char *), GFP_KERNEL);
4064 if (!w_param_text) {
4069 template.num_kcontrols = 1;
4070 template.kcontrol_news =
4071 snd_soc_dapm_alloc_kcontrol(card,
4073 rtd->dai_link->params,
4074 rtd->dai_link->num_params,
4075 w_param_text, &private_value);
4076 if (!template.kcontrol_news) {
4081 w_param_text = NULL;
4083 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4085 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4088 goto outfree_kcontrol_news;
4095 outfree_kcontrol_news:
4096 devm_kfree(card->dev, (void *)template.kcontrol_news);
4097 snd_soc_dapm_free_kcontrol(card, &private_value,
4098 rtd->dai_link->num_params, w_param_text);
4100 devm_kfree(card->dev, link_name);
4101 return ERR_PTR(ret);
4104 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4105 struct snd_soc_dai *dai)
4107 struct snd_soc_dapm_widget template;
4108 struct snd_soc_dapm_widget *w;
4110 WARN_ON(dapm->dev != dai->dev);
4112 memset(&template, 0, sizeof(template));
4113 template.reg = SND_SOC_NOPM;
4115 if (dai->driver->playback.stream_name) {
4116 template.id = snd_soc_dapm_dai_in;
4117 template.name = dai->driver->playback.stream_name;
4118 template.sname = dai->driver->playback.stream_name;
4120 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4123 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4128 dai->playback_widget = w;
4131 if (dai->driver->capture.stream_name) {
4132 template.id = snd_soc_dapm_dai_out;
4133 template.name = dai->driver->capture.stream_name;
4134 template.sname = dai->driver->capture.stream_name;
4136 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4139 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4144 dai->capture_widget = w;
4150 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4152 struct snd_soc_dapm_widget *dai_w, *w;
4153 struct snd_soc_dapm_widget *src, *sink;
4154 struct snd_soc_dai *dai;
4156 /* For each DAI widget... */
4157 list_for_each_entry(dai_w, &card->widgets, list) {
4158 switch (dai_w->id) {
4159 case snd_soc_dapm_dai_in:
4160 case snd_soc_dapm_dai_out:
4166 /* let users know there is no DAI to link */
4168 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4175 /* ...find all widgets with the same stream and link them */
4176 list_for_each_entry(w, &card->widgets, list) {
4177 if (w->dapm != dai_w->dapm)
4181 case snd_soc_dapm_dai_in:
4182 case snd_soc_dapm_dai_out:
4188 if (!w->sname || !strstr(w->sname, dai_w->sname))
4191 if (dai_w->id == snd_soc_dapm_dai_in) {
4198 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4199 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4206 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4207 struct snd_soc_pcm_runtime *rtd)
4209 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4210 struct snd_soc_dai *codec_dai;
4211 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4212 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
4215 if (rtd->dai_link->params) {
4216 playback_cpu = cpu_dai->capture_widget;
4217 capture_cpu = cpu_dai->playback_widget;
4219 playback = cpu_dai->playback_widget;
4220 capture = cpu_dai->capture_widget;
4221 playback_cpu = playback;
4222 capture_cpu = capture;
4225 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4227 /* connect BE DAI playback if widgets are valid */
4228 codec = codec_dai->playback_widget;
4230 if (playback_cpu && codec) {
4232 playback = snd_soc_dapm_new_dai(card, rtd,
4235 if (IS_ERR(playback)) {
4237 "ASoC: Failed to create DAI %s: %ld\n",
4243 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4244 playback, NULL, NULL);
4247 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4248 cpu_dai->component->name, playback_cpu->name,
4249 codec_dai->component->name, codec->name);
4251 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4256 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4257 /* connect BE DAI capture if widgets are valid */
4258 codec = codec_dai->capture_widget;
4260 if (codec && capture_cpu) {
4262 capture = snd_soc_dapm_new_dai(card, rtd,
4265 if (IS_ERR(capture)) {
4267 "ASoC: Failed to create DAI %s: %ld\n",
4273 snd_soc_dapm_add_path(&card->dapm, capture,
4274 capture_cpu, NULL, NULL);
4277 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4278 codec_dai->component->name, codec->name,
4279 cpu_dai->component->name, capture_cpu->name);
4281 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4287 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4290 struct snd_soc_dapm_widget *w;
4293 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4294 w = dai->playback_widget;
4296 w = dai->capture_widget;
4299 dapm_mark_dirty(w, "stream event");
4301 if (w->id == snd_soc_dapm_dai_in) {
4302 ep = SND_SOC_DAPM_EP_SOURCE;
4303 dapm_widget_invalidate_input_paths(w);
4305 ep = SND_SOC_DAPM_EP_SINK;
4306 dapm_widget_invalidate_output_paths(w);
4310 case SND_SOC_DAPM_STREAM_START:
4314 case SND_SOC_DAPM_STREAM_STOP:
4318 case SND_SOC_DAPM_STREAM_SUSPEND:
4319 case SND_SOC_DAPM_STREAM_RESUME:
4320 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4321 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4327 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4329 struct snd_soc_pcm_runtime *rtd;
4331 /* for each BE DAI link... */
4332 for_each_card_rtds(card, rtd) {
4334 * dynamic FE links have no fixed DAI mapping.
4335 * CODEC<->CODEC links have no direct connection.
4337 if (rtd->dai_link->dynamic)
4340 dapm_connect_dai_link_widgets(card, rtd);
4344 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4347 struct snd_soc_dai *codec_dai;
4350 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4351 for_each_rtd_codec_dai(rtd, i, codec_dai)
4352 soc_dapm_dai_stream_event(codec_dai, stream, event);
4354 dapm_power_widgets(rtd->card, event);
4358 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4359 * @rtd: PCM runtime data
4360 * @stream: stream name
4361 * @event: stream event
4363 * Sends a stream event to the dapm core. The core then makes any
4364 * necessary widget power changes.
4366 * Returns 0 for success else error.
4368 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4371 struct snd_soc_card *card = rtd->card;
4373 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4374 soc_dapm_stream_event(rtd, stream, event);
4375 mutex_unlock(&card->dapm_mutex);
4379 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4380 * @dapm: DAPM context
4383 * Enables input/output pin and its parents or children widgets iff there is
4384 * a valid audio route and active audio stream.
4386 * Requires external locking.
4388 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4389 * do any widget power switching.
4391 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4394 return snd_soc_dapm_set_pin(dapm, pin, 1);
4396 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4399 * snd_soc_dapm_enable_pin - enable pin.
4400 * @dapm: DAPM context
4403 * Enables input/output pin and its parents or children widgets iff there is
4404 * a valid audio route and active audio stream.
4406 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4407 * do any widget power switching.
4409 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4413 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4415 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4417 mutex_unlock(&dapm->card->dapm_mutex);
4421 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4424 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4425 * @dapm: DAPM context
4428 * Enables input/output pin regardless of any other state. This is
4429 * intended for use with microphone bias supplies used in microphone
4432 * Requires external locking.
4434 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4435 * do any widget power switching.
4437 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4440 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4443 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4447 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4448 if (!w->connected) {
4450 * w->force does not affect the number of input or output paths,
4451 * so we only have to recheck if w->connected is changed
4453 dapm_widget_invalidate_input_paths(w);
4454 dapm_widget_invalidate_output_paths(w);
4458 dapm_mark_dirty(w, "force enable");
4462 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4465 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4466 * @dapm: DAPM context
4469 * Enables input/output pin regardless of any other state. This is
4470 * intended for use with microphone bias supplies used in microphone
4473 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4474 * do any widget power switching.
4476 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4481 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4483 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4485 mutex_unlock(&dapm->card->dapm_mutex);
4489 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4492 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4493 * @dapm: DAPM context
4496 * Disables input/output pin and its parents or children widgets.
4498 * Requires external locking.
4500 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4501 * do any widget power switching.
4503 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4506 return snd_soc_dapm_set_pin(dapm, pin, 0);
4508 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4511 * snd_soc_dapm_disable_pin - disable pin.
4512 * @dapm: DAPM context
4515 * Disables input/output pin and its parents or children widgets.
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(struct snd_soc_dapm_context *dapm,
4525 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4527 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4529 mutex_unlock(&dapm->card->dapm_mutex);
4533 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4536 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4537 * @dapm: DAPM context
4540 * Marks the specified pin as being not connected, disabling it along
4541 * any parent or child widgets. At present this is identical to
4542 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4543 * additional things such as disabling controls which only affect
4544 * paths through the pin.
4546 * Requires external locking.
4548 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4549 * do any widget power switching.
4551 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4554 return snd_soc_dapm_set_pin(dapm, pin, 0);
4556 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4559 * snd_soc_dapm_nc_pin - permanently disable pin.
4560 * @dapm: DAPM context
4563 * Marks the specified pin as being not connected, disabling it along
4564 * any parent or child widgets. At present this is identical to
4565 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4566 * additional things such as disabling controls which only affect
4567 * paths through the pin.
4569 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4570 * do any widget power switching.
4572 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4576 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4578 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4580 mutex_unlock(&dapm->card->dapm_mutex);
4584 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4587 * snd_soc_dapm_get_pin_status - get audio pin status
4588 * @dapm: DAPM context
4589 * @pin: audio signal pin endpoint (or start point)
4591 * Get audio pin status - connected or disconnected.
4593 * Returns 1 for connected otherwise 0.
4595 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4598 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4601 return w->connected;
4605 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4608 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4609 * @dapm: DAPM context
4610 * @pin: audio signal pin endpoint (or start point)
4612 * Mark the given endpoint or pin as ignoring suspend. When the
4613 * system is disabled a path between two endpoints flagged as ignoring
4614 * suspend will not be disabled. The path must already be enabled via
4615 * normal means at suspend time, it will not be turned on if it was not
4618 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4621 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4624 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4628 w->ignore_suspend = 1;
4632 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4635 * snd_soc_dapm_free - free dapm resources
4636 * @dapm: DAPM context
4638 * Free all dapm widgets and resources.
4640 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4642 dapm_debugfs_cleanup(dapm);
4643 dapm_free_widgets(dapm);
4644 list_del(&dapm->list);
4646 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4648 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4650 struct snd_soc_card *card = dapm->card;
4651 struct snd_soc_dapm_widget *w;
4652 LIST_HEAD(down_list);
4655 mutex_lock(&card->dapm_mutex);
4657 list_for_each_entry(w, &dapm->card->widgets, list) {
4658 if (w->dapm != dapm)
4661 dapm_seq_insert(w, &down_list, false);
4667 /* If there were no widgets to power down we're already in
4671 if (dapm->bias_level == SND_SOC_BIAS_ON)
4672 snd_soc_dapm_set_bias_level(dapm,
4673 SND_SOC_BIAS_PREPARE);
4674 dapm_seq_run(card, &down_list, 0, false);
4675 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4676 snd_soc_dapm_set_bias_level(dapm,
4677 SND_SOC_BIAS_STANDBY);
4680 mutex_unlock(&card->dapm_mutex);
4684 * snd_soc_dapm_shutdown - callback for system shutdown
4686 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4688 struct snd_soc_dapm_context *dapm;
4690 list_for_each_entry(dapm, &card->dapm_list, list) {
4691 if (dapm != &card->dapm) {
4692 soc_dapm_shutdown_dapm(dapm);
4693 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4694 snd_soc_dapm_set_bias_level(dapm,
4699 soc_dapm_shutdown_dapm(&card->dapm);
4700 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4701 snd_soc_dapm_set_bias_level(&card->dapm,
4705 /* Module information */
4706 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4707 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4708 MODULE_LICENSE("GPL");