2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/headphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed power down of audio subsystem to reduce pops between a quick
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/pinctrl/consumer.h>
39 #include <linux/clk.h>
40 #include <linux/slab.h>
41 #include <sound/core.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/initval.h>
47 #include <trace/events/asoc.h>
49 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
51 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
52 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
54 #define snd_soc_dapm_for_each_direction(dir) \
55 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
58 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
59 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
61 int (*connected)(struct snd_soc_dapm_widget *source,
62 struct snd_soc_dapm_widget *sink));
64 struct snd_soc_dapm_widget *
65 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
66 const struct snd_soc_dapm_widget *widget);
68 struct snd_soc_dapm_widget *
69 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
70 const struct snd_soc_dapm_widget *widget);
72 /* dapm power sequences - make this per codec in the future */
73 static int dapm_up_seq[] = {
74 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_regulator_supply] = 1,
76 [snd_soc_dapm_pinctrl] = 1,
77 [snd_soc_dapm_clock_supply] = 1,
78 [snd_soc_dapm_supply] = 2,
79 [snd_soc_dapm_micbias] = 3,
80 [snd_soc_dapm_dai_link] = 2,
81 [snd_soc_dapm_dai_in] = 4,
82 [snd_soc_dapm_dai_out] = 4,
83 [snd_soc_dapm_aif_in] = 4,
84 [snd_soc_dapm_aif_out] = 4,
85 [snd_soc_dapm_mic] = 5,
86 [snd_soc_dapm_mux] = 6,
87 [snd_soc_dapm_demux] = 6,
88 [snd_soc_dapm_dac] = 7,
89 [snd_soc_dapm_switch] = 8,
90 [snd_soc_dapm_mixer] = 8,
91 [snd_soc_dapm_mixer_named_ctl] = 8,
92 [snd_soc_dapm_pga] = 9,
93 [snd_soc_dapm_adc] = 10,
94 [snd_soc_dapm_out_drv] = 11,
95 [snd_soc_dapm_hp] = 11,
96 [snd_soc_dapm_spk] = 11,
97 [snd_soc_dapm_line] = 11,
98 [snd_soc_dapm_kcontrol] = 12,
99 [snd_soc_dapm_post] = 13,
102 static int dapm_down_seq[] = {
103 [snd_soc_dapm_pre] = 0,
104 [snd_soc_dapm_kcontrol] = 1,
105 [snd_soc_dapm_adc] = 2,
106 [snd_soc_dapm_hp] = 3,
107 [snd_soc_dapm_spk] = 3,
108 [snd_soc_dapm_line] = 3,
109 [snd_soc_dapm_out_drv] = 3,
110 [snd_soc_dapm_pga] = 4,
111 [snd_soc_dapm_switch] = 5,
112 [snd_soc_dapm_mixer_named_ctl] = 5,
113 [snd_soc_dapm_mixer] = 5,
114 [snd_soc_dapm_dac] = 6,
115 [snd_soc_dapm_mic] = 7,
116 [snd_soc_dapm_micbias] = 8,
117 [snd_soc_dapm_mux] = 9,
118 [snd_soc_dapm_demux] = 9,
119 [snd_soc_dapm_aif_in] = 10,
120 [snd_soc_dapm_aif_out] = 10,
121 [snd_soc_dapm_dai_in] = 10,
122 [snd_soc_dapm_dai_out] = 10,
123 [snd_soc_dapm_dai_link] = 11,
124 [snd_soc_dapm_supply] = 12,
125 [snd_soc_dapm_clock_supply] = 13,
126 [snd_soc_dapm_pinctrl] = 13,
127 [snd_soc_dapm_regulator_supply] = 13,
128 [snd_soc_dapm_post] = 14,
131 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
133 if (dapm->card && dapm->card->instantiated)
134 lockdep_assert_held(&dapm->card->dapm_mutex);
137 static void pop_wait(u32 pop_time)
140 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
143 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
151 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
156 vsnprintf(buf, PAGE_SIZE, fmt, args);
157 dev_info(dev, "%s", buf);
163 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
165 return !list_empty(&w->dirty);
168 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
170 dapm_assert_locked(w->dapm);
172 if (!dapm_dirty_widget(w)) {
173 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
175 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
180 * Common implementation for dapm_widget_invalidate_input_paths() and
181 * dapm_widget_invalidate_output_paths(). The function is inlined since the
182 * combined size of the two specialized functions is only marginally larger then
183 * the size of the generic function and at the same time the fast path of the
184 * specialized functions is significantly smaller than the generic function.
186 static __always_inline void dapm_widget_invalidate_paths(
187 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
189 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
190 struct snd_soc_dapm_widget *node;
191 struct snd_soc_dapm_path *p;
194 dapm_assert_locked(w->dapm);
196 if (w->endpoints[dir] == -1)
199 list_add_tail(&w->work_list, &list);
200 w->endpoints[dir] = -1;
202 list_for_each_entry(w, &list, work_list) {
203 snd_soc_dapm_widget_for_each_path(w, dir, p) {
204 if (p->is_supply || p->weak || !p->connect)
206 node = p->node[rdir];
207 if (node->endpoints[dir] != -1) {
208 node->endpoints[dir] = -1;
209 list_add_tail(&node->work_list, &list);
216 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
218 * @w: The widget for which to invalidate the cached number of input paths
220 * Resets the cached number of inputs for the specified widget and all widgets
221 * that can be reached via outcoming paths from the widget.
223 * This function must be called if the number of output paths for a widget might
224 * have changed. E.g. if the source state of a widget changes or a path is added
225 * or activated with the widget as the sink.
227 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
229 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
233 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
235 * @w: The widget for which to invalidate the cached number of output paths
237 * Resets the cached number of outputs for the specified widget and all widgets
238 * that can be reached via incoming 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 sink state of a widget changes or a path is added
242 * or activated with the widget as the source.
244 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
246 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
250 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
251 * for the widgets connected to a path
252 * @p: The path to invalidate
254 * Resets the cached number of inputs for the sink of the path and the cached
255 * number of outputs for the source of the path.
257 * This function must be called when a path is added, removed or the connected
260 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
263 * Weak paths or supply paths do not influence the number of input or
264 * output paths of their neighbors.
266 if (p->weak || p->is_supply)
270 * The number of connected endpoints is the sum of the number of
271 * connected endpoints of all neighbors. If a node with 0 connected
272 * endpoints is either connected or disconnected that sum won't change,
273 * so there is no need to re-check the path.
275 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
276 dapm_widget_invalidate_input_paths(p->sink);
277 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
278 dapm_widget_invalidate_output_paths(p->source);
281 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
283 struct snd_soc_dapm_widget *w;
285 mutex_lock(&card->dapm_mutex);
287 list_for_each_entry(w, &card->widgets, list) {
289 dapm_mark_dirty(w, "Rechecking endpoints");
290 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
291 dapm_widget_invalidate_output_paths(w);
292 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
293 dapm_widget_invalidate_input_paths(w);
297 mutex_unlock(&card->dapm_mutex);
299 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
301 /* create a new dapm widget */
302 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
303 const struct snd_soc_dapm_widget *_widget)
305 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
308 struct dapm_kcontrol_data {
310 struct snd_soc_dapm_widget *widget;
311 struct list_head paths;
312 struct snd_soc_dapm_widget_list *wlist;
315 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
316 struct snd_kcontrol *kcontrol, const char *ctrl_name)
318 struct dapm_kcontrol_data *data;
319 struct soc_mixer_control *mc;
324 data = kzalloc(sizeof(*data), GFP_KERNEL);
328 INIT_LIST_HEAD(&data->paths);
330 switch (widget->id) {
331 case snd_soc_dapm_switch:
332 case snd_soc_dapm_mixer:
333 case snd_soc_dapm_mixer_named_ctl:
334 mc = (struct soc_mixer_control *)kcontrol->private_value;
336 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
337 dev_warn(widget->dapm->dev,
338 "ASoC: Unsupported stereo autodisable control '%s'\n",
341 if (mc->autodisable) {
342 struct snd_soc_dapm_widget template;
344 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
351 memset(&template, 0, sizeof(template));
352 template.reg = mc->reg;
353 template.mask = (1 << fls(mc->max)) - 1;
354 template.shift = mc->shift;
356 template.off_val = mc->max;
358 template.off_val = 0;
359 template.on_val = template.off_val;
360 template.id = snd_soc_dapm_kcontrol;
361 template.name = name;
363 data->value = template.on_val;
366 snd_soc_dapm_new_control_unlocked(widget->dapm,
369 if (IS_ERR(data->widget)) {
370 ret = PTR_ERR(data->widget);
379 case snd_soc_dapm_demux:
380 case snd_soc_dapm_mux:
381 e = (struct soc_enum *)kcontrol->private_value;
383 if (e->autodisable) {
384 struct snd_soc_dapm_widget template;
386 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
393 memset(&template, 0, sizeof(template));
394 template.reg = e->reg;
395 template.mask = e->mask << e->shift_l;
396 template.shift = e->shift_l;
397 template.off_val = snd_soc_enum_item_to_val(e, 0);
398 template.on_val = template.off_val;
399 template.id = snd_soc_dapm_kcontrol;
400 template.name = name;
402 data->value = template.on_val;
404 data->widget = snd_soc_dapm_new_control_unlocked(
405 widget->dapm, &template);
407 if (IS_ERR(data->widget)) {
408 ret = PTR_ERR(data->widget);
416 snd_soc_dapm_add_path(widget->dapm, data->widget,
424 kcontrol->private_data = data;
433 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
435 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
440 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
441 const struct snd_kcontrol *kcontrol)
443 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
448 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
449 struct snd_soc_dapm_widget *widget)
451 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
452 struct snd_soc_dapm_widget_list *new_wlist;
456 n = data->wlist->num_widgets + 1;
460 new_wlist = krealloc(data->wlist,
461 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
465 new_wlist->widgets[n - 1] = widget;
466 new_wlist->num_widgets = n;
468 data->wlist = new_wlist;
473 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
474 struct snd_soc_dapm_path *path)
476 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
478 list_add_tail(&path->list_kcontrol, &data->paths);
481 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
483 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
488 return data->widget->power;
491 static struct list_head *dapm_kcontrol_get_path_list(
492 const struct snd_kcontrol *kcontrol)
494 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
499 #define dapm_kcontrol_for_each_path(path, kcontrol) \
500 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
503 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
505 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
509 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
511 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
514 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
516 if (data->value == value)
520 data->widget->on_val = value;
528 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
530 * @kcontrol: The kcontrol
532 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
533 struct snd_kcontrol *kcontrol)
535 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
537 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
540 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
542 * @kcontrol: The kcontrol
544 * Note: This function must only be used on kcontrols that are known to have
545 * been registered for a CODEC. Otherwise the behaviour is undefined.
547 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
548 struct snd_kcontrol *kcontrol)
550 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
552 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
554 static void dapm_reset(struct snd_soc_card *card)
556 struct snd_soc_dapm_widget *w;
558 lockdep_assert_held(&card->dapm_mutex);
560 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
562 list_for_each_entry(w, &card->widgets, list) {
563 w->new_power = w->power;
564 w->power_checked = false;
568 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
570 if (!dapm->component)
572 return dapm->component->name_prefix;
575 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
578 if (!dapm->component)
580 return snd_soc_component_read(dapm->component, reg, value);
583 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
584 int reg, unsigned int mask, unsigned int value)
586 if (!dapm->component)
588 return snd_soc_component_update_bits(dapm->component, reg,
592 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
593 int reg, unsigned int mask, unsigned int value)
595 if (!dapm->component)
597 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
600 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
603 snd_soc_component_async_complete(dapm->component);
606 static struct snd_soc_dapm_widget *
607 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
609 struct snd_soc_dapm_widget *w = wcache->widget;
610 struct list_head *wlist;
615 wlist = &w->dapm->card->widgets;
617 list_for_each_entry_from(w, wlist, list) {
618 if (!strcmp(name, w->name))
629 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
630 struct snd_soc_dapm_widget *w)
636 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
637 * @dapm: The DAPM context for which to set the level
638 * @level: The level to set
640 * Forces the DAPM bias level to a specific state. It will call the bias level
641 * callback of DAPM context with the specified level. This will even happen if
642 * the context is already at the same level. Furthermore it will not go through
643 * the normal bias level sequencing, meaning any intermediate states between the
644 * current and the target state will not be entered.
646 * Note that the change in bias level is only temporary and the next time
647 * snd_soc_dapm_sync() is called the state will be set to the level as
648 * determined by the DAPM core. The function is mainly intended to be used to
649 * used during probe or resume from suspend to power up the device so
650 * initialization can be done, before the DAPM core takes over.
652 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
653 enum snd_soc_bias_level level)
657 if (dapm->set_bias_level)
658 ret = dapm->set_bias_level(dapm, level);
661 dapm->bias_level = level;
665 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
668 * snd_soc_dapm_set_bias_level - set the bias level for the system
669 * @dapm: DAPM context
670 * @level: level to configure
672 * Configure the bias (power) levels for the SoC audio device.
674 * Returns 0 for success else error.
676 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
677 enum snd_soc_bias_level level)
679 struct snd_soc_card *card = dapm->card;
682 trace_snd_soc_bias_level_start(card, level);
684 if (card && card->set_bias_level)
685 ret = card->set_bias_level(card, dapm, level);
689 if (!card || dapm != &card->dapm)
690 ret = snd_soc_dapm_force_bias_level(dapm, level);
695 if (card && card->set_bias_level_post)
696 ret = card->set_bias_level_post(card, dapm, level);
698 trace_snd_soc_bias_level_done(card, level);
703 /* connect mux widget to its interconnecting audio paths */
704 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
705 struct snd_soc_dapm_path *path, const char *control_name,
706 struct snd_soc_dapm_widget *w)
708 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
709 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
710 unsigned int val, item;
713 if (e->reg != SND_SOC_NOPM) {
714 soc_dapm_read(dapm, e->reg, &val);
715 val = (val >> e->shift_l) & e->mask;
716 item = snd_soc_enum_val_to_item(e, val);
718 /* since a virtual mux has no backing registers to
719 * decide which path to connect, it will try to match
720 * with the first enumeration. This is to ensure
721 * that the default mux choice (the first) will be
722 * correctly powered up during initialization.
727 for (i = 0; i < e->items; i++) {
728 if (!(strcmp(control_name, e->texts[i]))) {
729 path->name = e->texts[i];
741 /* set up initial codec paths */
742 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
745 struct soc_mixer_control *mc = (struct soc_mixer_control *)
746 p->sink->kcontrol_news[i].private_value;
747 unsigned int reg = mc->reg;
748 unsigned int shift = mc->shift;
749 unsigned int max = mc->max;
750 unsigned int mask = (1 << fls(max)) - 1;
751 unsigned int invert = mc->invert;
754 if (reg != SND_SOC_NOPM) {
755 soc_dapm_read(p->sink->dapm, reg, &val);
757 * The nth_path argument allows this function to know
758 * which path of a kcontrol it is setting the initial
759 * status for. Ideally this would support any number
760 * of paths and channels. But since kcontrols only come
761 * in mono and stereo variants, we are limited to 2
764 * The following code assumes for stereo controls the
765 * first path is the left channel, and all remaining
766 * paths are the right channel.
768 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
770 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
771 val = (val >> mc->rshift) & mask;
773 val = (val >> shift) & mask;
783 /* connect mixer widget to its interconnecting audio paths */
784 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
785 struct snd_soc_dapm_path *path, const char *control_name)
789 /* search for mixer kcontrol */
790 for (i = 0; i < path->sink->num_kcontrols; i++) {
791 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
792 path->name = path->sink->kcontrol_news[i].name;
793 dapm_set_mixer_path_status(path, i, nth_path++);
800 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
801 struct snd_soc_dapm_widget *kcontrolw,
802 const struct snd_kcontrol_new *kcontrol_new,
803 struct snd_kcontrol **kcontrol)
805 struct snd_soc_dapm_widget *w;
810 list_for_each_entry(w, &dapm->card->widgets, list) {
811 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
813 for (i = 0; i < w->num_kcontrols; i++) {
814 if (&w->kcontrol_news[i] == kcontrol_new) {
816 *kcontrol = w->kcontrols[i];
826 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
827 * create it. Either way, add the widget into the control's widget list
829 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
832 struct snd_soc_dapm_context *dapm = w->dapm;
833 struct snd_card *card = dapm->card->snd_card;
837 struct snd_kcontrol *kcontrol;
838 bool wname_in_long_name, kcname_in_long_name;
839 char *long_name = NULL;
843 prefix = soc_dapm_prefix(dapm);
845 prefix_len = strlen(prefix) + 1;
849 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
854 wname_in_long_name = false;
855 kcname_in_long_name = true;
858 case snd_soc_dapm_switch:
859 case snd_soc_dapm_mixer:
860 case snd_soc_dapm_pga:
861 case snd_soc_dapm_out_drv:
862 wname_in_long_name = true;
863 kcname_in_long_name = true;
865 case snd_soc_dapm_mixer_named_ctl:
866 wname_in_long_name = false;
867 kcname_in_long_name = true;
869 case snd_soc_dapm_demux:
870 case snd_soc_dapm_mux:
871 wname_in_long_name = true;
872 kcname_in_long_name = false;
879 if (wname_in_long_name && kcname_in_long_name) {
881 * The control will get a prefix from the control
882 * creation process but we're also using the same
883 * prefix for widgets so cut the prefix off the
884 * front of the widget name.
886 long_name = kasprintf(GFP_KERNEL, "%s %s",
887 w->name + prefix_len,
888 w->kcontrol_news[kci].name);
889 if (long_name == NULL)
893 } else if (wname_in_long_name) {
895 name = w->name + prefix_len;
898 name = w->kcontrol_news[kci].name;
901 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
908 kcontrol->private_free = dapm_kcontrol_free;
910 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
912 snd_ctl_free_one(kcontrol);
916 ret = snd_ctl_add(card, kcontrol);
919 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
925 ret = dapm_kcontrol_add_widget(kcontrol, w);
927 w->kcontrols[kci] = kcontrol;
935 /* create new dapm mixer control */
936 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
939 struct snd_soc_dapm_path *path;
940 struct dapm_kcontrol_data *data;
943 for (i = 0; i < w->num_kcontrols; i++) {
945 snd_soc_dapm_widget_for_each_source_path(w, path) {
946 /* mixer/mux paths name must match control name */
947 if (path->name != (char *)w->kcontrol_news[i].name)
950 if (!w->kcontrols[i]) {
951 ret = dapm_create_or_share_kcontrol(w, i);
956 dapm_kcontrol_add_path(w->kcontrols[i], path);
958 data = snd_kcontrol_chip(w->kcontrols[i]);
960 snd_soc_dapm_add_path(data->widget->dapm,
970 /* create new dapm mux control */
971 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
973 struct snd_soc_dapm_context *dapm = w->dapm;
974 enum snd_soc_dapm_direction dir;
975 struct snd_soc_dapm_path *path;
980 case snd_soc_dapm_mux:
981 dir = SND_SOC_DAPM_DIR_OUT;
984 case snd_soc_dapm_demux:
985 dir = SND_SOC_DAPM_DIR_IN;
992 if (w->num_kcontrols != 1) {
994 "ASoC: %s %s has incorrect number of controls\n", type,
999 if (list_empty(&w->edges[dir])) {
1000 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1004 ret = dapm_create_or_share_kcontrol(w, 0);
1008 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1010 dapm_kcontrol_add_path(w->kcontrols[0], path);
1016 /* create new dapm volume control */
1017 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1021 for (i = 0; i < w->num_kcontrols; i++) {
1022 ret = dapm_create_or_share_kcontrol(w, i);
1030 /* create new dapm dai link control */
1031 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1034 struct snd_kcontrol *kcontrol;
1035 struct snd_soc_dapm_context *dapm = w->dapm;
1036 struct snd_card *card = dapm->card->snd_card;
1038 /* create control for links with > 1 config */
1039 if (w->num_params <= 1)
1043 for (i = 0; i < w->num_kcontrols; i++) {
1044 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1046 ret = snd_ctl_add(card, kcontrol);
1049 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1050 w->name, w->kcontrol_news[i].name, ret);
1053 kcontrol->private_data = w;
1054 w->kcontrols[i] = kcontrol;
1060 /* We implement power down on suspend by checking the power state of
1061 * the ALSA card - when we are suspending the ALSA state for the card
1064 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1066 int level = snd_power_get_state(widget->dapm->card->snd_card);
1069 case SNDRV_CTL_POWER_D3hot:
1070 case SNDRV_CTL_POWER_D3cold:
1071 if (widget->ignore_suspend)
1072 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1074 return widget->ignore_suspend;
1080 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1081 struct list_head *widgets)
1083 struct snd_soc_dapm_widget *w;
1084 struct list_head *it;
1085 unsigned int size = 0;
1088 list_for_each(it, widgets)
1091 *list = kzalloc(sizeof(**list) + size * sizeof(*w), GFP_KERNEL);
1095 list_for_each_entry(w, widgets, work_list)
1096 (*list)->widgets[i++] = w;
1098 (*list)->num_widgets = i;
1104 * Common implementation for is_connected_output_ep() and
1105 * is_connected_input_ep(). The function is inlined since the combined size of
1106 * the two specialized functions is only marginally larger then the size of the
1107 * generic function and at the same time the fast path of the specialized
1108 * functions is significantly smaller than the generic function.
1110 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1111 struct list_head *list, enum snd_soc_dapm_direction dir,
1112 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1113 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1114 enum snd_soc_dapm_direction)),
1115 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1116 enum snd_soc_dapm_direction))
1118 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1119 struct snd_soc_dapm_path *path;
1122 if (widget->endpoints[dir] >= 0)
1123 return widget->endpoints[dir];
1125 DAPM_UPDATE_STAT(widget, path_checks);
1127 /* do we need to add this widget to the list ? */
1129 list_add_tail(&widget->work_list, list);
1131 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1132 widget->endpoints[dir] = 1;
1133 return widget->endpoints[dir];
1136 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1137 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1138 return widget->endpoints[dir];
1141 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1142 DAPM_UPDATE_STAT(widget, neighbour_checks);
1144 if (path->weak || path->is_supply)
1150 trace_snd_soc_dapm_path(widget, dir, path);
1152 if (path->connect) {
1154 con += fn(path->node[dir], list, custom_stop_condition);
1159 widget->endpoints[dir] = con;
1165 * Recursively check for a completed path to an active or physically connected
1166 * output widget. Returns number of complete paths.
1168 * Optionally, can be supplied with a function acting as a stopping condition.
1169 * This function takes the dapm widget currently being examined and the walk
1170 * direction as an arguments, it should return true if the walk should be
1171 * stopped and false otherwise.
1173 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1174 struct list_head *list,
1175 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1176 enum snd_soc_dapm_direction))
1178 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1179 is_connected_output_ep, custom_stop_condition);
1183 * Recursively check for a completed path to an active or physically connected
1184 * input widget. Returns number of complete paths.
1186 * Optionally, can be supplied with a function acting as a stopping condition.
1187 * This function takes the dapm widget currently being examined and the walk
1188 * direction as an arguments, it should return true if the walk should be
1189 * stopped and false otherwise.
1191 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1192 struct list_head *list,
1193 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1194 enum snd_soc_dapm_direction))
1196 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1197 is_connected_input_ep, custom_stop_condition);
1201 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1202 * @dai: the soc DAI.
1203 * @stream: stream direction.
1204 * @list: list of active widgets for this stream.
1205 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1206 * walk based on custom logic.
1208 * Queries DAPM graph as to whether a valid audio stream path exists for
1209 * the initial stream specified by name. This takes into account
1210 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1212 * Optionally, can be supplied with a function acting as a stopping condition.
1213 * This function takes the dapm widget currently being examined and the walk
1214 * direction as an arguments, it should return true if the walk should be
1215 * stopped and false otherwise.
1217 * Returns the number of valid paths or negative error.
1219 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1220 struct snd_soc_dapm_widget_list **list,
1221 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1222 enum snd_soc_dapm_direction))
1224 struct snd_soc_card *card = dai->component->card;
1225 struct snd_soc_dapm_widget *w;
1230 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1233 * For is_connected_{output,input}_ep fully discover the graph we need
1234 * to reset the cached number of inputs and outputs.
1236 list_for_each_entry(w, &card->widgets, list) {
1237 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1238 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1241 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1242 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1243 custom_stop_condition);
1245 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1246 custom_stop_condition);
1248 /* Drop starting point */
1249 list_del(widgets.next);
1251 ret = dapm_widget_list_create(list, &widgets);
1255 trace_snd_soc_dapm_connected(paths, stream);
1256 mutex_unlock(&card->dapm_mutex);
1262 * Handler for regulator supply widget.
1264 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1265 struct snd_kcontrol *kcontrol, int event)
1269 soc_dapm_async_complete(w->dapm);
1271 if (SND_SOC_DAPM_EVENT_ON(event)) {
1272 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1273 ret = regulator_allow_bypass(w->regulator, false);
1275 dev_warn(w->dapm->dev,
1276 "ASoC: Failed to unbypass %s: %d\n",
1280 return regulator_enable(w->regulator);
1282 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1283 ret = regulator_allow_bypass(w->regulator, true);
1285 dev_warn(w->dapm->dev,
1286 "ASoC: Failed to bypass %s: %d\n",
1290 return regulator_disable_deferred(w->regulator, w->shift);
1293 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1296 * Handler for pinctrl widget.
1298 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1299 struct snd_kcontrol *kcontrol, int event)
1301 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1302 struct pinctrl *p = w->pinctrl;
1303 struct pinctrl_state *s;
1308 if (SND_SOC_DAPM_EVENT_ON(event))
1309 s = pinctrl_lookup_state(p, priv->active_state);
1311 s = pinctrl_lookup_state(p, priv->sleep_state);
1316 return pinctrl_select_state(p, s);
1318 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1321 * Handler for clock supply widget.
1323 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1324 struct snd_kcontrol *kcontrol, int event)
1329 soc_dapm_async_complete(w->dapm);
1331 #ifdef CONFIG_HAVE_CLK
1332 if (SND_SOC_DAPM_EVENT_ON(event)) {
1333 return clk_prepare_enable(w->clk);
1335 clk_disable_unprepare(w->clk);
1341 EXPORT_SYMBOL_GPL(dapm_clock_event);
1343 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1345 if (w->power_checked)
1346 return w->new_power;
1351 w->new_power = w->power_check(w);
1353 w->power_checked = true;
1355 return w->new_power;
1358 /* Generic check to see if a widget should be powered. */
1359 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1363 DAPM_UPDATE_STAT(w, power_checks);
1365 in = is_connected_input_ep(w, NULL, NULL);
1366 out = is_connected_output_ep(w, NULL, NULL);
1367 return out != 0 && in != 0;
1370 /* Check to see if a power supply is needed */
1371 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1373 struct snd_soc_dapm_path *path;
1375 DAPM_UPDATE_STAT(w, power_checks);
1377 /* Check if one of our outputs is connected */
1378 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1379 DAPM_UPDATE_STAT(w, neighbour_checks);
1384 if (path->connected &&
1385 !path->connected(path->source, path->sink))
1388 if (dapm_widget_power_check(path->sink))
1395 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1397 return w->connected;
1400 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1401 struct snd_soc_dapm_widget *b,
1409 sort = dapm_down_seq;
1411 if (sort[a->id] != sort[b->id])
1412 return sort[a->id] - sort[b->id];
1413 if (a->subseq != b->subseq) {
1415 return a->subseq - b->subseq;
1417 return b->subseq - a->subseq;
1419 if (a->reg != b->reg)
1420 return a->reg - b->reg;
1421 if (a->dapm != b->dapm)
1422 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1427 /* Insert a widget in order into a DAPM power sequence. */
1428 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1429 struct list_head *list,
1432 struct snd_soc_dapm_widget *w;
1434 list_for_each_entry(w, list, power_list)
1435 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1436 list_add_tail(&new_widget->power_list, &w->power_list);
1440 list_add_tail(&new_widget->power_list, list);
1443 static void dapm_seq_check_event(struct snd_soc_card *card,
1444 struct snd_soc_dapm_widget *w, int event)
1446 const char *ev_name;
1450 case SND_SOC_DAPM_PRE_PMU:
1451 ev_name = "PRE_PMU";
1454 case SND_SOC_DAPM_POST_PMU:
1455 ev_name = "POST_PMU";
1458 case SND_SOC_DAPM_PRE_PMD:
1459 ev_name = "PRE_PMD";
1462 case SND_SOC_DAPM_POST_PMD:
1463 ev_name = "POST_PMD";
1466 case SND_SOC_DAPM_WILL_PMU:
1467 ev_name = "WILL_PMU";
1470 case SND_SOC_DAPM_WILL_PMD:
1471 ev_name = "WILL_PMD";
1475 WARN(1, "Unknown event %d\n", event);
1479 if (w->new_power != power)
1482 if (w->event && (w->event_flags & event)) {
1483 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1485 soc_dapm_async_complete(w->dapm);
1486 trace_snd_soc_dapm_widget_event_start(w, event);
1487 ret = w->event(w, NULL, event);
1488 trace_snd_soc_dapm_widget_event_done(w, event);
1490 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1491 ev_name, w->name, ret);
1495 /* Apply the coalesced changes from a DAPM sequence */
1496 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1497 struct list_head *pending)
1499 struct snd_soc_dapm_context *dapm;
1500 struct snd_soc_dapm_widget *w;
1502 unsigned int value = 0;
1503 unsigned int mask = 0;
1505 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1509 list_for_each_entry(w, pending, power_list) {
1510 WARN_ON(reg != w->reg || dapm != w->dapm);
1511 w->power = w->new_power;
1513 mask |= w->mask << w->shift;
1515 value |= w->on_val << w->shift;
1517 value |= w->off_val << w->shift;
1519 pop_dbg(dapm->dev, card->pop_time,
1520 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1521 w->name, reg, value, mask);
1523 /* Check for events */
1524 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1525 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1529 /* Any widget will do, they should all be updating the
1533 pop_dbg(dapm->dev, card->pop_time,
1534 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1535 value, mask, reg, card->pop_time);
1536 pop_wait(card->pop_time);
1537 soc_dapm_update_bits(dapm, reg, mask, value);
1540 list_for_each_entry(w, pending, power_list) {
1541 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1542 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1546 /* Apply a DAPM power sequence.
1548 * We walk over a pre-sorted list of widgets to apply power to. In
1549 * order to minimise the number of writes to the device required
1550 * multiple widgets will be updated in a single write where possible.
1551 * Currently anything that requires more than a single write is not
1554 static void dapm_seq_run(struct snd_soc_card *card,
1555 struct list_head *list, int event, bool power_up)
1557 struct snd_soc_dapm_widget *w, *n;
1558 struct snd_soc_dapm_context *d;
1561 int cur_subseq = -1;
1562 int cur_reg = SND_SOC_NOPM;
1563 struct snd_soc_dapm_context *cur_dapm = NULL;
1570 sort = dapm_down_seq;
1572 list_for_each_entry_safe(w, n, list, power_list) {
1575 /* Do we need to apply any queued changes? */
1576 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1577 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1578 if (!list_empty(&pending))
1579 dapm_seq_run_coalesced(card, &pending);
1581 if (cur_dapm && cur_dapm->seq_notifier) {
1582 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1583 if (sort[i] == cur_sort)
1584 cur_dapm->seq_notifier(cur_dapm,
1589 if (cur_dapm && w->dapm != cur_dapm)
1590 soc_dapm_async_complete(cur_dapm);
1592 INIT_LIST_HEAD(&pending);
1594 cur_subseq = INT_MIN;
1595 cur_reg = SND_SOC_NOPM;
1600 case snd_soc_dapm_pre:
1602 list_for_each_entry_safe_continue(w, n, list,
1605 if (event == SND_SOC_DAPM_STREAM_START)
1607 NULL, SND_SOC_DAPM_PRE_PMU);
1608 else if (event == SND_SOC_DAPM_STREAM_STOP)
1610 NULL, SND_SOC_DAPM_PRE_PMD);
1613 case snd_soc_dapm_post:
1615 list_for_each_entry_safe_continue(w, n, list,
1618 if (event == SND_SOC_DAPM_STREAM_START)
1620 NULL, SND_SOC_DAPM_POST_PMU);
1621 else if (event == SND_SOC_DAPM_STREAM_STOP)
1623 NULL, SND_SOC_DAPM_POST_PMD);
1627 /* Queue it up for application */
1628 cur_sort = sort[w->id];
1629 cur_subseq = w->subseq;
1632 list_move(&w->power_list, &pending);
1637 dev_err(w->dapm->dev,
1638 "ASoC: Failed to apply widget power: %d\n", ret);
1641 if (!list_empty(&pending))
1642 dapm_seq_run_coalesced(card, &pending);
1644 if (cur_dapm && cur_dapm->seq_notifier) {
1645 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1646 if (sort[i] == cur_sort)
1647 cur_dapm->seq_notifier(cur_dapm,
1651 list_for_each_entry(d, &card->dapm_list, list) {
1652 soc_dapm_async_complete(d);
1656 static void dapm_widget_update(struct snd_soc_card *card)
1658 struct snd_soc_dapm_update *update = card->update;
1659 struct snd_soc_dapm_widget_list *wlist;
1660 struct snd_soc_dapm_widget *w = NULL;
1664 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1667 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1669 for (wi = 0; wi < wlist->num_widgets; wi++) {
1670 w = wlist->widgets[wi];
1672 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1673 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1675 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1683 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1686 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1689 if (update->has_second_set) {
1690 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1691 update->mask2, update->val2);
1693 dev_err(w->dapm->dev,
1694 "ASoC: %s DAPM update failed: %d\n",
1698 for (wi = 0; wi < wlist->num_widgets; wi++) {
1699 w = wlist->widgets[wi];
1701 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1702 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1704 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1710 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1711 * they're changing state.
1713 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1715 struct snd_soc_dapm_context *d = data;
1718 /* If we're off and we're not supposed to go into STANDBY */
1719 if (d->bias_level == SND_SOC_BIAS_OFF &&
1720 d->target_bias_level != SND_SOC_BIAS_OFF) {
1722 pm_runtime_get_sync(d->dev);
1724 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1727 "ASoC: Failed to turn on bias: %d\n", ret);
1730 /* Prepare for a transition to ON or away from ON */
1731 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1732 d->bias_level != SND_SOC_BIAS_ON) ||
1733 (d->target_bias_level != SND_SOC_BIAS_ON &&
1734 d->bias_level == SND_SOC_BIAS_ON)) {
1735 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1738 "ASoC: Failed to prepare bias: %d\n", ret);
1742 /* Async callback run prior to DAPM sequences - brings to their final
1745 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1747 struct snd_soc_dapm_context *d = data;
1750 /* If we just powered the last thing off drop to standby bias */
1751 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1752 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1753 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1754 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1756 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1760 /* If we're in standby and can support bias off then do that */
1761 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1762 d->target_bias_level == SND_SOC_BIAS_OFF) {
1763 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1765 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1769 pm_runtime_put(d->dev);
1772 /* If we just powered up then move to active bias */
1773 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1774 d->target_bias_level == SND_SOC_BIAS_ON) {
1775 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1777 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1782 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1783 bool power, bool connect)
1785 /* If a connection is being made or broken then that update
1786 * will have marked the peer dirty, otherwise the widgets are
1787 * not connected and this update has no impact. */
1791 /* If the peer is already in the state we're moving to then we
1792 * won't have an impact on it. */
1793 if (power != peer->power)
1794 dapm_mark_dirty(peer, "peer state change");
1797 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1798 struct list_head *up_list,
1799 struct list_head *down_list)
1801 struct snd_soc_dapm_path *path;
1803 if (w->power == power)
1806 trace_snd_soc_dapm_widget_power(w, power);
1808 /* If we changed our power state perhaps our neigbours changed
1811 snd_soc_dapm_widget_for_each_source_path(w, path)
1812 dapm_widget_set_peer_power(path->source, power, path->connect);
1814 /* Supplies can't affect their outputs, only their inputs */
1815 if (!w->is_supply) {
1816 snd_soc_dapm_widget_for_each_sink_path(w, path)
1817 dapm_widget_set_peer_power(path->sink, power,
1822 dapm_seq_insert(w, up_list, true);
1824 dapm_seq_insert(w, down_list, false);
1827 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1828 struct list_head *up_list,
1829 struct list_head *down_list)
1834 case snd_soc_dapm_pre:
1835 dapm_seq_insert(w, down_list, false);
1837 case snd_soc_dapm_post:
1838 dapm_seq_insert(w, up_list, true);
1842 power = dapm_widget_power_check(w);
1844 dapm_widget_set_power(w, power, up_list, down_list);
1849 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1851 if (dapm->idle_bias_off)
1854 switch (snd_power_get_state(dapm->card->snd_card)) {
1855 case SNDRV_CTL_POWER_D3hot:
1856 case SNDRV_CTL_POWER_D3cold:
1857 return dapm->suspend_bias_off;
1866 * Scan each dapm widget for complete audio path.
1867 * A complete path is a route that has valid endpoints i.e.:-
1869 * o DAC to output pin.
1870 * o Input pin to ADC.
1871 * o Input pin to Output pin (bypass, sidetone)
1872 * o DAC to ADC (loopback).
1874 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1876 struct snd_soc_dapm_widget *w;
1877 struct snd_soc_dapm_context *d;
1879 LIST_HEAD(down_list);
1880 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1881 enum snd_soc_bias_level bias;
1883 lockdep_assert_held(&card->dapm_mutex);
1885 trace_snd_soc_dapm_start(card);
1887 list_for_each_entry(d, &card->dapm_list, list) {
1888 if (dapm_idle_bias_off(d))
1889 d->target_bias_level = SND_SOC_BIAS_OFF;
1891 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1896 /* Check which widgets we need to power and store them in
1897 * lists indicating if they should be powered up or down. We
1898 * only check widgets that have been flagged as dirty but note
1899 * that new widgets may be added to the dirty list while we
1902 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1903 dapm_power_one_widget(w, &up_list, &down_list);
1906 list_for_each_entry(w, &card->widgets, list) {
1908 case snd_soc_dapm_pre:
1909 case snd_soc_dapm_post:
1910 /* These widgets always need to be powered */
1913 list_del_init(&w->dirty);
1920 /* Supplies and micbiases only bring the
1921 * context up to STANDBY as unless something
1922 * else is active and passing audio they
1923 * generally don't require full power. Signal
1924 * generators are virtual pins and have no
1925 * power impact themselves.
1928 case snd_soc_dapm_siggen:
1929 case snd_soc_dapm_vmid:
1931 case snd_soc_dapm_supply:
1932 case snd_soc_dapm_regulator_supply:
1933 case snd_soc_dapm_pinctrl:
1934 case snd_soc_dapm_clock_supply:
1935 case snd_soc_dapm_micbias:
1936 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1937 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1940 d->target_bias_level = SND_SOC_BIAS_ON;
1947 /* Force all contexts in the card to the same bias state if
1948 * they're not ground referenced.
1950 bias = SND_SOC_BIAS_OFF;
1951 list_for_each_entry(d, &card->dapm_list, list)
1952 if (d->target_bias_level > bias)
1953 bias = d->target_bias_level;
1954 list_for_each_entry(d, &card->dapm_list, list)
1955 if (!dapm_idle_bias_off(d))
1956 d->target_bias_level = bias;
1958 trace_snd_soc_dapm_walk_done(card);
1960 /* Run card bias changes at first */
1961 dapm_pre_sequence_async(&card->dapm, 0);
1962 /* Run other bias changes in parallel */
1963 list_for_each_entry(d, &card->dapm_list, list) {
1964 if (d != &card->dapm)
1965 async_schedule_domain(dapm_pre_sequence_async, d,
1968 async_synchronize_full_domain(&async_domain);
1970 list_for_each_entry(w, &down_list, power_list) {
1971 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1974 list_for_each_entry(w, &up_list, power_list) {
1975 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1978 /* Power down widgets first; try to avoid amplifying pops. */
1979 dapm_seq_run(card, &down_list, event, false);
1981 dapm_widget_update(card);
1984 dapm_seq_run(card, &up_list, event, true);
1986 /* Run all the bias changes in parallel */
1987 list_for_each_entry(d, &card->dapm_list, list) {
1988 if (d != &card->dapm)
1989 async_schedule_domain(dapm_post_sequence_async, d,
1992 async_synchronize_full_domain(&async_domain);
1993 /* Run card bias changes at last */
1994 dapm_post_sequence_async(&card->dapm, 0);
1996 /* do we need to notify any clients that DAPM event is complete */
1997 list_for_each_entry(d, &card->dapm_list, list) {
1998 if (d->stream_event)
1999 d->stream_event(d, event);
2002 pop_dbg(card->dev, card->pop_time,
2003 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2004 pop_wait(card->pop_time);
2006 trace_snd_soc_dapm_done(card);
2011 #ifdef CONFIG_DEBUG_FS
2012 static ssize_t dapm_widget_power_read_file(struct file *file,
2013 char __user *user_buf,
2014 size_t count, loff_t *ppos)
2016 struct snd_soc_dapm_widget *w = file->private_data;
2017 struct snd_soc_card *card = w->dapm->card;
2018 enum snd_soc_dapm_direction dir, rdir;
2022 struct snd_soc_dapm_path *p = NULL;
2024 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2028 mutex_lock(&card->dapm_mutex);
2030 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2035 in = is_connected_input_ep(w, NULL, NULL);
2036 out = is_connected_output_ep(w, NULL, NULL);
2039 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2040 w->name, w->power ? "On" : "Off",
2041 w->force ? " (forced)" : "", in, out);
2044 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2045 " - R%d(0x%x) mask 0x%x",
2046 w->reg, w->reg, w->mask << w->shift);
2048 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
2051 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2053 w->active ? "active" : "inactive");
2055 snd_soc_dapm_for_each_direction(dir) {
2056 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2057 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2058 if (p->connected && !p->connected(p->source, p->sink))
2064 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2065 " %s \"%s\" \"%s\"\n",
2066 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2067 p->name ? p->name : "static",
2068 p->node[rdir]->name);
2072 mutex_unlock(&card->dapm_mutex);
2074 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2080 static const struct file_operations dapm_widget_power_fops = {
2081 .open = simple_open,
2082 .read = dapm_widget_power_read_file,
2083 .llseek = default_llseek,
2086 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2087 size_t count, loff_t *ppos)
2089 struct snd_soc_dapm_context *dapm = file->private_data;
2092 switch (dapm->bias_level) {
2093 case SND_SOC_BIAS_ON:
2096 case SND_SOC_BIAS_PREPARE:
2097 level = "Prepare\n";
2099 case SND_SOC_BIAS_STANDBY:
2100 level = "Standby\n";
2102 case SND_SOC_BIAS_OFF:
2106 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2107 level = "Unknown\n";
2111 return simple_read_from_buffer(user_buf, count, ppos, level,
2115 static const struct file_operations dapm_bias_fops = {
2116 .open = simple_open,
2117 .read = dapm_bias_read_file,
2118 .llseek = default_llseek,
2121 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2122 struct dentry *parent)
2129 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2131 if (!dapm->debugfs_dapm) {
2133 "ASoC: Failed to create DAPM debugfs directory\n");
2137 d = debugfs_create_file("bias_level", 0444,
2138 dapm->debugfs_dapm, dapm,
2142 "ASoC: Failed to create bias level debugfs file\n");
2145 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2147 struct snd_soc_dapm_context *dapm = w->dapm;
2150 if (!dapm->debugfs_dapm || !w->name)
2153 d = debugfs_create_file(w->name, 0444,
2154 dapm->debugfs_dapm, w,
2155 &dapm_widget_power_fops);
2157 dev_warn(w->dapm->dev,
2158 "ASoC: Failed to create %s debugfs file\n",
2162 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2164 debugfs_remove_recursive(dapm->debugfs_dapm);
2168 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2169 struct dentry *parent)
2173 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2177 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2184 * soc_dapm_connect_path() - Connects or disconnects a path
2185 * @path: The path to update
2186 * @connect: The new connect state of the path. True if the path is connected,
2187 * false if it is disconnected.
2188 * @reason: The reason why the path changed (for debugging only)
2190 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2191 bool connect, const char *reason)
2193 if (path->connect == connect)
2196 path->connect = connect;
2197 dapm_mark_dirty(path->source, reason);
2198 dapm_mark_dirty(path->sink, reason);
2199 dapm_path_invalidate(path);
2202 /* test and update the power status of a mux widget */
2203 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2204 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2206 struct snd_soc_dapm_path *path;
2210 lockdep_assert_held(&card->dapm_mutex);
2212 /* find dapm widget path assoc with kcontrol */
2213 dapm_kcontrol_for_each_path(path, kcontrol) {
2215 /* we now need to match the string in the enum to the path */
2216 if (!(strcmp(path->name, e->texts[mux])))
2221 soc_dapm_connect_path(path, connect, "mux update");
2225 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2230 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2231 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2232 struct snd_soc_dapm_update *update)
2234 struct snd_soc_card *card = dapm->card;
2237 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2238 card->update = update;
2239 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2240 card->update = NULL;
2241 mutex_unlock(&card->dapm_mutex);
2243 soc_dpcm_runtime_update(card);
2246 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2248 /* test and update the power status of a mixer or switch widget */
2249 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2250 struct snd_kcontrol *kcontrol,
2251 int connect, int rconnect)
2253 struct snd_soc_dapm_path *path;
2256 lockdep_assert_held(&card->dapm_mutex);
2258 /* find dapm widget path assoc with kcontrol */
2259 dapm_kcontrol_for_each_path(path, kcontrol) {
2261 * Ideally this function should support any number of
2262 * paths and channels. But since kcontrols only come
2263 * in mono and stereo variants, we are limited to 2
2266 * The following code assumes for stereo controls the
2267 * first path (when 'found == 0') is the left channel,
2268 * and all remaining paths (when 'found == 1') are the
2271 * A stereo control is signified by a valid 'rconnect'
2272 * value, either 0 for unconnected, or >= 0 for connected.
2273 * This is chosen instead of using snd_soc_volsw_is_stereo,
2274 * so that the behavior of snd_soc_dapm_mixer_update_power
2275 * doesn't change even when the kcontrol passed in is
2278 * It passes 'connect' as the path connect status for
2279 * the left channel, and 'rconnect' for the right
2282 if (found && rconnect >= 0)
2283 soc_dapm_connect_path(path, rconnect, "mixer update");
2285 soc_dapm_connect_path(path, connect, "mixer update");
2290 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2295 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2296 struct snd_kcontrol *kcontrol, int connect,
2297 struct snd_soc_dapm_update *update)
2299 struct snd_soc_card *card = dapm->card;
2302 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2303 card->update = update;
2304 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2305 card->update = NULL;
2306 mutex_unlock(&card->dapm_mutex);
2308 soc_dpcm_runtime_update(card);
2311 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2313 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2316 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2317 struct snd_soc_dapm_widget *w;
2319 char *state = "not set";
2321 /* card won't be set for the dummy component, as a spot fix
2322 * we're checking for that case specifically here but in future
2323 * we will ensure that the dummy component looks like others.
2328 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2329 if (w->dapm != dapm)
2332 /* only display widgets that burn power */
2334 case snd_soc_dapm_hp:
2335 case snd_soc_dapm_mic:
2336 case snd_soc_dapm_spk:
2337 case snd_soc_dapm_line:
2338 case snd_soc_dapm_micbias:
2339 case snd_soc_dapm_dac:
2340 case snd_soc_dapm_adc:
2341 case snd_soc_dapm_pga:
2342 case snd_soc_dapm_out_drv:
2343 case snd_soc_dapm_mixer:
2344 case snd_soc_dapm_mixer_named_ctl:
2345 case snd_soc_dapm_supply:
2346 case snd_soc_dapm_regulator_supply:
2347 case snd_soc_dapm_pinctrl:
2348 case snd_soc_dapm_clock_supply:
2350 count += sprintf(buf + count, "%s: %s\n",
2351 w->name, w->power ? "On":"Off");
2358 switch (snd_soc_dapm_get_bias_level(dapm)) {
2359 case SND_SOC_BIAS_ON:
2362 case SND_SOC_BIAS_PREPARE:
2365 case SND_SOC_BIAS_STANDBY:
2368 case SND_SOC_BIAS_OFF:
2372 count += sprintf(buf + count, "PM State: %s\n", state);
2377 /* show dapm widget status in sys fs */
2378 static ssize_t dapm_widget_show(struct device *dev,
2379 struct device_attribute *attr, char *buf)
2381 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2384 mutex_lock(&rtd->card->dapm_mutex);
2386 for (i = 0; i < rtd->num_codecs; i++) {
2387 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2389 count += dapm_widget_show_component(cmpnt, buf + count);
2392 mutex_unlock(&rtd->card->dapm_mutex);
2397 static DEVICE_ATTR_RO(dapm_widget);
2399 struct attribute *soc_dapm_dev_attrs[] = {
2400 &dev_attr_dapm_widget.attr,
2404 static void dapm_free_path(struct snd_soc_dapm_path *path)
2406 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2407 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2408 list_del(&path->list_kcontrol);
2409 list_del(&path->list);
2413 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2415 struct snd_soc_dapm_path *p, *next_p;
2416 enum snd_soc_dapm_direction dir;
2420 * remove source and sink paths associated to this widget.
2421 * While removing the path, remove reference to it from both
2422 * source and sink widgets so that path is removed only once.
2424 snd_soc_dapm_for_each_direction(dir) {
2425 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2429 kfree(w->kcontrols);
2430 kfree_const(w->name);
2434 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2436 dapm->path_sink_cache.widget = NULL;
2437 dapm->path_source_cache.widget = NULL;
2440 /* free all dapm widgets and resources */
2441 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2443 struct snd_soc_dapm_widget *w, *next_w;
2445 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2446 if (w->dapm != dapm)
2448 snd_soc_dapm_free_widget(w);
2450 snd_soc_dapm_reset_cache(dapm);
2453 static struct snd_soc_dapm_widget *dapm_find_widget(
2454 struct snd_soc_dapm_context *dapm, const char *pin,
2455 bool search_other_contexts)
2457 struct snd_soc_dapm_widget *w;
2458 struct snd_soc_dapm_widget *fallback = NULL;
2460 list_for_each_entry(w, &dapm->card->widgets, list) {
2461 if (!strcmp(w->name, pin)) {
2462 if (w->dapm == dapm)
2469 if (search_other_contexts)
2475 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2476 const char *pin, int status)
2478 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2480 dapm_assert_locked(dapm);
2483 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2487 if (w->connected != status) {
2488 dapm_mark_dirty(w, "pin configuration");
2489 dapm_widget_invalidate_input_paths(w);
2490 dapm_widget_invalidate_output_paths(w);
2493 w->connected = status;
2501 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2502 * @dapm: DAPM context
2504 * Walks all dapm audio paths and powers widgets according to their
2505 * stream or path usage.
2507 * Requires external locking.
2509 * Returns 0 for success.
2511 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2514 * Suppress early reports (eg, jacks syncing their state) to avoid
2515 * silly DAPM runs during card startup.
2517 if (!dapm->card || !dapm->card->instantiated)
2520 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2522 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2525 * snd_soc_dapm_sync - 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 * Returns 0 for success.
2533 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2537 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2538 ret = snd_soc_dapm_sync_unlocked(dapm);
2539 mutex_unlock(&dapm->card->dapm_mutex);
2542 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2545 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2546 * @w: The widget for which to update the flags
2548 * Some widgets have a dynamic category which depends on which neighbors they
2549 * are connected to. This function update the category for these widgets.
2551 * This function must be called whenever a path is added or removed to a widget.
2553 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2555 enum snd_soc_dapm_direction dir;
2556 struct snd_soc_dapm_path *p;
2560 case snd_soc_dapm_input:
2561 /* On a fully routed card an input is never a source */
2562 if (w->dapm->card->fully_routed)
2564 ep = SND_SOC_DAPM_EP_SOURCE;
2565 snd_soc_dapm_widget_for_each_source_path(w, p) {
2566 if (p->source->id == snd_soc_dapm_micbias ||
2567 p->source->id == snd_soc_dapm_mic ||
2568 p->source->id == snd_soc_dapm_line ||
2569 p->source->id == snd_soc_dapm_output) {
2575 case snd_soc_dapm_output:
2576 /* On a fully routed card a output is never a sink */
2577 if (w->dapm->card->fully_routed)
2579 ep = SND_SOC_DAPM_EP_SINK;
2580 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2581 if (p->sink->id == snd_soc_dapm_spk ||
2582 p->sink->id == snd_soc_dapm_hp ||
2583 p->sink->id == snd_soc_dapm_line ||
2584 p->sink->id == snd_soc_dapm_input) {
2590 case snd_soc_dapm_line:
2592 snd_soc_dapm_for_each_direction(dir) {
2593 if (!list_empty(&w->edges[dir]))
2594 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2604 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2605 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2606 const char *control)
2608 bool dynamic_source = false;
2609 bool dynamic_sink = false;
2614 switch (source->id) {
2615 case snd_soc_dapm_demux:
2616 dynamic_source = true;
2623 case snd_soc_dapm_mux:
2624 case snd_soc_dapm_switch:
2625 case snd_soc_dapm_mixer:
2626 case snd_soc_dapm_mixer_named_ctl:
2627 dynamic_sink = true;
2633 if (dynamic_source && dynamic_sink) {
2635 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2636 source->name, control, sink->name);
2638 } else if (!dynamic_source && !dynamic_sink) {
2640 "Control not supported for path %s -> [%s] -> %s\n",
2641 source->name, control, sink->name);
2648 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2649 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2650 const char *control,
2651 int (*connected)(struct snd_soc_dapm_widget *source,
2652 struct snd_soc_dapm_widget *sink))
2654 struct snd_soc_dapm_widget *widgets[2];
2655 enum snd_soc_dapm_direction dir;
2656 struct snd_soc_dapm_path *path;
2659 if (wsink->is_supply && !wsource->is_supply) {
2661 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2662 wsource->name, wsink->name);
2666 if (connected && !wsource->is_supply) {
2668 "connected() callback only supported for supply widgets (%s -> %s)\n",
2669 wsource->name, wsink->name);
2673 if (wsource->is_supply && control) {
2675 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2676 wsource->name, control, wsink->name);
2680 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2684 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2688 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2689 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2690 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2691 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2693 path->connected = connected;
2694 INIT_LIST_HEAD(&path->list);
2695 INIT_LIST_HEAD(&path->list_kcontrol);
2697 if (wsource->is_supply || wsink->is_supply)
2698 path->is_supply = 1;
2700 /* connect static paths */
2701 if (control == NULL) {
2704 switch (wsource->id) {
2705 case snd_soc_dapm_demux:
2706 ret = dapm_connect_mux(dapm, path, control, wsource);
2714 switch (wsink->id) {
2715 case snd_soc_dapm_mux:
2716 ret = dapm_connect_mux(dapm, path, control, wsink);
2720 case snd_soc_dapm_switch:
2721 case snd_soc_dapm_mixer:
2722 case snd_soc_dapm_mixer_named_ctl:
2723 ret = dapm_connect_mixer(dapm, path, control);
2732 list_add(&path->list, &dapm->card->paths);
2733 snd_soc_dapm_for_each_direction(dir)
2734 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2736 snd_soc_dapm_for_each_direction(dir) {
2737 dapm_update_widget_flags(widgets[dir]);
2738 dapm_mark_dirty(widgets[dir], "Route added");
2741 if (dapm->card->instantiated && path->connect)
2742 dapm_path_invalidate(path);
2750 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2751 const struct snd_soc_dapm_route *route)
2753 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2754 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2757 char prefixed_sink[80];
2758 char prefixed_source[80];
2762 prefix = soc_dapm_prefix(dapm);
2764 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2765 prefix, route->sink);
2766 sink = prefixed_sink;
2767 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2768 prefix, route->source);
2769 source = prefixed_source;
2772 source = route->source;
2775 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2776 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2778 if (wsink && wsource)
2782 * find src and dest widgets over all widgets but favor a widget from
2783 * current DAPM context
2785 list_for_each_entry(w, &dapm->card->widgets, list) {
2786 if (!wsink && !(strcmp(w->name, sink))) {
2788 if (w->dapm == dapm) {
2795 if (!wsource && !(strcmp(w->name, source))) {
2797 if (w->dapm == dapm) {
2804 /* use widget from another DAPM context if not found from this */
2810 if (wsource == NULL) {
2811 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2815 if (wsink == NULL) {
2816 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2822 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2823 dapm_wcache_update(&dapm->path_source_cache, wsource);
2825 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2832 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2833 source, route->control, sink);
2837 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2838 const struct snd_soc_dapm_route *route)
2840 struct snd_soc_dapm_widget *wsource, *wsink;
2841 struct snd_soc_dapm_path *path, *p;
2844 char prefixed_sink[80];
2845 char prefixed_source[80];
2848 if (route->control) {
2850 "ASoC: Removal of routes with controls not supported\n");
2854 prefix = soc_dapm_prefix(dapm);
2856 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2857 prefix, route->sink);
2858 sink = prefixed_sink;
2859 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2860 prefix, route->source);
2861 source = prefixed_source;
2864 source = route->source;
2868 list_for_each_entry(p, &dapm->card->paths, list) {
2869 if (strcmp(p->source->name, source) != 0)
2871 if (strcmp(p->sink->name, sink) != 0)
2878 wsource = path->source;
2881 dapm_mark_dirty(wsource, "Route removed");
2882 dapm_mark_dirty(wsink, "Route removed");
2884 dapm_path_invalidate(path);
2886 dapm_free_path(path);
2888 /* Update any path related flags */
2889 dapm_update_widget_flags(wsource);
2890 dapm_update_widget_flags(wsink);
2892 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2900 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2901 * @dapm: DAPM context
2902 * @route: audio routes
2903 * @num: number of routes
2905 * Connects 2 dapm widgets together via a named audio path. The sink is
2906 * the widget receiving the audio signal, whilst the source is the sender
2907 * of the audio signal.
2909 * Returns 0 for success else error. On error all resources can be freed
2910 * with a call to snd_soc_card_free().
2912 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2913 const struct snd_soc_dapm_route *route, int num)
2917 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2918 for (i = 0; i < num; i++) {
2919 r = snd_soc_dapm_add_route(dapm, route);
2921 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2923 route->control ? route->control : "direct",
2929 mutex_unlock(&dapm->card->dapm_mutex);
2933 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2936 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2937 * @dapm: DAPM context
2938 * @route: audio routes
2939 * @num: number of routes
2941 * Removes routes from the DAPM context.
2943 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2944 const struct snd_soc_dapm_route *route, int num)
2948 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2949 for (i = 0; i < num; i++) {
2950 snd_soc_dapm_del_route(dapm, route);
2953 mutex_unlock(&dapm->card->dapm_mutex);
2957 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2959 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2960 const struct snd_soc_dapm_route *route)
2962 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2965 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2968 struct snd_soc_dapm_path *path;
2972 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2978 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2983 if (route->control || route->connected)
2984 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2985 route->source, route->sink);
2987 snd_soc_dapm_widget_for_each_sink_path(source, path) {
2988 if (path->sink == sink) {
2995 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2996 route->source, route->sink);
2998 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2999 count, route->source, route->sink);
3005 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3006 * @dapm: DAPM context
3007 * @route: audio routes
3008 * @num: number of routes
3010 * Mark existing routes matching those specified in the passed array
3011 * as being weak, meaning that they are ignored for the purpose of
3012 * power decisions. The main intended use case is for sidetone paths
3013 * which couple audio between other independent paths if they are both
3014 * active in order to make the combination work better at the user
3015 * level but which aren't intended to be "used".
3017 * Note that CODEC drivers should not use this as sidetone type paths
3018 * can frequently also be used as bypass paths.
3020 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3021 const struct snd_soc_dapm_route *route, int num)
3026 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3027 for (i = 0; i < num; i++) {
3028 err = snd_soc_dapm_weak_route(dapm, route);
3033 mutex_unlock(&dapm->card->dapm_mutex);
3037 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3040 * snd_soc_dapm_new_widgets - add new dapm widgets
3041 * @card: card to be checked for new dapm widgets
3043 * Checks the codec for any new dapm widgets and creates them if found.
3045 * Returns 0 for success.
3047 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3049 struct snd_soc_dapm_widget *w;
3052 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3054 list_for_each_entry(w, &card->widgets, list)
3059 if (w->num_kcontrols) {
3060 w->kcontrols = kzalloc(w->num_kcontrols *
3061 sizeof(struct snd_kcontrol *),
3063 if (!w->kcontrols) {
3064 mutex_unlock(&card->dapm_mutex);
3070 case snd_soc_dapm_switch:
3071 case snd_soc_dapm_mixer:
3072 case snd_soc_dapm_mixer_named_ctl:
3075 case snd_soc_dapm_mux:
3076 case snd_soc_dapm_demux:
3079 case snd_soc_dapm_pga:
3080 case snd_soc_dapm_out_drv:
3083 case snd_soc_dapm_dai_link:
3084 dapm_new_dai_link(w);
3090 /* Read the initial power state from the device */
3092 soc_dapm_read(w->dapm, w->reg, &val);
3093 val = val >> w->shift;
3095 if (val == w->on_val)
3101 dapm_mark_dirty(w, "new widget");
3102 dapm_debugfs_add_widget(w);
3105 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3106 mutex_unlock(&card->dapm_mutex);
3109 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3112 * snd_soc_dapm_get_volsw - dapm mixer get callback
3113 * @kcontrol: mixer control
3114 * @ucontrol: control element information
3116 * Callback to get the value of a dapm mixer control.
3118 * Returns 0 for success.
3120 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3121 struct snd_ctl_elem_value *ucontrol)
3123 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3124 struct snd_soc_card *card = dapm->card;
3125 struct soc_mixer_control *mc =
3126 (struct soc_mixer_control *)kcontrol->private_value;
3128 unsigned int shift = mc->shift;
3130 unsigned int width = fls(max);
3131 unsigned int mask = (1 << fls(max)) - 1;
3132 unsigned int invert = mc->invert;
3133 unsigned int reg_val, val, rval = 0;
3136 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3137 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3138 ret = soc_dapm_read(dapm, reg, ®_val);
3139 val = (reg_val >> shift) & mask;
3141 if (ret == 0 && reg != mc->rreg)
3142 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3144 if (snd_soc_volsw_is_stereo(mc))
3145 rval = (reg_val >> mc->rshift) & mask;
3147 reg_val = dapm_kcontrol_get_value(kcontrol);
3148 val = reg_val & mask;
3150 if (snd_soc_volsw_is_stereo(mc))
3151 rval = (reg_val >> width) & mask;
3153 mutex_unlock(&card->dapm_mutex);
3159 ucontrol->value.integer.value[0] = max - val;
3161 ucontrol->value.integer.value[0] = val;
3163 if (snd_soc_volsw_is_stereo(mc)) {
3165 ucontrol->value.integer.value[1] = max - rval;
3167 ucontrol->value.integer.value[1] = rval;
3172 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3175 * snd_soc_dapm_put_volsw - dapm mixer set callback
3176 * @kcontrol: mixer control
3177 * @ucontrol: control element information
3179 * Callback to set the value of a dapm mixer control.
3181 * Returns 0 for success.
3183 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3184 struct snd_ctl_elem_value *ucontrol)
3186 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3187 struct snd_soc_card *card = dapm->card;
3188 struct soc_mixer_control *mc =
3189 (struct soc_mixer_control *)kcontrol->private_value;
3191 unsigned int shift = mc->shift;
3193 unsigned int width = fls(max);
3194 unsigned int mask = (1 << width) - 1;
3195 unsigned int invert = mc->invert;
3196 unsigned int val, rval = 0;
3197 int connect, rconnect = -1, change, reg_change = 0;
3198 struct snd_soc_dapm_update update = {};
3201 val = (ucontrol->value.integer.value[0] & mask);
3207 if (snd_soc_volsw_is_stereo(mc)) {
3208 rval = (ucontrol->value.integer.value[1] & mask);
3214 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3216 /* This assumes field width < (bits in unsigned int / 2) */
3217 if (width > sizeof(unsigned int) * 8 / 2)
3219 "ASoC: control %s field width limit exceeded\n",
3221 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3223 if (reg != SND_SOC_NOPM) {
3225 rval = rval << mc->rshift;
3227 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3229 if (snd_soc_volsw_is_stereo(mc))
3230 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3235 if (change || reg_change) {
3237 if (snd_soc_volsw_is_stereo(mc)) {
3238 update.has_second_set = true;
3239 update.reg2 = mc->rreg;
3240 update.mask2 = mask << mc->rshift;
3243 update.kcontrol = kcontrol;
3245 update.mask = mask << shift;
3247 card->update = &update;
3249 change |= reg_change;
3251 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3254 card->update = NULL;
3257 mutex_unlock(&card->dapm_mutex);
3260 soc_dpcm_runtime_update(card);
3264 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3267 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3268 * @kcontrol: mixer control
3269 * @ucontrol: control element information
3271 * Callback to get the value of a dapm enumerated double mixer control.
3273 * Returns 0 for success.
3275 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3276 struct snd_ctl_elem_value *ucontrol)
3278 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3279 struct snd_soc_card *card = dapm->card;
3280 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3281 unsigned int reg_val, val;
3283 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3284 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3285 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3287 mutex_unlock(&card->dapm_mutex);
3291 reg_val = dapm_kcontrol_get_value(kcontrol);
3293 mutex_unlock(&card->dapm_mutex);
3295 val = (reg_val >> e->shift_l) & e->mask;
3296 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3297 if (e->shift_l != e->shift_r) {
3298 val = (reg_val >> e->shift_r) & e->mask;
3299 val = snd_soc_enum_val_to_item(e, val);
3300 ucontrol->value.enumerated.item[1] = val;
3305 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3308 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3309 * @kcontrol: mixer control
3310 * @ucontrol: control element information
3312 * Callback to set the value of a dapm enumerated double mixer control.
3314 * Returns 0 for success.
3316 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3317 struct snd_ctl_elem_value *ucontrol)
3319 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3320 struct snd_soc_card *card = dapm->card;
3321 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3322 unsigned int *item = ucontrol->value.enumerated.item;
3323 unsigned int val, change, reg_change = 0;
3325 struct snd_soc_dapm_update update = {};
3328 if (item[0] >= e->items)
3331 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3332 mask = e->mask << e->shift_l;
3333 if (e->shift_l != e->shift_r) {
3334 if (item[1] > e->items)
3336 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3337 mask |= e->mask << e->shift_r;
3340 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3342 change = dapm_kcontrol_set_value(kcontrol, val);
3344 if (e->reg != SND_SOC_NOPM)
3345 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3347 if (change || reg_change) {
3349 update.kcontrol = kcontrol;
3350 update.reg = e->reg;
3353 card->update = &update;
3355 change |= reg_change;
3357 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3359 card->update = NULL;
3362 mutex_unlock(&card->dapm_mutex);
3365 soc_dpcm_runtime_update(card);
3369 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3372 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3374 * @kcontrol: mixer control
3375 * @uinfo: control element information
3377 * Callback to provide information about a pin switch control.
3379 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3380 struct snd_ctl_elem_info *uinfo)
3382 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3384 uinfo->value.integer.min = 0;
3385 uinfo->value.integer.max = 1;
3389 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3392 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3394 * @kcontrol: mixer control
3397 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3398 struct snd_ctl_elem_value *ucontrol)
3400 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3401 const char *pin = (const char *)kcontrol->private_value;
3403 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3405 ucontrol->value.integer.value[0] =
3406 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3408 mutex_unlock(&card->dapm_mutex);
3412 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3415 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3417 * @kcontrol: mixer control
3420 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3421 struct snd_ctl_elem_value *ucontrol)
3423 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3424 const char *pin = (const char *)kcontrol->private_value;
3426 if (ucontrol->value.integer.value[0])
3427 snd_soc_dapm_enable_pin(&card->dapm, pin);
3429 snd_soc_dapm_disable_pin(&card->dapm, pin);
3431 snd_soc_dapm_sync(&card->dapm);
3434 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3436 struct snd_soc_dapm_widget *
3437 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3438 const struct snd_soc_dapm_widget *widget)
3440 struct snd_soc_dapm_widget *w;
3442 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3443 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3444 /* Do not nag about probe deferrals */
3446 int ret = PTR_ERR(w);
3448 if (ret != -EPROBE_DEFER)
3450 "ASoC: Failed to create DAPM control %s (%d)\n",
3456 "ASoC: Failed to create DAPM control %s\n",
3460 mutex_unlock(&dapm->card->dapm_mutex);
3463 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3465 struct snd_soc_dapm_widget *
3466 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3467 const struct snd_soc_dapm_widget *widget)
3469 enum snd_soc_dapm_direction dir;
3470 struct snd_soc_dapm_widget *w;
3474 if ((w = dapm_cnew_widget(widget)) == NULL)
3478 case snd_soc_dapm_regulator_supply:
3479 w->regulator = devm_regulator_get(dapm->dev, w->name);
3480 if (IS_ERR(w->regulator)) {
3481 ret = PTR_ERR(w->regulator);
3482 if (ret == -EPROBE_DEFER)
3483 return ERR_PTR(ret);
3484 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3489 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3490 ret = regulator_allow_bypass(w->regulator, true);
3492 dev_warn(w->dapm->dev,
3493 "ASoC: Failed to bypass %s: %d\n",
3497 case snd_soc_dapm_pinctrl:
3498 w->pinctrl = devm_pinctrl_get(dapm->dev);
3499 if (IS_ERR_OR_NULL(w->pinctrl)) {
3500 ret = PTR_ERR(w->pinctrl);
3501 if (ret == -EPROBE_DEFER)
3502 return ERR_PTR(ret);
3503 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3508 case snd_soc_dapm_clock_supply:
3509 #ifdef CONFIG_CLKDEV_LOOKUP
3510 w->clk = devm_clk_get(dapm->dev, w->name);
3511 if (IS_ERR(w->clk)) {
3512 ret = PTR_ERR(w->clk);
3513 if (ret == -EPROBE_DEFER)
3514 return ERR_PTR(ret);
3515 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3527 prefix = soc_dapm_prefix(dapm);
3529 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3531 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3532 if (w->name == NULL) {
3538 case snd_soc_dapm_mic:
3539 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3540 w->power_check = dapm_generic_check_power;
3542 case snd_soc_dapm_input:
3543 if (!dapm->card->fully_routed)
3544 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3545 w->power_check = dapm_generic_check_power;
3547 case snd_soc_dapm_spk:
3548 case snd_soc_dapm_hp:
3549 w->is_ep = SND_SOC_DAPM_EP_SINK;
3550 w->power_check = dapm_generic_check_power;
3552 case snd_soc_dapm_output:
3553 if (!dapm->card->fully_routed)
3554 w->is_ep = SND_SOC_DAPM_EP_SINK;
3555 w->power_check = dapm_generic_check_power;
3557 case snd_soc_dapm_vmid:
3558 case snd_soc_dapm_siggen:
3559 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3560 w->power_check = dapm_always_on_check_power;
3562 case snd_soc_dapm_sink:
3563 w->is_ep = SND_SOC_DAPM_EP_SINK;
3564 w->power_check = dapm_always_on_check_power;
3567 case snd_soc_dapm_mux:
3568 case snd_soc_dapm_demux:
3569 case snd_soc_dapm_switch:
3570 case snd_soc_dapm_mixer:
3571 case snd_soc_dapm_mixer_named_ctl:
3572 case snd_soc_dapm_adc:
3573 case snd_soc_dapm_aif_out:
3574 case snd_soc_dapm_dac:
3575 case snd_soc_dapm_aif_in:
3576 case snd_soc_dapm_pga:
3577 case snd_soc_dapm_out_drv:
3578 case snd_soc_dapm_micbias:
3579 case snd_soc_dapm_line:
3580 case snd_soc_dapm_dai_link:
3581 case snd_soc_dapm_dai_out:
3582 case snd_soc_dapm_dai_in:
3583 w->power_check = dapm_generic_check_power;
3585 case snd_soc_dapm_supply:
3586 case snd_soc_dapm_regulator_supply:
3587 case snd_soc_dapm_pinctrl:
3588 case snd_soc_dapm_clock_supply:
3589 case snd_soc_dapm_kcontrol:
3591 w->power_check = dapm_supply_check_power;
3594 w->power_check = dapm_always_on_check_power;
3599 INIT_LIST_HEAD(&w->list);
3600 INIT_LIST_HEAD(&w->dirty);
3601 list_add_tail(&w->list, &dapm->card->widgets);
3603 snd_soc_dapm_for_each_direction(dir) {
3604 INIT_LIST_HEAD(&w->edges[dir]);
3605 w->endpoints[dir] = -1;
3608 /* machine layer sets up unconnected pins and insertions */
3614 * snd_soc_dapm_new_controls - create new dapm controls
3615 * @dapm: DAPM context
3616 * @widget: widget array
3617 * @num: number of widgets
3619 * Creates new DAPM controls based upon the templates.
3621 * Returns 0 for success else error.
3623 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3624 const struct snd_soc_dapm_widget *widget,
3627 struct snd_soc_dapm_widget *w;
3631 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3632 for (i = 0; i < num; i++) {
3633 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3636 /* Do not nag about probe deferrals */
3637 if (ret == -EPROBE_DEFER)
3640 "ASoC: Failed to create DAPM control %s (%d)\n",
3646 "ASoC: Failed to create DAPM control %s\n",
3653 mutex_unlock(&dapm->card->dapm_mutex);
3656 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3658 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3659 struct snd_kcontrol *kcontrol, int event)
3661 struct snd_soc_dapm_path *source_p, *sink_p;
3662 struct snd_soc_dai *source, *sink;
3663 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
3664 struct snd_pcm_substream substream;
3665 struct snd_pcm_hw_params *params = NULL;
3666 struct snd_pcm_runtime *runtime = NULL;
3670 if (WARN_ON(!config) ||
3671 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3672 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3675 /* We only support a single source and sink, pick the first */
3676 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3677 struct snd_soc_dapm_path,
3678 list_node[SND_SOC_DAPM_DIR_OUT]);
3679 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3680 struct snd_soc_dapm_path,
3681 list_node[SND_SOC_DAPM_DIR_IN]);
3683 source = source_p->source->priv;
3684 sink = sink_p->sink->priv;
3686 /* Be a little careful as we don't want to overflow the mask array */
3687 if (config->formats) {
3688 fmt = ffs(config->formats) - 1;
3690 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3695 /* Currently very limited parameter selection */
3696 params = kzalloc(sizeof(*params), GFP_KERNEL);
3701 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3703 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3705 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3708 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3709 = config->channels_min;
3710 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3711 = config->channels_max;
3713 memset(&substream, 0, sizeof(substream));
3715 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3716 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3721 substream.runtime = runtime;
3724 case SND_SOC_DAPM_PRE_PMU:
3725 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3726 if (source->driver->ops->startup) {
3727 ret = source->driver->ops->startup(&substream, source);
3729 dev_err(source->dev,
3730 "ASoC: startup() failed: %d\n", ret);
3735 ret = soc_dai_hw_params(&substream, params, source);
3739 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3740 if (sink->driver->ops->startup) {
3741 ret = sink->driver->ops->startup(&substream, sink);
3744 "ASoC: startup() failed: %d\n", ret);
3749 ret = soc_dai_hw_params(&substream, params, sink);
3754 case SND_SOC_DAPM_POST_PMU:
3755 ret = snd_soc_dai_digital_mute(sink, 0,
3756 SNDRV_PCM_STREAM_PLAYBACK);
3757 if (ret != 0 && ret != -ENOTSUPP)
3758 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3762 case SND_SOC_DAPM_PRE_PMD:
3763 ret = snd_soc_dai_digital_mute(sink, 1,
3764 SNDRV_PCM_STREAM_PLAYBACK);
3765 if (ret != 0 && ret != -ENOTSUPP)
3766 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3770 if (source->driver->ops->shutdown) {
3771 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3772 source->driver->ops->shutdown(&substream, source);
3776 if (sink->driver->ops->shutdown) {
3777 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3778 sink->driver->ops->shutdown(&substream, sink);
3783 WARN(1, "Unknown event %d\n", event);
3793 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3794 struct snd_ctl_elem_value *ucontrol)
3796 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3798 ucontrol->value.enumerated.item[0] = w->params_select;
3803 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3804 struct snd_ctl_elem_value *ucontrol)
3806 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3808 /* Can't change the config when widget is already powered */
3812 if (ucontrol->value.enumerated.item[0] == w->params_select)
3815 if (ucontrol->value.enumerated.item[0] >= w->num_params)
3818 w->params_select = ucontrol->value.enumerated.item[0];
3824 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3825 unsigned long *private_value,
3827 const char **w_param_text)
3831 devm_kfree(card->dev, (void *)*private_value);
3832 for (count = 0 ; count < num_params; count++)
3833 devm_kfree(card->dev, (void *)w_param_text[count]);
3834 devm_kfree(card->dev, w_param_text);
3837 static struct snd_kcontrol_new *
3838 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3840 const struct snd_soc_pcm_stream *params,
3841 int num_params, const char **w_param_text,
3842 unsigned long *private_value)
3844 struct soc_enum w_param_enum[] = {
3845 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3847 struct snd_kcontrol_new kcontrol_dai_link[] = {
3848 SOC_ENUM_EXT(NULL, w_param_enum[0],
3849 snd_soc_dapm_dai_link_get,
3850 snd_soc_dapm_dai_link_put),
3852 struct snd_kcontrol_new *kcontrol_news;
3853 const struct snd_soc_pcm_stream *config = params;
3856 for (count = 0 ; count < num_params; count++) {
3857 if (!config->stream_name) {
3858 dev_warn(card->dapm.dev,
3859 "ASoC: anonymous config %d for dai link %s\n",
3861 w_param_text[count] =
3862 devm_kasprintf(card->dev, GFP_KERNEL,
3863 "Anonymous Configuration %d",
3866 w_param_text[count] = devm_kmemdup(card->dev,
3867 config->stream_name,
3868 strlen(config->stream_name) + 1,
3871 if (!w_param_text[count])
3872 goto outfree_w_param;
3876 w_param_enum[0].items = num_params;
3877 w_param_enum[0].texts = w_param_text;
3880 (unsigned long) devm_kmemdup(card->dev,
3881 (void *)(kcontrol_dai_link[0].private_value),
3882 sizeof(struct soc_enum), GFP_KERNEL);
3883 if (!*private_value) {
3884 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3886 goto outfree_w_param;
3888 kcontrol_dai_link[0].private_value = *private_value;
3889 /* duplicate kcontrol_dai_link on heap so that memory persists */
3890 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3891 sizeof(struct snd_kcontrol_new),
3893 if (!kcontrol_news) {
3894 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3896 goto outfree_w_param;
3898 return kcontrol_news;
3901 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
3905 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3906 const struct snd_soc_pcm_stream *params,
3907 unsigned int num_params,
3908 struct snd_soc_dapm_widget *source,
3909 struct snd_soc_dapm_widget *sink)
3911 struct snd_soc_dapm_widget template;
3912 struct snd_soc_dapm_widget *w;
3913 const char **w_param_text;
3914 unsigned long private_value;
3918 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3919 source->name, sink->name);
3923 memset(&template, 0, sizeof(template));
3924 template.reg = SND_SOC_NOPM;
3925 template.id = snd_soc_dapm_dai_link;
3926 template.name = link_name;
3927 template.event = snd_soc_dai_link_event;
3928 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3929 SND_SOC_DAPM_PRE_PMD;
3930 template.kcontrol_news = NULL;
3932 /* allocate memory for control, only in case of multiple configs */
3933 if (num_params > 1) {
3934 w_param_text = devm_kcalloc(card->dev, num_params,
3935 sizeof(char *), GFP_KERNEL);
3936 if (!w_param_text) {
3941 template.num_kcontrols = 1;
3942 template.kcontrol_news =
3943 snd_soc_dapm_alloc_kcontrol(card,
3944 link_name, params, num_params,
3945 w_param_text, &private_value);
3946 if (!template.kcontrol_news) {
3951 w_param_text = NULL;
3953 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3955 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
3958 /* Do not nag about probe deferrals */
3959 if (ret != -EPROBE_DEFER)
3961 "ASoC: Failed to create %s widget (%d)\n",
3963 goto outfree_kcontrol_news;
3966 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3969 goto outfree_kcontrol_news;
3973 w->num_params = num_params;
3975 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3978 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3981 devm_kfree(card->dev, w);
3982 outfree_kcontrol_news:
3983 devm_kfree(card->dev, (void *)template.kcontrol_news);
3984 snd_soc_dapm_free_kcontrol(card, &private_value, num_params, w_param_text);
3986 devm_kfree(card->dev, link_name);
3990 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3991 struct snd_soc_dai *dai)
3993 struct snd_soc_dapm_widget template;
3994 struct snd_soc_dapm_widget *w;
3996 WARN_ON(dapm->dev != dai->dev);
3998 memset(&template, 0, sizeof(template));
3999 template.reg = SND_SOC_NOPM;
4001 if (dai->driver->playback.stream_name) {
4002 template.id = snd_soc_dapm_dai_in;
4003 template.name = dai->driver->playback.stream_name;
4004 template.sname = dai->driver->playback.stream_name;
4006 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4009 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4011 int ret = PTR_ERR(w);
4013 /* Do not nag about probe deferrals */
4014 if (ret != -EPROBE_DEFER)
4016 "ASoC: Failed to create %s widget (%d)\n",
4017 dai->driver->playback.stream_name, ret);
4021 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4022 dai->driver->playback.stream_name);
4027 dai->playback_widget = w;
4030 if (dai->driver->capture.stream_name) {
4031 template.id = snd_soc_dapm_dai_out;
4032 template.name = dai->driver->capture.stream_name;
4033 template.sname = dai->driver->capture.stream_name;
4035 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4038 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4040 int ret = PTR_ERR(w);
4042 /* Do not nag about probe deferrals */
4043 if (ret != -EPROBE_DEFER)
4045 "ASoC: Failed to create %s widget (%d)\n",
4046 dai->driver->playback.stream_name, ret);
4050 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4051 dai->driver->capture.stream_name);
4056 dai->capture_widget = w;
4062 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4064 struct snd_soc_dapm_widget *dai_w, *w;
4065 struct snd_soc_dapm_widget *src, *sink;
4066 struct snd_soc_dai *dai;
4068 /* For each DAI widget... */
4069 list_for_each_entry(dai_w, &card->widgets, list) {
4070 switch (dai_w->id) {
4071 case snd_soc_dapm_dai_in:
4072 case snd_soc_dapm_dai_out:
4080 /* ...find all widgets with the same stream and link them */
4081 list_for_each_entry(w, &card->widgets, list) {
4082 if (w->dapm != dai_w->dapm)
4086 case snd_soc_dapm_dai_in:
4087 case snd_soc_dapm_dai_out:
4093 if (!w->sname || !strstr(w->sname, dai_w->sname))
4096 if (dai_w->id == snd_soc_dapm_dai_in) {
4103 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4104 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4111 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4112 struct snd_soc_pcm_runtime *rtd)
4114 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4115 struct snd_soc_dapm_widget *sink, *source;
4118 for (i = 0; i < rtd->num_codecs; i++) {
4119 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
4121 /* connect BE DAI playback if widgets are valid */
4122 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
4123 source = cpu_dai->playback_widget;
4124 sink = codec_dai->playback_widget;
4125 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4126 cpu_dai->component->name, source->name,
4127 codec_dai->component->name, sink->name);
4129 snd_soc_dapm_add_path(&card->dapm, source, sink,
4133 /* connect BE DAI capture if widgets are valid */
4134 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
4135 source = codec_dai->capture_widget;
4136 sink = cpu_dai->capture_widget;
4137 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4138 codec_dai->component->name, source->name,
4139 cpu_dai->component->name, sink->name);
4141 snd_soc_dapm_add_path(&card->dapm, source, sink,
4147 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4150 struct snd_soc_dapm_widget *w;
4153 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4154 w = dai->playback_widget;
4156 w = dai->capture_widget;
4159 dapm_mark_dirty(w, "stream event");
4161 if (w->id == snd_soc_dapm_dai_in) {
4162 ep = SND_SOC_DAPM_EP_SOURCE;
4163 dapm_widget_invalidate_input_paths(w);
4165 ep = SND_SOC_DAPM_EP_SINK;
4166 dapm_widget_invalidate_output_paths(w);
4170 case SND_SOC_DAPM_STREAM_START:
4174 case SND_SOC_DAPM_STREAM_STOP:
4178 case SND_SOC_DAPM_STREAM_SUSPEND:
4179 case SND_SOC_DAPM_STREAM_RESUME:
4180 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4181 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4187 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4189 struct snd_soc_pcm_runtime *rtd;
4191 /* for each BE DAI link... */
4192 list_for_each_entry(rtd, &card->rtd_list, list) {
4194 * dynamic FE links have no fixed DAI mapping.
4195 * CODEC<->CODEC links have no direct connection.
4197 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4200 dapm_connect_dai_link_widgets(card, rtd);
4204 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4209 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4210 for (i = 0; i < rtd->num_codecs; i++)
4211 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
4213 dapm_power_widgets(rtd->card, event);
4217 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4218 * @rtd: PCM runtime data
4219 * @stream: stream name
4220 * @event: stream event
4222 * Sends a stream event to the dapm core. The core then makes any
4223 * necessary widget power changes.
4225 * Returns 0 for success else error.
4227 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4230 struct snd_soc_card *card = rtd->card;
4232 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4233 soc_dapm_stream_event(rtd, stream, event);
4234 mutex_unlock(&card->dapm_mutex);
4238 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4239 * @dapm: DAPM context
4242 * Enables input/output pin and its parents or children widgets iff there is
4243 * a valid audio route and active audio stream.
4245 * Requires external locking.
4247 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4248 * do any widget power switching.
4250 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4253 return snd_soc_dapm_set_pin(dapm, pin, 1);
4255 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4258 * snd_soc_dapm_enable_pin - enable pin.
4259 * @dapm: DAPM context
4262 * Enables input/output pin and its parents or children widgets iff there is
4263 * a valid audio route and active audio stream.
4265 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4266 * do any widget power switching.
4268 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4272 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4274 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4276 mutex_unlock(&dapm->card->dapm_mutex);
4280 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4283 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4284 * @dapm: DAPM context
4287 * Enables input/output pin regardless of any other state. This is
4288 * intended for use with microphone bias supplies used in microphone
4291 * Requires external locking.
4293 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4294 * do any widget power switching.
4296 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4299 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4302 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4306 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4307 if (!w->connected) {
4309 * w->force does not affect the number of input or output paths,
4310 * so we only have to recheck if w->connected is changed
4312 dapm_widget_invalidate_input_paths(w);
4313 dapm_widget_invalidate_output_paths(w);
4317 dapm_mark_dirty(w, "force enable");
4321 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4324 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4325 * @dapm: DAPM context
4328 * Enables input/output pin regardless of any other state. This is
4329 * intended for use with microphone bias supplies used in microphone
4332 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4333 * do any widget power switching.
4335 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4340 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4342 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4344 mutex_unlock(&dapm->card->dapm_mutex);
4348 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4351 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4352 * @dapm: DAPM context
4355 * Disables input/output pin and its parents or children widgets.
4357 * Requires external locking.
4359 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4360 * do any widget power switching.
4362 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4365 return snd_soc_dapm_set_pin(dapm, pin, 0);
4367 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4370 * snd_soc_dapm_disable_pin - disable pin.
4371 * @dapm: DAPM context
4374 * Disables input/output pin and its parents or children widgets.
4376 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4377 * do any widget power switching.
4379 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4384 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4386 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4388 mutex_unlock(&dapm->card->dapm_mutex);
4392 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4395 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4396 * @dapm: DAPM context
4399 * Marks the specified pin as being not connected, disabling it along
4400 * any parent or child widgets. At present this is identical to
4401 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4402 * additional things such as disabling controls which only affect
4403 * paths through the pin.
4405 * Requires external locking.
4407 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4408 * do any widget power switching.
4410 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4413 return snd_soc_dapm_set_pin(dapm, pin, 0);
4415 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4418 * snd_soc_dapm_nc_pin - permanently disable pin.
4419 * @dapm: DAPM context
4422 * Marks the specified pin as being not connected, disabling it along
4423 * any parent or child widgets. At present this is identical to
4424 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4425 * additional things such as disabling controls which only affect
4426 * paths through the pin.
4428 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4429 * do any widget power switching.
4431 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4435 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4437 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4439 mutex_unlock(&dapm->card->dapm_mutex);
4443 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4446 * snd_soc_dapm_get_pin_status - get audio pin status
4447 * @dapm: DAPM context
4448 * @pin: audio signal pin endpoint (or start point)
4450 * Get audio pin status - connected or disconnected.
4452 * Returns 1 for connected otherwise 0.
4454 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4457 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4460 return w->connected;
4464 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4467 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4468 * @dapm: DAPM context
4469 * @pin: audio signal pin endpoint (or start point)
4471 * Mark the given endpoint or pin as ignoring suspend. When the
4472 * system is disabled a path between two endpoints flagged as ignoring
4473 * suspend will not be disabled. The path must already be enabled via
4474 * normal means at suspend time, it will not be turned on if it was not
4477 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4480 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4483 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4487 w->ignore_suspend = 1;
4491 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4494 * snd_soc_dapm_free - free dapm resources
4495 * @dapm: DAPM context
4497 * Free all dapm widgets and resources.
4499 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4501 dapm_debugfs_cleanup(dapm);
4502 dapm_free_widgets(dapm);
4503 list_del(&dapm->list);
4505 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4507 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4509 struct snd_soc_card *card = dapm->card;
4510 struct snd_soc_dapm_widget *w;
4511 LIST_HEAD(down_list);
4514 mutex_lock(&card->dapm_mutex);
4516 list_for_each_entry(w, &dapm->card->widgets, list) {
4517 if (w->dapm != dapm)
4520 dapm_seq_insert(w, &down_list, false);
4526 /* If there were no widgets to power down we're already in
4530 if (dapm->bias_level == SND_SOC_BIAS_ON)
4531 snd_soc_dapm_set_bias_level(dapm,
4532 SND_SOC_BIAS_PREPARE);
4533 dapm_seq_run(card, &down_list, 0, false);
4534 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4535 snd_soc_dapm_set_bias_level(dapm,
4536 SND_SOC_BIAS_STANDBY);
4539 mutex_unlock(&card->dapm_mutex);
4543 * snd_soc_dapm_shutdown - callback for system shutdown
4545 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4547 struct snd_soc_dapm_context *dapm;
4549 list_for_each_entry(dapm, &card->dapm_list, list) {
4550 if (dapm != &card->dapm) {
4551 soc_dapm_shutdown_dapm(dapm);
4552 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4553 snd_soc_dapm_set_bias_level(dapm,
4558 soc_dapm_shutdown_dapm(&card->dapm);
4559 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4560 snd_soc_dapm_set_bias_level(&card->dapm,
4564 /* Module information */
4566 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4567 MODULE_LICENSE("GPL");