1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
17 // o Delayed power down of audio subsystem to reduce pops between a quick
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/async.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/platform_device.h>
27 #include <linux/jiffies.h>
28 #include <linux/debugfs.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/clk.h>
33 #include <linux/slab.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/initval.h>
40 #include <trace/events/asoc.h>
42 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
44 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
45 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
47 #define snd_soc_dapm_for_each_direction(dir) \
48 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
51 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
52 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
54 int (*connected)(struct snd_soc_dapm_widget *source,
55 struct snd_soc_dapm_widget *sink));
57 struct snd_soc_dapm_widget *
58 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
59 const struct snd_soc_dapm_widget *widget);
61 struct snd_soc_dapm_widget *
62 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
63 const struct snd_soc_dapm_widget *widget);
65 /* dapm power sequences - make this per codec in the future */
66 static int dapm_up_seq[] = {
67 [snd_soc_dapm_pre] = 0,
68 [snd_soc_dapm_regulator_supply] = 1,
69 [snd_soc_dapm_pinctrl] = 1,
70 [snd_soc_dapm_clock_supply] = 1,
71 [snd_soc_dapm_supply] = 2,
72 [snd_soc_dapm_micbias] = 3,
73 [snd_soc_dapm_vmid] = 3,
74 [snd_soc_dapm_dai_link] = 2,
75 [snd_soc_dapm_dai_in] = 4,
76 [snd_soc_dapm_dai_out] = 4,
77 [snd_soc_dapm_aif_in] = 4,
78 [snd_soc_dapm_aif_out] = 4,
79 [snd_soc_dapm_mic] = 5,
80 [snd_soc_dapm_siggen] = 5,
81 [snd_soc_dapm_input] = 5,
82 [snd_soc_dapm_output] = 5,
83 [snd_soc_dapm_mux] = 6,
84 [snd_soc_dapm_demux] = 6,
85 [snd_soc_dapm_dac] = 7,
86 [snd_soc_dapm_switch] = 8,
87 [snd_soc_dapm_mixer] = 8,
88 [snd_soc_dapm_mixer_named_ctl] = 8,
89 [snd_soc_dapm_pga] = 9,
90 [snd_soc_dapm_buffer] = 9,
91 [snd_soc_dapm_scheduler] = 9,
92 [snd_soc_dapm_effect] = 9,
93 [snd_soc_dapm_src] = 9,
94 [snd_soc_dapm_asrc] = 9,
95 [snd_soc_dapm_encoder] = 9,
96 [snd_soc_dapm_decoder] = 9,
97 [snd_soc_dapm_adc] = 10,
98 [snd_soc_dapm_out_drv] = 11,
99 [snd_soc_dapm_hp] = 11,
100 [snd_soc_dapm_spk] = 11,
101 [snd_soc_dapm_line] = 11,
102 [snd_soc_dapm_sink] = 11,
103 [snd_soc_dapm_kcontrol] = 12,
104 [snd_soc_dapm_post] = 13,
107 static int dapm_down_seq[] = {
108 [snd_soc_dapm_pre] = 0,
109 [snd_soc_dapm_kcontrol] = 1,
110 [snd_soc_dapm_adc] = 2,
111 [snd_soc_dapm_hp] = 3,
112 [snd_soc_dapm_spk] = 3,
113 [snd_soc_dapm_line] = 3,
114 [snd_soc_dapm_out_drv] = 3,
115 [snd_soc_dapm_sink] = 3,
116 [snd_soc_dapm_pga] = 4,
117 [snd_soc_dapm_buffer] = 4,
118 [snd_soc_dapm_scheduler] = 4,
119 [snd_soc_dapm_effect] = 4,
120 [snd_soc_dapm_src] = 4,
121 [snd_soc_dapm_asrc] = 4,
122 [snd_soc_dapm_encoder] = 4,
123 [snd_soc_dapm_decoder] = 4,
124 [snd_soc_dapm_switch] = 5,
125 [snd_soc_dapm_mixer_named_ctl] = 5,
126 [snd_soc_dapm_mixer] = 5,
127 [snd_soc_dapm_dac] = 6,
128 [snd_soc_dapm_mic] = 7,
129 [snd_soc_dapm_siggen] = 7,
130 [snd_soc_dapm_input] = 7,
131 [snd_soc_dapm_output] = 7,
132 [snd_soc_dapm_micbias] = 8,
133 [snd_soc_dapm_vmid] = 8,
134 [snd_soc_dapm_mux] = 9,
135 [snd_soc_dapm_demux] = 9,
136 [snd_soc_dapm_aif_in] = 10,
137 [snd_soc_dapm_aif_out] = 10,
138 [snd_soc_dapm_dai_in] = 10,
139 [snd_soc_dapm_dai_out] = 10,
140 [snd_soc_dapm_dai_link] = 11,
141 [snd_soc_dapm_supply] = 12,
142 [snd_soc_dapm_clock_supply] = 13,
143 [snd_soc_dapm_pinctrl] = 13,
144 [snd_soc_dapm_regulator_supply] = 13,
145 [snd_soc_dapm_post] = 14,
148 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
150 if (dapm->card && dapm->card->instantiated)
151 lockdep_assert_held(&dapm->card->dapm_mutex);
154 static void pop_wait(u32 pop_time)
157 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
160 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
168 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
173 vsnprintf(buf, PAGE_SIZE, fmt, args);
174 dev_info(dev, "%s", buf);
180 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
182 return !list_empty(&w->dirty);
185 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
187 dapm_assert_locked(w->dapm);
189 if (!dapm_dirty_widget(w)) {
190 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
192 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
197 * Common implementation for dapm_widget_invalidate_input_paths() and
198 * dapm_widget_invalidate_output_paths(). The function is inlined since the
199 * combined size of the two specialized functions is only marginally larger then
200 * the size of the generic function and at the same time the fast path of the
201 * specialized functions is significantly smaller than the generic function.
203 static __always_inline void dapm_widget_invalidate_paths(
204 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
206 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
207 struct snd_soc_dapm_widget *node;
208 struct snd_soc_dapm_path *p;
211 dapm_assert_locked(w->dapm);
213 if (w->endpoints[dir] == -1)
216 list_add_tail(&w->work_list, &list);
217 w->endpoints[dir] = -1;
219 list_for_each_entry(w, &list, work_list) {
220 snd_soc_dapm_widget_for_each_path(w, dir, p) {
221 if (p->is_supply || p->weak || !p->connect)
223 node = p->node[rdir];
224 if (node->endpoints[dir] != -1) {
225 node->endpoints[dir] = -1;
226 list_add_tail(&node->work_list, &list);
233 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
235 * @w: The widget for which to invalidate the cached number of input paths
237 * Resets the cached number of inputs for the specified widget and all widgets
238 * that can be reached via outcoming paths from the widget.
240 * This function must be called if the number of output paths for a widget might
241 * have changed. E.g. if the source state of a widget changes or a path is added
242 * or activated with the widget as the sink.
244 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
246 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
250 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
252 * @w: The widget for which to invalidate the cached number of output paths
254 * Resets the cached number of outputs for the specified widget and all widgets
255 * that can be reached via incoming paths from the widget.
257 * This function must be called if the number of output paths for a widget might
258 * have changed. E.g. if the sink state of a widget changes or a path is added
259 * or activated with the widget as the source.
261 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
263 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
267 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
268 * for the widgets connected to a path
269 * @p: The path to invalidate
271 * Resets the cached number of inputs for the sink of the path and the cached
272 * number of outputs for the source of the path.
274 * This function must be called when a path is added, removed or the connected
277 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
280 * Weak paths or supply paths do not influence the number of input or
281 * output paths of their neighbors.
283 if (p->weak || p->is_supply)
287 * The number of connected endpoints is the sum of the number of
288 * connected endpoints of all neighbors. If a node with 0 connected
289 * endpoints is either connected or disconnected that sum won't change,
290 * so there is no need to re-check the path.
292 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
293 dapm_widget_invalidate_input_paths(p->sink);
294 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
295 dapm_widget_invalidate_output_paths(p->source);
298 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
300 struct snd_soc_dapm_widget *w;
302 mutex_lock(&card->dapm_mutex);
304 list_for_each_entry(w, &card->widgets, list) {
306 dapm_mark_dirty(w, "Rechecking endpoints");
307 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
308 dapm_widget_invalidate_output_paths(w);
309 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
310 dapm_widget_invalidate_input_paths(w);
314 mutex_unlock(&card->dapm_mutex);
316 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
318 /* create a new dapm widget */
319 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
320 const struct snd_soc_dapm_widget *_widget)
322 struct snd_soc_dapm_widget *w;
324 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
329 * w->name is duplicated in caller, but w->sname isn't.
330 * Duplicate it here if defined
332 if (_widget->sname) {
333 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
340 struct dapm_kcontrol_data {
342 struct snd_soc_dapm_widget *widget;
343 struct list_head paths;
344 struct snd_soc_dapm_widget_list *wlist;
347 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
348 struct snd_kcontrol *kcontrol, const char *ctrl_name)
350 struct dapm_kcontrol_data *data;
351 struct soc_mixer_control *mc;
356 data = kzalloc(sizeof(*data), GFP_KERNEL);
360 INIT_LIST_HEAD(&data->paths);
362 switch (widget->id) {
363 case snd_soc_dapm_switch:
364 case snd_soc_dapm_mixer:
365 case snd_soc_dapm_mixer_named_ctl:
366 mc = (struct soc_mixer_control *)kcontrol->private_value;
368 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
369 dev_warn(widget->dapm->dev,
370 "ASoC: Unsupported stereo autodisable control '%s'\n",
373 if (mc->autodisable) {
374 struct snd_soc_dapm_widget template;
376 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
383 memset(&template, 0, sizeof(template));
384 template.reg = mc->reg;
385 template.mask = (1 << fls(mc->max)) - 1;
386 template.shift = mc->shift;
388 template.off_val = mc->max;
390 template.off_val = 0;
391 template.on_val = template.off_val;
392 template.id = snd_soc_dapm_kcontrol;
393 template.name = name;
395 data->value = template.on_val;
398 snd_soc_dapm_new_control_unlocked(widget->dapm,
401 if (IS_ERR(data->widget)) {
402 ret = PTR_ERR(data->widget);
407 case snd_soc_dapm_demux:
408 case snd_soc_dapm_mux:
409 e = (struct soc_enum *)kcontrol->private_value;
411 if (e->autodisable) {
412 struct snd_soc_dapm_widget template;
414 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
421 memset(&template, 0, sizeof(template));
422 template.reg = e->reg;
423 template.mask = e->mask << e->shift_l;
424 template.shift = e->shift_l;
425 template.off_val = snd_soc_enum_item_to_val(e, 0);
426 template.on_val = template.off_val;
427 template.id = snd_soc_dapm_kcontrol;
428 template.name = name;
430 data->value = template.on_val;
432 data->widget = snd_soc_dapm_new_control_unlocked(
433 widget->dapm, &template);
435 if (IS_ERR(data->widget)) {
436 ret = PTR_ERR(data->widget);
440 snd_soc_dapm_add_path(widget->dapm, data->widget,
448 kcontrol->private_data = data;
457 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
459 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
461 list_del(&data->paths);
466 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
467 const struct snd_kcontrol *kcontrol)
469 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
474 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
475 struct snd_soc_dapm_widget *widget)
477 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
478 struct snd_soc_dapm_widget_list *new_wlist;
482 n = data->wlist->num_widgets + 1;
486 new_wlist = krealloc(data->wlist,
487 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
491 new_wlist->widgets[n - 1] = widget;
492 new_wlist->num_widgets = n;
494 data->wlist = new_wlist;
499 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
500 struct snd_soc_dapm_path *path)
502 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
504 list_add_tail(&path->list_kcontrol, &data->paths);
507 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
509 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
514 return data->widget->power;
517 static struct list_head *dapm_kcontrol_get_path_list(
518 const struct snd_kcontrol *kcontrol)
520 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
525 #define dapm_kcontrol_for_each_path(path, kcontrol) \
526 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
529 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
531 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
535 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
537 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
540 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
542 if (data->value == value)
546 data->widget->on_val = value;
554 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
556 * @kcontrol: The kcontrol
558 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
559 struct snd_kcontrol *kcontrol)
561 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
563 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
566 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
568 * @kcontrol: The kcontrol
570 * Note: This function must only be used on kcontrols that are known to have
571 * been registered for a CODEC. Otherwise the behaviour is undefined.
573 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
574 struct snd_kcontrol *kcontrol)
576 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
578 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
580 static void dapm_reset(struct snd_soc_card *card)
582 struct snd_soc_dapm_widget *w;
584 lockdep_assert_held(&card->dapm_mutex);
586 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
588 list_for_each_entry(w, &card->widgets, list) {
589 w->new_power = w->power;
590 w->power_checked = false;
594 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
596 if (!dapm->component)
598 return dapm->component->name_prefix;
601 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
604 if (!dapm->component)
606 return snd_soc_component_read(dapm->component, reg, value);
609 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
610 int reg, unsigned int mask, unsigned int value)
612 if (!dapm->component)
614 return snd_soc_component_update_bits(dapm->component, reg,
618 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
619 int reg, unsigned int mask, unsigned int value)
621 if (!dapm->component)
623 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
626 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
629 snd_soc_component_async_complete(dapm->component);
632 static struct snd_soc_dapm_widget *
633 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
635 struct snd_soc_dapm_widget *w = wcache->widget;
636 struct list_head *wlist;
641 wlist = &w->dapm->card->widgets;
643 list_for_each_entry_from(w, wlist, list) {
644 if (!strcmp(name, w->name))
655 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
656 struct snd_soc_dapm_widget *w)
662 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
663 * @dapm: The DAPM context for which to set the level
664 * @level: The level to set
666 * Forces the DAPM bias level to a specific state. It will call the bias level
667 * callback of DAPM context with the specified level. This will even happen if
668 * the context is already at the same level. Furthermore it will not go through
669 * the normal bias level sequencing, meaning any intermediate states between the
670 * current and the target state will not be entered.
672 * Note that the change in bias level is only temporary and the next time
673 * snd_soc_dapm_sync() is called the state will be set to the level as
674 * determined by the DAPM core. The function is mainly intended to be used to
675 * used during probe or resume from suspend to power up the device so
676 * initialization can be done, before the DAPM core takes over.
678 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
679 enum snd_soc_bias_level level)
683 if (dapm->set_bias_level)
684 ret = dapm->set_bias_level(dapm, level);
687 dapm->bias_level = level;
691 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
694 * snd_soc_dapm_set_bias_level - set the bias level for the system
695 * @dapm: DAPM context
696 * @level: level to configure
698 * Configure the bias (power) levels for the SoC audio device.
700 * Returns 0 for success else error.
702 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
703 enum snd_soc_bias_level level)
705 struct snd_soc_card *card = dapm->card;
708 trace_snd_soc_bias_level_start(card, level);
710 if (card && card->set_bias_level)
711 ret = card->set_bias_level(card, dapm, level);
715 if (!card || dapm != &card->dapm)
716 ret = snd_soc_dapm_force_bias_level(dapm, level);
721 if (card && card->set_bias_level_post)
722 ret = card->set_bias_level_post(card, dapm, level);
724 trace_snd_soc_bias_level_done(card, level);
729 /* connect mux widget to its interconnecting audio paths */
730 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
731 struct snd_soc_dapm_path *path, const char *control_name,
732 struct snd_soc_dapm_widget *w)
734 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
735 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
736 unsigned int val, item;
739 if (e->reg != SND_SOC_NOPM) {
740 soc_dapm_read(dapm, e->reg, &val);
741 val = (val >> e->shift_l) & e->mask;
742 item = snd_soc_enum_val_to_item(e, val);
744 /* since a virtual mux has no backing registers to
745 * decide which path to connect, it will try to match
746 * with the first enumeration. This is to ensure
747 * that the default mux choice (the first) will be
748 * correctly powered up during initialization.
753 i = match_string(e->texts, e->items, control_name);
757 path->name = e->texts[i];
758 path->connect = (i == item);
763 /* set up initial codec paths */
764 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
767 struct soc_mixer_control *mc = (struct soc_mixer_control *)
768 p->sink->kcontrol_news[i].private_value;
769 unsigned int reg = mc->reg;
770 unsigned int shift = mc->shift;
771 unsigned int max = mc->max;
772 unsigned int mask = (1 << fls(max)) - 1;
773 unsigned int invert = mc->invert;
776 if (reg != SND_SOC_NOPM) {
777 soc_dapm_read(p->sink->dapm, reg, &val);
779 * The nth_path argument allows this function to know
780 * which path of a kcontrol it is setting the initial
781 * status for. Ideally this would support any number
782 * of paths and channels. But since kcontrols only come
783 * in mono and stereo variants, we are limited to 2
786 * The following code assumes for stereo controls the
787 * first path is the left channel, and all remaining
788 * paths are the right channel.
790 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
792 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
793 val = (val >> mc->rshift) & mask;
795 val = (val >> shift) & mask;
805 /* connect mixer widget to its interconnecting audio paths */
806 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
807 struct snd_soc_dapm_path *path, const char *control_name)
811 /* search for mixer kcontrol */
812 for (i = 0; i < path->sink->num_kcontrols; i++) {
813 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
814 path->name = path->sink->kcontrol_news[i].name;
815 dapm_set_mixer_path_status(path, i, nth_path++);
822 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
823 struct snd_soc_dapm_widget *kcontrolw,
824 const struct snd_kcontrol_new *kcontrol_new,
825 struct snd_kcontrol **kcontrol)
827 struct snd_soc_dapm_widget *w;
832 list_for_each_entry(w, &dapm->card->widgets, list) {
833 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
835 for (i = 0; i < w->num_kcontrols; i++) {
836 if (&w->kcontrol_news[i] == kcontrol_new) {
838 *kcontrol = w->kcontrols[i];
848 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
849 * create it. Either way, add the widget into the control's widget list
851 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
854 struct snd_soc_dapm_context *dapm = w->dapm;
855 struct snd_card *card = dapm->card->snd_card;
859 struct snd_kcontrol *kcontrol;
860 bool wname_in_long_name, kcname_in_long_name;
861 char *long_name = NULL;
865 prefix = soc_dapm_prefix(dapm);
867 prefix_len = strlen(prefix) + 1;
871 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
876 wname_in_long_name = false;
877 kcname_in_long_name = true;
880 case snd_soc_dapm_switch:
881 case snd_soc_dapm_mixer:
882 case snd_soc_dapm_pga:
883 case snd_soc_dapm_out_drv:
884 wname_in_long_name = true;
885 kcname_in_long_name = true;
887 case snd_soc_dapm_mixer_named_ctl:
888 wname_in_long_name = false;
889 kcname_in_long_name = true;
891 case snd_soc_dapm_demux:
892 case snd_soc_dapm_mux:
893 wname_in_long_name = true;
894 kcname_in_long_name = false;
901 if (wname_in_long_name && kcname_in_long_name) {
903 * The control will get a prefix from the control
904 * creation process but we're also using the same
905 * prefix for widgets so cut the prefix off the
906 * front of the widget name.
908 long_name = kasprintf(GFP_KERNEL, "%s %s",
909 w->name + prefix_len,
910 w->kcontrol_news[kci].name);
911 if (long_name == NULL)
915 } else if (wname_in_long_name) {
917 name = w->name + prefix_len;
920 name = w->kcontrol_news[kci].name;
923 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
930 kcontrol->private_free = dapm_kcontrol_free;
932 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
934 snd_ctl_free_one(kcontrol);
938 ret = snd_ctl_add(card, kcontrol);
941 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
947 ret = dapm_kcontrol_add_widget(kcontrol, w);
949 w->kcontrols[kci] = kcontrol;
957 /* create new dapm mixer control */
958 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
961 struct snd_soc_dapm_path *path;
962 struct dapm_kcontrol_data *data;
965 for (i = 0; i < w->num_kcontrols; i++) {
967 snd_soc_dapm_widget_for_each_source_path(w, path) {
968 /* mixer/mux paths name must match control name */
969 if (path->name != (char *)w->kcontrol_news[i].name)
972 if (!w->kcontrols[i]) {
973 ret = dapm_create_or_share_kcontrol(w, i);
978 dapm_kcontrol_add_path(w->kcontrols[i], path);
980 data = snd_kcontrol_chip(w->kcontrols[i]);
982 snd_soc_dapm_add_path(data->widget->dapm,
992 /* create new dapm mux control */
993 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
995 struct snd_soc_dapm_context *dapm = w->dapm;
996 enum snd_soc_dapm_direction dir;
997 struct snd_soc_dapm_path *path;
1002 case snd_soc_dapm_mux:
1003 dir = SND_SOC_DAPM_DIR_OUT;
1006 case snd_soc_dapm_demux:
1007 dir = SND_SOC_DAPM_DIR_IN;
1014 if (w->num_kcontrols != 1) {
1016 "ASoC: %s %s has incorrect number of controls\n", type,
1021 if (list_empty(&w->edges[dir])) {
1022 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1026 ret = dapm_create_or_share_kcontrol(w, 0);
1030 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1032 dapm_kcontrol_add_path(w->kcontrols[0], path);
1038 /* create new dapm volume control */
1039 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1043 for (i = 0; i < w->num_kcontrols; i++) {
1044 ret = dapm_create_or_share_kcontrol(w, i);
1052 /* create new dapm dai link control */
1053 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1056 struct snd_kcontrol *kcontrol;
1057 struct snd_soc_dapm_context *dapm = w->dapm;
1058 struct snd_card *card = dapm->card->snd_card;
1059 struct snd_soc_pcm_runtime *rtd = w->priv;
1061 /* create control for links with > 1 config */
1062 if (rtd->dai_link->num_params <= 1)
1066 for (i = 0; i < w->num_kcontrols; i++) {
1067 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1069 ret = snd_ctl_add(card, kcontrol);
1072 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1073 w->name, w->kcontrol_news[i].name, ret);
1076 kcontrol->private_data = w;
1077 w->kcontrols[i] = kcontrol;
1083 /* We implement power down on suspend by checking the power state of
1084 * the ALSA card - when we are suspending the ALSA state for the card
1087 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1089 int level = snd_power_get_state(widget->dapm->card->snd_card);
1092 case SNDRV_CTL_POWER_D3hot:
1093 case SNDRV_CTL_POWER_D3cold:
1094 if (widget->ignore_suspend)
1095 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1097 return widget->ignore_suspend;
1103 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1104 struct list_head *widgets)
1106 struct snd_soc_dapm_widget *w;
1107 struct list_head *it;
1108 unsigned int size = 0;
1111 list_for_each(it, widgets)
1114 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1118 list_for_each_entry(w, widgets, work_list)
1119 (*list)->widgets[i++] = w;
1121 (*list)->num_widgets = i;
1127 * Common implementation for is_connected_output_ep() and
1128 * is_connected_input_ep(). The function is inlined since the combined size of
1129 * the two specialized functions is only marginally larger then the size of the
1130 * generic function and at the same time the fast path of the specialized
1131 * functions is significantly smaller than the generic function.
1133 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1134 struct list_head *list, enum snd_soc_dapm_direction dir,
1135 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1136 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1137 enum snd_soc_dapm_direction)),
1138 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1139 enum snd_soc_dapm_direction))
1141 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1142 struct snd_soc_dapm_path *path;
1145 if (widget->endpoints[dir] >= 0)
1146 return widget->endpoints[dir];
1148 DAPM_UPDATE_STAT(widget, path_checks);
1150 /* do we need to add this widget to the list ? */
1152 list_add_tail(&widget->work_list, list);
1154 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1155 widget->endpoints[dir] = 1;
1156 return widget->endpoints[dir];
1159 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1160 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1161 return widget->endpoints[dir];
1164 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1165 DAPM_UPDATE_STAT(widget, neighbour_checks);
1167 if (path->weak || path->is_supply)
1173 trace_snd_soc_dapm_path(widget, dir, path);
1175 if (path->connect) {
1177 con += fn(path->node[dir], list, custom_stop_condition);
1182 widget->endpoints[dir] = con;
1188 * Recursively check for a completed path to an active or physically connected
1189 * output widget. Returns number of complete paths.
1191 * Optionally, can be supplied with a function acting as a stopping condition.
1192 * This function takes the dapm widget currently being examined and the walk
1193 * direction as an arguments, it should return true if the walk should be
1194 * stopped and false otherwise.
1196 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1197 struct list_head *list,
1198 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1199 enum snd_soc_dapm_direction))
1201 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1202 is_connected_output_ep, custom_stop_condition);
1206 * Recursively check for a completed path to an active or physically connected
1207 * input widget. Returns number of complete paths.
1209 * Optionally, can be supplied with a function acting as a stopping condition.
1210 * This function takes the dapm widget currently being examined and the walk
1211 * direction as an arguments, it should return true if the walk should be
1212 * stopped and false otherwise.
1214 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1215 struct list_head *list,
1216 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1217 enum snd_soc_dapm_direction))
1219 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1220 is_connected_input_ep, custom_stop_condition);
1224 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1225 * @dai: the soc DAI.
1226 * @stream: stream direction.
1227 * @list: list of active widgets for this stream.
1228 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1229 * walk based on custom logic.
1231 * Queries DAPM graph as to whether a valid audio stream path exists for
1232 * the initial stream specified by name. This takes into account
1233 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1235 * Optionally, can be supplied with a function acting as a stopping condition.
1236 * This function takes the dapm widget currently being examined and the walk
1237 * direction as an arguments, it should return true if the walk should be
1238 * stopped and false otherwise.
1240 * Returns the number of valid paths or negative error.
1242 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1243 struct snd_soc_dapm_widget_list **list,
1244 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1245 enum snd_soc_dapm_direction))
1247 struct snd_soc_card *card = dai->component->card;
1248 struct snd_soc_dapm_widget *w;
1253 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1256 * For is_connected_{output,input}_ep fully discover the graph we need
1257 * to reset the cached number of inputs and outputs.
1259 list_for_each_entry(w, &card->widgets, list) {
1260 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1261 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1264 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1265 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1266 custom_stop_condition);
1268 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1269 custom_stop_condition);
1271 /* Drop starting point */
1272 list_del(widgets.next);
1274 ret = dapm_widget_list_create(list, &widgets);
1278 trace_snd_soc_dapm_connected(paths, stream);
1279 mutex_unlock(&card->dapm_mutex);
1285 * Handler for regulator supply widget.
1287 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1288 struct snd_kcontrol *kcontrol, int event)
1292 soc_dapm_async_complete(w->dapm);
1294 if (SND_SOC_DAPM_EVENT_ON(event)) {
1295 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1296 ret = regulator_allow_bypass(w->regulator, false);
1298 dev_warn(w->dapm->dev,
1299 "ASoC: Failed to unbypass %s: %d\n",
1303 return regulator_enable(w->regulator);
1305 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1306 ret = regulator_allow_bypass(w->regulator, true);
1308 dev_warn(w->dapm->dev,
1309 "ASoC: Failed to bypass %s: %d\n",
1313 return regulator_disable_deferred(w->regulator, w->shift);
1316 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1319 * Handler for pinctrl widget.
1321 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1322 struct snd_kcontrol *kcontrol, int event)
1324 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1325 struct pinctrl *p = w->pinctrl;
1326 struct pinctrl_state *s;
1331 if (SND_SOC_DAPM_EVENT_ON(event))
1332 s = pinctrl_lookup_state(p, priv->active_state);
1334 s = pinctrl_lookup_state(p, priv->sleep_state);
1339 return pinctrl_select_state(p, s);
1341 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1344 * Handler for clock supply widget.
1346 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1347 struct snd_kcontrol *kcontrol, int event)
1352 soc_dapm_async_complete(w->dapm);
1354 if (SND_SOC_DAPM_EVENT_ON(event)) {
1355 return clk_prepare_enable(w->clk);
1357 clk_disable_unprepare(w->clk);
1363 EXPORT_SYMBOL_GPL(dapm_clock_event);
1365 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1367 if (w->power_checked)
1368 return w->new_power;
1373 w->new_power = w->power_check(w);
1375 w->power_checked = true;
1377 return w->new_power;
1380 /* Generic check to see if a widget should be powered. */
1381 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1385 DAPM_UPDATE_STAT(w, power_checks);
1387 in = is_connected_input_ep(w, NULL, NULL);
1388 out = is_connected_output_ep(w, NULL, NULL);
1389 return out != 0 && in != 0;
1392 /* Check to see if a power supply is needed */
1393 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1395 struct snd_soc_dapm_path *path;
1397 DAPM_UPDATE_STAT(w, power_checks);
1399 /* Check if one of our outputs is connected */
1400 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1401 DAPM_UPDATE_STAT(w, neighbour_checks);
1406 if (path->connected &&
1407 !path->connected(path->source, path->sink))
1410 if (dapm_widget_power_check(path->sink))
1417 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1419 return w->connected;
1422 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1423 struct snd_soc_dapm_widget *b,
1431 sort = dapm_down_seq;
1433 if (sort[a->id] != sort[b->id])
1434 return sort[a->id] - sort[b->id];
1435 if (a->subseq != b->subseq) {
1437 return a->subseq - b->subseq;
1439 return b->subseq - a->subseq;
1441 if (a->reg != b->reg)
1442 return a->reg - b->reg;
1443 if (a->dapm != b->dapm)
1444 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1449 /* Insert a widget in order into a DAPM power sequence. */
1450 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1451 struct list_head *list,
1454 struct snd_soc_dapm_widget *w;
1456 list_for_each_entry(w, list, power_list)
1457 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1458 list_add_tail(&new_widget->power_list, &w->power_list);
1462 list_add_tail(&new_widget->power_list, list);
1465 static void dapm_seq_check_event(struct snd_soc_card *card,
1466 struct snd_soc_dapm_widget *w, int event)
1468 const char *ev_name;
1472 case SND_SOC_DAPM_PRE_PMU:
1473 ev_name = "PRE_PMU";
1476 case SND_SOC_DAPM_POST_PMU:
1477 ev_name = "POST_PMU";
1480 case SND_SOC_DAPM_PRE_PMD:
1481 ev_name = "PRE_PMD";
1484 case SND_SOC_DAPM_POST_PMD:
1485 ev_name = "POST_PMD";
1488 case SND_SOC_DAPM_WILL_PMU:
1489 ev_name = "WILL_PMU";
1492 case SND_SOC_DAPM_WILL_PMD:
1493 ev_name = "WILL_PMD";
1497 WARN(1, "Unknown event %d\n", event);
1501 if (w->new_power != power)
1504 if (w->event && (w->event_flags & event)) {
1505 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1507 soc_dapm_async_complete(w->dapm);
1508 trace_snd_soc_dapm_widget_event_start(w, event);
1509 ret = w->event(w, NULL, event);
1510 trace_snd_soc_dapm_widget_event_done(w, event);
1512 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1513 ev_name, w->name, ret);
1517 /* Apply the coalesced changes from a DAPM sequence */
1518 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1519 struct list_head *pending)
1521 struct snd_soc_dapm_context *dapm;
1522 struct snd_soc_dapm_widget *w;
1524 unsigned int value = 0;
1525 unsigned int mask = 0;
1527 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1531 list_for_each_entry(w, pending, power_list) {
1532 WARN_ON(reg != w->reg || dapm != w->dapm);
1533 w->power = w->new_power;
1535 mask |= w->mask << w->shift;
1537 value |= w->on_val << w->shift;
1539 value |= w->off_val << w->shift;
1541 pop_dbg(dapm->dev, card->pop_time,
1542 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1543 w->name, reg, value, mask);
1545 /* Check for events */
1546 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1547 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1551 /* Any widget will do, they should all be updating the
1555 pop_dbg(dapm->dev, card->pop_time,
1556 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1557 value, mask, reg, card->pop_time);
1558 pop_wait(card->pop_time);
1559 soc_dapm_update_bits(dapm, reg, mask, value);
1562 list_for_each_entry(w, pending, power_list) {
1563 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1564 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1568 /* Apply a DAPM power sequence.
1570 * We walk over a pre-sorted list of widgets to apply power to. In
1571 * order to minimise the number of writes to the device required
1572 * multiple widgets will be updated in a single write where possible.
1573 * Currently anything that requires more than a single write is not
1576 static void dapm_seq_run(struct snd_soc_card *card,
1577 struct list_head *list, int event, bool power_up)
1579 struct snd_soc_dapm_widget *w, *n;
1580 struct snd_soc_dapm_context *d;
1583 int cur_subseq = -1;
1584 int cur_reg = SND_SOC_NOPM;
1585 struct snd_soc_dapm_context *cur_dapm = NULL;
1592 sort = dapm_down_seq;
1594 list_for_each_entry_safe(w, n, list, power_list) {
1597 /* Do we need to apply any queued changes? */
1598 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1599 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1600 if (!list_empty(&pending))
1601 dapm_seq_run_coalesced(card, &pending);
1603 if (cur_dapm && cur_dapm->seq_notifier) {
1604 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1605 if (sort[i] == cur_sort)
1606 cur_dapm->seq_notifier(cur_dapm,
1611 if (cur_dapm && w->dapm != cur_dapm)
1612 soc_dapm_async_complete(cur_dapm);
1614 INIT_LIST_HEAD(&pending);
1616 cur_subseq = INT_MIN;
1617 cur_reg = SND_SOC_NOPM;
1622 case snd_soc_dapm_pre:
1624 list_for_each_entry_safe_continue(w, n, list,
1627 if (event == SND_SOC_DAPM_STREAM_START)
1629 NULL, SND_SOC_DAPM_PRE_PMU);
1630 else if (event == SND_SOC_DAPM_STREAM_STOP)
1632 NULL, SND_SOC_DAPM_PRE_PMD);
1635 case snd_soc_dapm_post:
1637 list_for_each_entry_safe_continue(w, n, list,
1640 if (event == SND_SOC_DAPM_STREAM_START)
1642 NULL, SND_SOC_DAPM_POST_PMU);
1643 else if (event == SND_SOC_DAPM_STREAM_STOP)
1645 NULL, SND_SOC_DAPM_POST_PMD);
1649 /* Queue it up for application */
1650 cur_sort = sort[w->id];
1651 cur_subseq = w->subseq;
1654 list_move(&w->power_list, &pending);
1659 dev_err(w->dapm->dev,
1660 "ASoC: Failed to apply widget power: %d\n", ret);
1663 if (!list_empty(&pending))
1664 dapm_seq_run_coalesced(card, &pending);
1666 if (cur_dapm && cur_dapm->seq_notifier) {
1667 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1668 if (sort[i] == cur_sort)
1669 cur_dapm->seq_notifier(cur_dapm,
1673 list_for_each_entry(d, &card->dapm_list, list) {
1674 soc_dapm_async_complete(d);
1678 static void dapm_widget_update(struct snd_soc_card *card)
1680 struct snd_soc_dapm_update *update = card->update;
1681 struct snd_soc_dapm_widget_list *wlist;
1682 struct snd_soc_dapm_widget *w = NULL;
1686 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1689 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1691 for (wi = 0; wi < wlist->num_widgets; wi++) {
1692 w = wlist->widgets[wi];
1694 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1695 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1697 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1705 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1708 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1711 if (update->has_second_set) {
1712 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1713 update->mask2, update->val2);
1715 dev_err(w->dapm->dev,
1716 "ASoC: %s DAPM update failed: %d\n",
1720 for (wi = 0; wi < wlist->num_widgets; wi++) {
1721 w = wlist->widgets[wi];
1723 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1724 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1726 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1732 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1733 * they're changing state.
1735 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1737 struct snd_soc_dapm_context *d = data;
1740 /* If we're off and we're not supposed to go into STANDBY */
1741 if (d->bias_level == SND_SOC_BIAS_OFF &&
1742 d->target_bias_level != SND_SOC_BIAS_OFF) {
1744 pm_runtime_get_sync(d->dev);
1746 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1749 "ASoC: Failed to turn on bias: %d\n", ret);
1752 /* Prepare for a transition to ON or away from ON */
1753 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1754 d->bias_level != SND_SOC_BIAS_ON) ||
1755 (d->target_bias_level != SND_SOC_BIAS_ON &&
1756 d->bias_level == SND_SOC_BIAS_ON)) {
1757 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1760 "ASoC: Failed to prepare bias: %d\n", ret);
1764 /* Async callback run prior to DAPM sequences - brings to their final
1767 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1769 struct snd_soc_dapm_context *d = data;
1772 /* If we just powered the last thing off drop to standby bias */
1773 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1774 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1775 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1776 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1778 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1782 /* If we're in standby and can support bias off then do that */
1783 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1784 d->target_bias_level == SND_SOC_BIAS_OFF) {
1785 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1787 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1791 pm_runtime_put(d->dev);
1794 /* If we just powered up then move to active bias */
1795 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1796 d->target_bias_level == SND_SOC_BIAS_ON) {
1797 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1799 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1804 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1805 bool power, bool connect)
1807 /* If a connection is being made or broken then that update
1808 * will have marked the peer dirty, otherwise the widgets are
1809 * not connected and this update has no impact. */
1813 /* If the peer is already in the state we're moving to then we
1814 * won't have an impact on it. */
1815 if (power != peer->power)
1816 dapm_mark_dirty(peer, "peer state change");
1819 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1820 struct list_head *up_list,
1821 struct list_head *down_list)
1823 struct snd_soc_dapm_path *path;
1825 if (w->power == power)
1828 trace_snd_soc_dapm_widget_power(w, power);
1830 /* If we changed our power state perhaps our neigbours changed
1833 snd_soc_dapm_widget_for_each_source_path(w, path)
1834 dapm_widget_set_peer_power(path->source, power, path->connect);
1836 /* Supplies can't affect their outputs, only their inputs */
1837 if (!w->is_supply) {
1838 snd_soc_dapm_widget_for_each_sink_path(w, path)
1839 dapm_widget_set_peer_power(path->sink, power,
1844 dapm_seq_insert(w, up_list, true);
1846 dapm_seq_insert(w, down_list, false);
1849 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1850 struct list_head *up_list,
1851 struct list_head *down_list)
1856 case snd_soc_dapm_pre:
1857 dapm_seq_insert(w, down_list, false);
1859 case snd_soc_dapm_post:
1860 dapm_seq_insert(w, up_list, true);
1864 power = dapm_widget_power_check(w);
1866 dapm_widget_set_power(w, power, up_list, down_list);
1871 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1873 if (dapm->idle_bias_off)
1876 switch (snd_power_get_state(dapm->card->snd_card)) {
1877 case SNDRV_CTL_POWER_D3hot:
1878 case SNDRV_CTL_POWER_D3cold:
1879 return dapm->suspend_bias_off;
1888 * Scan each dapm widget for complete audio path.
1889 * A complete path is a route that has valid endpoints i.e.:-
1891 * o DAC to output pin.
1892 * o Input pin to ADC.
1893 * o Input pin to Output pin (bypass, sidetone)
1894 * o DAC to ADC (loopback).
1896 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1898 struct snd_soc_dapm_widget *w;
1899 struct snd_soc_dapm_context *d;
1901 LIST_HEAD(down_list);
1902 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1903 enum snd_soc_bias_level bias;
1905 lockdep_assert_held(&card->dapm_mutex);
1907 trace_snd_soc_dapm_start(card);
1909 list_for_each_entry(d, &card->dapm_list, list) {
1910 if (dapm_idle_bias_off(d))
1911 d->target_bias_level = SND_SOC_BIAS_OFF;
1913 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1918 /* Check which widgets we need to power and store them in
1919 * lists indicating if they should be powered up or down. We
1920 * only check widgets that have been flagged as dirty but note
1921 * that new widgets may be added to the dirty list while we
1924 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1925 dapm_power_one_widget(w, &up_list, &down_list);
1928 list_for_each_entry(w, &card->widgets, list) {
1930 case snd_soc_dapm_pre:
1931 case snd_soc_dapm_post:
1932 /* These widgets always need to be powered */
1935 list_del_init(&w->dirty);
1942 /* Supplies and micbiases only bring the
1943 * context up to STANDBY as unless something
1944 * else is active and passing audio they
1945 * generally don't require full power. Signal
1946 * generators are virtual pins and have no
1947 * power impact themselves.
1950 case snd_soc_dapm_siggen:
1951 case snd_soc_dapm_vmid:
1953 case snd_soc_dapm_supply:
1954 case snd_soc_dapm_regulator_supply:
1955 case snd_soc_dapm_pinctrl:
1956 case snd_soc_dapm_clock_supply:
1957 case snd_soc_dapm_micbias:
1958 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1959 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1962 d->target_bias_level = SND_SOC_BIAS_ON;
1969 /* Force all contexts in the card to the same bias state if
1970 * they're not ground referenced.
1972 bias = SND_SOC_BIAS_OFF;
1973 list_for_each_entry(d, &card->dapm_list, list)
1974 if (d->target_bias_level > bias)
1975 bias = d->target_bias_level;
1976 list_for_each_entry(d, &card->dapm_list, list)
1977 if (!dapm_idle_bias_off(d))
1978 d->target_bias_level = bias;
1980 trace_snd_soc_dapm_walk_done(card);
1982 /* Run card bias changes at first */
1983 dapm_pre_sequence_async(&card->dapm, 0);
1984 /* Run other bias changes in parallel */
1985 list_for_each_entry(d, &card->dapm_list, list) {
1986 if (d != &card->dapm && d->bias_level != d->target_bias_level)
1987 async_schedule_domain(dapm_pre_sequence_async, d,
1990 async_synchronize_full_domain(&async_domain);
1992 list_for_each_entry(w, &down_list, power_list) {
1993 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1996 list_for_each_entry(w, &up_list, power_list) {
1997 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2000 /* Power down widgets first; try to avoid amplifying pops. */
2001 dapm_seq_run(card, &down_list, event, false);
2003 dapm_widget_update(card);
2006 dapm_seq_run(card, &up_list, event, true);
2008 /* Run all the bias changes in parallel */
2009 list_for_each_entry(d, &card->dapm_list, list) {
2010 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2011 async_schedule_domain(dapm_post_sequence_async, d,
2014 async_synchronize_full_domain(&async_domain);
2015 /* Run card bias changes at last */
2016 dapm_post_sequence_async(&card->dapm, 0);
2018 /* do we need to notify any clients that DAPM event is complete */
2019 list_for_each_entry(d, &card->dapm_list, list) {
2020 if (d->stream_event)
2021 d->stream_event(d, event);
2024 pop_dbg(card->dev, card->pop_time,
2025 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2026 pop_wait(card->pop_time);
2028 trace_snd_soc_dapm_done(card);
2033 #ifdef CONFIG_DEBUG_FS
2034 static ssize_t dapm_widget_power_read_file(struct file *file,
2035 char __user *user_buf,
2036 size_t count, loff_t *ppos)
2038 struct snd_soc_dapm_widget *w = file->private_data;
2039 struct snd_soc_card *card = w->dapm->card;
2040 enum snd_soc_dapm_direction dir, rdir;
2044 struct snd_soc_dapm_path *p = NULL;
2046 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2050 mutex_lock(&card->dapm_mutex);
2052 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2057 in = is_connected_input_ep(w, NULL, NULL);
2058 out = is_connected_output_ep(w, NULL, NULL);
2061 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2062 w->name, w->power ? "On" : "Off",
2063 w->force ? " (forced)" : "", in, out);
2066 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2067 " - R%d(0x%x) mask 0x%x",
2068 w->reg, w->reg, w->mask << w->shift);
2070 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2073 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2075 w->active ? "active" : "inactive");
2077 snd_soc_dapm_for_each_direction(dir) {
2078 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2079 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2080 if (p->connected && !p->connected(p->source, p->sink))
2086 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2087 " %s \"%s\" \"%s\"\n",
2088 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2089 p->name ? p->name : "static",
2090 p->node[rdir]->name);
2094 mutex_unlock(&card->dapm_mutex);
2096 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2102 static const struct file_operations dapm_widget_power_fops = {
2103 .open = simple_open,
2104 .read = dapm_widget_power_read_file,
2105 .llseek = default_llseek,
2108 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2109 size_t count, loff_t *ppos)
2111 struct snd_soc_dapm_context *dapm = file->private_data;
2114 switch (dapm->bias_level) {
2115 case SND_SOC_BIAS_ON:
2118 case SND_SOC_BIAS_PREPARE:
2119 level = "Prepare\n";
2121 case SND_SOC_BIAS_STANDBY:
2122 level = "Standby\n";
2124 case SND_SOC_BIAS_OFF:
2128 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2129 level = "Unknown\n";
2133 return simple_read_from_buffer(user_buf, count, ppos, level,
2137 static const struct file_operations dapm_bias_fops = {
2138 .open = simple_open,
2139 .read = dapm_bias_read_file,
2140 .llseek = default_llseek,
2143 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2144 struct dentry *parent)
2151 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2153 if (!dapm->debugfs_dapm) {
2155 "ASoC: Failed to create DAPM debugfs directory\n");
2159 d = debugfs_create_file("bias_level", 0444,
2160 dapm->debugfs_dapm, dapm,
2164 "ASoC: Failed to create bias level debugfs file\n");
2167 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2169 struct snd_soc_dapm_context *dapm = w->dapm;
2172 if (!dapm->debugfs_dapm || !w->name)
2175 d = debugfs_create_file(w->name, 0444,
2176 dapm->debugfs_dapm, w,
2177 &dapm_widget_power_fops);
2179 dev_warn(w->dapm->dev,
2180 "ASoC: Failed to create %s debugfs file\n",
2184 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2186 debugfs_remove_recursive(dapm->debugfs_dapm);
2190 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2191 struct dentry *parent)
2195 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2199 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2206 * soc_dapm_connect_path() - Connects or disconnects a path
2207 * @path: The path to update
2208 * @connect: The new connect state of the path. True if the path is connected,
2209 * false if it is disconnected.
2210 * @reason: The reason why the path changed (for debugging only)
2212 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2213 bool connect, const char *reason)
2215 if (path->connect == connect)
2218 path->connect = connect;
2219 dapm_mark_dirty(path->source, reason);
2220 dapm_mark_dirty(path->sink, reason);
2221 dapm_path_invalidate(path);
2224 /* test and update the power status of a mux widget */
2225 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2226 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2228 struct snd_soc_dapm_path *path;
2232 lockdep_assert_held(&card->dapm_mutex);
2234 /* find dapm widget path assoc with kcontrol */
2235 dapm_kcontrol_for_each_path(path, kcontrol) {
2237 /* we now need to match the string in the enum to the path */
2238 if (!(strcmp(path->name, e->texts[mux])))
2243 soc_dapm_connect_path(path, connect, "mux update");
2247 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2252 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2253 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2254 struct snd_soc_dapm_update *update)
2256 struct snd_soc_card *card = dapm->card;
2259 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2260 card->update = update;
2261 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2262 card->update = NULL;
2263 mutex_unlock(&card->dapm_mutex);
2265 soc_dpcm_runtime_update(card);
2268 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2270 /* test and update the power status of a mixer or switch widget */
2271 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2272 struct snd_kcontrol *kcontrol,
2273 int connect, int rconnect)
2275 struct snd_soc_dapm_path *path;
2278 lockdep_assert_held(&card->dapm_mutex);
2280 /* find dapm widget path assoc with kcontrol */
2281 dapm_kcontrol_for_each_path(path, kcontrol) {
2283 * Ideally this function should support any number of
2284 * paths and channels. But since kcontrols only come
2285 * in mono and stereo variants, we are limited to 2
2288 * The following code assumes for stereo controls the
2289 * first path (when 'found == 0') is the left channel,
2290 * and all remaining paths (when 'found == 1') are the
2293 * A stereo control is signified by a valid 'rconnect'
2294 * value, either 0 for unconnected, or >= 0 for connected.
2295 * This is chosen instead of using snd_soc_volsw_is_stereo,
2296 * so that the behavior of snd_soc_dapm_mixer_update_power
2297 * doesn't change even when the kcontrol passed in is
2300 * It passes 'connect' as the path connect status for
2301 * the left channel, and 'rconnect' for the right
2304 if (found && rconnect >= 0)
2305 soc_dapm_connect_path(path, rconnect, "mixer update");
2307 soc_dapm_connect_path(path, connect, "mixer update");
2312 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2317 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2318 struct snd_kcontrol *kcontrol, int connect,
2319 struct snd_soc_dapm_update *update)
2321 struct snd_soc_card *card = dapm->card;
2324 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2325 card->update = update;
2326 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2327 card->update = NULL;
2328 mutex_unlock(&card->dapm_mutex);
2330 soc_dpcm_runtime_update(card);
2333 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2335 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2338 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2339 struct snd_soc_dapm_widget *w;
2341 char *state = "not set";
2343 /* card won't be set for the dummy component, as a spot fix
2344 * we're checking for that case specifically here but in future
2345 * we will ensure that the dummy component looks like others.
2350 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2351 if (w->dapm != dapm)
2354 /* only display widgets that burn power */
2356 case snd_soc_dapm_hp:
2357 case snd_soc_dapm_mic:
2358 case snd_soc_dapm_spk:
2359 case snd_soc_dapm_line:
2360 case snd_soc_dapm_micbias:
2361 case snd_soc_dapm_dac:
2362 case snd_soc_dapm_adc:
2363 case snd_soc_dapm_pga:
2364 case snd_soc_dapm_out_drv:
2365 case snd_soc_dapm_mixer:
2366 case snd_soc_dapm_mixer_named_ctl:
2367 case snd_soc_dapm_supply:
2368 case snd_soc_dapm_regulator_supply:
2369 case snd_soc_dapm_pinctrl:
2370 case snd_soc_dapm_clock_supply:
2372 count += sprintf(buf + count, "%s: %s\n",
2373 w->name, w->power ? "On":"Off");
2380 switch (snd_soc_dapm_get_bias_level(dapm)) {
2381 case SND_SOC_BIAS_ON:
2384 case SND_SOC_BIAS_PREPARE:
2387 case SND_SOC_BIAS_STANDBY:
2390 case SND_SOC_BIAS_OFF:
2394 count += sprintf(buf + count, "PM State: %s\n", state);
2399 /* show dapm widget status in sys fs */
2400 static ssize_t dapm_widget_show(struct device *dev,
2401 struct device_attribute *attr, char *buf)
2403 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2404 struct snd_soc_dai *codec_dai;
2407 mutex_lock(&rtd->card->dapm_mutex);
2409 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2410 struct snd_soc_component *cmpnt = codec_dai->component;
2412 count += dapm_widget_show_component(cmpnt, buf + count);
2415 mutex_unlock(&rtd->card->dapm_mutex);
2420 static DEVICE_ATTR_RO(dapm_widget);
2422 struct attribute *soc_dapm_dev_attrs[] = {
2423 &dev_attr_dapm_widget.attr,
2427 static void dapm_free_path(struct snd_soc_dapm_path *path)
2429 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2430 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2431 list_del(&path->list_kcontrol);
2432 list_del(&path->list);
2436 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2438 struct snd_soc_dapm_path *p, *next_p;
2439 enum snd_soc_dapm_direction dir;
2443 * remove source and sink paths associated to this widget.
2444 * While removing the path, remove reference to it from both
2445 * source and sink widgets so that path is removed only once.
2447 snd_soc_dapm_for_each_direction(dir) {
2448 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2452 kfree(w->kcontrols);
2453 kfree_const(w->name);
2454 kfree_const(w->sname);
2458 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2460 dapm->path_sink_cache.widget = NULL;
2461 dapm->path_source_cache.widget = NULL;
2464 /* free all dapm widgets and resources */
2465 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2467 struct snd_soc_dapm_widget *w, *next_w;
2469 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2470 if (w->dapm != dapm)
2472 snd_soc_dapm_free_widget(w);
2474 snd_soc_dapm_reset_cache(dapm);
2477 static struct snd_soc_dapm_widget *dapm_find_widget(
2478 struct snd_soc_dapm_context *dapm, const char *pin,
2479 bool search_other_contexts)
2481 struct snd_soc_dapm_widget *w;
2482 struct snd_soc_dapm_widget *fallback = NULL;
2484 list_for_each_entry(w, &dapm->card->widgets, list) {
2485 if (!strcmp(w->name, pin)) {
2486 if (w->dapm == dapm)
2493 if (search_other_contexts)
2499 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2500 const char *pin, int status)
2502 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2504 dapm_assert_locked(dapm);
2507 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2511 if (w->connected != status) {
2512 dapm_mark_dirty(w, "pin configuration");
2513 dapm_widget_invalidate_input_paths(w);
2514 dapm_widget_invalidate_output_paths(w);
2517 w->connected = status;
2525 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2526 * @dapm: DAPM context
2528 * Walks all dapm audio paths and powers widgets according to their
2529 * stream or path usage.
2531 * Requires external locking.
2533 * Returns 0 for success.
2535 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2538 * Suppress early reports (eg, jacks syncing their state) to avoid
2539 * silly DAPM runs during card startup.
2541 if (!dapm->card || !dapm->card->instantiated)
2544 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2546 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2549 * snd_soc_dapm_sync - scan and power dapm paths
2550 * @dapm: DAPM context
2552 * Walks all dapm audio paths and powers widgets according to their
2553 * stream or path usage.
2555 * Returns 0 for success.
2557 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2561 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2562 ret = snd_soc_dapm_sync_unlocked(dapm);
2563 mutex_unlock(&dapm->card->dapm_mutex);
2566 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2568 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2569 struct snd_soc_dapm_widget *w,
2573 case snd_soc_dapm_aif_out:
2574 case snd_soc_dapm_aif_in:
2580 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2581 w->channel < channels ? "Connecting" : "Disconnecting",
2582 p->source->name, p->sink->name);
2584 if (w->channel < channels)
2585 soc_dapm_connect_path(p, true, "dai update");
2587 soc_dapm_connect_path(p, false, "dai update");
2592 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2593 struct snd_pcm_hw_params *params,
2594 struct snd_soc_dai *dai)
2596 int dir = substream->stream;
2597 int channels = params_channels(params);
2598 struct snd_soc_dapm_path *p;
2599 struct snd_soc_dapm_widget *w;
2602 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
2603 w = dai->playback_widget;
2605 w = dai->capture_widget;
2610 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2611 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2613 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2614 ret = dapm_update_dai_chan(p, p->sink, channels);
2619 snd_soc_dapm_widget_for_each_source_path(w, p) {
2620 ret = dapm_update_dai_chan(p, p->source, channels);
2628 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2629 struct snd_pcm_hw_params *params,
2630 struct snd_soc_dai *dai)
2632 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2635 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2636 ret = dapm_update_dai_unlocked(substream, params, dai);
2637 mutex_unlock(&rtd->card->dapm_mutex);
2641 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2644 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2645 * @w: The widget for which to update the flags
2647 * Some widgets have a dynamic category which depends on which neighbors they
2648 * are connected to. This function update the category for these widgets.
2650 * This function must be called whenever a path is added or removed to a widget.
2652 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2654 enum snd_soc_dapm_direction dir;
2655 struct snd_soc_dapm_path *p;
2659 case snd_soc_dapm_input:
2660 /* On a fully routed card an input is never a source */
2661 if (w->dapm->card->fully_routed)
2663 ep = SND_SOC_DAPM_EP_SOURCE;
2664 snd_soc_dapm_widget_for_each_source_path(w, p) {
2665 if (p->source->id == snd_soc_dapm_micbias ||
2666 p->source->id == snd_soc_dapm_mic ||
2667 p->source->id == snd_soc_dapm_line ||
2668 p->source->id == snd_soc_dapm_output) {
2674 case snd_soc_dapm_output:
2675 /* On a fully routed card a output is never a sink */
2676 if (w->dapm->card->fully_routed)
2678 ep = SND_SOC_DAPM_EP_SINK;
2679 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2680 if (p->sink->id == snd_soc_dapm_spk ||
2681 p->sink->id == snd_soc_dapm_hp ||
2682 p->sink->id == snd_soc_dapm_line ||
2683 p->sink->id == snd_soc_dapm_input) {
2689 case snd_soc_dapm_line:
2691 snd_soc_dapm_for_each_direction(dir) {
2692 if (!list_empty(&w->edges[dir]))
2693 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2703 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2704 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2705 const char *control)
2707 bool dynamic_source = false;
2708 bool dynamic_sink = false;
2713 switch (source->id) {
2714 case snd_soc_dapm_demux:
2715 dynamic_source = true;
2722 case snd_soc_dapm_mux:
2723 case snd_soc_dapm_switch:
2724 case snd_soc_dapm_mixer:
2725 case snd_soc_dapm_mixer_named_ctl:
2726 dynamic_sink = true;
2732 if (dynamic_source && dynamic_sink) {
2734 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2735 source->name, control, sink->name);
2737 } else if (!dynamic_source && !dynamic_sink) {
2739 "Control not supported for path %s -> [%s] -> %s\n",
2740 source->name, control, sink->name);
2747 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2748 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2749 const char *control,
2750 int (*connected)(struct snd_soc_dapm_widget *source,
2751 struct snd_soc_dapm_widget *sink))
2753 struct snd_soc_dapm_widget *widgets[2];
2754 enum snd_soc_dapm_direction dir;
2755 struct snd_soc_dapm_path *path;
2758 if (wsink->is_supply && !wsource->is_supply) {
2760 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2761 wsource->name, wsink->name);
2765 if (connected && !wsource->is_supply) {
2767 "connected() callback only supported for supply widgets (%s -> %s)\n",
2768 wsource->name, wsink->name);
2772 if (wsource->is_supply && control) {
2774 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2775 wsource->name, control, wsink->name);
2779 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2783 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2787 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2788 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2789 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2790 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2792 path->connected = connected;
2793 INIT_LIST_HEAD(&path->list);
2794 INIT_LIST_HEAD(&path->list_kcontrol);
2796 if (wsource->is_supply || wsink->is_supply)
2797 path->is_supply = 1;
2799 /* connect static paths */
2800 if (control == NULL) {
2803 switch (wsource->id) {
2804 case snd_soc_dapm_demux:
2805 ret = dapm_connect_mux(dapm, path, control, wsource);
2813 switch (wsink->id) {
2814 case snd_soc_dapm_mux:
2815 ret = dapm_connect_mux(dapm, path, control, wsink);
2819 case snd_soc_dapm_switch:
2820 case snd_soc_dapm_mixer:
2821 case snd_soc_dapm_mixer_named_ctl:
2822 ret = dapm_connect_mixer(dapm, path, control);
2831 list_add(&path->list, &dapm->card->paths);
2832 snd_soc_dapm_for_each_direction(dir)
2833 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2835 snd_soc_dapm_for_each_direction(dir) {
2836 dapm_update_widget_flags(widgets[dir]);
2837 dapm_mark_dirty(widgets[dir], "Route added");
2840 if (dapm->card->instantiated && path->connect)
2841 dapm_path_invalidate(path);
2849 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2850 const struct snd_soc_dapm_route *route)
2852 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2853 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2856 char prefixed_sink[80];
2857 char prefixed_source[80];
2859 unsigned int sink_ref = 0;
2860 unsigned int source_ref = 0;
2863 prefix = soc_dapm_prefix(dapm);
2865 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2866 prefix, route->sink);
2867 sink = prefixed_sink;
2868 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2869 prefix, route->source);
2870 source = prefixed_source;
2873 source = route->source;
2876 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2877 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2879 if (wsink && wsource)
2883 * find src and dest widgets over all widgets but favor a widget from
2884 * current DAPM context
2886 list_for_each_entry(w, &dapm->card->widgets, list) {
2887 if (!wsink && !(strcmp(w->name, sink))) {
2889 if (w->dapm == dapm) {
2897 "ASoC: sink widget %s overwritten\n",
2901 if (!wsource && !(strcmp(w->name, source))) {
2903 if (w->dapm == dapm) {
2911 "ASoC: source widget %s overwritten\n",
2915 /* use widget from another DAPM context if not found from this */
2921 if (wsource == NULL) {
2922 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2926 if (wsink == NULL) {
2927 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2933 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2934 dapm_wcache_update(&dapm->path_source_cache, wsource);
2936 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2943 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2944 source, route->control, sink);
2948 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2949 const struct snd_soc_dapm_route *route)
2951 struct snd_soc_dapm_widget *wsource, *wsink;
2952 struct snd_soc_dapm_path *path, *p;
2955 char prefixed_sink[80];
2956 char prefixed_source[80];
2959 if (route->control) {
2961 "ASoC: Removal of routes with controls not supported\n");
2965 prefix = soc_dapm_prefix(dapm);
2967 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2968 prefix, route->sink);
2969 sink = prefixed_sink;
2970 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2971 prefix, route->source);
2972 source = prefixed_source;
2975 source = route->source;
2979 list_for_each_entry(p, &dapm->card->paths, list) {
2980 if (strcmp(p->source->name, source) != 0)
2982 if (strcmp(p->sink->name, sink) != 0)
2989 wsource = path->source;
2992 dapm_mark_dirty(wsource, "Route removed");
2993 dapm_mark_dirty(wsink, "Route removed");
2995 dapm_path_invalidate(path);
2997 dapm_free_path(path);
2999 /* Update any path related flags */
3000 dapm_update_widget_flags(wsource);
3001 dapm_update_widget_flags(wsink);
3003 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3011 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3012 * @dapm: DAPM context
3013 * @route: audio routes
3014 * @num: number of routes
3016 * Connects 2 dapm widgets together via a named audio path. The sink is
3017 * the widget receiving the audio signal, whilst the source is the sender
3018 * of the audio signal.
3020 * Returns 0 for success else error. On error all resources can be freed
3021 * with a call to snd_soc_card_free().
3023 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3024 const struct snd_soc_dapm_route *route, int num)
3028 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3029 for (i = 0; i < num; i++) {
3030 r = snd_soc_dapm_add_route(dapm, route);
3032 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3034 route->control ? route->control : "direct",
3040 mutex_unlock(&dapm->card->dapm_mutex);
3044 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3047 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3048 * @dapm: DAPM context
3049 * @route: audio routes
3050 * @num: number of routes
3052 * Removes routes from the DAPM context.
3054 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3055 const struct snd_soc_dapm_route *route, int num)
3059 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3060 for (i = 0; i < num; i++) {
3061 snd_soc_dapm_del_route(dapm, route);
3064 mutex_unlock(&dapm->card->dapm_mutex);
3068 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3070 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3071 const struct snd_soc_dapm_route *route)
3073 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3076 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3079 struct snd_soc_dapm_path *path;
3083 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3089 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3094 if (route->control || route->connected)
3095 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3096 route->source, route->sink);
3098 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3099 if (path->sink == sink) {
3106 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3107 route->source, route->sink);
3109 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3110 count, route->source, route->sink);
3116 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3117 * @dapm: DAPM context
3118 * @route: audio routes
3119 * @num: number of routes
3121 * Mark existing routes matching those specified in the passed array
3122 * as being weak, meaning that they are ignored for the purpose of
3123 * power decisions. The main intended use case is for sidetone paths
3124 * which couple audio between other independent paths if they are both
3125 * active in order to make the combination work better at the user
3126 * level but which aren't intended to be "used".
3128 * Note that CODEC drivers should not use this as sidetone type paths
3129 * can frequently also be used as bypass paths.
3131 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3132 const struct snd_soc_dapm_route *route, int num)
3137 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3138 for (i = 0; i < num; i++) {
3139 err = snd_soc_dapm_weak_route(dapm, route);
3144 mutex_unlock(&dapm->card->dapm_mutex);
3148 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3151 * snd_soc_dapm_new_widgets - add new dapm widgets
3152 * @card: card to be checked for new dapm widgets
3154 * Checks the codec for any new dapm widgets and creates them if found.
3156 * Returns 0 for success.
3158 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3160 struct snd_soc_dapm_widget *w;
3163 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3165 list_for_each_entry(w, &card->widgets, list)
3170 if (w->num_kcontrols) {
3171 w->kcontrols = kcalloc(w->num_kcontrols,
3172 sizeof(struct snd_kcontrol *),
3174 if (!w->kcontrols) {
3175 mutex_unlock(&card->dapm_mutex);
3181 case snd_soc_dapm_switch:
3182 case snd_soc_dapm_mixer:
3183 case snd_soc_dapm_mixer_named_ctl:
3186 case snd_soc_dapm_mux:
3187 case snd_soc_dapm_demux:
3190 case snd_soc_dapm_pga:
3191 case snd_soc_dapm_out_drv:
3194 case snd_soc_dapm_dai_link:
3195 dapm_new_dai_link(w);
3201 /* Read the initial power state from the device */
3203 soc_dapm_read(w->dapm, w->reg, &val);
3204 val = val >> w->shift;
3206 if (val == w->on_val)
3212 dapm_mark_dirty(w, "new widget");
3213 dapm_debugfs_add_widget(w);
3216 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3217 mutex_unlock(&card->dapm_mutex);
3220 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3223 * snd_soc_dapm_get_volsw - dapm mixer get callback
3224 * @kcontrol: mixer control
3225 * @ucontrol: control element information
3227 * Callback to get the value of a dapm mixer control.
3229 * Returns 0 for success.
3231 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3232 struct snd_ctl_elem_value *ucontrol)
3234 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3235 struct snd_soc_card *card = dapm->card;
3236 struct soc_mixer_control *mc =
3237 (struct soc_mixer_control *)kcontrol->private_value;
3239 unsigned int shift = mc->shift;
3241 unsigned int width = fls(max);
3242 unsigned int mask = (1 << fls(max)) - 1;
3243 unsigned int invert = mc->invert;
3244 unsigned int reg_val, val, rval = 0;
3247 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3248 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3249 ret = soc_dapm_read(dapm, reg, ®_val);
3250 val = (reg_val >> shift) & mask;
3252 if (ret == 0 && reg != mc->rreg)
3253 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3255 if (snd_soc_volsw_is_stereo(mc))
3256 rval = (reg_val >> mc->rshift) & mask;
3258 reg_val = dapm_kcontrol_get_value(kcontrol);
3259 val = reg_val & mask;
3261 if (snd_soc_volsw_is_stereo(mc))
3262 rval = (reg_val >> width) & mask;
3264 mutex_unlock(&card->dapm_mutex);
3270 ucontrol->value.integer.value[0] = max - val;
3272 ucontrol->value.integer.value[0] = val;
3274 if (snd_soc_volsw_is_stereo(mc)) {
3276 ucontrol->value.integer.value[1] = max - rval;
3278 ucontrol->value.integer.value[1] = rval;
3283 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3286 * snd_soc_dapm_put_volsw - dapm mixer set callback
3287 * @kcontrol: mixer control
3288 * @ucontrol: control element information
3290 * Callback to set the value of a dapm mixer control.
3292 * Returns 0 for success.
3294 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3295 struct snd_ctl_elem_value *ucontrol)
3297 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3298 struct snd_soc_card *card = dapm->card;
3299 struct soc_mixer_control *mc =
3300 (struct soc_mixer_control *)kcontrol->private_value;
3302 unsigned int shift = mc->shift;
3304 unsigned int width = fls(max);
3305 unsigned int mask = (1 << width) - 1;
3306 unsigned int invert = mc->invert;
3307 unsigned int val, rval = 0;
3308 int connect, rconnect = -1, change, reg_change = 0;
3309 struct snd_soc_dapm_update update = {};
3312 val = (ucontrol->value.integer.value[0] & mask);
3318 if (snd_soc_volsw_is_stereo(mc)) {
3319 rval = (ucontrol->value.integer.value[1] & mask);
3325 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3327 /* This assumes field width < (bits in unsigned int / 2) */
3328 if (width > sizeof(unsigned int) * 8 / 2)
3330 "ASoC: control %s field width limit exceeded\n",
3332 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3334 if (reg != SND_SOC_NOPM) {
3336 rval = rval << mc->rshift;
3338 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3340 if (snd_soc_volsw_is_stereo(mc))
3341 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3346 if (change || reg_change) {
3348 if (snd_soc_volsw_is_stereo(mc)) {
3349 update.has_second_set = true;
3350 update.reg2 = mc->rreg;
3351 update.mask2 = mask << mc->rshift;
3354 update.kcontrol = kcontrol;
3356 update.mask = mask << shift;
3358 card->update = &update;
3360 change |= reg_change;
3362 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3365 card->update = NULL;
3368 mutex_unlock(&card->dapm_mutex);
3371 soc_dpcm_runtime_update(card);
3375 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3378 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3379 * @kcontrol: mixer control
3380 * @ucontrol: control element information
3382 * Callback to get the value of a dapm enumerated double mixer control.
3384 * Returns 0 for success.
3386 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3387 struct snd_ctl_elem_value *ucontrol)
3389 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3390 struct snd_soc_card *card = dapm->card;
3391 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3392 unsigned int reg_val, val;
3394 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3395 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3396 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3398 mutex_unlock(&card->dapm_mutex);
3402 reg_val = dapm_kcontrol_get_value(kcontrol);
3404 mutex_unlock(&card->dapm_mutex);
3406 val = (reg_val >> e->shift_l) & e->mask;
3407 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3408 if (e->shift_l != e->shift_r) {
3409 val = (reg_val >> e->shift_r) & e->mask;
3410 val = snd_soc_enum_val_to_item(e, val);
3411 ucontrol->value.enumerated.item[1] = val;
3416 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3419 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3420 * @kcontrol: mixer control
3421 * @ucontrol: control element information
3423 * Callback to set the value of a dapm enumerated double mixer control.
3425 * Returns 0 for success.
3427 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3428 struct snd_ctl_elem_value *ucontrol)
3430 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3431 struct snd_soc_card *card = dapm->card;
3432 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3433 unsigned int *item = ucontrol->value.enumerated.item;
3434 unsigned int val, change, reg_change = 0;
3436 struct snd_soc_dapm_update update = {};
3439 if (item[0] >= e->items)
3442 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3443 mask = e->mask << e->shift_l;
3444 if (e->shift_l != e->shift_r) {
3445 if (item[1] > e->items)
3447 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3448 mask |= e->mask << e->shift_r;
3451 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3453 change = dapm_kcontrol_set_value(kcontrol, val);
3455 if (e->reg != SND_SOC_NOPM)
3456 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3458 if (change || reg_change) {
3460 update.kcontrol = kcontrol;
3461 update.reg = e->reg;
3464 card->update = &update;
3466 change |= reg_change;
3468 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3470 card->update = NULL;
3473 mutex_unlock(&card->dapm_mutex);
3476 soc_dpcm_runtime_update(card);
3480 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3483 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3485 * @kcontrol: mixer control
3486 * @uinfo: control element information
3488 * Callback to provide information about a pin switch control.
3490 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3491 struct snd_ctl_elem_info *uinfo)
3493 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3495 uinfo->value.integer.min = 0;
3496 uinfo->value.integer.max = 1;
3500 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3503 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3505 * @kcontrol: mixer control
3508 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3509 struct snd_ctl_elem_value *ucontrol)
3511 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3512 const char *pin = (const char *)kcontrol->private_value;
3514 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3516 ucontrol->value.integer.value[0] =
3517 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3519 mutex_unlock(&card->dapm_mutex);
3523 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3526 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3528 * @kcontrol: mixer control
3531 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3532 struct snd_ctl_elem_value *ucontrol)
3534 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3535 const char *pin = (const char *)kcontrol->private_value;
3537 if (ucontrol->value.integer.value[0])
3538 snd_soc_dapm_enable_pin(&card->dapm, pin);
3540 snd_soc_dapm_disable_pin(&card->dapm, pin);
3542 snd_soc_dapm_sync(&card->dapm);
3545 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3547 struct snd_soc_dapm_widget *
3548 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3549 const struct snd_soc_dapm_widget *widget)
3551 enum snd_soc_dapm_direction dir;
3552 struct snd_soc_dapm_widget *w;
3556 if ((w = dapm_cnew_widget(widget)) == NULL)
3557 return ERR_PTR(-ENOMEM);
3560 case snd_soc_dapm_regulator_supply:
3561 w->regulator = devm_regulator_get(dapm->dev, w->name);
3562 if (IS_ERR(w->regulator)) {
3563 ret = PTR_ERR(w->regulator);
3564 goto request_failed;
3567 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3568 ret = regulator_allow_bypass(w->regulator, true);
3571 "ASoC: Failed to bypass %s: %d\n",
3575 case snd_soc_dapm_pinctrl:
3576 w->pinctrl = devm_pinctrl_get(dapm->dev);
3577 if (IS_ERR(w->pinctrl)) {
3578 ret = PTR_ERR(w->pinctrl);
3579 goto request_failed;
3582 case snd_soc_dapm_clock_supply:
3583 w->clk = devm_clk_get(dapm->dev, w->name);
3584 if (IS_ERR(w->clk)) {
3585 ret = PTR_ERR(w->clk);
3586 goto request_failed;
3593 prefix = soc_dapm_prefix(dapm);
3595 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3597 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3598 if (w->name == NULL) {
3599 kfree_const(w->sname);
3601 return ERR_PTR(-ENOMEM);
3605 case snd_soc_dapm_mic:
3606 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3607 w->power_check = dapm_generic_check_power;
3609 case snd_soc_dapm_input:
3610 if (!dapm->card->fully_routed)
3611 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3612 w->power_check = dapm_generic_check_power;
3614 case snd_soc_dapm_spk:
3615 case snd_soc_dapm_hp:
3616 w->is_ep = SND_SOC_DAPM_EP_SINK;
3617 w->power_check = dapm_generic_check_power;
3619 case snd_soc_dapm_output:
3620 if (!dapm->card->fully_routed)
3621 w->is_ep = SND_SOC_DAPM_EP_SINK;
3622 w->power_check = dapm_generic_check_power;
3624 case snd_soc_dapm_vmid:
3625 case snd_soc_dapm_siggen:
3626 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3627 w->power_check = dapm_always_on_check_power;
3629 case snd_soc_dapm_sink:
3630 w->is_ep = SND_SOC_DAPM_EP_SINK;
3631 w->power_check = dapm_always_on_check_power;
3634 case snd_soc_dapm_mux:
3635 case snd_soc_dapm_demux:
3636 case snd_soc_dapm_switch:
3637 case snd_soc_dapm_mixer:
3638 case snd_soc_dapm_mixer_named_ctl:
3639 case snd_soc_dapm_adc:
3640 case snd_soc_dapm_aif_out:
3641 case snd_soc_dapm_dac:
3642 case snd_soc_dapm_aif_in:
3643 case snd_soc_dapm_pga:
3644 case snd_soc_dapm_out_drv:
3645 case snd_soc_dapm_micbias:
3646 case snd_soc_dapm_line:
3647 case snd_soc_dapm_dai_link:
3648 case snd_soc_dapm_dai_out:
3649 case snd_soc_dapm_dai_in:
3650 w->power_check = dapm_generic_check_power;
3652 case snd_soc_dapm_supply:
3653 case snd_soc_dapm_regulator_supply:
3654 case snd_soc_dapm_pinctrl:
3655 case snd_soc_dapm_clock_supply:
3656 case snd_soc_dapm_kcontrol:
3658 w->power_check = dapm_supply_check_power;
3661 w->power_check = dapm_always_on_check_power;
3666 INIT_LIST_HEAD(&w->list);
3667 INIT_LIST_HEAD(&w->dirty);
3668 list_add_tail(&w->list, &dapm->card->widgets);
3670 snd_soc_dapm_for_each_direction(dir) {
3671 INIT_LIST_HEAD(&w->edges[dir]);
3672 w->endpoints[dir] = -1;
3675 /* machine layer sets up unconnected pins and insertions */
3680 if (ret != -EPROBE_DEFER)
3681 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3684 return ERR_PTR(ret);
3688 * snd_soc_dapm_new_control - create new dapm control
3689 * @dapm: DAPM context
3690 * @widget: widget template
3692 * Creates new DAPM control based upon a template.
3694 * Returns a widget pointer on success or an error pointer on failure
3696 struct snd_soc_dapm_widget *
3697 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3698 const struct snd_soc_dapm_widget *widget)
3700 struct snd_soc_dapm_widget *w;
3702 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3703 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3704 mutex_unlock(&dapm->card->dapm_mutex);
3708 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3711 * snd_soc_dapm_new_controls - create new dapm controls
3712 * @dapm: DAPM context
3713 * @widget: widget array
3714 * @num: number of widgets
3716 * Creates new DAPM controls based upon the templates.
3718 * Returns 0 for success else error.
3720 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3721 const struct snd_soc_dapm_widget *widget,
3724 struct snd_soc_dapm_widget *w;
3728 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3729 for (i = 0; i < num; i++) {
3730 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3737 mutex_unlock(&dapm->card->dapm_mutex);
3740 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3742 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3743 struct snd_kcontrol *kcontrol, int event)
3745 struct snd_soc_dapm_path *path;
3746 struct snd_soc_dai *source, *sink;
3747 struct snd_soc_pcm_runtime *rtd = w->priv;
3748 const struct snd_soc_pcm_stream *config;
3749 struct snd_pcm_substream substream;
3750 struct snd_pcm_hw_params *params = NULL;
3751 struct snd_pcm_runtime *runtime = NULL;
3755 config = rtd->dai_link->params + rtd->params_select;
3757 if (WARN_ON(!config) ||
3758 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3759 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3762 /* Be a little careful as we don't want to overflow the mask array */
3763 if (config->formats) {
3764 fmt = ffs(config->formats) - 1;
3766 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3771 /* Currently very limited parameter selection */
3772 params = kzalloc(sizeof(*params), GFP_KERNEL);
3777 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3779 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3781 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3784 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3785 = config->channels_min;
3786 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3787 = config->channels_max;
3789 memset(&substream, 0, sizeof(substream));
3791 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3792 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3797 substream.runtime = runtime;
3798 substream.private_data = rtd;
3801 case SND_SOC_DAPM_PRE_PMU:
3802 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3803 snd_soc_dapm_widget_for_each_source_path(w, path) {
3804 source = path->source->priv;
3806 if (source->driver->ops->startup) {
3807 ret = source->driver->ops->startup(&substream,
3810 dev_err(source->dev,
3811 "ASoC: startup() failed: %d\n",
3817 ret = soc_dai_hw_params(&substream, params, source);
3821 dapm_update_dai_unlocked(&substream, params, source);
3824 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3825 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3826 sink = path->sink->priv;
3828 if (sink->driver->ops->startup) {
3829 ret = sink->driver->ops->startup(&substream,
3833 "ASoC: startup() failed: %d\n",
3839 ret = soc_dai_hw_params(&substream, params, sink);
3843 dapm_update_dai_unlocked(&substream, params, sink);
3847 case SND_SOC_DAPM_POST_PMU:
3848 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3849 sink = path->sink->priv;
3851 ret = snd_soc_dai_digital_mute(sink, 0,
3852 SNDRV_PCM_STREAM_PLAYBACK);
3853 if (ret != 0 && ret != -ENOTSUPP)
3855 "ASoC: Failed to unmute: %d\n", ret);
3860 case SND_SOC_DAPM_PRE_PMD:
3861 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3862 sink = path->sink->priv;
3864 ret = snd_soc_dai_digital_mute(sink, 1,
3865 SNDRV_PCM_STREAM_PLAYBACK);
3866 if (ret != 0 && ret != -ENOTSUPP)
3868 "ASoC: Failed to mute: %d\n", ret);
3872 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3873 snd_soc_dapm_widget_for_each_source_path(w, path) {
3874 source = path->source->priv;
3876 if (source->driver->ops->hw_free)
3877 source->driver->ops->hw_free(&substream,
3881 if (source->driver->ops->shutdown)
3882 source->driver->ops->shutdown(&substream,
3886 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3887 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3888 sink = path->sink->priv;
3890 if (sink->driver->ops->hw_free)
3891 sink->driver->ops->hw_free(&substream, sink);
3894 if (sink->driver->ops->shutdown)
3895 sink->driver->ops->shutdown(&substream, sink);
3900 WARN(1, "Unknown event %d\n", event);
3910 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3911 struct snd_ctl_elem_value *ucontrol)
3913 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3914 struct snd_soc_pcm_runtime *rtd = w->priv;
3916 ucontrol->value.enumerated.item[0] = rtd->params_select;
3921 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3922 struct snd_ctl_elem_value *ucontrol)
3924 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3925 struct snd_soc_pcm_runtime *rtd = w->priv;
3927 /* Can't change the config when widget is already powered */
3931 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
3934 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
3937 rtd->params_select = ucontrol->value.enumerated.item[0];
3943 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3944 unsigned long *private_value,
3946 const char **w_param_text)
3950 devm_kfree(card->dev, (void *)*private_value);
3951 for (count = 0 ; count < num_params; count++)
3952 devm_kfree(card->dev, (void *)w_param_text[count]);
3953 devm_kfree(card->dev, w_param_text);
3956 static struct snd_kcontrol_new *
3957 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3959 const struct snd_soc_pcm_stream *params,
3960 int num_params, const char **w_param_text,
3961 unsigned long *private_value)
3963 struct soc_enum w_param_enum[] = {
3964 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3966 struct snd_kcontrol_new kcontrol_dai_link[] = {
3967 SOC_ENUM_EXT(NULL, w_param_enum[0],
3968 snd_soc_dapm_dai_link_get,
3969 snd_soc_dapm_dai_link_put),
3971 struct snd_kcontrol_new *kcontrol_news;
3972 const struct snd_soc_pcm_stream *config = params;
3975 for (count = 0 ; count < num_params; count++) {
3976 if (!config->stream_name) {
3977 dev_warn(card->dapm.dev,
3978 "ASoC: anonymous config %d for dai link %s\n",
3980 w_param_text[count] =
3981 devm_kasprintf(card->dev, GFP_KERNEL,
3982 "Anonymous Configuration %d",
3985 w_param_text[count] = devm_kmemdup(card->dev,
3986 config->stream_name,
3987 strlen(config->stream_name) + 1,
3990 if (!w_param_text[count])
3991 goto outfree_w_param;
3995 w_param_enum[0].items = num_params;
3996 w_param_enum[0].texts = w_param_text;
3999 (unsigned long) devm_kmemdup(card->dev,
4000 (void *)(kcontrol_dai_link[0].private_value),
4001 sizeof(struct soc_enum), GFP_KERNEL);
4002 if (!*private_value) {
4003 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4005 goto outfree_w_param;
4007 kcontrol_dai_link[0].private_value = *private_value;
4008 /* duplicate kcontrol_dai_link on heap so that memory persists */
4009 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4010 sizeof(struct snd_kcontrol_new),
4012 if (!kcontrol_news) {
4013 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4015 goto outfree_w_param;
4017 return kcontrol_news;
4020 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4024 static struct snd_soc_dapm_widget *
4025 snd_soc_dapm_new_dai(struct snd_soc_card *card, struct snd_soc_pcm_runtime *rtd,
4026 struct snd_soc_dapm_widget *source,
4027 struct snd_soc_dapm_widget *sink)
4029 struct snd_soc_dapm_widget template;
4030 struct snd_soc_dapm_widget *w;
4031 const char **w_param_text;
4032 unsigned long private_value;
4036 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4037 source->name, sink->name);
4039 return ERR_PTR(-ENOMEM);
4041 memset(&template, 0, sizeof(template));
4042 template.reg = SND_SOC_NOPM;
4043 template.id = snd_soc_dapm_dai_link;
4044 template.name = link_name;
4045 template.event = snd_soc_dai_link_event;
4046 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4047 SND_SOC_DAPM_PRE_PMD;
4048 template.kcontrol_news = NULL;
4050 /* allocate memory for control, only in case of multiple configs */
4051 if (rtd->dai_link->num_params > 1) {
4052 w_param_text = devm_kcalloc(card->dev,
4053 rtd->dai_link->num_params,
4054 sizeof(char *), GFP_KERNEL);
4055 if (!w_param_text) {
4060 template.num_kcontrols = 1;
4061 template.kcontrol_news =
4062 snd_soc_dapm_alloc_kcontrol(card,
4064 rtd->dai_link->params,
4065 rtd->dai_link->num_params,
4066 w_param_text, &private_value);
4067 if (!template.kcontrol_news) {
4072 w_param_text = NULL;
4074 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4076 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4079 goto outfree_kcontrol_news;
4086 outfree_kcontrol_news:
4087 devm_kfree(card->dev, (void *)template.kcontrol_news);
4088 snd_soc_dapm_free_kcontrol(card, &private_value,
4089 rtd->dai_link->num_params, w_param_text);
4091 devm_kfree(card->dev, link_name);
4092 return ERR_PTR(ret);
4095 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4096 struct snd_soc_dai *dai)
4098 struct snd_soc_dapm_widget template;
4099 struct snd_soc_dapm_widget *w;
4101 WARN_ON(dapm->dev != dai->dev);
4103 memset(&template, 0, sizeof(template));
4104 template.reg = SND_SOC_NOPM;
4106 if (dai->driver->playback.stream_name) {
4107 template.id = snd_soc_dapm_dai_in;
4108 template.name = dai->driver->playback.stream_name;
4109 template.sname = dai->driver->playback.stream_name;
4111 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4114 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4119 dai->playback_widget = w;
4122 if (dai->driver->capture.stream_name) {
4123 template.id = snd_soc_dapm_dai_out;
4124 template.name = dai->driver->capture.stream_name;
4125 template.sname = dai->driver->capture.stream_name;
4127 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4130 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4135 dai->capture_widget = w;
4141 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4143 struct snd_soc_dapm_widget *dai_w, *w;
4144 struct snd_soc_dapm_widget *src, *sink;
4145 struct snd_soc_dai *dai;
4147 /* For each DAI widget... */
4148 list_for_each_entry(dai_w, &card->widgets, list) {
4149 switch (dai_w->id) {
4150 case snd_soc_dapm_dai_in:
4151 case snd_soc_dapm_dai_out:
4157 /* let users know there is no DAI to link */
4159 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4166 /* ...find all widgets with the same stream and link them */
4167 list_for_each_entry(w, &card->widgets, list) {
4168 if (w->dapm != dai_w->dapm)
4172 case snd_soc_dapm_dai_in:
4173 case snd_soc_dapm_dai_out:
4179 if (!w->sname || !strstr(w->sname, dai_w->sname))
4182 if (dai_w->id == snd_soc_dapm_dai_in) {
4189 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4190 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4197 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4198 struct snd_soc_pcm_runtime *rtd)
4200 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4201 struct snd_soc_dai *codec_dai;
4202 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4203 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
4206 if (rtd->dai_link->params) {
4207 playback_cpu = cpu_dai->capture_widget;
4208 capture_cpu = cpu_dai->playback_widget;
4210 playback = cpu_dai->playback_widget;
4211 capture = cpu_dai->capture_widget;
4212 playback_cpu = playback;
4213 capture_cpu = capture;
4216 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4218 /* connect BE DAI playback if widgets are valid */
4219 codec = codec_dai->playback_widget;
4221 if (playback_cpu && codec) {
4223 playback = snd_soc_dapm_new_dai(card, rtd,
4226 if (IS_ERR(playback)) {
4228 "ASoC: Failed to create DAI %s: %ld\n",
4234 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4235 playback, NULL, NULL);
4238 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4239 cpu_dai->component->name, playback_cpu->name,
4240 codec_dai->component->name, codec->name);
4242 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4247 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4248 /* connect BE DAI capture if widgets are valid */
4249 codec = codec_dai->capture_widget;
4251 if (codec && capture_cpu) {
4253 capture = snd_soc_dapm_new_dai(card, rtd,
4256 if (IS_ERR(capture)) {
4258 "ASoC: Failed to create DAI %s: %ld\n",
4264 snd_soc_dapm_add_path(&card->dapm, capture,
4265 capture_cpu, NULL, NULL);
4268 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4269 codec_dai->component->name, codec->name,
4270 cpu_dai->component->name, capture_cpu->name);
4272 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4278 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4281 struct snd_soc_dapm_widget *w;
4284 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4285 w = dai->playback_widget;
4287 w = dai->capture_widget;
4290 dapm_mark_dirty(w, "stream event");
4292 if (w->id == snd_soc_dapm_dai_in) {
4293 ep = SND_SOC_DAPM_EP_SOURCE;
4294 dapm_widget_invalidate_input_paths(w);
4296 ep = SND_SOC_DAPM_EP_SINK;
4297 dapm_widget_invalidate_output_paths(w);
4301 case SND_SOC_DAPM_STREAM_START:
4305 case SND_SOC_DAPM_STREAM_STOP:
4309 case SND_SOC_DAPM_STREAM_SUSPEND:
4310 case SND_SOC_DAPM_STREAM_RESUME:
4311 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4312 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4318 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4320 struct snd_soc_pcm_runtime *rtd;
4322 /* for each BE DAI link... */
4323 for_each_card_rtds(card, rtd) {
4325 * dynamic FE links have no fixed DAI mapping.
4326 * CODEC<->CODEC links have no direct connection.
4328 if (rtd->dai_link->dynamic)
4331 dapm_connect_dai_link_widgets(card, rtd);
4335 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4338 struct snd_soc_dai *codec_dai;
4341 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4342 for_each_rtd_codec_dai(rtd, i, codec_dai)
4343 soc_dapm_dai_stream_event(codec_dai, stream, event);
4345 dapm_power_widgets(rtd->card, event);
4349 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4350 * @rtd: PCM runtime data
4351 * @stream: stream name
4352 * @event: stream event
4354 * Sends a stream event to the dapm core. The core then makes any
4355 * necessary widget power changes.
4357 * Returns 0 for success else error.
4359 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4362 struct snd_soc_card *card = rtd->card;
4364 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4365 soc_dapm_stream_event(rtd, stream, event);
4366 mutex_unlock(&card->dapm_mutex);
4370 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4371 * @dapm: DAPM context
4374 * Enables input/output pin and its parents or children widgets iff there is
4375 * a valid audio route and active audio stream.
4377 * Requires external locking.
4379 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4380 * do any widget power switching.
4382 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4385 return snd_soc_dapm_set_pin(dapm, pin, 1);
4387 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4390 * snd_soc_dapm_enable_pin - enable pin.
4391 * @dapm: DAPM context
4394 * Enables input/output pin and its parents or children widgets iff there is
4395 * a valid audio route and active audio stream.
4397 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4398 * do any widget power switching.
4400 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4404 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4406 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4408 mutex_unlock(&dapm->card->dapm_mutex);
4412 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4415 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4416 * @dapm: DAPM context
4419 * Enables input/output pin regardless of any other state. This is
4420 * intended for use with microphone bias supplies used in microphone
4423 * Requires external locking.
4425 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4426 * do any widget power switching.
4428 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4431 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4434 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4438 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4439 if (!w->connected) {
4441 * w->force does not affect the number of input or output paths,
4442 * so we only have to recheck if w->connected is changed
4444 dapm_widget_invalidate_input_paths(w);
4445 dapm_widget_invalidate_output_paths(w);
4449 dapm_mark_dirty(w, "force enable");
4453 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4456 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4457 * @dapm: DAPM context
4460 * Enables input/output pin regardless of any other state. This is
4461 * intended for use with microphone bias supplies used in microphone
4464 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4465 * do any widget power switching.
4467 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4472 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4474 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4476 mutex_unlock(&dapm->card->dapm_mutex);
4480 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4483 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4484 * @dapm: DAPM context
4487 * Disables input/output pin and its parents or children widgets.
4489 * Requires external locking.
4491 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4492 * do any widget power switching.
4494 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4497 return snd_soc_dapm_set_pin(dapm, pin, 0);
4499 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4502 * snd_soc_dapm_disable_pin - disable pin.
4503 * @dapm: DAPM context
4506 * Disables input/output pin and its parents or children widgets.
4508 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4509 * do any widget power switching.
4511 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4516 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4518 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4520 mutex_unlock(&dapm->card->dapm_mutex);
4524 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4527 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4528 * @dapm: DAPM context
4531 * Marks the specified pin as being not connected, disabling it along
4532 * any parent or child widgets. At present this is identical to
4533 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4534 * additional things such as disabling controls which only affect
4535 * paths through the pin.
4537 * Requires external locking.
4539 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4540 * do any widget power switching.
4542 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4545 return snd_soc_dapm_set_pin(dapm, pin, 0);
4547 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4550 * snd_soc_dapm_nc_pin - permanently disable pin.
4551 * @dapm: DAPM context
4554 * Marks the specified pin as being not connected, disabling it along
4555 * any parent or child widgets. At present this is identical to
4556 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4557 * additional things such as disabling controls which only affect
4558 * paths through the pin.
4560 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4561 * do any widget power switching.
4563 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4567 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4569 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4571 mutex_unlock(&dapm->card->dapm_mutex);
4575 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4578 * snd_soc_dapm_get_pin_status - get audio pin status
4579 * @dapm: DAPM context
4580 * @pin: audio signal pin endpoint (or start point)
4582 * Get audio pin status - connected or disconnected.
4584 * Returns 1 for connected otherwise 0.
4586 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4589 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4592 return w->connected;
4596 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4599 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4600 * @dapm: DAPM context
4601 * @pin: audio signal pin endpoint (or start point)
4603 * Mark the given endpoint or pin as ignoring suspend. When the
4604 * system is disabled a path between two endpoints flagged as ignoring
4605 * suspend will not be disabled. The path must already be enabled via
4606 * normal means at suspend time, it will not be turned on if it was not
4609 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4612 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4615 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4619 w->ignore_suspend = 1;
4623 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4626 * snd_soc_dapm_free - free dapm resources
4627 * @dapm: DAPM context
4629 * Free all dapm widgets and resources.
4631 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4633 dapm_debugfs_cleanup(dapm);
4634 dapm_free_widgets(dapm);
4635 list_del(&dapm->list);
4637 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4639 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4641 struct snd_soc_card *card = dapm->card;
4642 struct snd_soc_dapm_widget *w;
4643 LIST_HEAD(down_list);
4646 mutex_lock(&card->dapm_mutex);
4648 list_for_each_entry(w, &dapm->card->widgets, list) {
4649 if (w->dapm != dapm)
4652 dapm_seq_insert(w, &down_list, false);
4658 /* If there were no widgets to power down we're already in
4662 if (dapm->bias_level == SND_SOC_BIAS_ON)
4663 snd_soc_dapm_set_bias_level(dapm,
4664 SND_SOC_BIAS_PREPARE);
4665 dapm_seq_run(card, &down_list, 0, false);
4666 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4667 snd_soc_dapm_set_bias_level(dapm,
4668 SND_SOC_BIAS_STANDBY);
4671 mutex_unlock(&card->dapm_mutex);
4675 * snd_soc_dapm_shutdown - callback for system shutdown
4677 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4679 struct snd_soc_dapm_context *dapm;
4681 list_for_each_entry(dapm, &card->dapm_list, list) {
4682 if (dapm != &card->dapm) {
4683 soc_dapm_shutdown_dapm(dapm);
4684 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4685 snd_soc_dapm_set_bias_level(dapm,
4690 soc_dapm_shutdown_dapm(&card->dapm);
4691 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4692 snd_soc_dapm_set_bias_level(&card->dapm,
4696 /* Module information */
4697 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4698 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4699 MODULE_LICENSE("GPL");