2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
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 i = match_string(e->texts, e->items, control_name);
731 path->name = e->texts[i];
732 path->connect = (i == item);
737 /* set up initial codec paths */
738 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
741 struct soc_mixer_control *mc = (struct soc_mixer_control *)
742 p->sink->kcontrol_news[i].private_value;
743 unsigned int reg = mc->reg;
744 unsigned int shift = mc->shift;
745 unsigned int max = mc->max;
746 unsigned int mask = (1 << fls(max)) - 1;
747 unsigned int invert = mc->invert;
750 if (reg != SND_SOC_NOPM) {
751 soc_dapm_read(p->sink->dapm, reg, &val);
753 * The nth_path argument allows this function to know
754 * which path of a kcontrol it is setting the initial
755 * status for. Ideally this would support any number
756 * of paths and channels. But since kcontrols only come
757 * in mono and stereo variants, we are limited to 2
760 * The following code assumes for stereo controls the
761 * first path is the left channel, and all remaining
762 * paths are the right channel.
764 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
766 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
767 val = (val >> mc->rshift) & mask;
769 val = (val >> shift) & mask;
779 /* connect mixer widget to its interconnecting audio paths */
780 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
781 struct snd_soc_dapm_path *path, const char *control_name)
785 /* search for mixer kcontrol */
786 for (i = 0; i < path->sink->num_kcontrols; i++) {
787 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
788 path->name = path->sink->kcontrol_news[i].name;
789 dapm_set_mixer_path_status(path, i, nth_path++);
796 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
797 struct snd_soc_dapm_widget *kcontrolw,
798 const struct snd_kcontrol_new *kcontrol_new,
799 struct snd_kcontrol **kcontrol)
801 struct snd_soc_dapm_widget *w;
806 list_for_each_entry(w, &dapm->card->widgets, list) {
807 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
809 for (i = 0; i < w->num_kcontrols; i++) {
810 if (&w->kcontrol_news[i] == kcontrol_new) {
812 *kcontrol = w->kcontrols[i];
822 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
823 * create it. Either way, add the widget into the control's widget list
825 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
828 struct snd_soc_dapm_context *dapm = w->dapm;
829 struct snd_card *card = dapm->card->snd_card;
833 struct snd_kcontrol *kcontrol;
834 bool wname_in_long_name, kcname_in_long_name;
835 char *long_name = NULL;
839 prefix = soc_dapm_prefix(dapm);
841 prefix_len = strlen(prefix) + 1;
845 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
850 wname_in_long_name = false;
851 kcname_in_long_name = true;
854 case snd_soc_dapm_switch:
855 case snd_soc_dapm_mixer:
856 case snd_soc_dapm_pga:
857 case snd_soc_dapm_out_drv:
858 wname_in_long_name = true;
859 kcname_in_long_name = true;
861 case snd_soc_dapm_mixer_named_ctl:
862 wname_in_long_name = false;
863 kcname_in_long_name = true;
865 case snd_soc_dapm_demux:
866 case snd_soc_dapm_mux:
867 wname_in_long_name = true;
868 kcname_in_long_name = false;
875 if (wname_in_long_name && kcname_in_long_name) {
877 * The control will get a prefix from the control
878 * creation process but we're also using the same
879 * prefix for widgets so cut the prefix off the
880 * front of the widget name.
882 long_name = kasprintf(GFP_KERNEL, "%s %s",
883 w->name + prefix_len,
884 w->kcontrol_news[kci].name);
885 if (long_name == NULL)
889 } else if (wname_in_long_name) {
891 name = w->name + prefix_len;
894 name = w->kcontrol_news[kci].name;
897 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
904 kcontrol->private_free = dapm_kcontrol_free;
906 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
908 snd_ctl_free_one(kcontrol);
912 ret = snd_ctl_add(card, kcontrol);
915 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
921 ret = dapm_kcontrol_add_widget(kcontrol, w);
923 w->kcontrols[kci] = kcontrol;
931 /* create new dapm mixer control */
932 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
935 struct snd_soc_dapm_path *path;
936 struct dapm_kcontrol_data *data;
939 for (i = 0; i < w->num_kcontrols; i++) {
941 snd_soc_dapm_widget_for_each_source_path(w, path) {
942 /* mixer/mux paths name must match control name */
943 if (path->name != (char *)w->kcontrol_news[i].name)
946 if (!w->kcontrols[i]) {
947 ret = dapm_create_or_share_kcontrol(w, i);
952 dapm_kcontrol_add_path(w->kcontrols[i], path);
954 data = snd_kcontrol_chip(w->kcontrols[i]);
956 snd_soc_dapm_add_path(data->widget->dapm,
966 /* create new dapm mux control */
967 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
969 struct snd_soc_dapm_context *dapm = w->dapm;
970 enum snd_soc_dapm_direction dir;
971 struct snd_soc_dapm_path *path;
976 case snd_soc_dapm_mux:
977 dir = SND_SOC_DAPM_DIR_OUT;
980 case snd_soc_dapm_demux:
981 dir = SND_SOC_DAPM_DIR_IN;
988 if (w->num_kcontrols != 1) {
990 "ASoC: %s %s has incorrect number of controls\n", type,
995 if (list_empty(&w->edges[dir])) {
996 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1000 ret = dapm_create_or_share_kcontrol(w, 0);
1004 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1006 dapm_kcontrol_add_path(w->kcontrols[0], path);
1012 /* create new dapm volume control */
1013 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1017 for (i = 0; i < w->num_kcontrols; i++) {
1018 ret = dapm_create_or_share_kcontrol(w, i);
1026 /* create new dapm dai link control */
1027 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1030 struct snd_kcontrol *kcontrol;
1031 struct snd_soc_dapm_context *dapm = w->dapm;
1032 struct snd_card *card = dapm->card->snd_card;
1034 /* create control for links with > 1 config */
1035 if (w->num_params <= 1)
1039 for (i = 0; i < w->num_kcontrols; i++) {
1040 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1042 ret = snd_ctl_add(card, kcontrol);
1045 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1046 w->name, w->kcontrol_news[i].name, ret);
1049 kcontrol->private_data = w;
1050 w->kcontrols[i] = kcontrol;
1056 /* We implement power down on suspend by checking the power state of
1057 * the ALSA card - when we are suspending the ALSA state for the card
1060 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1062 int level = snd_power_get_state(widget->dapm->card->snd_card);
1065 case SNDRV_CTL_POWER_D3hot:
1066 case SNDRV_CTL_POWER_D3cold:
1067 if (widget->ignore_suspend)
1068 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1070 return widget->ignore_suspend;
1076 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1077 struct list_head *widgets)
1079 struct snd_soc_dapm_widget *w;
1080 struct list_head *it;
1081 unsigned int size = 0;
1084 list_for_each(it, widgets)
1087 *list = kzalloc(sizeof(**list) + size * sizeof(*w), GFP_KERNEL);
1091 list_for_each_entry(w, widgets, work_list)
1092 (*list)->widgets[i++] = w;
1094 (*list)->num_widgets = i;
1100 * Common implementation for is_connected_output_ep() and
1101 * is_connected_input_ep(). The function is inlined since the combined size of
1102 * the two specialized functions is only marginally larger then the size of the
1103 * generic function and at the same time the fast path of the specialized
1104 * functions is significantly smaller than the generic function.
1106 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1107 struct list_head *list, enum snd_soc_dapm_direction dir,
1108 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1109 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1110 enum snd_soc_dapm_direction)),
1111 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1112 enum snd_soc_dapm_direction))
1114 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1115 struct snd_soc_dapm_path *path;
1118 if (widget->endpoints[dir] >= 0)
1119 return widget->endpoints[dir];
1121 DAPM_UPDATE_STAT(widget, path_checks);
1123 /* do we need to add this widget to the list ? */
1125 list_add_tail(&widget->work_list, list);
1127 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1128 widget->endpoints[dir] = 1;
1129 return widget->endpoints[dir];
1132 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1133 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1134 return widget->endpoints[dir];
1137 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1138 DAPM_UPDATE_STAT(widget, neighbour_checks);
1140 if (path->weak || path->is_supply)
1146 trace_snd_soc_dapm_path(widget, dir, path);
1148 if (path->connect) {
1150 con += fn(path->node[dir], list, custom_stop_condition);
1155 widget->endpoints[dir] = con;
1161 * Recursively check for a completed path to an active or physically connected
1162 * output widget. Returns number of complete paths.
1164 * Optionally, can be supplied with a function acting as a stopping condition.
1165 * This function takes the dapm widget currently being examined and the walk
1166 * direction as an arguments, it should return true if the walk should be
1167 * stopped and false otherwise.
1169 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1170 struct list_head *list,
1171 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1172 enum snd_soc_dapm_direction))
1174 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1175 is_connected_output_ep, custom_stop_condition);
1179 * Recursively check for a completed path to an active or physically connected
1180 * input widget. Returns number of complete paths.
1182 * Optionally, can be supplied with a function acting as a stopping condition.
1183 * This function takes the dapm widget currently being examined and the walk
1184 * direction as an arguments, it should return true if the walk should be
1185 * stopped and false otherwise.
1187 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1188 struct list_head *list,
1189 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1190 enum snd_soc_dapm_direction))
1192 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1193 is_connected_input_ep, custom_stop_condition);
1197 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1198 * @dai: the soc DAI.
1199 * @stream: stream direction.
1200 * @list: list of active widgets for this stream.
1201 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1202 * walk based on custom logic.
1204 * Queries DAPM graph as to whether a valid audio stream path exists for
1205 * the initial stream specified by name. This takes into account
1206 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1208 * Optionally, can be supplied with a function acting as a stopping condition.
1209 * This function takes the dapm widget currently being examined and the walk
1210 * direction as an arguments, it should return true if the walk should be
1211 * stopped and false otherwise.
1213 * Returns the number of valid paths or negative error.
1215 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1216 struct snd_soc_dapm_widget_list **list,
1217 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1218 enum snd_soc_dapm_direction))
1220 struct snd_soc_card *card = dai->component->card;
1221 struct snd_soc_dapm_widget *w;
1226 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1229 * For is_connected_{output,input}_ep fully discover the graph we need
1230 * to reset the cached number of inputs and outputs.
1232 list_for_each_entry(w, &card->widgets, list) {
1233 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1234 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1237 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1238 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1239 custom_stop_condition);
1241 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1242 custom_stop_condition);
1244 /* Drop starting point */
1245 list_del(widgets.next);
1247 ret = dapm_widget_list_create(list, &widgets);
1251 trace_snd_soc_dapm_connected(paths, stream);
1252 mutex_unlock(&card->dapm_mutex);
1258 * Handler for regulator supply widget.
1260 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1261 struct snd_kcontrol *kcontrol, int event)
1265 soc_dapm_async_complete(w->dapm);
1267 if (SND_SOC_DAPM_EVENT_ON(event)) {
1268 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1269 ret = regulator_allow_bypass(w->regulator, false);
1271 dev_warn(w->dapm->dev,
1272 "ASoC: Failed to unbypass %s: %d\n",
1276 return regulator_enable(w->regulator);
1278 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1279 ret = regulator_allow_bypass(w->regulator, true);
1281 dev_warn(w->dapm->dev,
1282 "ASoC: Failed to bypass %s: %d\n",
1286 return regulator_disable_deferred(w->regulator, w->shift);
1289 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1292 * Handler for pinctrl widget.
1294 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1295 struct snd_kcontrol *kcontrol, int event)
1297 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1298 struct pinctrl *p = w->pinctrl;
1299 struct pinctrl_state *s;
1304 if (SND_SOC_DAPM_EVENT_ON(event))
1305 s = pinctrl_lookup_state(p, priv->active_state);
1307 s = pinctrl_lookup_state(p, priv->sleep_state);
1312 return pinctrl_select_state(p, s);
1314 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1317 * Handler for clock supply widget.
1319 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1320 struct snd_kcontrol *kcontrol, int event)
1325 soc_dapm_async_complete(w->dapm);
1327 #ifdef CONFIG_HAVE_CLK
1328 if (SND_SOC_DAPM_EVENT_ON(event)) {
1329 return clk_prepare_enable(w->clk);
1331 clk_disable_unprepare(w->clk);
1337 EXPORT_SYMBOL_GPL(dapm_clock_event);
1339 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1341 if (w->power_checked)
1342 return w->new_power;
1347 w->new_power = w->power_check(w);
1349 w->power_checked = true;
1351 return w->new_power;
1354 /* Generic check to see if a widget should be powered. */
1355 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1359 DAPM_UPDATE_STAT(w, power_checks);
1361 in = is_connected_input_ep(w, NULL, NULL);
1362 out = is_connected_output_ep(w, NULL, NULL);
1363 return out != 0 && in != 0;
1366 /* Check to see if a power supply is needed */
1367 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1369 struct snd_soc_dapm_path *path;
1371 DAPM_UPDATE_STAT(w, power_checks);
1373 /* Check if one of our outputs is connected */
1374 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1375 DAPM_UPDATE_STAT(w, neighbour_checks);
1380 if (path->connected &&
1381 !path->connected(path->source, path->sink))
1384 if (dapm_widget_power_check(path->sink))
1391 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1393 return w->connected;
1396 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1397 struct snd_soc_dapm_widget *b,
1405 sort = dapm_down_seq;
1407 if (sort[a->id] != sort[b->id])
1408 return sort[a->id] - sort[b->id];
1409 if (a->subseq != b->subseq) {
1411 return a->subseq - b->subseq;
1413 return b->subseq - a->subseq;
1415 if (a->reg != b->reg)
1416 return a->reg - b->reg;
1417 if (a->dapm != b->dapm)
1418 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1423 /* Insert a widget in order into a DAPM power sequence. */
1424 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1425 struct list_head *list,
1428 struct snd_soc_dapm_widget *w;
1430 list_for_each_entry(w, list, power_list)
1431 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1432 list_add_tail(&new_widget->power_list, &w->power_list);
1436 list_add_tail(&new_widget->power_list, list);
1439 static void dapm_seq_check_event(struct snd_soc_card *card,
1440 struct snd_soc_dapm_widget *w, int event)
1442 const char *ev_name;
1446 case SND_SOC_DAPM_PRE_PMU:
1447 ev_name = "PRE_PMU";
1450 case SND_SOC_DAPM_POST_PMU:
1451 ev_name = "POST_PMU";
1454 case SND_SOC_DAPM_PRE_PMD:
1455 ev_name = "PRE_PMD";
1458 case SND_SOC_DAPM_POST_PMD:
1459 ev_name = "POST_PMD";
1462 case SND_SOC_DAPM_WILL_PMU:
1463 ev_name = "WILL_PMU";
1466 case SND_SOC_DAPM_WILL_PMD:
1467 ev_name = "WILL_PMD";
1471 WARN(1, "Unknown event %d\n", event);
1475 if (w->new_power != power)
1478 if (w->event && (w->event_flags & event)) {
1479 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1481 soc_dapm_async_complete(w->dapm);
1482 trace_snd_soc_dapm_widget_event_start(w, event);
1483 ret = w->event(w, NULL, event);
1484 trace_snd_soc_dapm_widget_event_done(w, event);
1486 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1487 ev_name, w->name, ret);
1491 /* Apply the coalesced changes from a DAPM sequence */
1492 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1493 struct list_head *pending)
1495 struct snd_soc_dapm_context *dapm;
1496 struct snd_soc_dapm_widget *w;
1498 unsigned int value = 0;
1499 unsigned int mask = 0;
1501 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1505 list_for_each_entry(w, pending, power_list) {
1506 WARN_ON(reg != w->reg || dapm != w->dapm);
1507 w->power = w->new_power;
1509 mask |= w->mask << w->shift;
1511 value |= w->on_val << w->shift;
1513 value |= w->off_val << w->shift;
1515 pop_dbg(dapm->dev, card->pop_time,
1516 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1517 w->name, reg, value, mask);
1519 /* Check for events */
1520 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1521 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1525 /* Any widget will do, they should all be updating the
1529 pop_dbg(dapm->dev, card->pop_time,
1530 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1531 value, mask, reg, card->pop_time);
1532 pop_wait(card->pop_time);
1533 soc_dapm_update_bits(dapm, reg, mask, value);
1536 list_for_each_entry(w, pending, power_list) {
1537 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1538 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1542 /* Apply a DAPM power sequence.
1544 * We walk over a pre-sorted list of widgets to apply power to. In
1545 * order to minimise the number of writes to the device required
1546 * multiple widgets will be updated in a single write where possible.
1547 * Currently anything that requires more than a single write is not
1550 static void dapm_seq_run(struct snd_soc_card *card,
1551 struct list_head *list, int event, bool power_up)
1553 struct snd_soc_dapm_widget *w, *n;
1554 struct snd_soc_dapm_context *d;
1557 int cur_subseq = -1;
1558 int cur_reg = SND_SOC_NOPM;
1559 struct snd_soc_dapm_context *cur_dapm = NULL;
1566 sort = dapm_down_seq;
1568 list_for_each_entry_safe(w, n, list, power_list) {
1571 /* Do we need to apply any queued changes? */
1572 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1573 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1574 if (!list_empty(&pending))
1575 dapm_seq_run_coalesced(card, &pending);
1577 if (cur_dapm && cur_dapm->seq_notifier) {
1578 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1579 if (sort[i] == cur_sort)
1580 cur_dapm->seq_notifier(cur_dapm,
1585 if (cur_dapm && w->dapm != cur_dapm)
1586 soc_dapm_async_complete(cur_dapm);
1588 INIT_LIST_HEAD(&pending);
1590 cur_subseq = INT_MIN;
1591 cur_reg = SND_SOC_NOPM;
1596 case snd_soc_dapm_pre:
1598 list_for_each_entry_safe_continue(w, n, list,
1601 if (event == SND_SOC_DAPM_STREAM_START)
1603 NULL, SND_SOC_DAPM_PRE_PMU);
1604 else if (event == SND_SOC_DAPM_STREAM_STOP)
1606 NULL, SND_SOC_DAPM_PRE_PMD);
1609 case snd_soc_dapm_post:
1611 list_for_each_entry_safe_continue(w, n, list,
1614 if (event == SND_SOC_DAPM_STREAM_START)
1616 NULL, SND_SOC_DAPM_POST_PMU);
1617 else if (event == SND_SOC_DAPM_STREAM_STOP)
1619 NULL, SND_SOC_DAPM_POST_PMD);
1623 /* Queue it up for application */
1624 cur_sort = sort[w->id];
1625 cur_subseq = w->subseq;
1628 list_move(&w->power_list, &pending);
1633 dev_err(w->dapm->dev,
1634 "ASoC: Failed to apply widget power: %d\n", ret);
1637 if (!list_empty(&pending))
1638 dapm_seq_run_coalesced(card, &pending);
1640 if (cur_dapm && cur_dapm->seq_notifier) {
1641 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1642 if (sort[i] == cur_sort)
1643 cur_dapm->seq_notifier(cur_dapm,
1647 list_for_each_entry(d, &card->dapm_list, list) {
1648 soc_dapm_async_complete(d);
1652 static void dapm_widget_update(struct snd_soc_card *card)
1654 struct snd_soc_dapm_update *update = card->update;
1655 struct snd_soc_dapm_widget_list *wlist;
1656 struct snd_soc_dapm_widget *w = NULL;
1660 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1663 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1665 for (wi = 0; wi < wlist->num_widgets; wi++) {
1666 w = wlist->widgets[wi];
1668 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1669 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1671 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1679 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1682 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1685 if (update->has_second_set) {
1686 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1687 update->mask2, update->val2);
1689 dev_err(w->dapm->dev,
1690 "ASoC: %s DAPM update failed: %d\n",
1694 for (wi = 0; wi < wlist->num_widgets; wi++) {
1695 w = wlist->widgets[wi];
1697 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1698 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1700 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1706 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1707 * they're changing state.
1709 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1711 struct snd_soc_dapm_context *d = data;
1714 /* If we're off and we're not supposed to go into STANDBY */
1715 if (d->bias_level == SND_SOC_BIAS_OFF &&
1716 d->target_bias_level != SND_SOC_BIAS_OFF) {
1718 pm_runtime_get_sync(d->dev);
1720 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1723 "ASoC: Failed to turn on bias: %d\n", ret);
1726 /* Prepare for a transition to ON or away from ON */
1727 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1728 d->bias_level != SND_SOC_BIAS_ON) ||
1729 (d->target_bias_level != SND_SOC_BIAS_ON &&
1730 d->bias_level == SND_SOC_BIAS_ON)) {
1731 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1734 "ASoC: Failed to prepare bias: %d\n", ret);
1738 /* Async callback run prior to DAPM sequences - brings to their final
1741 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1743 struct snd_soc_dapm_context *d = data;
1746 /* If we just powered the last thing off drop to standby bias */
1747 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1748 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1749 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1750 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1752 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1756 /* If we're in standby and can support bias off then do that */
1757 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1758 d->target_bias_level == SND_SOC_BIAS_OFF) {
1759 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1761 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1765 pm_runtime_put(d->dev);
1768 /* If we just powered up then move to active bias */
1769 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1770 d->target_bias_level == SND_SOC_BIAS_ON) {
1771 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1773 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1778 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1779 bool power, bool connect)
1781 /* If a connection is being made or broken then that update
1782 * will have marked the peer dirty, otherwise the widgets are
1783 * not connected and this update has no impact. */
1787 /* If the peer is already in the state we're moving to then we
1788 * won't have an impact on it. */
1789 if (power != peer->power)
1790 dapm_mark_dirty(peer, "peer state change");
1793 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1794 struct list_head *up_list,
1795 struct list_head *down_list)
1797 struct snd_soc_dapm_path *path;
1799 if (w->power == power)
1802 trace_snd_soc_dapm_widget_power(w, power);
1804 /* If we changed our power state perhaps our neigbours changed
1807 snd_soc_dapm_widget_for_each_source_path(w, path)
1808 dapm_widget_set_peer_power(path->source, power, path->connect);
1810 /* Supplies can't affect their outputs, only their inputs */
1811 if (!w->is_supply) {
1812 snd_soc_dapm_widget_for_each_sink_path(w, path)
1813 dapm_widget_set_peer_power(path->sink, power,
1818 dapm_seq_insert(w, up_list, true);
1820 dapm_seq_insert(w, down_list, false);
1823 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1824 struct list_head *up_list,
1825 struct list_head *down_list)
1830 case snd_soc_dapm_pre:
1831 dapm_seq_insert(w, down_list, false);
1833 case snd_soc_dapm_post:
1834 dapm_seq_insert(w, up_list, true);
1838 power = dapm_widget_power_check(w);
1840 dapm_widget_set_power(w, power, up_list, down_list);
1845 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1847 if (dapm->idle_bias_off)
1850 switch (snd_power_get_state(dapm->card->snd_card)) {
1851 case SNDRV_CTL_POWER_D3hot:
1852 case SNDRV_CTL_POWER_D3cold:
1853 return dapm->suspend_bias_off;
1862 * Scan each dapm widget for complete audio path.
1863 * A complete path is a route that has valid endpoints i.e.:-
1865 * o DAC to output pin.
1866 * o Input pin to ADC.
1867 * o Input pin to Output pin (bypass, sidetone)
1868 * o DAC to ADC (loopback).
1870 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1872 struct snd_soc_dapm_widget *w;
1873 struct snd_soc_dapm_context *d;
1875 LIST_HEAD(down_list);
1876 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1877 enum snd_soc_bias_level bias;
1879 lockdep_assert_held(&card->dapm_mutex);
1881 trace_snd_soc_dapm_start(card);
1883 list_for_each_entry(d, &card->dapm_list, list) {
1884 if (dapm_idle_bias_off(d))
1885 d->target_bias_level = SND_SOC_BIAS_OFF;
1887 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1892 /* Check which widgets we need to power and store them in
1893 * lists indicating if they should be powered up or down. We
1894 * only check widgets that have been flagged as dirty but note
1895 * that new widgets may be added to the dirty list while we
1898 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1899 dapm_power_one_widget(w, &up_list, &down_list);
1902 list_for_each_entry(w, &card->widgets, list) {
1904 case snd_soc_dapm_pre:
1905 case snd_soc_dapm_post:
1906 /* These widgets always need to be powered */
1909 list_del_init(&w->dirty);
1916 /* Supplies and micbiases only bring the
1917 * context up to STANDBY as unless something
1918 * else is active and passing audio they
1919 * generally don't require full power. Signal
1920 * generators are virtual pins and have no
1921 * power impact themselves.
1924 case snd_soc_dapm_siggen:
1925 case snd_soc_dapm_vmid:
1927 case snd_soc_dapm_supply:
1928 case snd_soc_dapm_regulator_supply:
1929 case snd_soc_dapm_pinctrl:
1930 case snd_soc_dapm_clock_supply:
1931 case snd_soc_dapm_micbias:
1932 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1933 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1936 d->target_bias_level = SND_SOC_BIAS_ON;
1943 /* Force all contexts in the card to the same bias state if
1944 * they're not ground referenced.
1946 bias = SND_SOC_BIAS_OFF;
1947 list_for_each_entry(d, &card->dapm_list, list)
1948 if (d->target_bias_level > bias)
1949 bias = d->target_bias_level;
1950 list_for_each_entry(d, &card->dapm_list, list)
1951 if (!dapm_idle_bias_off(d))
1952 d->target_bias_level = bias;
1954 trace_snd_soc_dapm_walk_done(card);
1956 /* Run card bias changes at first */
1957 dapm_pre_sequence_async(&card->dapm, 0);
1958 /* Run other bias changes in parallel */
1959 list_for_each_entry(d, &card->dapm_list, list) {
1960 if (d != &card->dapm)
1961 async_schedule_domain(dapm_pre_sequence_async, d,
1964 async_synchronize_full_domain(&async_domain);
1966 list_for_each_entry(w, &down_list, power_list) {
1967 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1970 list_for_each_entry(w, &up_list, power_list) {
1971 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1974 /* Power down widgets first; try to avoid amplifying pops. */
1975 dapm_seq_run(card, &down_list, event, false);
1977 dapm_widget_update(card);
1980 dapm_seq_run(card, &up_list, event, true);
1982 /* Run all the bias changes in parallel */
1983 list_for_each_entry(d, &card->dapm_list, list) {
1984 if (d != &card->dapm)
1985 async_schedule_domain(dapm_post_sequence_async, d,
1988 async_synchronize_full_domain(&async_domain);
1989 /* Run card bias changes at last */
1990 dapm_post_sequence_async(&card->dapm, 0);
1992 /* do we need to notify any clients that DAPM event is complete */
1993 list_for_each_entry(d, &card->dapm_list, list) {
1994 if (d->stream_event)
1995 d->stream_event(d, event);
1998 pop_dbg(card->dev, card->pop_time,
1999 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2000 pop_wait(card->pop_time);
2002 trace_snd_soc_dapm_done(card);
2007 #ifdef CONFIG_DEBUG_FS
2008 static ssize_t dapm_widget_power_read_file(struct file *file,
2009 char __user *user_buf,
2010 size_t count, loff_t *ppos)
2012 struct snd_soc_dapm_widget *w = file->private_data;
2013 struct snd_soc_card *card = w->dapm->card;
2014 enum snd_soc_dapm_direction dir, rdir;
2018 struct snd_soc_dapm_path *p = NULL;
2020 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2024 mutex_lock(&card->dapm_mutex);
2026 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2031 in = is_connected_input_ep(w, NULL, NULL);
2032 out = is_connected_output_ep(w, NULL, NULL);
2035 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2036 w->name, w->power ? "On" : "Off",
2037 w->force ? " (forced)" : "", in, out);
2040 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2041 " - R%d(0x%x) mask 0x%x",
2042 w->reg, w->reg, w->mask << w->shift);
2044 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
2047 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2049 w->active ? "active" : "inactive");
2051 snd_soc_dapm_for_each_direction(dir) {
2052 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2053 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2054 if (p->connected && !p->connected(p->source, p->sink))
2060 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2061 " %s \"%s\" \"%s\"\n",
2062 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2063 p->name ? p->name : "static",
2064 p->node[rdir]->name);
2068 mutex_unlock(&card->dapm_mutex);
2070 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2076 static const struct file_operations dapm_widget_power_fops = {
2077 .open = simple_open,
2078 .read = dapm_widget_power_read_file,
2079 .llseek = default_llseek,
2082 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2083 size_t count, loff_t *ppos)
2085 struct snd_soc_dapm_context *dapm = file->private_data;
2088 switch (dapm->bias_level) {
2089 case SND_SOC_BIAS_ON:
2092 case SND_SOC_BIAS_PREPARE:
2093 level = "Prepare\n";
2095 case SND_SOC_BIAS_STANDBY:
2096 level = "Standby\n";
2098 case SND_SOC_BIAS_OFF:
2102 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2103 level = "Unknown\n";
2107 return simple_read_from_buffer(user_buf, count, ppos, level,
2111 static const struct file_operations dapm_bias_fops = {
2112 .open = simple_open,
2113 .read = dapm_bias_read_file,
2114 .llseek = default_llseek,
2117 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2118 struct dentry *parent)
2125 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2127 if (!dapm->debugfs_dapm) {
2129 "ASoC: Failed to create DAPM debugfs directory\n");
2133 d = debugfs_create_file("bias_level", 0444,
2134 dapm->debugfs_dapm, dapm,
2138 "ASoC: Failed to create bias level debugfs file\n");
2141 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2143 struct snd_soc_dapm_context *dapm = w->dapm;
2146 if (!dapm->debugfs_dapm || !w->name)
2149 d = debugfs_create_file(w->name, 0444,
2150 dapm->debugfs_dapm, w,
2151 &dapm_widget_power_fops);
2153 dev_warn(w->dapm->dev,
2154 "ASoC: Failed to create %s debugfs file\n",
2158 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2160 debugfs_remove_recursive(dapm->debugfs_dapm);
2164 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2165 struct dentry *parent)
2169 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2173 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2180 * soc_dapm_connect_path() - Connects or disconnects a path
2181 * @path: The path to update
2182 * @connect: The new connect state of the path. True if the path is connected,
2183 * false if it is disconnected.
2184 * @reason: The reason why the path changed (for debugging only)
2186 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2187 bool connect, const char *reason)
2189 if (path->connect == connect)
2192 path->connect = connect;
2193 dapm_mark_dirty(path->source, reason);
2194 dapm_mark_dirty(path->sink, reason);
2195 dapm_path_invalidate(path);
2198 /* test and update the power status of a mux widget */
2199 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2200 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2202 struct snd_soc_dapm_path *path;
2206 lockdep_assert_held(&card->dapm_mutex);
2208 /* find dapm widget path assoc with kcontrol */
2209 dapm_kcontrol_for_each_path(path, kcontrol) {
2211 /* we now need to match the string in the enum to the path */
2212 if (!(strcmp(path->name, e->texts[mux])))
2217 soc_dapm_connect_path(path, connect, "mux update");
2221 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2226 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2227 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2228 struct snd_soc_dapm_update *update)
2230 struct snd_soc_card *card = dapm->card;
2233 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2234 card->update = update;
2235 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2236 card->update = NULL;
2237 mutex_unlock(&card->dapm_mutex);
2239 soc_dpcm_runtime_update(card);
2242 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2244 /* test and update the power status of a mixer or switch widget */
2245 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2246 struct snd_kcontrol *kcontrol,
2247 int connect, int rconnect)
2249 struct snd_soc_dapm_path *path;
2252 lockdep_assert_held(&card->dapm_mutex);
2254 /* find dapm widget path assoc with kcontrol */
2255 dapm_kcontrol_for_each_path(path, kcontrol) {
2257 * Ideally this function should support any number of
2258 * paths and channels. But since kcontrols only come
2259 * in mono and stereo variants, we are limited to 2
2262 * The following code assumes for stereo controls the
2263 * first path (when 'found == 0') is the left channel,
2264 * and all remaining paths (when 'found == 1') are the
2267 * A stereo control is signified by a valid 'rconnect'
2268 * value, either 0 for unconnected, or >= 0 for connected.
2269 * This is chosen instead of using snd_soc_volsw_is_stereo,
2270 * so that the behavior of snd_soc_dapm_mixer_update_power
2271 * doesn't change even when the kcontrol passed in is
2274 * It passes 'connect' as the path connect status for
2275 * the left channel, and 'rconnect' for the right
2278 if (found && rconnect >= 0)
2279 soc_dapm_connect_path(path, rconnect, "mixer update");
2281 soc_dapm_connect_path(path, connect, "mixer update");
2286 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2291 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2292 struct snd_kcontrol *kcontrol, int connect,
2293 struct snd_soc_dapm_update *update)
2295 struct snd_soc_card *card = dapm->card;
2298 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2299 card->update = update;
2300 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2301 card->update = NULL;
2302 mutex_unlock(&card->dapm_mutex);
2304 soc_dpcm_runtime_update(card);
2307 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2309 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2312 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2313 struct snd_soc_dapm_widget *w;
2315 char *state = "not set";
2317 /* card won't be set for the dummy component, as a spot fix
2318 * we're checking for that case specifically here but in future
2319 * we will ensure that the dummy component looks like others.
2324 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2325 if (w->dapm != dapm)
2328 /* only display widgets that burn power */
2330 case snd_soc_dapm_hp:
2331 case snd_soc_dapm_mic:
2332 case snd_soc_dapm_spk:
2333 case snd_soc_dapm_line:
2334 case snd_soc_dapm_micbias:
2335 case snd_soc_dapm_dac:
2336 case snd_soc_dapm_adc:
2337 case snd_soc_dapm_pga:
2338 case snd_soc_dapm_out_drv:
2339 case snd_soc_dapm_mixer:
2340 case snd_soc_dapm_mixer_named_ctl:
2341 case snd_soc_dapm_supply:
2342 case snd_soc_dapm_regulator_supply:
2343 case snd_soc_dapm_pinctrl:
2344 case snd_soc_dapm_clock_supply:
2346 count += sprintf(buf + count, "%s: %s\n",
2347 w->name, w->power ? "On":"Off");
2354 switch (snd_soc_dapm_get_bias_level(dapm)) {
2355 case SND_SOC_BIAS_ON:
2358 case SND_SOC_BIAS_PREPARE:
2361 case SND_SOC_BIAS_STANDBY:
2364 case SND_SOC_BIAS_OFF:
2368 count += sprintf(buf + count, "PM State: %s\n", state);
2373 /* show dapm widget status in sys fs */
2374 static ssize_t dapm_widget_show(struct device *dev,
2375 struct device_attribute *attr, char *buf)
2377 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2380 mutex_lock(&rtd->card->dapm_mutex);
2382 for (i = 0; i < rtd->num_codecs; i++) {
2383 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2385 count += dapm_widget_show_component(cmpnt, buf + count);
2388 mutex_unlock(&rtd->card->dapm_mutex);
2393 static DEVICE_ATTR_RO(dapm_widget);
2395 struct attribute *soc_dapm_dev_attrs[] = {
2396 &dev_attr_dapm_widget.attr,
2400 static void dapm_free_path(struct snd_soc_dapm_path *path)
2402 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2403 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2404 list_del(&path->list_kcontrol);
2405 list_del(&path->list);
2409 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2411 struct snd_soc_dapm_path *p, *next_p;
2412 enum snd_soc_dapm_direction dir;
2416 * remove source and sink paths associated to this widget.
2417 * While removing the path, remove reference to it from both
2418 * source and sink widgets so that path is removed only once.
2420 snd_soc_dapm_for_each_direction(dir) {
2421 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2425 kfree(w->kcontrols);
2426 kfree_const(w->name);
2430 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2432 dapm->path_sink_cache.widget = NULL;
2433 dapm->path_source_cache.widget = NULL;
2436 /* free all dapm widgets and resources */
2437 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2439 struct snd_soc_dapm_widget *w, *next_w;
2441 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2442 if (w->dapm != dapm)
2444 snd_soc_dapm_free_widget(w);
2446 snd_soc_dapm_reset_cache(dapm);
2449 static struct snd_soc_dapm_widget *dapm_find_widget(
2450 struct snd_soc_dapm_context *dapm, const char *pin,
2451 bool search_other_contexts)
2453 struct snd_soc_dapm_widget *w;
2454 struct snd_soc_dapm_widget *fallback = NULL;
2456 list_for_each_entry(w, &dapm->card->widgets, list) {
2457 if (!strcmp(w->name, pin)) {
2458 if (w->dapm == dapm)
2465 if (search_other_contexts)
2471 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2472 const char *pin, int status)
2474 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2476 dapm_assert_locked(dapm);
2479 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2483 if (w->connected != status) {
2484 dapm_mark_dirty(w, "pin configuration");
2485 dapm_widget_invalidate_input_paths(w);
2486 dapm_widget_invalidate_output_paths(w);
2489 w->connected = status;
2497 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2498 * @dapm: DAPM context
2500 * Walks all dapm audio paths and powers widgets according to their
2501 * stream or path usage.
2503 * Requires external locking.
2505 * Returns 0 for success.
2507 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2510 * Suppress early reports (eg, jacks syncing their state) to avoid
2511 * silly DAPM runs during card startup.
2513 if (!dapm->card || !dapm->card->instantiated)
2516 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2518 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2521 * snd_soc_dapm_sync - scan and power dapm paths
2522 * @dapm: DAPM context
2524 * Walks all dapm audio paths and powers widgets according to their
2525 * stream or path usage.
2527 * Returns 0 for success.
2529 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2533 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2534 ret = snd_soc_dapm_sync_unlocked(dapm);
2535 mutex_unlock(&dapm->card->dapm_mutex);
2538 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2541 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2542 * @w: The widget for which to update the flags
2544 * Some widgets have a dynamic category which depends on which neighbors they
2545 * are connected to. This function update the category for these widgets.
2547 * This function must be called whenever a path is added or removed to a widget.
2549 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2551 enum snd_soc_dapm_direction dir;
2552 struct snd_soc_dapm_path *p;
2556 case snd_soc_dapm_input:
2557 /* On a fully routed card an input is never a source */
2558 if (w->dapm->card->fully_routed)
2560 ep = SND_SOC_DAPM_EP_SOURCE;
2561 snd_soc_dapm_widget_for_each_source_path(w, p) {
2562 if (p->source->id == snd_soc_dapm_micbias ||
2563 p->source->id == snd_soc_dapm_mic ||
2564 p->source->id == snd_soc_dapm_line ||
2565 p->source->id == snd_soc_dapm_output) {
2571 case snd_soc_dapm_output:
2572 /* On a fully routed card a output is never a sink */
2573 if (w->dapm->card->fully_routed)
2575 ep = SND_SOC_DAPM_EP_SINK;
2576 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2577 if (p->sink->id == snd_soc_dapm_spk ||
2578 p->sink->id == snd_soc_dapm_hp ||
2579 p->sink->id == snd_soc_dapm_line ||
2580 p->sink->id == snd_soc_dapm_input) {
2586 case snd_soc_dapm_line:
2588 snd_soc_dapm_for_each_direction(dir) {
2589 if (!list_empty(&w->edges[dir]))
2590 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2600 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2601 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2602 const char *control)
2604 bool dynamic_source = false;
2605 bool dynamic_sink = false;
2610 switch (source->id) {
2611 case snd_soc_dapm_demux:
2612 dynamic_source = true;
2619 case snd_soc_dapm_mux:
2620 case snd_soc_dapm_switch:
2621 case snd_soc_dapm_mixer:
2622 case snd_soc_dapm_mixer_named_ctl:
2623 dynamic_sink = true;
2629 if (dynamic_source && dynamic_sink) {
2631 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2632 source->name, control, sink->name);
2634 } else if (!dynamic_source && !dynamic_sink) {
2636 "Control not supported for path %s -> [%s] -> %s\n",
2637 source->name, control, sink->name);
2644 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2645 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2646 const char *control,
2647 int (*connected)(struct snd_soc_dapm_widget *source,
2648 struct snd_soc_dapm_widget *sink))
2650 struct snd_soc_dapm_widget *widgets[2];
2651 enum snd_soc_dapm_direction dir;
2652 struct snd_soc_dapm_path *path;
2655 if (wsink->is_supply && !wsource->is_supply) {
2657 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2658 wsource->name, wsink->name);
2662 if (connected && !wsource->is_supply) {
2664 "connected() callback only supported for supply widgets (%s -> %s)\n",
2665 wsource->name, wsink->name);
2669 if (wsource->is_supply && control) {
2671 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2672 wsource->name, control, wsink->name);
2676 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2680 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2684 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2685 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2686 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2687 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2689 path->connected = connected;
2690 INIT_LIST_HEAD(&path->list);
2691 INIT_LIST_HEAD(&path->list_kcontrol);
2693 if (wsource->is_supply || wsink->is_supply)
2694 path->is_supply = 1;
2696 /* connect static paths */
2697 if (control == NULL) {
2700 switch (wsource->id) {
2701 case snd_soc_dapm_demux:
2702 ret = dapm_connect_mux(dapm, path, control, wsource);
2710 switch (wsink->id) {
2711 case snd_soc_dapm_mux:
2712 ret = dapm_connect_mux(dapm, path, control, wsink);
2716 case snd_soc_dapm_switch:
2717 case snd_soc_dapm_mixer:
2718 case snd_soc_dapm_mixer_named_ctl:
2719 ret = dapm_connect_mixer(dapm, path, control);
2728 list_add(&path->list, &dapm->card->paths);
2729 snd_soc_dapm_for_each_direction(dir)
2730 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2732 snd_soc_dapm_for_each_direction(dir) {
2733 dapm_update_widget_flags(widgets[dir]);
2734 dapm_mark_dirty(widgets[dir], "Route added");
2737 if (dapm->card->instantiated && path->connect)
2738 dapm_path_invalidate(path);
2746 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2747 const struct snd_soc_dapm_route *route)
2749 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2750 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2753 char prefixed_sink[80];
2754 char prefixed_source[80];
2758 prefix = soc_dapm_prefix(dapm);
2760 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2761 prefix, route->sink);
2762 sink = prefixed_sink;
2763 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2764 prefix, route->source);
2765 source = prefixed_source;
2768 source = route->source;
2771 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2772 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2774 if (wsink && wsource)
2778 * find src and dest widgets over all widgets but favor a widget from
2779 * current DAPM context
2781 list_for_each_entry(w, &dapm->card->widgets, list) {
2782 if (!wsink && !(strcmp(w->name, sink))) {
2784 if (w->dapm == dapm) {
2791 if (!wsource && !(strcmp(w->name, source))) {
2793 if (w->dapm == dapm) {
2800 /* use widget from another DAPM context if not found from this */
2806 if (wsource == NULL) {
2807 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2811 if (wsink == NULL) {
2812 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2818 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2819 dapm_wcache_update(&dapm->path_source_cache, wsource);
2821 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2828 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2829 source, route->control, sink);
2833 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2834 const struct snd_soc_dapm_route *route)
2836 struct snd_soc_dapm_widget *wsource, *wsink;
2837 struct snd_soc_dapm_path *path, *p;
2840 char prefixed_sink[80];
2841 char prefixed_source[80];
2844 if (route->control) {
2846 "ASoC: Removal of routes with controls not supported\n");
2850 prefix = soc_dapm_prefix(dapm);
2852 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2853 prefix, route->sink);
2854 sink = prefixed_sink;
2855 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2856 prefix, route->source);
2857 source = prefixed_source;
2860 source = route->source;
2864 list_for_each_entry(p, &dapm->card->paths, list) {
2865 if (strcmp(p->source->name, source) != 0)
2867 if (strcmp(p->sink->name, sink) != 0)
2874 wsource = path->source;
2877 dapm_mark_dirty(wsource, "Route removed");
2878 dapm_mark_dirty(wsink, "Route removed");
2880 dapm_path_invalidate(path);
2882 dapm_free_path(path);
2884 /* Update any path related flags */
2885 dapm_update_widget_flags(wsource);
2886 dapm_update_widget_flags(wsink);
2888 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2896 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2897 * @dapm: DAPM context
2898 * @route: audio routes
2899 * @num: number of routes
2901 * Connects 2 dapm widgets together via a named audio path. The sink is
2902 * the widget receiving the audio signal, whilst the source is the sender
2903 * of the audio signal.
2905 * Returns 0 for success else error. On error all resources can be freed
2906 * with a call to snd_soc_card_free().
2908 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2909 const struct snd_soc_dapm_route *route, int num)
2913 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2914 for (i = 0; i < num; i++) {
2915 r = snd_soc_dapm_add_route(dapm, route);
2917 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2919 route->control ? route->control : "direct",
2925 mutex_unlock(&dapm->card->dapm_mutex);
2929 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2932 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2933 * @dapm: DAPM context
2934 * @route: audio routes
2935 * @num: number of routes
2937 * Removes routes from the DAPM context.
2939 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2940 const struct snd_soc_dapm_route *route, int num)
2944 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2945 for (i = 0; i < num; i++) {
2946 snd_soc_dapm_del_route(dapm, route);
2949 mutex_unlock(&dapm->card->dapm_mutex);
2953 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2955 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2956 const struct snd_soc_dapm_route *route)
2958 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2961 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2964 struct snd_soc_dapm_path *path;
2968 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2974 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2979 if (route->control || route->connected)
2980 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2981 route->source, route->sink);
2983 snd_soc_dapm_widget_for_each_sink_path(source, path) {
2984 if (path->sink == sink) {
2991 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2992 route->source, route->sink);
2994 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2995 count, route->source, route->sink);
3001 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3002 * @dapm: DAPM context
3003 * @route: audio routes
3004 * @num: number of routes
3006 * Mark existing routes matching those specified in the passed array
3007 * as being weak, meaning that they are ignored for the purpose of
3008 * power decisions. The main intended use case is for sidetone paths
3009 * which couple audio between other independent paths if they are both
3010 * active in order to make the combination work better at the user
3011 * level but which aren't intended to be "used".
3013 * Note that CODEC drivers should not use this as sidetone type paths
3014 * can frequently also be used as bypass paths.
3016 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3017 const struct snd_soc_dapm_route *route, int num)
3022 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3023 for (i = 0; i < num; i++) {
3024 err = snd_soc_dapm_weak_route(dapm, route);
3029 mutex_unlock(&dapm->card->dapm_mutex);
3033 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3036 * snd_soc_dapm_new_widgets - add new dapm widgets
3037 * @card: card to be checked for new dapm widgets
3039 * Checks the codec for any new dapm widgets and creates them if found.
3041 * Returns 0 for success.
3043 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3045 struct snd_soc_dapm_widget *w;
3048 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3050 list_for_each_entry(w, &card->widgets, list)
3055 if (w->num_kcontrols) {
3056 w->kcontrols = kzalloc(w->num_kcontrols *
3057 sizeof(struct snd_kcontrol *),
3059 if (!w->kcontrols) {
3060 mutex_unlock(&card->dapm_mutex);
3066 case snd_soc_dapm_switch:
3067 case snd_soc_dapm_mixer:
3068 case snd_soc_dapm_mixer_named_ctl:
3071 case snd_soc_dapm_mux:
3072 case snd_soc_dapm_demux:
3075 case snd_soc_dapm_pga:
3076 case snd_soc_dapm_out_drv:
3079 case snd_soc_dapm_dai_link:
3080 dapm_new_dai_link(w);
3086 /* Read the initial power state from the device */
3088 soc_dapm_read(w->dapm, w->reg, &val);
3089 val = val >> w->shift;
3091 if (val == w->on_val)
3097 dapm_mark_dirty(w, "new widget");
3098 dapm_debugfs_add_widget(w);
3101 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3102 mutex_unlock(&card->dapm_mutex);
3105 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3108 * snd_soc_dapm_get_volsw - dapm mixer get callback
3109 * @kcontrol: mixer control
3110 * @ucontrol: control element information
3112 * Callback to get the value of a dapm mixer control.
3114 * Returns 0 for success.
3116 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3117 struct snd_ctl_elem_value *ucontrol)
3119 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3120 struct snd_soc_card *card = dapm->card;
3121 struct soc_mixer_control *mc =
3122 (struct soc_mixer_control *)kcontrol->private_value;
3124 unsigned int shift = mc->shift;
3126 unsigned int width = fls(max);
3127 unsigned int mask = (1 << fls(max)) - 1;
3128 unsigned int invert = mc->invert;
3129 unsigned int reg_val, val, rval = 0;
3132 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3133 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3134 ret = soc_dapm_read(dapm, reg, ®_val);
3135 val = (reg_val >> shift) & mask;
3137 if (ret == 0 && reg != mc->rreg)
3138 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3140 if (snd_soc_volsw_is_stereo(mc))
3141 rval = (reg_val >> mc->rshift) & mask;
3143 reg_val = dapm_kcontrol_get_value(kcontrol);
3144 val = reg_val & mask;
3146 if (snd_soc_volsw_is_stereo(mc))
3147 rval = (reg_val >> width) & mask;
3149 mutex_unlock(&card->dapm_mutex);
3155 ucontrol->value.integer.value[0] = max - val;
3157 ucontrol->value.integer.value[0] = val;
3159 if (snd_soc_volsw_is_stereo(mc)) {
3161 ucontrol->value.integer.value[1] = max - rval;
3163 ucontrol->value.integer.value[1] = rval;
3168 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3171 * snd_soc_dapm_put_volsw - dapm mixer set callback
3172 * @kcontrol: mixer control
3173 * @ucontrol: control element information
3175 * Callback to set the value of a dapm mixer control.
3177 * Returns 0 for success.
3179 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3180 struct snd_ctl_elem_value *ucontrol)
3182 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3183 struct snd_soc_card *card = dapm->card;
3184 struct soc_mixer_control *mc =
3185 (struct soc_mixer_control *)kcontrol->private_value;
3187 unsigned int shift = mc->shift;
3189 unsigned int width = fls(max);
3190 unsigned int mask = (1 << width) - 1;
3191 unsigned int invert = mc->invert;
3192 unsigned int val, rval = 0;
3193 int connect, rconnect = -1, change, reg_change = 0;
3194 struct snd_soc_dapm_update update = {};
3197 val = (ucontrol->value.integer.value[0] & mask);
3203 if (snd_soc_volsw_is_stereo(mc)) {
3204 rval = (ucontrol->value.integer.value[1] & mask);
3210 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3212 /* This assumes field width < (bits in unsigned int / 2) */
3213 if (width > sizeof(unsigned int) * 8 / 2)
3215 "ASoC: control %s field width limit exceeded\n",
3217 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3219 if (reg != SND_SOC_NOPM) {
3221 rval = rval << mc->rshift;
3223 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3225 if (snd_soc_volsw_is_stereo(mc))
3226 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3231 if (change || reg_change) {
3233 if (snd_soc_volsw_is_stereo(mc)) {
3234 update.has_second_set = true;
3235 update.reg2 = mc->rreg;
3236 update.mask2 = mask << mc->rshift;
3239 update.kcontrol = kcontrol;
3241 update.mask = mask << shift;
3243 card->update = &update;
3245 change |= reg_change;
3247 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3250 card->update = NULL;
3253 mutex_unlock(&card->dapm_mutex);
3256 soc_dpcm_runtime_update(card);
3260 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3263 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3264 * @kcontrol: mixer control
3265 * @ucontrol: control element information
3267 * Callback to get the value of a dapm enumerated double mixer control.
3269 * Returns 0 for success.
3271 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3272 struct snd_ctl_elem_value *ucontrol)
3274 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3275 struct snd_soc_card *card = dapm->card;
3276 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3277 unsigned int reg_val, val;
3279 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3280 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3281 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3283 mutex_unlock(&card->dapm_mutex);
3287 reg_val = dapm_kcontrol_get_value(kcontrol);
3289 mutex_unlock(&card->dapm_mutex);
3291 val = (reg_val >> e->shift_l) & e->mask;
3292 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3293 if (e->shift_l != e->shift_r) {
3294 val = (reg_val >> e->shift_r) & e->mask;
3295 val = snd_soc_enum_val_to_item(e, val);
3296 ucontrol->value.enumerated.item[1] = val;
3301 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3304 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3305 * @kcontrol: mixer control
3306 * @ucontrol: control element information
3308 * Callback to set the value of a dapm enumerated double mixer control.
3310 * Returns 0 for success.
3312 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3313 struct snd_ctl_elem_value *ucontrol)
3315 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3316 struct snd_soc_card *card = dapm->card;
3317 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3318 unsigned int *item = ucontrol->value.enumerated.item;
3319 unsigned int val, change, reg_change = 0;
3321 struct snd_soc_dapm_update update = {};
3324 if (item[0] >= e->items)
3327 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3328 mask = e->mask << e->shift_l;
3329 if (e->shift_l != e->shift_r) {
3330 if (item[1] > e->items)
3332 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3333 mask |= e->mask << e->shift_r;
3336 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3338 change = dapm_kcontrol_set_value(kcontrol, val);
3340 if (e->reg != SND_SOC_NOPM)
3341 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3343 if (change || reg_change) {
3345 update.kcontrol = kcontrol;
3346 update.reg = e->reg;
3349 card->update = &update;
3351 change |= reg_change;
3353 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3355 card->update = NULL;
3358 mutex_unlock(&card->dapm_mutex);
3361 soc_dpcm_runtime_update(card);
3365 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3368 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3370 * @kcontrol: mixer control
3371 * @uinfo: control element information
3373 * Callback to provide information about a pin switch control.
3375 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3376 struct snd_ctl_elem_info *uinfo)
3378 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3380 uinfo->value.integer.min = 0;
3381 uinfo->value.integer.max = 1;
3385 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3388 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3390 * @kcontrol: mixer control
3393 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3394 struct snd_ctl_elem_value *ucontrol)
3396 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3397 const char *pin = (const char *)kcontrol->private_value;
3399 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3401 ucontrol->value.integer.value[0] =
3402 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3404 mutex_unlock(&card->dapm_mutex);
3408 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3411 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3413 * @kcontrol: mixer control
3416 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_value *ucontrol)
3419 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3420 const char *pin = (const char *)kcontrol->private_value;
3422 if (ucontrol->value.integer.value[0])
3423 snd_soc_dapm_enable_pin(&card->dapm, pin);
3425 snd_soc_dapm_disable_pin(&card->dapm, pin);
3427 snd_soc_dapm_sync(&card->dapm);
3430 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3432 struct snd_soc_dapm_widget *
3433 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3434 const struct snd_soc_dapm_widget *widget)
3436 struct snd_soc_dapm_widget *w;
3438 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3439 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3440 /* Do not nag about probe deferrals */
3442 int ret = PTR_ERR(w);
3444 if (ret != -EPROBE_DEFER)
3446 "ASoC: Failed to create DAPM control %s (%d)\n",
3452 "ASoC: Failed to create DAPM control %s\n",
3456 mutex_unlock(&dapm->card->dapm_mutex);
3459 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3461 struct snd_soc_dapm_widget *
3462 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3463 const struct snd_soc_dapm_widget *widget)
3465 enum snd_soc_dapm_direction dir;
3466 struct snd_soc_dapm_widget *w;
3470 if ((w = dapm_cnew_widget(widget)) == NULL)
3474 case snd_soc_dapm_regulator_supply:
3475 w->regulator = devm_regulator_get(dapm->dev, w->name);
3476 if (IS_ERR(w->regulator)) {
3477 ret = PTR_ERR(w->regulator);
3478 if (ret == -EPROBE_DEFER)
3479 return ERR_PTR(ret);
3480 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3485 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3486 ret = regulator_allow_bypass(w->regulator, true);
3488 dev_warn(w->dapm->dev,
3489 "ASoC: Failed to bypass %s: %d\n",
3493 case snd_soc_dapm_pinctrl:
3494 w->pinctrl = devm_pinctrl_get(dapm->dev);
3495 if (IS_ERR_OR_NULL(w->pinctrl)) {
3496 ret = PTR_ERR(w->pinctrl);
3497 if (ret == -EPROBE_DEFER)
3498 return ERR_PTR(ret);
3499 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3504 case snd_soc_dapm_clock_supply:
3505 #ifdef CONFIG_CLKDEV_LOOKUP
3506 w->clk = devm_clk_get(dapm->dev, w->name);
3507 if (IS_ERR(w->clk)) {
3508 ret = PTR_ERR(w->clk);
3509 if (ret == -EPROBE_DEFER)
3510 return ERR_PTR(ret);
3511 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3523 prefix = soc_dapm_prefix(dapm);
3525 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3527 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3528 if (w->name == NULL) {
3534 case snd_soc_dapm_mic:
3535 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3536 w->power_check = dapm_generic_check_power;
3538 case snd_soc_dapm_input:
3539 if (!dapm->card->fully_routed)
3540 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3541 w->power_check = dapm_generic_check_power;
3543 case snd_soc_dapm_spk:
3544 case snd_soc_dapm_hp:
3545 w->is_ep = SND_SOC_DAPM_EP_SINK;
3546 w->power_check = dapm_generic_check_power;
3548 case snd_soc_dapm_output:
3549 if (!dapm->card->fully_routed)
3550 w->is_ep = SND_SOC_DAPM_EP_SINK;
3551 w->power_check = dapm_generic_check_power;
3553 case snd_soc_dapm_vmid:
3554 case snd_soc_dapm_siggen:
3555 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3556 w->power_check = dapm_always_on_check_power;
3558 case snd_soc_dapm_sink:
3559 w->is_ep = SND_SOC_DAPM_EP_SINK;
3560 w->power_check = dapm_always_on_check_power;
3563 case snd_soc_dapm_mux:
3564 case snd_soc_dapm_demux:
3565 case snd_soc_dapm_switch:
3566 case snd_soc_dapm_mixer:
3567 case snd_soc_dapm_mixer_named_ctl:
3568 case snd_soc_dapm_adc:
3569 case snd_soc_dapm_aif_out:
3570 case snd_soc_dapm_dac:
3571 case snd_soc_dapm_aif_in:
3572 case snd_soc_dapm_pga:
3573 case snd_soc_dapm_out_drv:
3574 case snd_soc_dapm_micbias:
3575 case snd_soc_dapm_line:
3576 case snd_soc_dapm_dai_link:
3577 case snd_soc_dapm_dai_out:
3578 case snd_soc_dapm_dai_in:
3579 w->power_check = dapm_generic_check_power;
3581 case snd_soc_dapm_supply:
3582 case snd_soc_dapm_regulator_supply:
3583 case snd_soc_dapm_pinctrl:
3584 case snd_soc_dapm_clock_supply:
3585 case snd_soc_dapm_kcontrol:
3587 w->power_check = dapm_supply_check_power;
3590 w->power_check = dapm_always_on_check_power;
3595 INIT_LIST_HEAD(&w->list);
3596 INIT_LIST_HEAD(&w->dirty);
3597 list_add_tail(&w->list, &dapm->card->widgets);
3599 snd_soc_dapm_for_each_direction(dir) {
3600 INIT_LIST_HEAD(&w->edges[dir]);
3601 w->endpoints[dir] = -1;
3604 /* machine layer sets up unconnected pins and insertions */
3610 * snd_soc_dapm_new_controls - create new dapm controls
3611 * @dapm: DAPM context
3612 * @widget: widget array
3613 * @num: number of widgets
3615 * Creates new DAPM controls based upon the templates.
3617 * Returns 0 for success else error.
3619 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3620 const struct snd_soc_dapm_widget *widget,
3623 struct snd_soc_dapm_widget *w;
3627 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3628 for (i = 0; i < num; i++) {
3629 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3632 /* Do not nag about probe deferrals */
3633 if (ret == -EPROBE_DEFER)
3636 "ASoC: Failed to create DAPM control %s (%d)\n",
3642 "ASoC: Failed to create DAPM control %s\n",
3649 mutex_unlock(&dapm->card->dapm_mutex);
3652 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3654 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3655 struct snd_kcontrol *kcontrol, int event)
3657 struct snd_soc_dapm_path *source_p, *sink_p;
3658 struct snd_soc_dai *source, *sink;
3659 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
3660 struct snd_pcm_substream substream;
3661 struct snd_pcm_hw_params *params = NULL;
3662 struct snd_pcm_runtime *runtime = NULL;
3666 if (WARN_ON(!config) ||
3667 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3668 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3671 /* We only support a single source and sink, pick the first */
3672 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3673 struct snd_soc_dapm_path,
3674 list_node[SND_SOC_DAPM_DIR_OUT]);
3675 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3676 struct snd_soc_dapm_path,
3677 list_node[SND_SOC_DAPM_DIR_IN]);
3679 source = source_p->source->priv;
3680 sink = sink_p->sink->priv;
3682 /* Be a little careful as we don't want to overflow the mask array */
3683 if (config->formats) {
3684 fmt = ffs(config->formats) - 1;
3686 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3691 /* Currently very limited parameter selection */
3692 params = kzalloc(sizeof(*params), GFP_KERNEL);
3697 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3699 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3701 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3704 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3705 = config->channels_min;
3706 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3707 = config->channels_max;
3709 memset(&substream, 0, sizeof(substream));
3711 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3712 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3717 substream.runtime = runtime;
3720 case SND_SOC_DAPM_PRE_PMU:
3721 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3722 if (source->driver->ops->startup) {
3723 ret = source->driver->ops->startup(&substream, source);
3725 dev_err(source->dev,
3726 "ASoC: startup() failed: %d\n", ret);
3731 ret = soc_dai_hw_params(&substream, params, source);
3735 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3736 if (sink->driver->ops->startup) {
3737 ret = sink->driver->ops->startup(&substream, sink);
3740 "ASoC: startup() failed: %d\n", ret);
3745 ret = soc_dai_hw_params(&substream, params, sink);
3750 case SND_SOC_DAPM_POST_PMU:
3751 ret = snd_soc_dai_digital_mute(sink, 0,
3752 SNDRV_PCM_STREAM_PLAYBACK);
3753 if (ret != 0 && ret != -ENOTSUPP)
3754 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3758 case SND_SOC_DAPM_PRE_PMD:
3759 ret = snd_soc_dai_digital_mute(sink, 1,
3760 SNDRV_PCM_STREAM_PLAYBACK);
3761 if (ret != 0 && ret != -ENOTSUPP)
3762 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3766 if (source->driver->ops->shutdown) {
3767 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3768 source->driver->ops->shutdown(&substream, source);
3772 if (sink->driver->ops->shutdown) {
3773 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3774 sink->driver->ops->shutdown(&substream, sink);
3779 WARN(1, "Unknown event %d\n", event);
3789 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3790 struct snd_ctl_elem_value *ucontrol)
3792 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3794 ucontrol->value.enumerated.item[0] = w->params_select;
3799 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3800 struct snd_ctl_elem_value *ucontrol)
3802 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3804 /* Can't change the config when widget is already powered */
3808 if (ucontrol->value.enumerated.item[0] == w->params_select)
3811 if (ucontrol->value.enumerated.item[0] >= w->num_params)
3814 w->params_select = ucontrol->value.enumerated.item[0];
3820 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3821 unsigned long *private_value,
3823 const char **w_param_text)
3827 devm_kfree(card->dev, (void *)*private_value);
3828 for (count = 0 ; count < num_params; count++)
3829 devm_kfree(card->dev, (void *)w_param_text[count]);
3830 devm_kfree(card->dev, w_param_text);
3833 static struct snd_kcontrol_new *
3834 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3836 const struct snd_soc_pcm_stream *params,
3837 int num_params, const char **w_param_text,
3838 unsigned long *private_value)
3840 struct soc_enum w_param_enum[] = {
3841 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3843 struct snd_kcontrol_new kcontrol_dai_link[] = {
3844 SOC_ENUM_EXT(NULL, w_param_enum[0],
3845 snd_soc_dapm_dai_link_get,
3846 snd_soc_dapm_dai_link_put),
3848 struct snd_kcontrol_new *kcontrol_news;
3849 const struct snd_soc_pcm_stream *config = params;
3852 for (count = 0 ; count < num_params; count++) {
3853 if (!config->stream_name) {
3854 dev_warn(card->dapm.dev,
3855 "ASoC: anonymous config %d for dai link %s\n",
3857 w_param_text[count] =
3858 devm_kasprintf(card->dev, GFP_KERNEL,
3859 "Anonymous Configuration %d",
3862 w_param_text[count] = devm_kmemdup(card->dev,
3863 config->stream_name,
3864 strlen(config->stream_name) + 1,
3867 if (!w_param_text[count])
3868 goto outfree_w_param;
3872 w_param_enum[0].items = num_params;
3873 w_param_enum[0].texts = w_param_text;
3876 (unsigned long) devm_kmemdup(card->dev,
3877 (void *)(kcontrol_dai_link[0].private_value),
3878 sizeof(struct soc_enum), GFP_KERNEL);
3879 if (!*private_value) {
3880 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3882 goto outfree_w_param;
3884 kcontrol_dai_link[0].private_value = *private_value;
3885 /* duplicate kcontrol_dai_link on heap so that memory persists */
3886 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3887 sizeof(struct snd_kcontrol_new),
3889 if (!kcontrol_news) {
3890 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3892 goto outfree_w_param;
3894 return kcontrol_news;
3897 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
3901 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3902 const struct snd_soc_pcm_stream *params,
3903 unsigned int num_params,
3904 struct snd_soc_dapm_widget *source,
3905 struct snd_soc_dapm_widget *sink)
3907 struct snd_soc_dapm_widget template;
3908 struct snd_soc_dapm_widget *w;
3909 const char **w_param_text;
3910 unsigned long private_value;
3914 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3915 source->name, sink->name);
3919 memset(&template, 0, sizeof(template));
3920 template.reg = SND_SOC_NOPM;
3921 template.id = snd_soc_dapm_dai_link;
3922 template.name = link_name;
3923 template.event = snd_soc_dai_link_event;
3924 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3925 SND_SOC_DAPM_PRE_PMD;
3926 template.kcontrol_news = NULL;
3928 /* allocate memory for control, only in case of multiple configs */
3929 if (num_params > 1) {
3930 w_param_text = devm_kcalloc(card->dev, num_params,
3931 sizeof(char *), GFP_KERNEL);
3932 if (!w_param_text) {
3937 template.num_kcontrols = 1;
3938 template.kcontrol_news =
3939 snd_soc_dapm_alloc_kcontrol(card,
3940 link_name, params, num_params,
3941 w_param_text, &private_value);
3942 if (!template.kcontrol_news) {
3947 w_param_text = NULL;
3949 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3951 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
3954 /* Do not nag about probe deferrals */
3955 if (ret != -EPROBE_DEFER)
3957 "ASoC: Failed to create %s widget (%d)\n",
3959 goto outfree_kcontrol_news;
3962 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3965 goto outfree_kcontrol_news;
3969 w->num_params = num_params;
3971 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3974 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3977 devm_kfree(card->dev, w);
3978 outfree_kcontrol_news:
3979 devm_kfree(card->dev, (void *)template.kcontrol_news);
3980 snd_soc_dapm_free_kcontrol(card, &private_value, num_params, w_param_text);
3982 devm_kfree(card->dev, link_name);
3986 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3987 struct snd_soc_dai *dai)
3989 struct snd_soc_dapm_widget template;
3990 struct snd_soc_dapm_widget *w;
3992 WARN_ON(dapm->dev != dai->dev);
3994 memset(&template, 0, sizeof(template));
3995 template.reg = SND_SOC_NOPM;
3997 if (dai->driver->playback.stream_name) {
3998 template.id = snd_soc_dapm_dai_in;
3999 template.name = dai->driver->playback.stream_name;
4000 template.sname = dai->driver->playback.stream_name;
4002 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4005 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4007 int ret = PTR_ERR(w);
4009 /* Do not nag about probe deferrals */
4010 if (ret != -EPROBE_DEFER)
4012 "ASoC: Failed to create %s widget (%d)\n",
4013 dai->driver->playback.stream_name, ret);
4017 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4018 dai->driver->playback.stream_name);
4023 dai->playback_widget = w;
4026 if (dai->driver->capture.stream_name) {
4027 template.id = snd_soc_dapm_dai_out;
4028 template.name = dai->driver->capture.stream_name;
4029 template.sname = dai->driver->capture.stream_name;
4031 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4034 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4036 int ret = PTR_ERR(w);
4038 /* Do not nag about probe deferrals */
4039 if (ret != -EPROBE_DEFER)
4041 "ASoC: Failed to create %s widget (%d)\n",
4042 dai->driver->playback.stream_name, ret);
4046 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4047 dai->driver->capture.stream_name);
4052 dai->capture_widget = w;
4058 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4060 struct snd_soc_dapm_widget *dai_w, *w;
4061 struct snd_soc_dapm_widget *src, *sink;
4062 struct snd_soc_dai *dai;
4064 /* For each DAI widget... */
4065 list_for_each_entry(dai_w, &card->widgets, list) {
4066 switch (dai_w->id) {
4067 case snd_soc_dapm_dai_in:
4068 case snd_soc_dapm_dai_out:
4076 /* ...find all widgets with the same stream and link them */
4077 list_for_each_entry(w, &card->widgets, list) {
4078 if (w->dapm != dai_w->dapm)
4082 case snd_soc_dapm_dai_in:
4083 case snd_soc_dapm_dai_out:
4089 if (!w->sname || !strstr(w->sname, dai_w->sname))
4092 if (dai_w->id == snd_soc_dapm_dai_in) {
4099 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4100 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4107 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4108 struct snd_soc_pcm_runtime *rtd)
4110 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4111 struct snd_soc_dapm_widget *sink, *source;
4114 for (i = 0; i < rtd->num_codecs; i++) {
4115 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
4117 /* connect BE DAI playback if widgets are valid */
4118 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
4119 source = cpu_dai->playback_widget;
4120 sink = codec_dai->playback_widget;
4121 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4122 cpu_dai->component->name, source->name,
4123 codec_dai->component->name, sink->name);
4125 snd_soc_dapm_add_path(&card->dapm, source, sink,
4129 /* connect BE DAI capture if widgets are valid */
4130 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
4131 source = codec_dai->capture_widget;
4132 sink = cpu_dai->capture_widget;
4133 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4134 codec_dai->component->name, source->name,
4135 cpu_dai->component->name, sink->name);
4137 snd_soc_dapm_add_path(&card->dapm, source, sink,
4143 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4146 struct snd_soc_dapm_widget *w;
4149 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4150 w = dai->playback_widget;
4152 w = dai->capture_widget;
4155 dapm_mark_dirty(w, "stream event");
4157 if (w->id == snd_soc_dapm_dai_in) {
4158 ep = SND_SOC_DAPM_EP_SOURCE;
4159 dapm_widget_invalidate_input_paths(w);
4161 ep = SND_SOC_DAPM_EP_SINK;
4162 dapm_widget_invalidate_output_paths(w);
4166 case SND_SOC_DAPM_STREAM_START:
4170 case SND_SOC_DAPM_STREAM_STOP:
4174 case SND_SOC_DAPM_STREAM_SUSPEND:
4175 case SND_SOC_DAPM_STREAM_RESUME:
4176 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4177 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4183 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4185 struct snd_soc_pcm_runtime *rtd;
4187 /* for each BE DAI link... */
4188 list_for_each_entry(rtd, &card->rtd_list, list) {
4190 * dynamic FE links have no fixed DAI mapping.
4191 * CODEC<->CODEC links have no direct connection.
4193 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4196 dapm_connect_dai_link_widgets(card, rtd);
4200 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4205 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4206 for (i = 0; i < rtd->num_codecs; i++)
4207 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
4209 dapm_power_widgets(rtd->card, event);
4213 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4214 * @rtd: PCM runtime data
4215 * @stream: stream name
4216 * @event: stream event
4218 * Sends a stream event to the dapm core. The core then makes any
4219 * necessary widget power changes.
4221 * Returns 0 for success else error.
4223 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4226 struct snd_soc_card *card = rtd->card;
4228 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4229 soc_dapm_stream_event(rtd, stream, event);
4230 mutex_unlock(&card->dapm_mutex);
4234 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4235 * @dapm: DAPM context
4238 * Enables input/output pin and its parents or children widgets iff there is
4239 * a valid audio route and active audio stream.
4241 * Requires external locking.
4243 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4244 * do any widget power switching.
4246 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4249 return snd_soc_dapm_set_pin(dapm, pin, 1);
4251 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4254 * snd_soc_dapm_enable_pin - enable pin.
4255 * @dapm: DAPM context
4258 * Enables input/output pin and its parents or children widgets iff there is
4259 * a valid audio route and active audio stream.
4261 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4262 * do any widget power switching.
4264 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4268 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4270 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4272 mutex_unlock(&dapm->card->dapm_mutex);
4276 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4279 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4280 * @dapm: DAPM context
4283 * Enables input/output pin regardless of any other state. This is
4284 * intended for use with microphone bias supplies used in microphone
4287 * Requires external locking.
4289 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4290 * do any widget power switching.
4292 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4295 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4298 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4302 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4303 if (!w->connected) {
4305 * w->force does not affect the number of input or output paths,
4306 * so we only have to recheck if w->connected is changed
4308 dapm_widget_invalidate_input_paths(w);
4309 dapm_widget_invalidate_output_paths(w);
4313 dapm_mark_dirty(w, "force enable");
4317 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4320 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4321 * @dapm: DAPM context
4324 * Enables input/output pin regardless of any other state. This is
4325 * intended for use with microphone bias supplies used in microphone
4328 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4329 * do any widget power switching.
4331 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4336 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4338 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4340 mutex_unlock(&dapm->card->dapm_mutex);
4344 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4347 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4348 * @dapm: DAPM context
4351 * Disables input/output pin and its parents or children widgets.
4353 * Requires external locking.
4355 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4356 * do any widget power switching.
4358 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4361 return snd_soc_dapm_set_pin(dapm, pin, 0);
4363 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4366 * snd_soc_dapm_disable_pin - disable pin.
4367 * @dapm: DAPM context
4370 * Disables input/output pin and its parents or children widgets.
4372 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4373 * do any widget power switching.
4375 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4380 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4382 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4384 mutex_unlock(&dapm->card->dapm_mutex);
4388 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4391 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4392 * @dapm: DAPM context
4395 * Marks the specified pin as being not connected, disabling it along
4396 * any parent or child widgets. At present this is identical to
4397 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4398 * additional things such as disabling controls which only affect
4399 * paths through the pin.
4401 * Requires external locking.
4403 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4404 * do any widget power switching.
4406 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4409 return snd_soc_dapm_set_pin(dapm, pin, 0);
4411 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4414 * snd_soc_dapm_nc_pin - permanently disable pin.
4415 * @dapm: DAPM context
4418 * Marks the specified pin as being not connected, disabling it along
4419 * any parent or child widgets. At present this is identical to
4420 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4421 * additional things such as disabling controls which only affect
4422 * paths through the pin.
4424 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4425 * do any widget power switching.
4427 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4431 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4433 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4435 mutex_unlock(&dapm->card->dapm_mutex);
4439 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4442 * snd_soc_dapm_get_pin_status - get audio pin status
4443 * @dapm: DAPM context
4444 * @pin: audio signal pin endpoint (or start point)
4446 * Get audio pin status - connected or disconnected.
4448 * Returns 1 for connected otherwise 0.
4450 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4453 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4456 return w->connected;
4460 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4463 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4464 * @dapm: DAPM context
4465 * @pin: audio signal pin endpoint (or start point)
4467 * Mark the given endpoint or pin as ignoring suspend. When the
4468 * system is disabled a path between two endpoints flagged as ignoring
4469 * suspend will not be disabled. The path must already be enabled via
4470 * normal means at suspend time, it will not be turned on if it was not
4473 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4476 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4479 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4483 w->ignore_suspend = 1;
4487 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4490 * snd_soc_dapm_free - free dapm resources
4491 * @dapm: DAPM context
4493 * Free all dapm widgets and resources.
4495 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4497 dapm_debugfs_cleanup(dapm);
4498 dapm_free_widgets(dapm);
4499 list_del(&dapm->list);
4501 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4503 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4505 struct snd_soc_card *card = dapm->card;
4506 struct snd_soc_dapm_widget *w;
4507 LIST_HEAD(down_list);
4510 mutex_lock(&card->dapm_mutex);
4512 list_for_each_entry(w, &dapm->card->widgets, list) {
4513 if (w->dapm != dapm)
4516 dapm_seq_insert(w, &down_list, false);
4522 /* If there were no widgets to power down we're already in
4526 if (dapm->bias_level == SND_SOC_BIAS_ON)
4527 snd_soc_dapm_set_bias_level(dapm,
4528 SND_SOC_BIAS_PREPARE);
4529 dapm_seq_run(card, &down_list, 0, false);
4530 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4531 snd_soc_dapm_set_bias_level(dapm,
4532 SND_SOC_BIAS_STANDBY);
4535 mutex_unlock(&card->dapm_mutex);
4539 * snd_soc_dapm_shutdown - callback for system shutdown
4541 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4543 struct snd_soc_dapm_context *dapm;
4545 list_for_each_entry(dapm, &card->dapm_list, list) {
4546 if (dapm != &card->dapm) {
4547 soc_dapm_shutdown_dapm(dapm);
4548 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4549 snd_soc_dapm_set_bias_level(dapm,
4554 soc_dapm_shutdown_dapm(&card->dapm);
4555 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4556 snd_soc_dapm_set_bias_level(&card->dapm,
4560 /* Module information */
4561 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4562 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4563 MODULE_LICENSE("GPL");