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 struct_size(new_wlist, widgets, n),
495 new_wlist->widgets[n - 1] = widget;
496 new_wlist->num_widgets = n;
498 data->wlist = new_wlist;
503 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
504 struct snd_soc_dapm_path *path)
506 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
508 list_add_tail(&path->list_kcontrol, &data->paths);
511 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
513 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
518 return data->widget->power;
521 static struct list_head *dapm_kcontrol_get_path_list(
522 const struct snd_kcontrol *kcontrol)
524 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
529 #define dapm_kcontrol_for_each_path(path, kcontrol) \
530 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
533 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
535 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
539 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
541 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
544 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
546 if (data->value == value)
550 data->widget->on_val = value;
558 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
560 * @kcontrol: The kcontrol
562 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
563 struct snd_kcontrol *kcontrol)
565 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
567 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
570 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
572 * @kcontrol: The kcontrol
574 * Note: This function must only be used on kcontrols that are known to have
575 * been registered for a CODEC. Otherwise the behaviour is undefined.
577 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
578 struct snd_kcontrol *kcontrol)
580 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
582 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
584 static void dapm_reset(struct snd_soc_card *card)
586 struct snd_soc_dapm_widget *w;
588 lockdep_assert_held(&card->dapm_mutex);
590 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
592 list_for_each_entry(w, &card->widgets, list) {
593 w->new_power = w->power;
594 w->power_checked = false;
598 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
600 if (!dapm->component)
602 return dapm->component->name_prefix;
605 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
608 if (!dapm->component)
610 return snd_soc_component_read(dapm->component, reg, value);
613 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
614 int reg, unsigned int mask, unsigned int value)
616 if (!dapm->component)
618 return snd_soc_component_update_bits(dapm->component, reg,
622 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
623 int reg, unsigned int mask, unsigned int value)
625 if (!dapm->component)
627 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
630 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
633 snd_soc_component_async_complete(dapm->component);
636 static struct snd_soc_dapm_widget *
637 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
639 struct snd_soc_dapm_widget *w = wcache->widget;
640 struct list_head *wlist;
645 wlist = &w->dapm->card->widgets;
647 list_for_each_entry_from(w, wlist, list) {
648 if (!strcmp(name, w->name))
659 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
660 struct snd_soc_dapm_widget *w)
666 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
667 * @dapm: The DAPM context for which to set the level
668 * @level: The level to set
670 * Forces the DAPM bias level to a specific state. It will call the bias level
671 * callback of DAPM context with the specified level. This will even happen if
672 * the context is already at the same level. Furthermore it will not go through
673 * the normal bias level sequencing, meaning any intermediate states between the
674 * current and the target state will not be entered.
676 * Note that the change in bias level is only temporary and the next time
677 * snd_soc_dapm_sync() is called the state will be set to the level as
678 * determined by the DAPM core. The function is mainly intended to be used to
679 * used during probe or resume from suspend to power up the device so
680 * initialization can be done, before the DAPM core takes over.
682 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
683 enum snd_soc_bias_level level)
688 ret = snd_soc_component_set_bias_level(dapm->component, level);
691 dapm->bias_level = level;
695 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
698 * snd_soc_dapm_set_bias_level - set the bias level for the system
699 * @dapm: DAPM context
700 * @level: level to configure
702 * Configure the bias (power) levels for the SoC audio device.
704 * Returns 0 for success else error.
706 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
707 enum snd_soc_bias_level level)
709 struct snd_soc_card *card = dapm->card;
712 trace_snd_soc_bias_level_start(card, level);
714 if (card && card->set_bias_level)
715 ret = card->set_bias_level(card, dapm, level);
719 if (!card || dapm != &card->dapm)
720 ret = snd_soc_dapm_force_bias_level(dapm, level);
725 if (card && card->set_bias_level_post)
726 ret = card->set_bias_level_post(card, dapm, level);
728 trace_snd_soc_bias_level_done(card, level);
733 /* connect mux widget to its interconnecting audio paths */
734 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
735 struct snd_soc_dapm_path *path, const char *control_name,
736 struct snd_soc_dapm_widget *w)
738 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
739 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
740 unsigned int val, item;
743 if (e->reg != SND_SOC_NOPM) {
744 soc_dapm_read(dapm, e->reg, &val);
745 val = (val >> e->shift_l) & e->mask;
746 item = snd_soc_enum_val_to_item(e, val);
748 /* since a virtual mux has no backing registers to
749 * decide which path to connect, it will try to match
750 * with the first enumeration. This is to ensure
751 * that the default mux choice (the first) will be
752 * correctly powered up during initialization.
757 i = match_string(e->texts, e->items, control_name);
761 path->name = e->texts[i];
762 path->connect = (i == item);
767 /* set up initial codec paths */
768 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
771 struct soc_mixer_control *mc = (struct soc_mixer_control *)
772 p->sink->kcontrol_news[i].private_value;
773 unsigned int reg = mc->reg;
774 unsigned int shift = mc->shift;
775 unsigned int max = mc->max;
776 unsigned int mask = (1 << fls(max)) - 1;
777 unsigned int invert = mc->invert;
780 if (reg != SND_SOC_NOPM) {
781 soc_dapm_read(p->sink->dapm, reg, &val);
783 * The nth_path argument allows this function to know
784 * which path of a kcontrol it is setting the initial
785 * status for. Ideally this would support any number
786 * of paths and channels. But since kcontrols only come
787 * in mono and stereo variants, we are limited to 2
790 * The following code assumes for stereo controls the
791 * first path is the left channel, and all remaining
792 * paths are the right channel.
794 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
796 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
797 val = (val >> mc->rshift) & mask;
799 val = (val >> shift) & mask;
809 /* connect mixer widget to its interconnecting audio paths */
810 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
811 struct snd_soc_dapm_path *path, const char *control_name)
815 /* search for mixer kcontrol */
816 for (i = 0; i < path->sink->num_kcontrols; i++) {
817 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
818 path->name = path->sink->kcontrol_news[i].name;
819 dapm_set_mixer_path_status(path, i, nth_path++);
826 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
827 struct snd_soc_dapm_widget *kcontrolw,
828 const struct snd_kcontrol_new *kcontrol_new,
829 struct snd_kcontrol **kcontrol)
831 struct snd_soc_dapm_widget *w;
836 list_for_each_entry(w, &dapm->card->widgets, list) {
837 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
839 for (i = 0; i < w->num_kcontrols; i++) {
840 if (&w->kcontrol_news[i] == kcontrol_new) {
842 *kcontrol = w->kcontrols[i];
852 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
853 * create it. Either way, add the widget into the control's widget list
855 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
858 struct snd_soc_dapm_context *dapm = w->dapm;
859 struct snd_card *card = dapm->card->snd_card;
863 struct snd_kcontrol *kcontrol;
864 bool wname_in_long_name, kcname_in_long_name;
865 char *long_name = NULL;
869 prefix = soc_dapm_prefix(dapm);
871 prefix_len = strlen(prefix) + 1;
875 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
880 wname_in_long_name = false;
881 kcname_in_long_name = true;
884 case snd_soc_dapm_switch:
885 case snd_soc_dapm_mixer:
886 case snd_soc_dapm_pga:
887 case snd_soc_dapm_effect:
888 case snd_soc_dapm_out_drv:
889 wname_in_long_name = true;
890 kcname_in_long_name = true;
892 case snd_soc_dapm_mixer_named_ctl:
893 wname_in_long_name = false;
894 kcname_in_long_name = true;
896 case snd_soc_dapm_demux:
897 case snd_soc_dapm_mux:
898 wname_in_long_name = true;
899 kcname_in_long_name = false;
906 if (wname_in_long_name && kcname_in_long_name) {
908 * The control will get a prefix from the control
909 * creation process but we're also using the same
910 * prefix for widgets so cut the prefix off the
911 * front of the widget name.
913 long_name = kasprintf(GFP_KERNEL, "%s %s",
914 w->name + prefix_len,
915 w->kcontrol_news[kci].name);
916 if (long_name == NULL)
920 } else if (wname_in_long_name) {
922 name = w->name + prefix_len;
925 name = w->kcontrol_news[kci].name;
928 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
935 kcontrol->private_free = dapm_kcontrol_free;
937 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
939 snd_ctl_free_one(kcontrol);
943 ret = snd_ctl_add(card, kcontrol);
946 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
952 ret = dapm_kcontrol_add_widget(kcontrol, w);
954 w->kcontrols[kci] = kcontrol;
962 /* create new dapm mixer control */
963 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
966 struct snd_soc_dapm_path *path;
967 struct dapm_kcontrol_data *data;
970 for (i = 0; i < w->num_kcontrols; i++) {
972 snd_soc_dapm_widget_for_each_source_path(w, path) {
973 /* mixer/mux paths name must match control name */
974 if (path->name != (char *)w->kcontrol_news[i].name)
977 if (!w->kcontrols[i]) {
978 ret = dapm_create_or_share_kcontrol(w, i);
983 dapm_kcontrol_add_path(w->kcontrols[i], path);
985 data = snd_kcontrol_chip(w->kcontrols[i]);
987 snd_soc_dapm_add_path(data->widget->dapm,
997 /* create new dapm mux control */
998 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1000 struct snd_soc_dapm_context *dapm = w->dapm;
1001 enum snd_soc_dapm_direction dir;
1002 struct snd_soc_dapm_path *path;
1007 case snd_soc_dapm_mux:
1008 dir = SND_SOC_DAPM_DIR_OUT;
1011 case snd_soc_dapm_demux:
1012 dir = SND_SOC_DAPM_DIR_IN;
1019 if (w->num_kcontrols != 1) {
1021 "ASoC: %s %s has incorrect number of controls\n", type,
1026 if (list_empty(&w->edges[dir])) {
1027 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1031 ret = dapm_create_or_share_kcontrol(w, 0);
1035 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1037 dapm_kcontrol_add_path(w->kcontrols[0], path);
1043 /* create new dapm volume control */
1044 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1048 for (i = 0; i < w->num_kcontrols; i++) {
1049 ret = dapm_create_or_share_kcontrol(w, i);
1057 /* create new dapm dai link control */
1058 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1061 struct snd_kcontrol *kcontrol;
1062 struct snd_soc_dapm_context *dapm = w->dapm;
1063 struct snd_card *card = dapm->card->snd_card;
1064 struct snd_soc_pcm_runtime *rtd = w->priv;
1066 /* create control for links with > 1 config */
1067 if (rtd->dai_link->num_params <= 1)
1071 for (i = 0; i < w->num_kcontrols; i++) {
1072 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1074 ret = snd_ctl_add(card, kcontrol);
1077 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1078 w->name, w->kcontrol_news[i].name, ret);
1081 kcontrol->private_data = w;
1082 w->kcontrols[i] = kcontrol;
1088 /* We implement power down on suspend by checking the power state of
1089 * the ALSA card - when we are suspending the ALSA state for the card
1092 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1094 int level = snd_power_get_state(widget->dapm->card->snd_card);
1097 case SNDRV_CTL_POWER_D3hot:
1098 case SNDRV_CTL_POWER_D3cold:
1099 if (widget->ignore_suspend)
1100 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1102 return widget->ignore_suspend;
1108 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1113 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1114 struct list_head *widgets)
1116 struct snd_soc_dapm_widget *w;
1117 struct list_head *it;
1118 unsigned int size = 0;
1121 list_for_each(it, widgets)
1124 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1128 list_for_each_entry(w, widgets, work_list)
1129 (*list)->widgets[i++] = w;
1131 (*list)->num_widgets = i;
1137 * Recursively reset the cached number of inputs or outputs for the specified
1138 * widget and all widgets that can be reached via incoming or outcoming paths
1141 static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1142 enum snd_soc_dapm_direction dir)
1144 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1145 struct snd_soc_dapm_path *path;
1147 widget->endpoints[dir] = -1;
1149 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1150 if (path->weak || path->is_supply)
1156 if (path->connect) {
1158 invalidate_paths_ep(path->node[dir], dir);
1165 * Common implementation for is_connected_output_ep() and
1166 * is_connected_input_ep(). The function is inlined since the combined size of
1167 * the two specialized functions is only marginally larger then the size of the
1168 * generic function and at the same time the fast path of the specialized
1169 * functions is significantly smaller than the generic function.
1171 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1172 struct list_head *list, enum snd_soc_dapm_direction dir,
1173 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1174 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1175 enum snd_soc_dapm_direction)),
1176 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1177 enum snd_soc_dapm_direction))
1179 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1180 struct snd_soc_dapm_path *path;
1183 if (widget->endpoints[dir] >= 0)
1184 return widget->endpoints[dir];
1186 DAPM_UPDATE_STAT(widget, path_checks);
1188 /* do we need to add this widget to the list ? */
1190 list_add_tail(&widget->work_list, list);
1192 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1194 custom_stop_condition = NULL;
1197 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1198 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1199 return widget->endpoints[dir];
1202 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1203 DAPM_UPDATE_STAT(widget, neighbour_checks);
1205 if (path->weak || path->is_supply)
1211 trace_snd_soc_dapm_path(widget, dir, path);
1213 if (path->connect) {
1215 con += fn(path->node[dir], list, custom_stop_condition);
1220 widget->endpoints[dir] = con;
1226 * Recursively check for a completed path to an active or physically connected
1227 * output widget. Returns number of complete paths.
1229 * Optionally, can be supplied with a function acting as a stopping condition.
1230 * This function takes the dapm widget currently being examined and the walk
1231 * direction as an arguments, it should return true if widgets from that point
1232 * in the graph onwards should not be added to the widget list.
1234 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1235 struct list_head *list,
1236 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1237 enum snd_soc_dapm_direction))
1239 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1240 is_connected_output_ep, custom_stop_condition);
1244 * Recursively check for a completed path to an active or physically connected
1245 * input widget. Returns number of complete paths.
1247 * Optionally, can be supplied with a function acting as a stopping condition.
1248 * This function takes the dapm widget currently being examined and the walk
1249 * direction as an arguments, it should return true if the walk should be
1250 * stopped and false otherwise.
1252 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1253 struct list_head *list,
1254 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1255 enum snd_soc_dapm_direction))
1257 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1258 is_connected_input_ep, custom_stop_condition);
1262 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1263 * @dai: the soc DAI.
1264 * @stream: stream direction.
1265 * @list: list of active widgets for this stream.
1266 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1267 * walk based on custom logic.
1269 * Queries DAPM graph as to whether a valid audio stream path exists for
1270 * the initial stream specified by name. This takes into account
1271 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1273 * Optionally, can be supplied with a function acting as a stopping condition.
1274 * This function takes the dapm widget currently being examined and the walk
1275 * direction as an arguments, it should return true if the walk should be
1276 * stopped and false otherwise.
1278 * Returns the number of valid paths or negative error.
1280 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1281 struct snd_soc_dapm_widget_list **list,
1282 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1283 enum snd_soc_dapm_direction))
1285 struct snd_soc_card *card = dai->component->card;
1286 struct snd_soc_dapm_widget *w;
1291 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1293 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1294 w = dai->playback_widget;
1295 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1296 paths = is_connected_output_ep(w, &widgets,
1297 custom_stop_condition);
1299 w = dai->capture_widget;
1300 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1301 paths = is_connected_input_ep(w, &widgets,
1302 custom_stop_condition);
1305 /* Drop starting point */
1306 list_del(widgets.next);
1308 ret = dapm_widget_list_create(list, &widgets);
1312 trace_snd_soc_dapm_connected(paths, stream);
1313 mutex_unlock(&card->dapm_mutex);
1318 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1320 dapm_widget_list_free(list);
1324 * Handler for regulator supply widget.
1326 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1327 struct snd_kcontrol *kcontrol, int event)
1331 soc_dapm_async_complete(w->dapm);
1333 if (SND_SOC_DAPM_EVENT_ON(event)) {
1334 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1335 ret = regulator_allow_bypass(w->regulator, false);
1337 dev_warn(w->dapm->dev,
1338 "ASoC: Failed to unbypass %s: %d\n",
1342 return regulator_enable(w->regulator);
1344 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1345 ret = regulator_allow_bypass(w->regulator, true);
1347 dev_warn(w->dapm->dev,
1348 "ASoC: Failed to bypass %s: %d\n",
1352 return regulator_disable_deferred(w->regulator, w->shift);
1355 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1358 * Handler for pinctrl widget.
1360 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1361 struct snd_kcontrol *kcontrol, int event)
1363 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1364 struct pinctrl *p = w->pinctrl;
1365 struct pinctrl_state *s;
1370 if (SND_SOC_DAPM_EVENT_ON(event))
1371 s = pinctrl_lookup_state(p, priv->active_state);
1373 s = pinctrl_lookup_state(p, priv->sleep_state);
1378 return pinctrl_select_state(p, s);
1380 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1383 * Handler for clock supply widget.
1385 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1386 struct snd_kcontrol *kcontrol, int event)
1391 soc_dapm_async_complete(w->dapm);
1393 if (SND_SOC_DAPM_EVENT_ON(event)) {
1394 return clk_prepare_enable(w->clk);
1396 clk_disable_unprepare(w->clk);
1402 EXPORT_SYMBOL_GPL(dapm_clock_event);
1404 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1406 if (w->power_checked)
1407 return w->new_power;
1412 w->new_power = w->power_check(w);
1414 w->power_checked = true;
1416 return w->new_power;
1419 /* Generic check to see if a widget should be powered. */
1420 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1424 DAPM_UPDATE_STAT(w, power_checks);
1426 in = is_connected_input_ep(w, NULL, NULL);
1427 out = is_connected_output_ep(w, NULL, NULL);
1428 return out != 0 && in != 0;
1431 /* Check to see if a power supply is needed */
1432 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1434 struct snd_soc_dapm_path *path;
1436 DAPM_UPDATE_STAT(w, power_checks);
1438 /* Check if one of our outputs is connected */
1439 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1440 DAPM_UPDATE_STAT(w, neighbour_checks);
1445 if (path->connected &&
1446 !path->connected(path->source, path->sink))
1449 if (dapm_widget_power_check(path->sink))
1456 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1458 return w->connected;
1461 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1462 struct snd_soc_dapm_widget *b,
1467 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1468 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1473 sort = dapm_down_seq;
1475 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1476 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1478 if (sort[a->id] != sort[b->id])
1479 return sort[a->id] - sort[b->id];
1480 if (a->subseq != b->subseq) {
1482 return a->subseq - b->subseq;
1484 return b->subseq - a->subseq;
1486 if (a->reg != b->reg)
1487 return a->reg - b->reg;
1488 if (a->dapm != b->dapm)
1489 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1494 /* Insert a widget in order into a DAPM power sequence. */
1495 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1496 struct list_head *list,
1499 struct snd_soc_dapm_widget *w;
1501 list_for_each_entry(w, list, power_list)
1502 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1503 list_add_tail(&new_widget->power_list, &w->power_list);
1507 list_add_tail(&new_widget->power_list, list);
1510 static void dapm_seq_check_event(struct snd_soc_card *card,
1511 struct snd_soc_dapm_widget *w, int event)
1513 const char *ev_name;
1517 case SND_SOC_DAPM_PRE_PMU:
1518 ev_name = "PRE_PMU";
1521 case SND_SOC_DAPM_POST_PMU:
1522 ev_name = "POST_PMU";
1525 case SND_SOC_DAPM_PRE_PMD:
1526 ev_name = "PRE_PMD";
1529 case SND_SOC_DAPM_POST_PMD:
1530 ev_name = "POST_PMD";
1533 case SND_SOC_DAPM_WILL_PMU:
1534 ev_name = "WILL_PMU";
1537 case SND_SOC_DAPM_WILL_PMD:
1538 ev_name = "WILL_PMD";
1542 WARN(1, "Unknown event %d\n", event);
1546 if (w->new_power != power)
1549 if (w->event && (w->event_flags & event)) {
1550 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1552 soc_dapm_async_complete(w->dapm);
1553 trace_snd_soc_dapm_widget_event_start(w, event);
1554 ret = w->event(w, NULL, event);
1555 trace_snd_soc_dapm_widget_event_done(w, event);
1557 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1558 ev_name, w->name, ret);
1562 /* Apply the coalesced changes from a DAPM sequence */
1563 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1564 struct list_head *pending)
1566 struct snd_soc_dapm_context *dapm;
1567 struct snd_soc_dapm_widget *w;
1569 unsigned int value = 0;
1570 unsigned int mask = 0;
1572 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1576 list_for_each_entry(w, pending, power_list) {
1577 WARN_ON(reg != w->reg || dapm != w->dapm);
1578 w->power = w->new_power;
1580 mask |= w->mask << w->shift;
1582 value |= w->on_val << w->shift;
1584 value |= w->off_val << w->shift;
1586 pop_dbg(dapm->dev, card->pop_time,
1587 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1588 w->name, reg, value, mask);
1590 /* Check for events */
1591 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1592 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1596 /* Any widget will do, they should all be updating the
1600 pop_dbg(dapm->dev, card->pop_time,
1601 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1602 value, mask, reg, card->pop_time);
1603 pop_wait(card->pop_time);
1604 soc_dapm_update_bits(dapm, reg, mask, value);
1607 list_for_each_entry(w, pending, power_list) {
1608 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1609 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1613 /* Apply a DAPM power sequence.
1615 * We walk over a pre-sorted list of widgets to apply power to. In
1616 * order to minimise the number of writes to the device required
1617 * multiple widgets will be updated in a single write where possible.
1618 * Currently anything that requires more than a single write is not
1621 static void dapm_seq_run(struct snd_soc_card *card,
1622 struct list_head *list, int event, bool power_up)
1624 struct snd_soc_dapm_widget *w, *n;
1625 struct snd_soc_dapm_context *d;
1628 int cur_subseq = -1;
1629 int cur_reg = SND_SOC_NOPM;
1630 struct snd_soc_dapm_context *cur_dapm = NULL;
1637 sort = dapm_down_seq;
1639 list_for_each_entry_safe(w, n, list, power_list) {
1642 /* Do we need to apply any queued changes? */
1643 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1644 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1645 if (!list_empty(&pending))
1646 dapm_seq_run_coalesced(card, &pending);
1648 if (cur_dapm && cur_dapm->component) {
1649 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1650 if (sort[i] == cur_sort)
1651 snd_soc_component_seq_notifier(
1652 cur_dapm->component,
1656 if (cur_dapm && w->dapm != cur_dapm)
1657 soc_dapm_async_complete(cur_dapm);
1659 INIT_LIST_HEAD(&pending);
1661 cur_subseq = INT_MIN;
1662 cur_reg = SND_SOC_NOPM;
1667 case snd_soc_dapm_pre:
1669 list_for_each_entry_safe_continue(w, n, list,
1672 if (event == SND_SOC_DAPM_STREAM_START)
1674 NULL, SND_SOC_DAPM_PRE_PMU);
1675 else if (event == SND_SOC_DAPM_STREAM_STOP)
1677 NULL, SND_SOC_DAPM_PRE_PMD);
1680 case snd_soc_dapm_post:
1682 list_for_each_entry_safe_continue(w, n, list,
1685 if (event == SND_SOC_DAPM_STREAM_START)
1687 NULL, SND_SOC_DAPM_POST_PMU);
1688 else if (event == SND_SOC_DAPM_STREAM_STOP)
1690 NULL, SND_SOC_DAPM_POST_PMD);
1694 /* Queue it up for application */
1695 cur_sort = sort[w->id];
1696 cur_subseq = w->subseq;
1699 list_move(&w->power_list, &pending);
1704 dev_err(w->dapm->dev,
1705 "ASoC: Failed to apply widget power: %d\n", ret);
1708 if (!list_empty(&pending))
1709 dapm_seq_run_coalesced(card, &pending);
1711 if (cur_dapm && cur_dapm->component) {
1712 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1713 if (sort[i] == cur_sort)
1714 snd_soc_component_seq_notifier(
1715 cur_dapm->component,
1719 list_for_each_entry(d, &card->dapm_list, list) {
1720 soc_dapm_async_complete(d);
1724 static void dapm_widget_update(struct snd_soc_card *card)
1726 struct snd_soc_dapm_update *update = card->update;
1727 struct snd_soc_dapm_widget_list *wlist;
1728 struct snd_soc_dapm_widget *w = NULL;
1732 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1735 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1737 for_each_dapm_widgets(wlist, wi, w) {
1738 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1739 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1741 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1749 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1752 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1755 if (update->has_second_set) {
1756 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1757 update->mask2, update->val2);
1759 dev_err(w->dapm->dev,
1760 "ASoC: %s DAPM update failed: %d\n",
1764 for_each_dapm_widgets(wlist, wi, w) {
1765 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1766 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1768 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1774 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1775 * they're changing state.
1777 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1779 struct snd_soc_dapm_context *d = data;
1782 /* If we're off and we're not supposed to go into STANDBY */
1783 if (d->bias_level == SND_SOC_BIAS_OFF &&
1784 d->target_bias_level != SND_SOC_BIAS_OFF) {
1786 pm_runtime_get_sync(d->dev);
1788 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1791 "ASoC: Failed to turn on bias: %d\n", ret);
1794 /* Prepare for a transition to ON or away from ON */
1795 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1796 d->bias_level != SND_SOC_BIAS_ON) ||
1797 (d->target_bias_level != SND_SOC_BIAS_ON &&
1798 d->bias_level == SND_SOC_BIAS_ON)) {
1799 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1802 "ASoC: Failed to prepare bias: %d\n", ret);
1806 /* Async callback run prior to DAPM sequences - brings to their final
1809 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1811 struct snd_soc_dapm_context *d = data;
1814 /* If we just powered the last thing off drop to standby bias */
1815 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1816 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1817 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1818 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1820 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1824 /* If we're in standby and can support bias off then do that */
1825 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1826 d->target_bias_level == SND_SOC_BIAS_OFF) {
1827 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1829 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1833 pm_runtime_put(d->dev);
1836 /* If we just powered up then move to active bias */
1837 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1838 d->target_bias_level == SND_SOC_BIAS_ON) {
1839 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1841 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1846 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1847 bool power, bool connect)
1849 /* If a connection is being made or broken then that update
1850 * will have marked the peer dirty, otherwise the widgets are
1851 * not connected and this update has no impact. */
1855 /* If the peer is already in the state we're moving to then we
1856 * won't have an impact on it. */
1857 if (power != peer->power)
1858 dapm_mark_dirty(peer, "peer state change");
1861 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1862 struct list_head *up_list,
1863 struct list_head *down_list)
1865 struct snd_soc_dapm_path *path;
1867 if (w->power == power)
1870 trace_snd_soc_dapm_widget_power(w, power);
1872 /* If we changed our power state perhaps our neigbours changed
1875 snd_soc_dapm_widget_for_each_source_path(w, path)
1876 dapm_widget_set_peer_power(path->source, power, path->connect);
1878 /* Supplies can't affect their outputs, only their inputs */
1879 if (!w->is_supply) {
1880 snd_soc_dapm_widget_for_each_sink_path(w, path)
1881 dapm_widget_set_peer_power(path->sink, power,
1886 dapm_seq_insert(w, up_list, true);
1888 dapm_seq_insert(w, down_list, false);
1891 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1892 struct list_head *up_list,
1893 struct list_head *down_list)
1898 case snd_soc_dapm_pre:
1899 dapm_seq_insert(w, down_list, false);
1901 case snd_soc_dapm_post:
1902 dapm_seq_insert(w, up_list, true);
1906 power = dapm_widget_power_check(w);
1908 dapm_widget_set_power(w, power, up_list, down_list);
1913 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1915 if (dapm->idle_bias_off)
1918 switch (snd_power_get_state(dapm->card->snd_card)) {
1919 case SNDRV_CTL_POWER_D3hot:
1920 case SNDRV_CTL_POWER_D3cold:
1921 return dapm->suspend_bias_off;
1930 * Scan each dapm widget for complete audio path.
1931 * A complete path is a route that has valid endpoints i.e.:-
1933 * o DAC to output pin.
1934 * o Input pin to ADC.
1935 * o Input pin to Output pin (bypass, sidetone)
1936 * o DAC to ADC (loopback).
1938 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1940 struct snd_soc_dapm_widget *w;
1941 struct snd_soc_dapm_context *d;
1943 LIST_HEAD(down_list);
1944 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1945 enum snd_soc_bias_level bias;
1948 lockdep_assert_held(&card->dapm_mutex);
1950 trace_snd_soc_dapm_start(card);
1952 list_for_each_entry(d, &card->dapm_list, list) {
1953 if (dapm_idle_bias_off(d))
1954 d->target_bias_level = SND_SOC_BIAS_OFF;
1956 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1961 /* Check which widgets we need to power and store them in
1962 * lists indicating if they should be powered up or down. We
1963 * only check widgets that have been flagged as dirty but note
1964 * that new widgets may be added to the dirty list while we
1967 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1968 dapm_power_one_widget(w, &up_list, &down_list);
1971 list_for_each_entry(w, &card->widgets, list) {
1973 case snd_soc_dapm_pre:
1974 case snd_soc_dapm_post:
1975 /* These widgets always need to be powered */
1978 list_del_init(&w->dirty);
1985 /* Supplies and micbiases only bring the
1986 * context up to STANDBY as unless something
1987 * else is active and passing audio they
1988 * generally don't require full power. Signal
1989 * generators are virtual pins and have no
1990 * power impact themselves.
1993 case snd_soc_dapm_siggen:
1994 case snd_soc_dapm_vmid:
1996 case snd_soc_dapm_supply:
1997 case snd_soc_dapm_regulator_supply:
1998 case snd_soc_dapm_pinctrl:
1999 case snd_soc_dapm_clock_supply:
2000 case snd_soc_dapm_micbias:
2001 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2002 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2005 d->target_bias_level = SND_SOC_BIAS_ON;
2012 /* Force all contexts in the card to the same bias state if
2013 * they're not ground referenced.
2015 bias = SND_SOC_BIAS_OFF;
2016 list_for_each_entry(d, &card->dapm_list, list)
2017 if (d->target_bias_level > bias)
2018 bias = d->target_bias_level;
2019 list_for_each_entry(d, &card->dapm_list, list)
2020 if (!dapm_idle_bias_off(d))
2021 d->target_bias_level = bias;
2023 trace_snd_soc_dapm_walk_done(card);
2025 /* Run card bias changes at first */
2026 dapm_pre_sequence_async(&card->dapm, 0);
2027 /* Run other bias changes in parallel */
2028 list_for_each_entry(d, &card->dapm_list, list) {
2029 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2030 async_schedule_domain(dapm_pre_sequence_async, d,
2033 async_synchronize_full_domain(&async_domain);
2035 list_for_each_entry(w, &down_list, power_list) {
2036 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2039 list_for_each_entry(w, &up_list, power_list) {
2040 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2043 /* Power down widgets first; try to avoid amplifying pops. */
2044 dapm_seq_run(card, &down_list, event, false);
2046 dapm_widget_update(card);
2049 dapm_seq_run(card, &up_list, event, true);
2051 /* Run all the bias changes in parallel */
2052 list_for_each_entry(d, &card->dapm_list, list) {
2053 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2054 async_schedule_domain(dapm_post_sequence_async, d,
2057 async_synchronize_full_domain(&async_domain);
2058 /* Run card bias changes at last */
2059 dapm_post_sequence_async(&card->dapm, 0);
2061 /* do we need to notify any clients that DAPM event is complete */
2062 list_for_each_entry(d, &card->dapm_list, list) {
2066 ret = snd_soc_component_stream_event(d->component, event);
2071 pop_dbg(card->dev, card->pop_time,
2072 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2073 pop_wait(card->pop_time);
2075 trace_snd_soc_dapm_done(card);
2080 #ifdef CONFIG_DEBUG_FS
2081 static ssize_t dapm_widget_power_read_file(struct file *file,
2082 char __user *user_buf,
2083 size_t count, loff_t *ppos)
2085 struct snd_soc_dapm_widget *w = file->private_data;
2086 struct snd_soc_card *card = w->dapm->card;
2087 enum snd_soc_dapm_direction dir, rdir;
2091 struct snd_soc_dapm_path *p = NULL;
2093 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2097 mutex_lock(&card->dapm_mutex);
2099 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2104 in = is_connected_input_ep(w, NULL, NULL);
2105 out = is_connected_output_ep(w, NULL, NULL);
2108 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2109 w->name, w->power ? "On" : "Off",
2110 w->force ? " (forced)" : "", in, out);
2113 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2114 " - R%d(0x%x) mask 0x%x",
2115 w->reg, w->reg, w->mask << w->shift);
2117 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2120 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2122 w->active ? "active" : "inactive");
2124 snd_soc_dapm_for_each_direction(dir) {
2125 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2126 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2127 if (p->connected && !p->connected(p->source, p->sink))
2133 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2134 " %s \"%s\" \"%s\"\n",
2135 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2136 p->name ? p->name : "static",
2137 p->node[rdir]->name);
2141 mutex_unlock(&card->dapm_mutex);
2143 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2149 static const struct file_operations dapm_widget_power_fops = {
2150 .open = simple_open,
2151 .read = dapm_widget_power_read_file,
2152 .llseek = default_llseek,
2155 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2156 size_t count, loff_t *ppos)
2158 struct snd_soc_dapm_context *dapm = file->private_data;
2161 switch (dapm->bias_level) {
2162 case SND_SOC_BIAS_ON:
2165 case SND_SOC_BIAS_PREPARE:
2166 level = "Prepare\n";
2168 case SND_SOC_BIAS_STANDBY:
2169 level = "Standby\n";
2171 case SND_SOC_BIAS_OFF:
2175 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2176 level = "Unknown\n";
2180 return simple_read_from_buffer(user_buf, count, ppos, level,
2184 static const struct file_operations dapm_bias_fops = {
2185 .open = simple_open,
2186 .read = dapm_bias_read_file,
2187 .llseek = default_llseek,
2190 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2191 struct dentry *parent)
2193 if (!parent || IS_ERR(parent))
2196 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2198 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2202 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2204 struct snd_soc_dapm_context *dapm = w->dapm;
2206 if (!dapm->debugfs_dapm || !w->name)
2209 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2210 &dapm_widget_power_fops);
2213 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2215 debugfs_remove_recursive(dapm->debugfs_dapm);
2216 dapm->debugfs_dapm = NULL;
2220 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2221 struct dentry *parent)
2225 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2229 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2236 * soc_dapm_connect_path() - Connects or disconnects a path
2237 * @path: The path to update
2238 * @connect: The new connect state of the path. True if the path is connected,
2239 * false if it is disconnected.
2240 * @reason: The reason why the path changed (for debugging only)
2242 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2243 bool connect, const char *reason)
2245 if (path->connect == connect)
2248 path->connect = connect;
2249 dapm_mark_dirty(path->source, reason);
2250 dapm_mark_dirty(path->sink, reason);
2251 dapm_path_invalidate(path);
2254 /* test and update the power status of a mux widget */
2255 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2256 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2258 struct snd_soc_dapm_path *path;
2262 lockdep_assert_held(&card->dapm_mutex);
2264 /* find dapm widget path assoc with kcontrol */
2265 dapm_kcontrol_for_each_path(path, kcontrol) {
2267 /* we now need to match the string in the enum to the path */
2268 if (e && !(strcmp(path->name, e->texts[mux])))
2273 soc_dapm_connect_path(path, connect, "mux update");
2277 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2282 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2283 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2284 struct snd_soc_dapm_update *update)
2286 struct snd_soc_card *card = dapm->card;
2289 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2290 card->update = update;
2291 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2292 card->update = NULL;
2293 mutex_unlock(&card->dapm_mutex);
2295 soc_dpcm_runtime_update(card);
2298 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2300 /* test and update the power status of a mixer or switch widget */
2301 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2302 struct snd_kcontrol *kcontrol,
2303 int connect, int rconnect)
2305 struct snd_soc_dapm_path *path;
2308 lockdep_assert_held(&card->dapm_mutex);
2310 /* find dapm widget path assoc with kcontrol */
2311 dapm_kcontrol_for_each_path(path, kcontrol) {
2313 * Ideally this function should support any number of
2314 * paths and channels. But since kcontrols only come
2315 * in mono and stereo variants, we are limited to 2
2318 * The following code assumes for stereo controls the
2319 * first path (when 'found == 0') is the left channel,
2320 * and all remaining paths (when 'found == 1') are the
2323 * A stereo control is signified by a valid 'rconnect'
2324 * value, either 0 for unconnected, or >= 0 for connected.
2325 * This is chosen instead of using snd_soc_volsw_is_stereo,
2326 * so that the behavior of snd_soc_dapm_mixer_update_power
2327 * doesn't change even when the kcontrol passed in is
2330 * It passes 'connect' as the path connect status for
2331 * the left channel, and 'rconnect' for the right
2334 if (found && rconnect >= 0)
2335 soc_dapm_connect_path(path, rconnect, "mixer update");
2337 soc_dapm_connect_path(path, connect, "mixer update");
2342 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2347 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2348 struct snd_kcontrol *kcontrol, int connect,
2349 struct snd_soc_dapm_update *update)
2351 struct snd_soc_card *card = dapm->card;
2354 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2355 card->update = update;
2356 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2357 card->update = NULL;
2358 mutex_unlock(&card->dapm_mutex);
2360 soc_dpcm_runtime_update(card);
2363 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2365 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2368 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2369 struct snd_soc_dapm_widget *w;
2371 char *state = "not set";
2373 /* card won't be set for the dummy component, as a spot fix
2374 * we're checking for that case specifically here but in future
2375 * we will ensure that the dummy component looks like others.
2380 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2381 if (w->dapm != dapm)
2384 /* only display widgets that burn power */
2386 case snd_soc_dapm_hp:
2387 case snd_soc_dapm_mic:
2388 case snd_soc_dapm_spk:
2389 case snd_soc_dapm_line:
2390 case snd_soc_dapm_micbias:
2391 case snd_soc_dapm_dac:
2392 case snd_soc_dapm_adc:
2393 case snd_soc_dapm_pga:
2394 case snd_soc_dapm_effect:
2395 case snd_soc_dapm_out_drv:
2396 case snd_soc_dapm_mixer:
2397 case snd_soc_dapm_mixer_named_ctl:
2398 case snd_soc_dapm_supply:
2399 case snd_soc_dapm_regulator_supply:
2400 case snd_soc_dapm_pinctrl:
2401 case snd_soc_dapm_clock_supply:
2403 count += sprintf(buf + count, "%s: %s\n",
2404 w->name, w->power ? "On":"Off");
2411 switch (snd_soc_dapm_get_bias_level(dapm)) {
2412 case SND_SOC_BIAS_ON:
2415 case SND_SOC_BIAS_PREPARE:
2418 case SND_SOC_BIAS_STANDBY:
2421 case SND_SOC_BIAS_OFF:
2425 count += sprintf(buf + count, "PM State: %s\n", state);
2430 /* show dapm widget status in sys fs */
2431 static ssize_t dapm_widget_show(struct device *dev,
2432 struct device_attribute *attr, char *buf)
2434 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2435 struct snd_soc_dai *codec_dai;
2438 mutex_lock(&rtd->card->dapm_mutex);
2440 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2441 struct snd_soc_component *cmpnt = codec_dai->component;
2443 count += dapm_widget_show_component(cmpnt, buf + count);
2446 mutex_unlock(&rtd->card->dapm_mutex);
2451 static DEVICE_ATTR_RO(dapm_widget);
2453 struct attribute *soc_dapm_dev_attrs[] = {
2454 &dev_attr_dapm_widget.attr,
2458 static void dapm_free_path(struct snd_soc_dapm_path *path)
2460 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2461 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2462 list_del(&path->list_kcontrol);
2463 list_del(&path->list);
2467 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2469 struct snd_soc_dapm_path *p, *next_p;
2470 enum snd_soc_dapm_direction dir;
2474 * remove source and sink paths associated to this widget.
2475 * While removing the path, remove reference to it from both
2476 * source and sink widgets so that path is removed only once.
2478 snd_soc_dapm_for_each_direction(dir) {
2479 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2483 kfree(w->kcontrols);
2484 kfree_const(w->name);
2485 kfree_const(w->sname);
2489 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2491 dapm->path_sink_cache.widget = NULL;
2492 dapm->path_source_cache.widget = NULL;
2495 /* free all dapm widgets and resources */
2496 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2498 struct snd_soc_dapm_widget *w, *next_w;
2500 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2501 if (w->dapm != dapm)
2503 snd_soc_dapm_free_widget(w);
2505 snd_soc_dapm_reset_cache(dapm);
2508 static struct snd_soc_dapm_widget *dapm_find_widget(
2509 struct snd_soc_dapm_context *dapm, const char *pin,
2510 bool search_other_contexts)
2512 struct snd_soc_dapm_widget *w;
2513 struct snd_soc_dapm_widget *fallback = NULL;
2515 list_for_each_entry(w, &dapm->card->widgets, list) {
2516 if (!strcmp(w->name, pin)) {
2517 if (w->dapm == dapm)
2524 if (search_other_contexts)
2530 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2531 const char *pin, int status)
2533 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2535 dapm_assert_locked(dapm);
2538 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2542 if (w->connected != status) {
2543 dapm_mark_dirty(w, "pin configuration");
2544 dapm_widget_invalidate_input_paths(w);
2545 dapm_widget_invalidate_output_paths(w);
2548 w->connected = status;
2556 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2557 * @dapm: DAPM context
2559 * Walks all dapm audio paths and powers widgets according to their
2560 * stream or path usage.
2562 * Requires external locking.
2564 * Returns 0 for success.
2566 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2569 * Suppress early reports (eg, jacks syncing their state) to avoid
2570 * silly DAPM runs during card startup.
2572 if (!dapm->card || !dapm->card->instantiated)
2575 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2577 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2580 * snd_soc_dapm_sync - scan and power dapm paths
2581 * @dapm: DAPM context
2583 * Walks all dapm audio paths and powers widgets according to their
2584 * stream or path usage.
2586 * Returns 0 for success.
2588 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2592 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2593 ret = snd_soc_dapm_sync_unlocked(dapm);
2594 mutex_unlock(&dapm->card->dapm_mutex);
2597 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2599 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2600 struct snd_soc_dapm_widget *w,
2604 case snd_soc_dapm_aif_out:
2605 case snd_soc_dapm_aif_in:
2611 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2612 w->channel < channels ? "Connecting" : "Disconnecting",
2613 p->source->name, p->sink->name);
2615 if (w->channel < channels)
2616 soc_dapm_connect_path(p, true, "dai update");
2618 soc_dapm_connect_path(p, false, "dai update");
2623 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2624 struct snd_pcm_hw_params *params,
2625 struct snd_soc_dai *dai)
2627 int dir = substream->stream;
2628 int channels = params_channels(params);
2629 struct snd_soc_dapm_path *p;
2630 struct snd_soc_dapm_widget *w;
2633 w = snd_soc_dai_get_widget(dai, dir);
2638 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2639 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2641 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2642 ret = dapm_update_dai_chan(p, p->sink, channels);
2647 snd_soc_dapm_widget_for_each_source_path(w, p) {
2648 ret = dapm_update_dai_chan(p, p->source, channels);
2656 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2657 struct snd_pcm_hw_params *params,
2658 struct snd_soc_dai *dai)
2660 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2663 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2664 ret = dapm_update_dai_unlocked(substream, params, dai);
2665 mutex_unlock(&rtd->card->dapm_mutex);
2669 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2672 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2673 * @w: The widget for which to update the flags
2675 * Some widgets have a dynamic category which depends on which neighbors they
2676 * are connected to. This function update the category for these widgets.
2678 * This function must be called whenever a path is added or removed to a widget.
2680 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2682 enum snd_soc_dapm_direction dir;
2683 struct snd_soc_dapm_path *p;
2687 case snd_soc_dapm_input:
2688 /* On a fully routed card an input is never a source */
2689 if (w->dapm->card->fully_routed)
2691 ep = SND_SOC_DAPM_EP_SOURCE;
2692 snd_soc_dapm_widget_for_each_source_path(w, p) {
2693 if (p->source->id == snd_soc_dapm_micbias ||
2694 p->source->id == snd_soc_dapm_mic ||
2695 p->source->id == snd_soc_dapm_line ||
2696 p->source->id == snd_soc_dapm_output) {
2702 case snd_soc_dapm_output:
2703 /* On a fully routed card a output is never a sink */
2704 if (w->dapm->card->fully_routed)
2706 ep = SND_SOC_DAPM_EP_SINK;
2707 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2708 if (p->sink->id == snd_soc_dapm_spk ||
2709 p->sink->id == snd_soc_dapm_hp ||
2710 p->sink->id == snd_soc_dapm_line ||
2711 p->sink->id == snd_soc_dapm_input) {
2717 case snd_soc_dapm_line:
2719 snd_soc_dapm_for_each_direction(dir) {
2720 if (!list_empty(&w->edges[dir]))
2721 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2731 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2732 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2733 const char *control)
2735 bool dynamic_source = false;
2736 bool dynamic_sink = false;
2741 switch (source->id) {
2742 case snd_soc_dapm_demux:
2743 dynamic_source = true;
2750 case snd_soc_dapm_mux:
2751 case snd_soc_dapm_switch:
2752 case snd_soc_dapm_mixer:
2753 case snd_soc_dapm_mixer_named_ctl:
2754 dynamic_sink = true;
2760 if (dynamic_source && dynamic_sink) {
2762 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2763 source->name, control, sink->name);
2765 } else if (!dynamic_source && !dynamic_sink) {
2767 "Control not supported for path %s -> [%s] -> %s\n",
2768 source->name, control, sink->name);
2775 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2776 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2777 const char *control,
2778 int (*connected)(struct snd_soc_dapm_widget *source,
2779 struct snd_soc_dapm_widget *sink))
2781 struct snd_soc_dapm_widget *widgets[2];
2782 enum snd_soc_dapm_direction dir;
2783 struct snd_soc_dapm_path *path;
2786 if (wsink->is_supply && !wsource->is_supply) {
2788 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2789 wsource->name, wsink->name);
2793 if (connected && !wsource->is_supply) {
2795 "connected() callback only supported for supply widgets (%s -> %s)\n",
2796 wsource->name, wsink->name);
2800 if (wsource->is_supply && control) {
2802 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2803 wsource->name, control, wsink->name);
2807 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2811 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2815 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2816 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2817 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2818 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2820 path->connected = connected;
2821 INIT_LIST_HEAD(&path->list);
2822 INIT_LIST_HEAD(&path->list_kcontrol);
2824 if (wsource->is_supply || wsink->is_supply)
2825 path->is_supply = 1;
2827 /* connect static paths */
2828 if (control == NULL) {
2831 switch (wsource->id) {
2832 case snd_soc_dapm_demux:
2833 ret = dapm_connect_mux(dapm, path, control, wsource);
2841 switch (wsink->id) {
2842 case snd_soc_dapm_mux:
2843 ret = dapm_connect_mux(dapm, path, control, wsink);
2847 case snd_soc_dapm_switch:
2848 case snd_soc_dapm_mixer:
2849 case snd_soc_dapm_mixer_named_ctl:
2850 ret = dapm_connect_mixer(dapm, path, control);
2859 list_add(&path->list, &dapm->card->paths);
2860 snd_soc_dapm_for_each_direction(dir)
2861 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2863 snd_soc_dapm_for_each_direction(dir) {
2864 dapm_update_widget_flags(widgets[dir]);
2865 dapm_mark_dirty(widgets[dir], "Route added");
2868 if (dapm->card->instantiated && path->connect)
2869 dapm_path_invalidate(path);
2877 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2878 const struct snd_soc_dapm_route *route)
2880 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2881 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2884 char prefixed_sink[80];
2885 char prefixed_source[80];
2887 unsigned int sink_ref = 0;
2888 unsigned int source_ref = 0;
2891 prefix = soc_dapm_prefix(dapm);
2893 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2894 prefix, route->sink);
2895 sink = prefixed_sink;
2896 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2897 prefix, route->source);
2898 source = prefixed_source;
2901 source = route->source;
2904 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2905 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2907 if (wsink && wsource)
2911 * find src and dest widgets over all widgets but favor a widget from
2912 * current DAPM context
2914 list_for_each_entry(w, &dapm->card->widgets, list) {
2915 if (!wsink && !(strcmp(w->name, sink))) {
2917 if (w->dapm == dapm) {
2925 "ASoC: sink widget %s overwritten\n",
2929 if (!wsource && !(strcmp(w->name, source))) {
2931 if (w->dapm == dapm) {
2939 "ASoC: source widget %s overwritten\n",
2943 /* use widget from another DAPM context if not found from this */
2949 if (wsource == NULL) {
2950 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2954 if (wsink == NULL) {
2955 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2961 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2962 dapm_wcache_update(&dapm->path_source_cache, wsource);
2964 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2971 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2972 source, route->control, sink);
2976 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2977 const struct snd_soc_dapm_route *route)
2979 struct snd_soc_dapm_widget *wsource, *wsink;
2980 struct snd_soc_dapm_path *path, *p;
2983 char prefixed_sink[80];
2984 char prefixed_source[80];
2987 if (route->control) {
2989 "ASoC: Removal of routes with controls not supported\n");
2993 prefix = soc_dapm_prefix(dapm);
2995 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2996 prefix, route->sink);
2997 sink = prefixed_sink;
2998 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2999 prefix, route->source);
3000 source = prefixed_source;
3003 source = route->source;
3007 list_for_each_entry(p, &dapm->card->paths, list) {
3008 if (strcmp(p->source->name, source) != 0)
3010 if (strcmp(p->sink->name, sink) != 0)
3017 wsource = path->source;
3020 dapm_mark_dirty(wsource, "Route removed");
3021 dapm_mark_dirty(wsink, "Route removed");
3023 dapm_path_invalidate(path);
3025 dapm_free_path(path);
3027 /* Update any path related flags */
3028 dapm_update_widget_flags(wsource);
3029 dapm_update_widget_flags(wsink);
3031 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3039 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3040 * @dapm: DAPM context
3041 * @route: audio routes
3042 * @num: number of routes
3044 * Connects 2 dapm widgets together via a named audio path. The sink is
3045 * the widget receiving the audio signal, whilst the source is the sender
3046 * of the audio signal.
3048 * Returns 0 for success else error. On error all resources can be freed
3049 * with a call to snd_soc_card_free().
3051 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3052 const struct snd_soc_dapm_route *route, int num)
3056 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3057 for (i = 0; i < num; i++) {
3058 r = snd_soc_dapm_add_route(dapm, route);
3060 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3062 route->control ? route->control : "direct",
3068 mutex_unlock(&dapm->card->dapm_mutex);
3072 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3075 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3076 * @dapm: DAPM context
3077 * @route: audio routes
3078 * @num: number of routes
3080 * Removes routes from the DAPM context.
3082 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3083 const struct snd_soc_dapm_route *route, int num)
3087 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3088 for (i = 0; i < num; i++) {
3089 snd_soc_dapm_del_route(dapm, route);
3092 mutex_unlock(&dapm->card->dapm_mutex);
3096 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3098 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3099 const struct snd_soc_dapm_route *route)
3101 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3104 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3107 struct snd_soc_dapm_path *path;
3111 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3117 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3122 if (route->control || route->connected)
3123 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3124 route->source, route->sink);
3126 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3127 if (path->sink == sink) {
3134 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3135 route->source, route->sink);
3137 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3138 count, route->source, route->sink);
3144 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3145 * @dapm: DAPM context
3146 * @route: audio routes
3147 * @num: number of routes
3149 * Mark existing routes matching those specified in the passed array
3150 * as being weak, meaning that they are ignored for the purpose of
3151 * power decisions. The main intended use case is for sidetone paths
3152 * which couple audio between other independent paths if they are both
3153 * active in order to make the combination work better at the user
3154 * level but which aren't intended to be "used".
3156 * Note that CODEC drivers should not use this as sidetone type paths
3157 * can frequently also be used as bypass paths.
3159 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3160 const struct snd_soc_dapm_route *route, int num)
3165 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3166 for (i = 0; i < num; i++) {
3167 err = snd_soc_dapm_weak_route(dapm, route);
3172 mutex_unlock(&dapm->card->dapm_mutex);
3176 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3179 * snd_soc_dapm_new_widgets - add new dapm widgets
3180 * @card: card to be checked for new dapm widgets
3182 * Checks the codec for any new dapm widgets and creates them if found.
3184 * Returns 0 for success.
3186 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3188 struct snd_soc_dapm_widget *w;
3191 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3193 list_for_each_entry(w, &card->widgets, list)
3198 if (w->num_kcontrols) {
3199 w->kcontrols = kcalloc(w->num_kcontrols,
3200 sizeof(struct snd_kcontrol *),
3202 if (!w->kcontrols) {
3203 mutex_unlock(&card->dapm_mutex);
3209 case snd_soc_dapm_switch:
3210 case snd_soc_dapm_mixer:
3211 case snd_soc_dapm_mixer_named_ctl:
3214 case snd_soc_dapm_mux:
3215 case snd_soc_dapm_demux:
3218 case snd_soc_dapm_pga:
3219 case snd_soc_dapm_effect:
3220 case snd_soc_dapm_out_drv:
3223 case snd_soc_dapm_dai_link:
3224 dapm_new_dai_link(w);
3230 /* Read the initial power state from the device */
3232 soc_dapm_read(w->dapm, w->reg, &val);
3233 val = val >> w->shift;
3235 if (val == w->on_val)
3241 dapm_mark_dirty(w, "new widget");
3242 dapm_debugfs_add_widget(w);
3245 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3246 mutex_unlock(&card->dapm_mutex);
3249 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3252 * snd_soc_dapm_get_volsw - dapm mixer get callback
3253 * @kcontrol: mixer control
3254 * @ucontrol: control element information
3256 * Callback to get the value of a dapm mixer control.
3258 * Returns 0 for success.
3260 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3261 struct snd_ctl_elem_value *ucontrol)
3263 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3264 struct snd_soc_card *card = dapm->card;
3265 struct soc_mixer_control *mc =
3266 (struct soc_mixer_control *)kcontrol->private_value;
3268 unsigned int shift = mc->shift;
3270 unsigned int width = fls(max);
3271 unsigned int mask = (1 << fls(max)) - 1;
3272 unsigned int invert = mc->invert;
3273 unsigned int reg_val, val, rval = 0;
3276 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3277 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3278 ret = soc_dapm_read(dapm, reg, ®_val);
3279 val = (reg_val >> shift) & mask;
3281 if (ret == 0 && reg != mc->rreg)
3282 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3284 if (snd_soc_volsw_is_stereo(mc))
3285 rval = (reg_val >> mc->rshift) & mask;
3287 reg_val = dapm_kcontrol_get_value(kcontrol);
3288 val = reg_val & mask;
3290 if (snd_soc_volsw_is_stereo(mc))
3291 rval = (reg_val >> width) & mask;
3293 mutex_unlock(&card->dapm_mutex);
3299 ucontrol->value.integer.value[0] = max - val;
3301 ucontrol->value.integer.value[0] = val;
3303 if (snd_soc_volsw_is_stereo(mc)) {
3305 ucontrol->value.integer.value[1] = max - rval;
3307 ucontrol->value.integer.value[1] = rval;
3312 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3315 * snd_soc_dapm_put_volsw - dapm mixer set callback
3316 * @kcontrol: mixer control
3317 * @ucontrol: control element information
3319 * Callback to set the value of a dapm mixer control.
3321 * Returns 0 for success.
3323 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_value *ucontrol)
3326 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3327 struct snd_soc_card *card = dapm->card;
3328 struct soc_mixer_control *mc =
3329 (struct soc_mixer_control *)kcontrol->private_value;
3331 unsigned int shift = mc->shift;
3333 unsigned int width = fls(max);
3334 unsigned int mask = (1 << width) - 1;
3335 unsigned int invert = mc->invert;
3336 unsigned int val, rval = 0;
3337 int connect, rconnect = -1, change, reg_change = 0;
3338 struct snd_soc_dapm_update update = {};
3341 val = (ucontrol->value.integer.value[0] & mask);
3347 if (snd_soc_volsw_is_stereo(mc)) {
3348 rval = (ucontrol->value.integer.value[1] & mask);
3354 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3356 /* This assumes field width < (bits in unsigned int / 2) */
3357 if (width > sizeof(unsigned int) * 8 / 2)
3359 "ASoC: control %s field width limit exceeded\n",
3361 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3363 if (reg != SND_SOC_NOPM) {
3365 rval = rval << mc->rshift;
3367 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3369 if (snd_soc_volsw_is_stereo(mc))
3370 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3375 if (change || reg_change) {
3377 if (snd_soc_volsw_is_stereo(mc)) {
3378 update.has_second_set = true;
3379 update.reg2 = mc->rreg;
3380 update.mask2 = mask << mc->rshift;
3383 update.kcontrol = kcontrol;
3385 update.mask = mask << shift;
3387 card->update = &update;
3389 change |= reg_change;
3391 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3394 card->update = NULL;
3397 mutex_unlock(&card->dapm_mutex);
3400 soc_dpcm_runtime_update(card);
3404 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3407 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3408 * @kcontrol: mixer control
3409 * @ucontrol: control element information
3411 * Callback to get the value of a dapm enumerated double mixer control.
3413 * Returns 0 for success.
3415 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3416 struct snd_ctl_elem_value *ucontrol)
3418 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3419 struct snd_soc_card *card = dapm->card;
3420 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3421 unsigned int reg_val, val;
3423 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3424 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3425 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3427 mutex_unlock(&card->dapm_mutex);
3431 reg_val = dapm_kcontrol_get_value(kcontrol);
3433 mutex_unlock(&card->dapm_mutex);
3435 val = (reg_val >> e->shift_l) & e->mask;
3436 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3437 if (e->shift_l != e->shift_r) {
3438 val = (reg_val >> e->shift_r) & e->mask;
3439 val = snd_soc_enum_val_to_item(e, val);
3440 ucontrol->value.enumerated.item[1] = val;
3445 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3448 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3449 * @kcontrol: mixer control
3450 * @ucontrol: control element information
3452 * Callback to set the value of a dapm enumerated double mixer control.
3454 * Returns 0 for success.
3456 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3457 struct snd_ctl_elem_value *ucontrol)
3459 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3460 struct snd_soc_card *card = dapm->card;
3461 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3462 unsigned int *item = ucontrol->value.enumerated.item;
3463 unsigned int val, change, reg_change = 0;
3465 struct snd_soc_dapm_update update = {};
3468 if (item[0] >= e->items)
3471 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3472 mask = e->mask << e->shift_l;
3473 if (e->shift_l != e->shift_r) {
3474 if (item[1] > e->items)
3476 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3477 mask |= e->mask << e->shift_r;
3480 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3482 change = dapm_kcontrol_set_value(kcontrol, val);
3484 if (e->reg != SND_SOC_NOPM)
3485 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3487 if (change || reg_change) {
3489 update.kcontrol = kcontrol;
3490 update.reg = e->reg;
3493 card->update = &update;
3495 change |= reg_change;
3497 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3499 card->update = NULL;
3502 mutex_unlock(&card->dapm_mutex);
3505 soc_dpcm_runtime_update(card);
3509 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3512 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3514 * @kcontrol: mixer control
3515 * @uinfo: control element information
3517 * Callback to provide information about a pin switch control.
3519 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3520 struct snd_ctl_elem_info *uinfo)
3522 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3524 uinfo->value.integer.min = 0;
3525 uinfo->value.integer.max = 1;
3529 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3532 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3534 * @kcontrol: mixer control
3537 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3538 struct snd_ctl_elem_value *ucontrol)
3540 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3541 const char *pin = (const char *)kcontrol->private_value;
3543 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3545 ucontrol->value.integer.value[0] =
3546 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3548 mutex_unlock(&card->dapm_mutex);
3552 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3555 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3557 * @kcontrol: mixer control
3560 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3561 struct snd_ctl_elem_value *ucontrol)
3563 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3564 const char *pin = (const char *)kcontrol->private_value;
3566 if (ucontrol->value.integer.value[0])
3567 snd_soc_dapm_enable_pin(&card->dapm, pin);
3569 snd_soc_dapm_disable_pin(&card->dapm, pin);
3571 snd_soc_dapm_sync(&card->dapm);
3574 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3576 struct snd_soc_dapm_widget *
3577 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3578 const struct snd_soc_dapm_widget *widget)
3580 enum snd_soc_dapm_direction dir;
3581 struct snd_soc_dapm_widget *w;
3585 if ((w = dapm_cnew_widget(widget)) == NULL)
3586 return ERR_PTR(-ENOMEM);
3589 case snd_soc_dapm_regulator_supply:
3590 w->regulator = devm_regulator_get(dapm->dev, w->name);
3591 if (IS_ERR(w->regulator)) {
3592 ret = PTR_ERR(w->regulator);
3593 goto request_failed;
3596 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3597 ret = regulator_allow_bypass(w->regulator, true);
3600 "ASoC: Failed to bypass %s: %d\n",
3604 case snd_soc_dapm_pinctrl:
3605 w->pinctrl = devm_pinctrl_get(dapm->dev);
3606 if (IS_ERR(w->pinctrl)) {
3607 ret = PTR_ERR(w->pinctrl);
3608 goto request_failed;
3611 /* set to sleep_state when initializing */
3612 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3614 case snd_soc_dapm_clock_supply:
3615 w->clk = devm_clk_get(dapm->dev, w->name);
3616 if (IS_ERR(w->clk)) {
3617 ret = PTR_ERR(w->clk);
3618 goto request_failed;
3625 prefix = soc_dapm_prefix(dapm);
3627 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3629 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3630 if (w->name == NULL) {
3631 kfree_const(w->sname);
3633 return ERR_PTR(-ENOMEM);
3637 case snd_soc_dapm_mic:
3638 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3639 w->power_check = dapm_generic_check_power;
3641 case snd_soc_dapm_input:
3642 if (!dapm->card->fully_routed)
3643 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3644 w->power_check = dapm_generic_check_power;
3646 case snd_soc_dapm_spk:
3647 case snd_soc_dapm_hp:
3648 w->is_ep = SND_SOC_DAPM_EP_SINK;
3649 w->power_check = dapm_generic_check_power;
3651 case snd_soc_dapm_output:
3652 if (!dapm->card->fully_routed)
3653 w->is_ep = SND_SOC_DAPM_EP_SINK;
3654 w->power_check = dapm_generic_check_power;
3656 case snd_soc_dapm_vmid:
3657 case snd_soc_dapm_siggen:
3658 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3659 w->power_check = dapm_always_on_check_power;
3661 case snd_soc_dapm_sink:
3662 w->is_ep = SND_SOC_DAPM_EP_SINK;
3663 w->power_check = dapm_always_on_check_power;
3666 case snd_soc_dapm_mux:
3667 case snd_soc_dapm_demux:
3668 case snd_soc_dapm_switch:
3669 case snd_soc_dapm_mixer:
3670 case snd_soc_dapm_mixer_named_ctl:
3671 case snd_soc_dapm_adc:
3672 case snd_soc_dapm_aif_out:
3673 case snd_soc_dapm_dac:
3674 case snd_soc_dapm_aif_in:
3675 case snd_soc_dapm_pga:
3676 case snd_soc_dapm_buffer:
3677 case snd_soc_dapm_scheduler:
3678 case snd_soc_dapm_effect:
3679 case snd_soc_dapm_src:
3680 case snd_soc_dapm_asrc:
3681 case snd_soc_dapm_encoder:
3682 case snd_soc_dapm_decoder:
3683 case snd_soc_dapm_out_drv:
3684 case snd_soc_dapm_micbias:
3685 case snd_soc_dapm_line:
3686 case snd_soc_dapm_dai_link:
3687 case snd_soc_dapm_dai_out:
3688 case snd_soc_dapm_dai_in:
3689 w->power_check = dapm_generic_check_power;
3691 case snd_soc_dapm_supply:
3692 case snd_soc_dapm_regulator_supply:
3693 case snd_soc_dapm_pinctrl:
3694 case snd_soc_dapm_clock_supply:
3695 case snd_soc_dapm_kcontrol:
3697 w->power_check = dapm_supply_check_power;
3700 w->power_check = dapm_always_on_check_power;
3705 INIT_LIST_HEAD(&w->list);
3706 INIT_LIST_HEAD(&w->dirty);
3707 list_add_tail(&w->list, &dapm->card->widgets);
3709 snd_soc_dapm_for_each_direction(dir) {
3710 INIT_LIST_HEAD(&w->edges[dir]);
3711 w->endpoints[dir] = -1;
3714 /* machine layer sets up unconnected pins and insertions */
3719 if (ret != -EPROBE_DEFER)
3720 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3723 kfree_const(w->sname);
3725 return ERR_PTR(ret);
3729 * snd_soc_dapm_new_control - create new dapm control
3730 * @dapm: DAPM context
3731 * @widget: widget template
3733 * Creates new DAPM control based upon a template.
3735 * Returns a widget pointer on success or an error pointer on failure
3737 struct snd_soc_dapm_widget *
3738 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3739 const struct snd_soc_dapm_widget *widget)
3741 struct snd_soc_dapm_widget *w;
3743 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3744 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3745 mutex_unlock(&dapm->card->dapm_mutex);
3749 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3752 * snd_soc_dapm_new_controls - create new dapm controls
3753 * @dapm: DAPM context
3754 * @widget: widget array
3755 * @num: number of widgets
3757 * Creates new DAPM controls based upon the templates.
3759 * Returns 0 for success else error.
3761 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3762 const struct snd_soc_dapm_widget *widget,
3765 struct snd_soc_dapm_widget *w;
3769 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3770 for (i = 0; i < num; i++) {
3771 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3778 mutex_unlock(&dapm->card->dapm_mutex);
3781 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3784 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3785 struct snd_pcm_substream *substream)
3787 struct snd_soc_dapm_path *path;
3788 struct snd_soc_dai *source, *sink;
3789 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3790 struct snd_pcm_hw_params *params = NULL;
3791 const struct snd_soc_pcm_stream *config = NULL;
3792 struct snd_pcm_runtime *runtime = NULL;
3796 params = kzalloc(sizeof(*params), GFP_KERNEL);
3800 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3806 substream->runtime = runtime;
3808 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3809 snd_soc_dapm_widget_for_each_source_path(w, path) {
3810 source = path->source->priv;
3812 ret = snd_soc_dai_startup(source, substream);
3814 dev_err(source->dev,
3815 "ASoC: startup() failed: %d\n", ret);
3821 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3822 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3823 sink = path->sink->priv;
3825 ret = snd_soc_dai_startup(sink, substream);
3828 "ASoC: startup() failed: %d\n", ret);
3834 substream->hw_opened = 1;
3837 * Note: getting the config after .startup() gives a chance to
3838 * either party on the link to alter the configuration if
3841 config = rtd->dai_link->params + rtd->params_select;
3842 if (WARN_ON(!config)) {
3843 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3848 /* Be a little careful as we don't want to overflow the mask array */
3849 if (config->formats) {
3850 fmt = ffs(config->formats) - 1;
3852 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3859 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3860 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3862 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3864 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3865 = config->channels_min;
3866 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3867 = config->channels_max;
3869 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3870 snd_soc_dapm_widget_for_each_source_path(w, path) {
3871 source = path->source->priv;
3873 ret = snd_soc_dai_hw_params(source, substream, params);
3877 dapm_update_dai_unlocked(substream, params, source);
3880 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3881 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3882 sink = path->sink->priv;
3884 ret = snd_soc_dai_hw_params(sink, substream, params);
3888 dapm_update_dai_unlocked(substream, params, sink);
3891 runtime->format = params_format(params);
3892 runtime->subformat = params_subformat(params);
3893 runtime->channels = params_channels(params);
3894 runtime->rate = params_rate(params);
3901 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3902 struct snd_kcontrol *kcontrol, int event)
3904 struct snd_soc_dapm_path *path;
3905 struct snd_soc_dai *source, *sink;
3906 struct snd_pcm_substream *substream = w->priv;
3907 int ret = 0, saved_stream = substream->stream;
3909 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3910 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3914 case SND_SOC_DAPM_PRE_PMU:
3915 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
3921 case SND_SOC_DAPM_POST_PMU:
3922 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3923 sink = path->sink->priv;
3925 ret = snd_soc_dai_digital_mute(sink, 0,
3926 SNDRV_PCM_STREAM_PLAYBACK);
3927 if (ret != 0 && ret != -ENOTSUPP)
3929 "ASoC: Failed to unmute: %d\n", ret);
3934 case SND_SOC_DAPM_PRE_PMD:
3935 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3936 sink = path->sink->priv;
3938 ret = snd_soc_dai_digital_mute(sink, 1,
3939 SNDRV_PCM_STREAM_PLAYBACK);
3940 if (ret != 0 && ret != -ENOTSUPP)
3942 "ASoC: Failed to mute: %d\n", ret);
3946 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3947 snd_soc_dapm_widget_for_each_source_path(w, path) {
3948 source = path->source->priv;
3949 snd_soc_dai_hw_free(source, substream);
3952 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3953 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3954 sink = path->sink->priv;
3955 snd_soc_dai_hw_free(sink, substream);
3958 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3959 snd_soc_dapm_widget_for_each_source_path(w, path) {
3960 source = path->source->priv;
3962 snd_soc_dai_shutdown(source, substream);
3965 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3966 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3967 sink = path->sink->priv;
3969 snd_soc_dai_shutdown(sink, substream);
3973 case SND_SOC_DAPM_POST_PMD:
3974 kfree(substream->runtime);
3978 WARN(1, "Unknown event %d\n", event);
3983 /* Restore the substream direction */
3984 substream->stream = saved_stream;
3988 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3989 struct snd_ctl_elem_value *ucontrol)
3991 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3992 struct snd_soc_pcm_runtime *rtd = w->priv;
3994 ucontrol->value.enumerated.item[0] = rtd->params_select;
3999 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4000 struct snd_ctl_elem_value *ucontrol)
4002 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4003 struct snd_soc_pcm_runtime *rtd = w->priv;
4005 /* Can't change the config when widget is already powered */
4009 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
4012 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
4015 rtd->params_select = ucontrol->value.enumerated.item[0];
4021 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4022 unsigned long *private_value,
4024 const char **w_param_text)
4028 devm_kfree(card->dev, (void *)*private_value);
4033 for (count = 0 ; count < num_params; count++)
4034 devm_kfree(card->dev, (void *)w_param_text[count]);
4035 devm_kfree(card->dev, w_param_text);
4038 static struct snd_kcontrol_new *
4039 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4041 const struct snd_soc_pcm_stream *params,
4042 int num_params, const char **w_param_text,
4043 unsigned long *private_value)
4045 struct soc_enum w_param_enum[] = {
4046 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4048 struct snd_kcontrol_new kcontrol_dai_link[] = {
4049 SOC_ENUM_EXT(NULL, w_param_enum[0],
4050 snd_soc_dapm_dai_link_get,
4051 snd_soc_dapm_dai_link_put),
4053 struct snd_kcontrol_new *kcontrol_news;
4054 const struct snd_soc_pcm_stream *config = params;
4057 for (count = 0 ; count < num_params; count++) {
4058 if (!config->stream_name) {
4059 dev_warn(card->dapm.dev,
4060 "ASoC: anonymous config %d for dai link %s\n",
4062 w_param_text[count] =
4063 devm_kasprintf(card->dev, GFP_KERNEL,
4064 "Anonymous Configuration %d",
4067 w_param_text[count] = devm_kmemdup(card->dev,
4068 config->stream_name,
4069 strlen(config->stream_name) + 1,
4072 if (!w_param_text[count])
4073 goto outfree_w_param;
4077 w_param_enum[0].items = num_params;
4078 w_param_enum[0].texts = w_param_text;
4081 (unsigned long) devm_kmemdup(card->dev,
4082 (void *)(kcontrol_dai_link[0].private_value),
4083 sizeof(struct soc_enum), GFP_KERNEL);
4084 if (!*private_value) {
4085 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4087 goto outfree_w_param;
4089 kcontrol_dai_link[0].private_value = *private_value;
4090 /* duplicate kcontrol_dai_link on heap so that memory persists */
4091 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4092 sizeof(struct snd_kcontrol_new),
4094 if (!kcontrol_news) {
4095 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4097 goto outfree_w_param;
4099 return kcontrol_news;
4102 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4106 static struct snd_soc_dapm_widget *
4107 snd_soc_dapm_new_dai(struct snd_soc_card *card,
4108 struct snd_pcm_substream *substream,
4111 struct snd_soc_pcm_runtime *rtd = substream->private_data;
4112 struct snd_soc_dapm_widget template;
4113 struct snd_soc_dapm_widget *w;
4114 const char **w_param_text;
4115 unsigned long private_value = 0;
4119 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4120 rtd->dai_link->name, id);
4122 return ERR_PTR(-ENOMEM);
4124 memset(&template, 0, sizeof(template));
4125 template.reg = SND_SOC_NOPM;
4126 template.id = snd_soc_dapm_dai_link;
4127 template.name = link_name;
4128 template.event = snd_soc_dai_link_event;
4129 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4130 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4131 template.kcontrol_news = NULL;
4133 /* allocate memory for control, only in case of multiple configs */
4134 if (rtd->dai_link->num_params > 1) {
4135 w_param_text = devm_kcalloc(card->dev,
4136 rtd->dai_link->num_params,
4137 sizeof(char *), GFP_KERNEL);
4138 if (!w_param_text) {
4143 template.num_kcontrols = 1;
4144 template.kcontrol_news =
4145 snd_soc_dapm_alloc_kcontrol(card,
4147 rtd->dai_link->params,
4148 rtd->dai_link->num_params,
4149 w_param_text, &private_value);
4150 if (!template.kcontrol_news) {
4155 w_param_text = NULL;
4157 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4159 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4162 goto outfree_kcontrol_news;
4165 w->priv = substream;
4169 outfree_kcontrol_news:
4170 devm_kfree(card->dev, (void *)template.kcontrol_news);
4171 snd_soc_dapm_free_kcontrol(card, &private_value,
4172 rtd->dai_link->num_params, w_param_text);
4174 devm_kfree(card->dev, link_name);
4175 return ERR_PTR(ret);
4178 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4179 struct snd_soc_dai *dai)
4181 struct snd_soc_dapm_widget template;
4182 struct snd_soc_dapm_widget *w;
4184 WARN_ON(dapm->dev != dai->dev);
4186 memset(&template, 0, sizeof(template));
4187 template.reg = SND_SOC_NOPM;
4189 if (dai->driver->playback.stream_name) {
4190 template.id = snd_soc_dapm_dai_in;
4191 template.name = dai->driver->playback.stream_name;
4192 template.sname = dai->driver->playback.stream_name;
4194 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4197 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4202 dai->playback_widget = w;
4205 if (dai->driver->capture.stream_name) {
4206 template.id = snd_soc_dapm_dai_out;
4207 template.name = dai->driver->capture.stream_name;
4208 template.sname = dai->driver->capture.stream_name;
4210 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4213 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4218 dai->capture_widget = w;
4224 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4226 struct snd_soc_dapm_widget *dai_w, *w;
4227 struct snd_soc_dapm_widget *src, *sink;
4228 struct snd_soc_dai *dai;
4230 /* For each DAI widget... */
4231 list_for_each_entry(dai_w, &card->widgets, list) {
4232 switch (dai_w->id) {
4233 case snd_soc_dapm_dai_in:
4234 case snd_soc_dapm_dai_out:
4240 /* let users know there is no DAI to link */
4242 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4249 /* ...find all widgets with the same stream and link them */
4250 list_for_each_entry(w, &card->widgets, list) {
4251 if (w->dapm != dai_w->dapm)
4255 case snd_soc_dapm_dai_in:
4256 case snd_soc_dapm_dai_out:
4262 if (!w->sname || !strstr(w->sname, dai_w->sname))
4265 if (dai_w->id == snd_soc_dapm_dai_in) {
4272 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4273 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4280 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4281 struct snd_soc_pcm_runtime *rtd)
4283 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4284 struct snd_soc_dai *codec_dai;
4285 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4286 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
4287 struct snd_pcm_substream *substream;
4288 struct snd_pcm_str *streams = rtd->pcm->streams;
4291 if (rtd->dai_link->params) {
4292 playback_cpu = cpu_dai->capture_widget;
4293 capture_cpu = cpu_dai->playback_widget;
4295 playback = cpu_dai->playback_widget;
4296 capture = cpu_dai->capture_widget;
4297 playback_cpu = playback;
4298 capture_cpu = capture;
4301 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4302 /* connect BE DAI playback if widgets are valid */
4303 codec = codec_dai->playback_widget;
4305 if (playback_cpu && codec) {
4307 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
4308 playback = snd_soc_dapm_new_dai(card, substream,
4310 if (IS_ERR(playback)) {
4312 "ASoC: Failed to create DAI %s: %ld\n",
4318 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4319 playback, NULL, NULL);
4322 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4323 cpu_dai->component->name, playback_cpu->name,
4324 codec_dai->component->name, codec->name);
4326 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4331 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4332 /* connect BE DAI capture if widgets are valid */
4333 codec = codec_dai->capture_widget;
4335 if (codec && capture_cpu) {
4337 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
4338 capture = snd_soc_dapm_new_dai(card, substream,
4340 if (IS_ERR(capture)) {
4342 "ASoC: Failed to create DAI %s: %ld\n",
4348 snd_soc_dapm_add_path(&card->dapm, capture,
4349 capture_cpu, NULL, NULL);
4352 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4353 codec_dai->component->name, codec->name,
4354 cpu_dai->component->name, capture_cpu->name);
4356 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4362 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4365 struct snd_soc_dapm_widget *w;
4368 w = snd_soc_dai_get_widget(dai, stream);
4371 dapm_mark_dirty(w, "stream event");
4373 if (w->id == snd_soc_dapm_dai_in) {
4374 ep = SND_SOC_DAPM_EP_SOURCE;
4375 dapm_widget_invalidate_input_paths(w);
4377 ep = SND_SOC_DAPM_EP_SINK;
4378 dapm_widget_invalidate_output_paths(w);
4382 case SND_SOC_DAPM_STREAM_START:
4386 case SND_SOC_DAPM_STREAM_STOP:
4390 case SND_SOC_DAPM_STREAM_SUSPEND:
4391 case SND_SOC_DAPM_STREAM_RESUME:
4392 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4393 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4399 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4401 struct snd_soc_pcm_runtime *rtd;
4403 /* for each BE DAI link... */
4404 for_each_card_rtds(card, rtd) {
4406 * dynamic FE links have no fixed DAI mapping.
4407 * CODEC<->CODEC links have no direct connection.
4409 if (rtd->dai_link->dynamic)
4412 dapm_connect_dai_link_widgets(card, rtd);
4416 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4419 struct snd_soc_dai *codec_dai;
4422 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4423 for_each_rtd_codec_dai(rtd, i, codec_dai)
4424 soc_dapm_dai_stream_event(codec_dai, stream, event);
4426 dapm_power_widgets(rtd->card, event);
4430 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4431 * @rtd: PCM runtime data
4432 * @stream: stream name
4433 * @event: stream event
4435 * Sends a stream event to the dapm core. The core then makes any
4436 * necessary widget power changes.
4438 * Returns 0 for success else error.
4440 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4443 struct snd_soc_card *card = rtd->card;
4445 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4446 soc_dapm_stream_event(rtd, stream, event);
4447 mutex_unlock(&card->dapm_mutex);
4450 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4452 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4453 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4454 /* powered down playback stream now */
4455 snd_soc_dapm_stream_event(rtd,
4456 SNDRV_PCM_STREAM_PLAYBACK,
4457 SND_SOC_DAPM_STREAM_STOP);
4459 /* start delayed pop wq here for playback streams */
4461 queue_delayed_work(system_power_efficient_wq,
4463 msecs_to_jiffies(rtd->pmdown_time));
4466 /* capture streams can be powered down now */
4467 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4468 SND_SOC_DAPM_STREAM_STOP);
4471 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4474 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4475 * @dapm: DAPM context
4478 * Enables input/output pin and its parents or children widgets iff there is
4479 * a valid audio route and active audio stream.
4481 * Requires external locking.
4483 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4484 * do any widget power switching.
4486 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4489 return snd_soc_dapm_set_pin(dapm, pin, 1);
4491 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4494 * snd_soc_dapm_enable_pin - enable pin.
4495 * @dapm: DAPM context
4498 * Enables input/output pin and its parents or children widgets iff there is
4499 * a valid audio route and active audio stream.
4501 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4502 * do any widget power switching.
4504 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4508 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4510 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4512 mutex_unlock(&dapm->card->dapm_mutex);
4516 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4519 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4520 * @dapm: DAPM context
4523 * Enables input/output pin regardless of any other state. This is
4524 * intended for use with microphone bias supplies used in microphone
4527 * Requires external locking.
4529 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4530 * do any widget power switching.
4532 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4535 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4538 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4542 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4543 if (!w->connected) {
4545 * w->force does not affect the number of input or output paths,
4546 * so we only have to recheck if w->connected is changed
4548 dapm_widget_invalidate_input_paths(w);
4549 dapm_widget_invalidate_output_paths(w);
4553 dapm_mark_dirty(w, "force enable");
4557 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4560 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4561 * @dapm: DAPM context
4564 * Enables input/output pin regardless of any other state. This is
4565 * intended for use with microphone bias supplies used in microphone
4568 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4569 * do any widget power switching.
4571 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4576 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4578 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4580 mutex_unlock(&dapm->card->dapm_mutex);
4584 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4587 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4588 * @dapm: DAPM context
4591 * Disables input/output pin and its parents or children widgets.
4593 * Requires external locking.
4595 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4596 * do any widget power switching.
4598 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4601 return snd_soc_dapm_set_pin(dapm, pin, 0);
4603 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4606 * snd_soc_dapm_disable_pin - disable pin.
4607 * @dapm: DAPM context
4610 * Disables input/output pin and its parents or children widgets.
4612 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4613 * do any widget power switching.
4615 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4620 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4622 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4624 mutex_unlock(&dapm->card->dapm_mutex);
4628 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4631 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4632 * @dapm: DAPM context
4635 * Marks the specified pin as being not connected, disabling it along
4636 * any parent or child widgets. At present this is identical to
4637 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4638 * additional things such as disabling controls which only affect
4639 * paths through the pin.
4641 * Requires external locking.
4643 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4644 * do any widget power switching.
4646 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4649 return snd_soc_dapm_set_pin(dapm, pin, 0);
4651 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4654 * snd_soc_dapm_nc_pin - permanently disable pin.
4655 * @dapm: DAPM context
4658 * Marks the specified pin as being not connected, disabling it along
4659 * any parent or child widgets. At present this is identical to
4660 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4661 * additional things such as disabling controls which only affect
4662 * paths through the pin.
4664 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4665 * do any widget power switching.
4667 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4671 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4673 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4675 mutex_unlock(&dapm->card->dapm_mutex);
4679 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4682 * snd_soc_dapm_get_pin_status - get audio pin status
4683 * @dapm: DAPM context
4684 * @pin: audio signal pin endpoint (or start point)
4686 * Get audio pin status - connected or disconnected.
4688 * Returns 1 for connected otherwise 0.
4690 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4693 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4696 return w->connected;
4700 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4703 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4704 * @dapm: DAPM context
4705 * @pin: audio signal pin endpoint (or start point)
4707 * Mark the given endpoint or pin as ignoring suspend. When the
4708 * system is disabled a path between two endpoints flagged as ignoring
4709 * suspend will not be disabled. The path must already be enabled via
4710 * normal means at suspend time, it will not be turned on if it was not
4713 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4716 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4719 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4723 w->ignore_suspend = 1;
4727 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4730 * snd_soc_dapm_free - free dapm resources
4731 * @dapm: DAPM context
4733 * Free all dapm widgets and resources.
4735 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4737 dapm_debugfs_cleanup(dapm);
4738 dapm_free_widgets(dapm);
4739 list_del(&dapm->list);
4741 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4743 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4744 struct snd_soc_card *card,
4745 struct snd_soc_component *component)
4748 dapm->component = component;
4749 dapm->bias_level = SND_SOC_BIAS_OFF;
4752 dapm->dev = component->dev;
4753 dapm->idle_bias_off = !component->driver->idle_bias_on,
4754 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4756 dapm->dev = card->dev;
4759 INIT_LIST_HEAD(&dapm->list);
4760 list_add(&dapm->list, &card->dapm_list);
4762 EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4764 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4766 struct snd_soc_card *card = dapm->card;
4767 struct snd_soc_dapm_widget *w;
4768 LIST_HEAD(down_list);
4771 mutex_lock(&card->dapm_mutex);
4773 list_for_each_entry(w, &dapm->card->widgets, list) {
4774 if (w->dapm != dapm)
4777 dapm_seq_insert(w, &down_list, false);
4783 /* If there were no widgets to power down we're already in
4787 if (dapm->bias_level == SND_SOC_BIAS_ON)
4788 snd_soc_dapm_set_bias_level(dapm,
4789 SND_SOC_BIAS_PREPARE);
4790 dapm_seq_run(card, &down_list, 0, false);
4791 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4792 snd_soc_dapm_set_bias_level(dapm,
4793 SND_SOC_BIAS_STANDBY);
4796 mutex_unlock(&card->dapm_mutex);
4800 * snd_soc_dapm_shutdown - callback for system shutdown
4802 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4804 struct snd_soc_dapm_context *dapm;
4806 list_for_each_entry(dapm, &card->dapm_list, list) {
4807 if (dapm != &card->dapm) {
4808 soc_dapm_shutdown_dapm(dapm);
4809 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4810 snd_soc_dapm_set_bias_level(dapm,
4815 soc_dapm_shutdown_dapm(&card->dapm);
4816 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4817 snd_soc_dapm_set_bias_level(&card->dapm,
4821 /* Module information */
4822 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4823 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4824 MODULE_LICENSE("GPL");