1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
17 // o Delayed power down of audio subsystem to reduce pops between a quick
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/async.h>
24 #include <linux/delay.h>
26 #include <linux/bitops.h>
27 #include <linux/platform_device.h>
28 #include <linux/jiffies.h>
29 #include <linux/debugfs.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/pinctrl/consumer.h>
33 #include <linux/clk.h>
34 #include <linux/slab.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/initval.h>
41 #include <trace/events/asoc.h>
43 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
45 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
46 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
48 #define snd_soc_dapm_for_each_direction(dir) \
49 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
52 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
53 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
55 int (*connected)(struct snd_soc_dapm_widget *source,
56 struct snd_soc_dapm_widget *sink));
58 struct snd_soc_dapm_widget *
59 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
60 const struct snd_soc_dapm_widget *widget);
62 struct snd_soc_dapm_widget *
63 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
64 const struct snd_soc_dapm_widget *widget);
66 /* dapm power sequences - make this per codec in the future */
67 static int dapm_up_seq[] = {
68 [snd_soc_dapm_pre] = 0,
69 [snd_soc_dapm_regulator_supply] = 1,
70 [snd_soc_dapm_pinctrl] = 1,
71 [snd_soc_dapm_clock_supply] = 1,
72 [snd_soc_dapm_supply] = 2,
73 [snd_soc_dapm_micbias] = 3,
74 [snd_soc_dapm_dai_link] = 2,
75 [snd_soc_dapm_dai_in] = 4,
76 [snd_soc_dapm_dai_out] = 4,
77 [snd_soc_dapm_aif_in] = 4,
78 [snd_soc_dapm_aif_out] = 4,
79 [snd_soc_dapm_mic] = 5,
80 [snd_soc_dapm_mux] = 6,
81 [snd_soc_dapm_demux] = 6,
82 [snd_soc_dapm_dac] = 7,
83 [snd_soc_dapm_switch] = 8,
84 [snd_soc_dapm_mixer] = 8,
85 [snd_soc_dapm_mixer_named_ctl] = 8,
86 [snd_soc_dapm_pga] = 9,
87 [snd_soc_dapm_adc] = 10,
88 [snd_soc_dapm_out_drv] = 11,
89 [snd_soc_dapm_hp] = 11,
90 [snd_soc_dapm_spk] = 11,
91 [snd_soc_dapm_line] = 11,
92 [snd_soc_dapm_kcontrol] = 12,
93 [snd_soc_dapm_post] = 13,
96 static int dapm_down_seq[] = {
97 [snd_soc_dapm_pre] = 0,
98 [snd_soc_dapm_kcontrol] = 1,
99 [snd_soc_dapm_adc] = 2,
100 [snd_soc_dapm_hp] = 3,
101 [snd_soc_dapm_spk] = 3,
102 [snd_soc_dapm_line] = 3,
103 [snd_soc_dapm_out_drv] = 3,
104 [snd_soc_dapm_pga] = 4,
105 [snd_soc_dapm_switch] = 5,
106 [snd_soc_dapm_mixer_named_ctl] = 5,
107 [snd_soc_dapm_mixer] = 5,
108 [snd_soc_dapm_dac] = 6,
109 [snd_soc_dapm_mic] = 7,
110 [snd_soc_dapm_micbias] = 8,
111 [snd_soc_dapm_mux] = 9,
112 [snd_soc_dapm_demux] = 9,
113 [snd_soc_dapm_aif_in] = 10,
114 [snd_soc_dapm_aif_out] = 10,
115 [snd_soc_dapm_dai_in] = 10,
116 [snd_soc_dapm_dai_out] = 10,
117 [snd_soc_dapm_dai_link] = 11,
118 [snd_soc_dapm_supply] = 12,
119 [snd_soc_dapm_clock_supply] = 13,
120 [snd_soc_dapm_pinctrl] = 13,
121 [snd_soc_dapm_regulator_supply] = 13,
122 [snd_soc_dapm_post] = 14,
125 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
127 if (dapm->card && dapm->card->instantiated)
128 lockdep_assert_held(&dapm->card->dapm_mutex);
131 static void pop_wait(u32 pop_time)
134 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
137 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
145 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
150 vsnprintf(buf, PAGE_SIZE, fmt, args);
151 dev_info(dev, "%s", buf);
157 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
159 return !list_empty(&w->dirty);
162 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
164 dapm_assert_locked(w->dapm);
166 if (!dapm_dirty_widget(w)) {
167 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
169 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
174 * Common implementation for dapm_widget_invalidate_input_paths() and
175 * dapm_widget_invalidate_output_paths(). The function is inlined since the
176 * combined size of the two specialized functions is only marginally larger then
177 * the size of the generic function and at the same time the fast path of the
178 * specialized functions is significantly smaller than the generic function.
180 static __always_inline void dapm_widget_invalidate_paths(
181 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
183 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
184 struct snd_soc_dapm_widget *node;
185 struct snd_soc_dapm_path *p;
188 dapm_assert_locked(w->dapm);
190 if (w->endpoints[dir] == -1)
193 list_add_tail(&w->work_list, &list);
194 w->endpoints[dir] = -1;
196 list_for_each_entry(w, &list, work_list) {
197 snd_soc_dapm_widget_for_each_path(w, dir, p) {
198 if (p->is_supply || p->weak || !p->connect)
200 node = p->node[rdir];
201 if (node->endpoints[dir] != -1) {
202 node->endpoints[dir] = -1;
203 list_add_tail(&node->work_list, &list);
210 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
212 * @w: The widget for which to invalidate the cached number of input paths
214 * Resets the cached number of inputs for the specified widget and all widgets
215 * that can be reached via outcoming paths from the widget.
217 * This function must be called if the number of output paths for a widget might
218 * have changed. E.g. if the source state of a widget changes or a path is added
219 * or activated with the widget as the sink.
221 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
223 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
227 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
229 * @w: The widget for which to invalidate the cached number of output paths
231 * Resets the cached number of outputs for the specified widget and all widgets
232 * that can be reached via incoming paths from the widget.
234 * This function must be called if the number of output paths for a widget might
235 * have changed. E.g. if the sink state of a widget changes or a path is added
236 * or activated with the widget as the source.
238 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
240 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
244 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
245 * for the widgets connected to a path
246 * @p: The path to invalidate
248 * Resets the cached number of inputs for the sink of the path and the cached
249 * number of outputs for the source of the path.
251 * This function must be called when a path is added, removed or the connected
254 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
257 * Weak paths or supply paths do not influence the number of input or
258 * output paths of their neighbors.
260 if (p->weak || p->is_supply)
264 * The number of connected endpoints is the sum of the number of
265 * connected endpoints of all neighbors. If a node with 0 connected
266 * endpoints is either connected or disconnected that sum won't change,
267 * so there is no need to re-check the path.
269 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
270 dapm_widget_invalidate_input_paths(p->sink);
271 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
272 dapm_widget_invalidate_output_paths(p->source);
275 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
277 struct snd_soc_dapm_widget *w;
279 mutex_lock(&card->dapm_mutex);
281 list_for_each_entry(w, &card->widgets, list) {
283 dapm_mark_dirty(w, "Rechecking endpoints");
284 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
285 dapm_widget_invalidate_output_paths(w);
286 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
287 dapm_widget_invalidate_input_paths(w);
291 mutex_unlock(&card->dapm_mutex);
293 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
295 /* create a new dapm widget */
296 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
297 const struct snd_soc_dapm_widget *_widget)
299 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
302 struct dapm_kcontrol_data {
304 struct snd_soc_dapm_widget *widget;
305 struct list_head paths;
306 struct snd_soc_dapm_widget_list *wlist;
309 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
310 struct snd_kcontrol *kcontrol, const char *ctrl_name)
312 struct dapm_kcontrol_data *data;
313 struct soc_mixer_control *mc;
318 data = kzalloc(sizeof(*data), GFP_KERNEL);
322 INIT_LIST_HEAD(&data->paths);
324 switch (widget->id) {
325 case snd_soc_dapm_switch:
326 case snd_soc_dapm_mixer:
327 case snd_soc_dapm_mixer_named_ctl:
328 mc = (struct soc_mixer_control *)kcontrol->private_value;
330 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
331 dev_warn(widget->dapm->dev,
332 "ASoC: Unsupported stereo autodisable control '%s'\n",
335 if (mc->autodisable) {
336 struct snd_soc_dapm_widget template;
338 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
345 memset(&template, 0, sizeof(template));
346 template.reg = mc->reg;
347 template.mask = (1 << fls(mc->max)) - 1;
348 template.shift = mc->shift;
350 template.off_val = mc->max;
352 template.off_val = 0;
353 template.on_val = template.off_val;
354 template.id = snd_soc_dapm_kcontrol;
355 template.name = name;
357 data->value = template.on_val;
360 snd_soc_dapm_new_control_unlocked(widget->dapm,
363 if (IS_ERR(data->widget)) {
364 ret = PTR_ERR(data->widget);
373 case snd_soc_dapm_demux:
374 case snd_soc_dapm_mux:
375 e = (struct soc_enum *)kcontrol->private_value;
377 if (e->autodisable) {
378 struct snd_soc_dapm_widget template;
380 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
387 memset(&template, 0, sizeof(template));
388 template.reg = e->reg;
389 template.mask = e->mask << e->shift_l;
390 template.shift = e->shift_l;
391 template.off_val = snd_soc_enum_item_to_val(e, 0);
392 template.on_val = template.off_val;
393 template.id = snd_soc_dapm_kcontrol;
394 template.name = name;
396 data->value = template.on_val;
398 data->widget = snd_soc_dapm_new_control_unlocked(
399 widget->dapm, &template);
401 if (IS_ERR(data->widget)) {
402 ret = PTR_ERR(data->widget);
410 snd_soc_dapm_add_path(widget->dapm, data->widget,
418 kcontrol->private_data = data;
427 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
429 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
431 list_del(&data->paths);
436 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
437 const struct snd_kcontrol *kcontrol)
439 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
444 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
445 struct snd_soc_dapm_widget *widget)
447 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
448 struct snd_soc_dapm_widget_list *new_wlist;
452 n = data->wlist->num_widgets + 1;
456 new_wlist = krealloc(data->wlist,
457 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
461 new_wlist->widgets[n - 1] = widget;
462 new_wlist->num_widgets = n;
464 data->wlist = new_wlist;
469 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
470 struct snd_soc_dapm_path *path)
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
474 list_add_tail(&path->list_kcontrol, &data->paths);
477 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
479 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
484 return data->widget->power;
487 static struct list_head *dapm_kcontrol_get_path_list(
488 const struct snd_kcontrol *kcontrol)
490 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
495 #define dapm_kcontrol_for_each_path(path, kcontrol) \
496 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
499 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
501 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
505 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
507 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
510 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
512 if (data->value == value)
516 data->widget->on_val = value;
524 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
526 * @kcontrol: The kcontrol
528 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
529 struct snd_kcontrol *kcontrol)
531 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
533 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
536 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
538 * @kcontrol: The kcontrol
540 * Note: This function must only be used on kcontrols that are known to have
541 * been registered for a CODEC. Otherwise the behaviour is undefined.
543 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
544 struct snd_kcontrol *kcontrol)
546 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
548 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
550 static void dapm_reset(struct snd_soc_card *card)
552 struct snd_soc_dapm_widget *w;
554 lockdep_assert_held(&card->dapm_mutex);
556 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
558 list_for_each_entry(w, &card->widgets, list) {
559 w->new_power = w->power;
560 w->power_checked = false;
564 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
566 if (!dapm->component)
568 return dapm->component->name_prefix;
571 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
574 if (!dapm->component)
576 return snd_soc_component_read(dapm->component, reg, value);
579 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
580 int reg, unsigned int mask, unsigned int value)
582 if (!dapm->component)
584 return snd_soc_component_update_bits(dapm->component, reg,
588 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
589 int reg, unsigned int mask, unsigned int value)
591 if (!dapm->component)
593 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
596 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
599 snd_soc_component_async_complete(dapm->component);
602 static struct snd_soc_dapm_widget *
603 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
605 struct snd_soc_dapm_widget *w = wcache->widget;
606 struct list_head *wlist;
611 wlist = &w->dapm->card->widgets;
613 list_for_each_entry_from(w, wlist, list) {
614 if (!strcmp(name, w->name))
625 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
626 struct snd_soc_dapm_widget *w)
632 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
633 * @dapm: The DAPM context for which to set the level
634 * @level: The level to set
636 * Forces the DAPM bias level to a specific state. It will call the bias level
637 * callback of DAPM context with the specified level. This will even happen if
638 * the context is already at the same level. Furthermore it will not go through
639 * the normal bias level sequencing, meaning any intermediate states between the
640 * current and the target state will not be entered.
642 * Note that the change in bias level is only temporary and the next time
643 * snd_soc_dapm_sync() is called the state will be set to the level as
644 * determined by the DAPM core. The function is mainly intended to be used to
645 * used during probe or resume from suspend to power up the device so
646 * initialization can be done, before the DAPM core takes over.
648 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
649 enum snd_soc_bias_level level)
653 if (dapm->set_bias_level)
654 ret = dapm->set_bias_level(dapm, level);
657 dapm->bias_level = level;
661 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
664 * snd_soc_dapm_set_bias_level - set the bias level for the system
665 * @dapm: DAPM context
666 * @level: level to configure
668 * Configure the bias (power) levels for the SoC audio device.
670 * Returns 0 for success else error.
672 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
673 enum snd_soc_bias_level level)
675 struct snd_soc_card *card = dapm->card;
678 trace_snd_soc_bias_level_start(card, level);
680 if (card && card->set_bias_level)
681 ret = card->set_bias_level(card, dapm, level);
685 if (!card || dapm != &card->dapm)
686 ret = snd_soc_dapm_force_bias_level(dapm, level);
691 if (card && card->set_bias_level_post)
692 ret = card->set_bias_level_post(card, dapm, level);
694 trace_snd_soc_bias_level_done(card, level);
699 /* connect mux widget to its interconnecting audio paths */
700 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
701 struct snd_soc_dapm_path *path, const char *control_name,
702 struct snd_soc_dapm_widget *w)
704 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
705 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
706 unsigned int val, item;
709 if (e->reg != SND_SOC_NOPM) {
710 soc_dapm_read(dapm, e->reg, &val);
711 val = (val >> e->shift_l) & e->mask;
712 item = snd_soc_enum_val_to_item(e, val);
714 /* since a virtual mux has no backing registers to
715 * decide which path to connect, it will try to match
716 * with the first enumeration. This is to ensure
717 * that the default mux choice (the first) will be
718 * correctly powered up during initialization.
723 i = match_string(e->texts, e->items, control_name);
727 path->name = e->texts[i];
728 path->connect = (i == item);
733 /* set up initial codec paths */
734 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
737 struct soc_mixer_control *mc = (struct soc_mixer_control *)
738 p->sink->kcontrol_news[i].private_value;
739 unsigned int reg = mc->reg;
740 unsigned int shift = mc->shift;
741 unsigned int max = mc->max;
742 unsigned int mask = (1 << fls(max)) - 1;
743 unsigned int invert = mc->invert;
746 if (reg != SND_SOC_NOPM) {
747 soc_dapm_read(p->sink->dapm, reg, &val);
749 * The nth_path argument allows this function to know
750 * which path of a kcontrol it is setting the initial
751 * status for. Ideally this would support any number
752 * of paths and channels. But since kcontrols only come
753 * in mono and stereo variants, we are limited to 2
756 * The following code assumes for stereo controls the
757 * first path is the left channel, and all remaining
758 * paths are the right channel.
760 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
762 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
763 val = (val >> mc->rshift) & mask;
765 val = (val >> shift) & mask;
775 /* connect mixer widget to its interconnecting audio paths */
776 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
777 struct snd_soc_dapm_path *path, const char *control_name)
781 /* search for mixer kcontrol */
782 for (i = 0; i < path->sink->num_kcontrols; i++) {
783 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
784 path->name = path->sink->kcontrol_news[i].name;
785 dapm_set_mixer_path_status(path, i, nth_path++);
792 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
793 struct snd_soc_dapm_widget *kcontrolw,
794 const struct snd_kcontrol_new *kcontrol_new,
795 struct snd_kcontrol **kcontrol)
797 struct snd_soc_dapm_widget *w;
802 list_for_each_entry(w, &dapm->card->widgets, list) {
803 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
805 for (i = 0; i < w->num_kcontrols; i++) {
806 if (&w->kcontrol_news[i] == kcontrol_new) {
808 *kcontrol = w->kcontrols[i];
818 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
819 * create it. Either way, add the widget into the control's widget list
821 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
824 struct snd_soc_dapm_context *dapm = w->dapm;
825 struct snd_card *card = dapm->card->snd_card;
829 struct snd_kcontrol *kcontrol;
830 bool wname_in_long_name, kcname_in_long_name;
831 char *long_name = NULL;
835 prefix = soc_dapm_prefix(dapm);
837 prefix_len = strlen(prefix) + 1;
841 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
846 wname_in_long_name = false;
847 kcname_in_long_name = true;
850 case snd_soc_dapm_switch:
851 case snd_soc_dapm_mixer:
852 case snd_soc_dapm_pga:
853 case snd_soc_dapm_out_drv:
854 wname_in_long_name = true;
855 kcname_in_long_name = true;
857 case snd_soc_dapm_mixer_named_ctl:
858 wname_in_long_name = false;
859 kcname_in_long_name = true;
861 case snd_soc_dapm_demux:
862 case snd_soc_dapm_mux:
863 wname_in_long_name = true;
864 kcname_in_long_name = false;
871 if (wname_in_long_name && kcname_in_long_name) {
873 * The control will get a prefix from the control
874 * creation process but we're also using the same
875 * prefix for widgets so cut the prefix off the
876 * front of the widget name.
878 long_name = kasprintf(GFP_KERNEL, "%s %s",
879 w->name + prefix_len,
880 w->kcontrol_news[kci].name);
881 if (long_name == NULL)
885 } else if (wname_in_long_name) {
887 name = w->name + prefix_len;
890 name = w->kcontrol_news[kci].name;
893 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
900 kcontrol->private_free = dapm_kcontrol_free;
902 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
904 snd_ctl_free_one(kcontrol);
908 ret = snd_ctl_add(card, kcontrol);
911 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
917 ret = dapm_kcontrol_add_widget(kcontrol, w);
919 w->kcontrols[kci] = kcontrol;
927 /* create new dapm mixer control */
928 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
931 struct snd_soc_dapm_path *path;
932 struct dapm_kcontrol_data *data;
935 for (i = 0; i < w->num_kcontrols; i++) {
937 snd_soc_dapm_widget_for_each_source_path(w, path) {
938 /* mixer/mux paths name must match control name */
939 if (path->name != (char *)w->kcontrol_news[i].name)
942 if (!w->kcontrols[i]) {
943 ret = dapm_create_or_share_kcontrol(w, i);
948 dapm_kcontrol_add_path(w->kcontrols[i], path);
950 data = snd_kcontrol_chip(w->kcontrols[i]);
952 snd_soc_dapm_add_path(data->widget->dapm,
962 /* create new dapm mux control */
963 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
965 struct snd_soc_dapm_context *dapm = w->dapm;
966 enum snd_soc_dapm_direction dir;
967 struct snd_soc_dapm_path *path;
972 case snd_soc_dapm_mux:
973 dir = SND_SOC_DAPM_DIR_OUT;
976 case snd_soc_dapm_demux:
977 dir = SND_SOC_DAPM_DIR_IN;
984 if (w->num_kcontrols != 1) {
986 "ASoC: %s %s has incorrect number of controls\n", type,
991 if (list_empty(&w->edges[dir])) {
992 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
996 ret = dapm_create_or_share_kcontrol(w, 0);
1000 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1002 dapm_kcontrol_add_path(w->kcontrols[0], path);
1008 /* create new dapm volume control */
1009 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1013 for (i = 0; i < w->num_kcontrols; i++) {
1014 ret = dapm_create_or_share_kcontrol(w, i);
1022 /* create new dapm dai link control */
1023 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1026 struct snd_kcontrol *kcontrol;
1027 struct snd_soc_dapm_context *dapm = w->dapm;
1028 struct snd_card *card = dapm->card->snd_card;
1030 /* create control for links with > 1 config */
1031 if (w->num_params <= 1)
1035 for (i = 0; i < w->num_kcontrols; i++) {
1036 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1038 ret = snd_ctl_add(card, kcontrol);
1041 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1042 w->name, w->kcontrol_news[i].name, ret);
1045 kcontrol->private_data = w;
1046 w->kcontrols[i] = kcontrol;
1052 /* We implement power down on suspend by checking the power state of
1053 * the ALSA card - when we are suspending the ALSA state for the card
1056 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1058 int level = snd_power_get_state(widget->dapm->card->snd_card);
1061 case SNDRV_CTL_POWER_D3hot:
1062 case SNDRV_CTL_POWER_D3cold:
1063 if (widget->ignore_suspend)
1064 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1066 return widget->ignore_suspend;
1072 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1073 struct list_head *widgets)
1075 struct snd_soc_dapm_widget *w;
1076 struct list_head *it;
1077 unsigned int size = 0;
1080 list_for_each(it, widgets)
1083 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1087 list_for_each_entry(w, widgets, work_list)
1088 (*list)->widgets[i++] = w;
1090 (*list)->num_widgets = i;
1096 * Common implementation for is_connected_output_ep() and
1097 * is_connected_input_ep(). The function is inlined since the combined size of
1098 * the two specialized functions is only marginally larger then the size of the
1099 * generic function and at the same time the fast path of the specialized
1100 * functions is significantly smaller than the generic function.
1102 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1103 struct list_head *list, enum snd_soc_dapm_direction dir,
1104 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1105 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1106 enum snd_soc_dapm_direction)),
1107 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1108 enum snd_soc_dapm_direction))
1110 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1111 struct snd_soc_dapm_path *path;
1114 if (widget->endpoints[dir] >= 0)
1115 return widget->endpoints[dir];
1117 DAPM_UPDATE_STAT(widget, path_checks);
1119 /* do we need to add this widget to the list ? */
1121 list_add_tail(&widget->work_list, list);
1123 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1124 widget->endpoints[dir] = 1;
1125 return widget->endpoints[dir];
1128 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1129 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1130 return widget->endpoints[dir];
1133 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1134 DAPM_UPDATE_STAT(widget, neighbour_checks);
1136 if (path->weak || path->is_supply)
1142 trace_snd_soc_dapm_path(widget, dir, path);
1144 if (path->connect) {
1146 con += fn(path->node[dir], list, custom_stop_condition);
1151 widget->endpoints[dir] = con;
1157 * Recursively check for a completed path to an active or physically connected
1158 * output widget. Returns number of complete paths.
1160 * Optionally, can be supplied with a function acting as a stopping condition.
1161 * This function takes the dapm widget currently being examined and the walk
1162 * direction as an arguments, it should return true if the walk should be
1163 * stopped and false otherwise.
1165 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1166 struct list_head *list,
1167 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1168 enum snd_soc_dapm_direction))
1170 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1171 is_connected_output_ep, custom_stop_condition);
1175 * Recursively check for a completed path to an active or physically connected
1176 * input widget. Returns number of complete paths.
1178 * Optionally, can be supplied with a function acting as a stopping condition.
1179 * This function takes the dapm widget currently being examined and the walk
1180 * direction as an arguments, it should return true if the walk should be
1181 * stopped and false otherwise.
1183 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1184 struct list_head *list,
1185 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1186 enum snd_soc_dapm_direction))
1188 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1189 is_connected_input_ep, custom_stop_condition);
1193 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1194 * @dai: the soc DAI.
1195 * @stream: stream direction.
1196 * @list: list of active widgets for this stream.
1197 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1198 * walk based on custom logic.
1200 * Queries DAPM graph as to whether a valid audio stream path exists for
1201 * the initial stream specified by name. This takes into account
1202 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1204 * Optionally, can be supplied with a function acting as a stopping condition.
1205 * This function takes the dapm widget currently being examined and the walk
1206 * direction as an arguments, it should return true if the walk should be
1207 * stopped and false otherwise.
1209 * Returns the number of valid paths or negative error.
1211 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1212 struct snd_soc_dapm_widget_list **list,
1213 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1214 enum snd_soc_dapm_direction))
1216 struct snd_soc_card *card = dai->component->card;
1217 struct snd_soc_dapm_widget *w;
1222 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1225 * For is_connected_{output,input}_ep fully discover the graph we need
1226 * to reset the cached number of inputs and outputs.
1228 list_for_each_entry(w, &card->widgets, list) {
1229 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1230 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1233 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1234 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1235 custom_stop_condition);
1237 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1238 custom_stop_condition);
1240 /* Drop starting point */
1241 list_del(widgets.next);
1243 ret = dapm_widget_list_create(list, &widgets);
1247 trace_snd_soc_dapm_connected(paths, stream);
1248 mutex_unlock(&card->dapm_mutex);
1254 * Handler for regulator supply widget.
1256 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1257 struct snd_kcontrol *kcontrol, int event)
1261 soc_dapm_async_complete(w->dapm);
1263 if (SND_SOC_DAPM_EVENT_ON(event)) {
1264 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1265 ret = regulator_allow_bypass(w->regulator, false);
1267 dev_warn(w->dapm->dev,
1268 "ASoC: Failed to unbypass %s: %d\n",
1272 return regulator_enable(w->regulator);
1274 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1275 ret = regulator_allow_bypass(w->regulator, true);
1277 dev_warn(w->dapm->dev,
1278 "ASoC: Failed to bypass %s: %d\n",
1282 return regulator_disable_deferred(w->regulator, w->shift);
1285 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1288 * Handler for pinctrl widget.
1290 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1291 struct snd_kcontrol *kcontrol, int event)
1293 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1294 struct pinctrl *p = w->pinctrl;
1295 struct pinctrl_state *s;
1300 if (SND_SOC_DAPM_EVENT_ON(event))
1301 s = pinctrl_lookup_state(p, priv->active_state);
1303 s = pinctrl_lookup_state(p, priv->sleep_state);
1308 return pinctrl_select_state(p, s);
1310 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1313 * Handler for clock supply widget.
1315 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1316 struct snd_kcontrol *kcontrol, int event)
1321 soc_dapm_async_complete(w->dapm);
1323 #ifdef CONFIG_HAVE_CLK
1324 if (SND_SOC_DAPM_EVENT_ON(event)) {
1325 return clk_prepare_enable(w->clk);
1327 clk_disable_unprepare(w->clk);
1333 EXPORT_SYMBOL_GPL(dapm_clock_event);
1335 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1337 if (w->power_checked)
1338 return w->new_power;
1343 w->new_power = w->power_check(w);
1345 w->power_checked = true;
1347 return w->new_power;
1350 /* Generic check to see if a widget should be powered. */
1351 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1355 DAPM_UPDATE_STAT(w, power_checks);
1357 in = is_connected_input_ep(w, NULL, NULL);
1358 out = is_connected_output_ep(w, NULL, NULL);
1359 return out != 0 && in != 0;
1362 /* Check to see if a power supply is needed */
1363 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1365 struct snd_soc_dapm_path *path;
1367 DAPM_UPDATE_STAT(w, power_checks);
1369 /* Check if one of our outputs is connected */
1370 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1371 DAPM_UPDATE_STAT(w, neighbour_checks);
1376 if (path->connected &&
1377 !path->connected(path->source, path->sink))
1380 if (dapm_widget_power_check(path->sink))
1387 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1389 return w->connected;
1392 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1393 struct snd_soc_dapm_widget *b,
1401 sort = dapm_down_seq;
1403 if (sort[a->id] != sort[b->id])
1404 return sort[a->id] - sort[b->id];
1405 if (a->subseq != b->subseq) {
1407 return a->subseq - b->subseq;
1409 return b->subseq - a->subseq;
1411 if (a->reg != b->reg)
1412 return a->reg - b->reg;
1413 if (a->dapm != b->dapm)
1414 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1419 /* Insert a widget in order into a DAPM power sequence. */
1420 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1421 struct list_head *list,
1424 struct snd_soc_dapm_widget *w;
1426 list_for_each_entry(w, list, power_list)
1427 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1428 list_add_tail(&new_widget->power_list, &w->power_list);
1432 list_add_tail(&new_widget->power_list, list);
1435 static void dapm_seq_check_event(struct snd_soc_card *card,
1436 struct snd_soc_dapm_widget *w, int event)
1438 const char *ev_name;
1442 case SND_SOC_DAPM_PRE_PMU:
1443 ev_name = "PRE_PMU";
1446 case SND_SOC_DAPM_POST_PMU:
1447 ev_name = "POST_PMU";
1450 case SND_SOC_DAPM_PRE_PMD:
1451 ev_name = "PRE_PMD";
1454 case SND_SOC_DAPM_POST_PMD:
1455 ev_name = "POST_PMD";
1458 case SND_SOC_DAPM_WILL_PMU:
1459 ev_name = "WILL_PMU";
1462 case SND_SOC_DAPM_WILL_PMD:
1463 ev_name = "WILL_PMD";
1467 WARN(1, "Unknown event %d\n", event);
1471 if (w->new_power != power)
1474 if (w->event && (w->event_flags & event)) {
1475 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1477 soc_dapm_async_complete(w->dapm);
1478 trace_snd_soc_dapm_widget_event_start(w, event);
1479 ret = w->event(w, NULL, event);
1480 trace_snd_soc_dapm_widget_event_done(w, event);
1482 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1483 ev_name, w->name, ret);
1487 /* Apply the coalesced changes from a DAPM sequence */
1488 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1489 struct list_head *pending)
1491 struct snd_soc_dapm_context *dapm;
1492 struct snd_soc_dapm_widget *w;
1494 unsigned int value = 0;
1495 unsigned int mask = 0;
1497 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1501 list_for_each_entry(w, pending, power_list) {
1502 WARN_ON(reg != w->reg || dapm != w->dapm);
1503 w->power = w->new_power;
1505 mask |= w->mask << w->shift;
1507 value |= w->on_val << w->shift;
1509 value |= w->off_val << w->shift;
1511 pop_dbg(dapm->dev, card->pop_time,
1512 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1513 w->name, reg, value, mask);
1515 /* Check for events */
1516 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1517 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1521 /* Any widget will do, they should all be updating the
1525 pop_dbg(dapm->dev, card->pop_time,
1526 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1527 value, mask, reg, card->pop_time);
1528 pop_wait(card->pop_time);
1529 soc_dapm_update_bits(dapm, reg, mask, value);
1532 list_for_each_entry(w, pending, power_list) {
1533 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1534 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1538 /* Apply a DAPM power sequence.
1540 * We walk over a pre-sorted list of widgets to apply power to. In
1541 * order to minimise the number of writes to the device required
1542 * multiple widgets will be updated in a single write where possible.
1543 * Currently anything that requires more than a single write is not
1546 static void dapm_seq_run(struct snd_soc_card *card,
1547 struct list_head *list, int event, bool power_up)
1549 struct snd_soc_dapm_widget *w, *n;
1550 struct snd_soc_dapm_context *d;
1553 int cur_subseq = -1;
1554 int cur_reg = SND_SOC_NOPM;
1555 struct snd_soc_dapm_context *cur_dapm = NULL;
1562 sort = dapm_down_seq;
1564 list_for_each_entry_safe(w, n, list, power_list) {
1567 /* Do we need to apply any queued changes? */
1568 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1569 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1570 if (!list_empty(&pending))
1571 dapm_seq_run_coalesced(card, &pending);
1573 if (cur_dapm && cur_dapm->seq_notifier) {
1574 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1575 if (sort[i] == cur_sort)
1576 cur_dapm->seq_notifier(cur_dapm,
1581 if (cur_dapm && w->dapm != cur_dapm)
1582 soc_dapm_async_complete(cur_dapm);
1584 INIT_LIST_HEAD(&pending);
1586 cur_subseq = INT_MIN;
1587 cur_reg = SND_SOC_NOPM;
1592 case snd_soc_dapm_pre:
1594 list_for_each_entry_safe_continue(w, n, list,
1597 if (event == SND_SOC_DAPM_STREAM_START)
1599 NULL, SND_SOC_DAPM_PRE_PMU);
1600 else if (event == SND_SOC_DAPM_STREAM_STOP)
1602 NULL, SND_SOC_DAPM_PRE_PMD);
1605 case snd_soc_dapm_post:
1607 list_for_each_entry_safe_continue(w, n, list,
1610 if (event == SND_SOC_DAPM_STREAM_START)
1612 NULL, SND_SOC_DAPM_POST_PMU);
1613 else if (event == SND_SOC_DAPM_STREAM_STOP)
1615 NULL, SND_SOC_DAPM_POST_PMD);
1619 /* Queue it up for application */
1620 cur_sort = sort[w->id];
1621 cur_subseq = w->subseq;
1624 list_move(&w->power_list, &pending);
1629 dev_err(w->dapm->dev,
1630 "ASoC: Failed to apply widget power: %d\n", ret);
1633 if (!list_empty(&pending))
1634 dapm_seq_run_coalesced(card, &pending);
1636 if (cur_dapm && cur_dapm->seq_notifier) {
1637 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1638 if (sort[i] == cur_sort)
1639 cur_dapm->seq_notifier(cur_dapm,
1643 list_for_each_entry(d, &card->dapm_list, list) {
1644 soc_dapm_async_complete(d);
1648 static void dapm_widget_update(struct snd_soc_card *card)
1650 struct snd_soc_dapm_update *update = card->update;
1651 struct snd_soc_dapm_widget_list *wlist;
1652 struct snd_soc_dapm_widget *w = NULL;
1656 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1659 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1661 for (wi = 0; wi < wlist->num_widgets; wi++) {
1662 w = wlist->widgets[wi];
1664 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1665 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1667 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1675 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1678 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1681 if (update->has_second_set) {
1682 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1683 update->mask2, update->val2);
1685 dev_err(w->dapm->dev,
1686 "ASoC: %s DAPM update failed: %d\n",
1690 for (wi = 0; wi < wlist->num_widgets; wi++) {
1691 w = wlist->widgets[wi];
1693 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1694 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1696 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1702 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1703 * they're changing state.
1705 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1707 struct snd_soc_dapm_context *d = data;
1710 /* If we're off and we're not supposed to go into STANDBY */
1711 if (d->bias_level == SND_SOC_BIAS_OFF &&
1712 d->target_bias_level != SND_SOC_BIAS_OFF) {
1714 pm_runtime_get_sync(d->dev);
1716 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1719 "ASoC: Failed to turn on bias: %d\n", ret);
1722 /* Prepare for a transition to ON or away from ON */
1723 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1724 d->bias_level != SND_SOC_BIAS_ON) ||
1725 (d->target_bias_level != SND_SOC_BIAS_ON &&
1726 d->bias_level == SND_SOC_BIAS_ON)) {
1727 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1730 "ASoC: Failed to prepare bias: %d\n", ret);
1734 /* Async callback run prior to DAPM sequences - brings to their final
1737 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1739 struct snd_soc_dapm_context *d = data;
1742 /* If we just powered the last thing off drop to standby bias */
1743 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1744 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1745 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1746 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1748 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1752 /* If we're in standby and can support bias off then do that */
1753 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1754 d->target_bias_level == SND_SOC_BIAS_OFF) {
1755 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1757 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1761 pm_runtime_put(d->dev);
1764 /* If we just powered up then move to active bias */
1765 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1766 d->target_bias_level == SND_SOC_BIAS_ON) {
1767 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1769 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1774 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1775 bool power, bool connect)
1777 /* If a connection is being made or broken then that update
1778 * will have marked the peer dirty, otherwise the widgets are
1779 * not connected and this update has no impact. */
1783 /* If the peer is already in the state we're moving to then we
1784 * won't have an impact on it. */
1785 if (power != peer->power)
1786 dapm_mark_dirty(peer, "peer state change");
1789 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1790 struct list_head *up_list,
1791 struct list_head *down_list)
1793 struct snd_soc_dapm_path *path;
1795 if (w->power == power)
1798 trace_snd_soc_dapm_widget_power(w, power);
1800 /* If we changed our power state perhaps our neigbours changed
1803 snd_soc_dapm_widget_for_each_source_path(w, path)
1804 dapm_widget_set_peer_power(path->source, power, path->connect);
1806 /* Supplies can't affect their outputs, only their inputs */
1807 if (!w->is_supply) {
1808 snd_soc_dapm_widget_for_each_sink_path(w, path)
1809 dapm_widget_set_peer_power(path->sink, power,
1814 dapm_seq_insert(w, up_list, true);
1816 dapm_seq_insert(w, down_list, false);
1819 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1820 struct list_head *up_list,
1821 struct list_head *down_list)
1826 case snd_soc_dapm_pre:
1827 dapm_seq_insert(w, down_list, false);
1829 case snd_soc_dapm_post:
1830 dapm_seq_insert(w, up_list, true);
1834 power = dapm_widget_power_check(w);
1836 dapm_widget_set_power(w, power, up_list, down_list);
1841 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1843 if (dapm->idle_bias_off)
1846 switch (snd_power_get_state(dapm->card->snd_card)) {
1847 case SNDRV_CTL_POWER_D3hot:
1848 case SNDRV_CTL_POWER_D3cold:
1849 return dapm->suspend_bias_off;
1858 * Scan each dapm widget for complete audio path.
1859 * A complete path is a route that has valid endpoints i.e.:-
1861 * o DAC to output pin.
1862 * o Input pin to ADC.
1863 * o Input pin to Output pin (bypass, sidetone)
1864 * o DAC to ADC (loopback).
1866 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1868 struct snd_soc_dapm_widget *w;
1869 struct snd_soc_dapm_context *d;
1871 LIST_HEAD(down_list);
1872 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1873 enum snd_soc_bias_level bias;
1875 lockdep_assert_held(&card->dapm_mutex);
1877 trace_snd_soc_dapm_start(card);
1879 list_for_each_entry(d, &card->dapm_list, list) {
1880 if (dapm_idle_bias_off(d))
1881 d->target_bias_level = SND_SOC_BIAS_OFF;
1883 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1888 /* Check which widgets we need to power and store them in
1889 * lists indicating if they should be powered up or down. We
1890 * only check widgets that have been flagged as dirty but note
1891 * that new widgets may be added to the dirty list while we
1894 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1895 dapm_power_one_widget(w, &up_list, &down_list);
1898 list_for_each_entry(w, &card->widgets, list) {
1900 case snd_soc_dapm_pre:
1901 case snd_soc_dapm_post:
1902 /* These widgets always need to be powered */
1905 list_del_init(&w->dirty);
1912 /* Supplies and micbiases only bring the
1913 * context up to STANDBY as unless something
1914 * else is active and passing audio they
1915 * generally don't require full power. Signal
1916 * generators are virtual pins and have no
1917 * power impact themselves.
1920 case snd_soc_dapm_siggen:
1921 case snd_soc_dapm_vmid:
1923 case snd_soc_dapm_supply:
1924 case snd_soc_dapm_regulator_supply:
1925 case snd_soc_dapm_pinctrl:
1926 case snd_soc_dapm_clock_supply:
1927 case snd_soc_dapm_micbias:
1928 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1929 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1932 d->target_bias_level = SND_SOC_BIAS_ON;
1939 /* Force all contexts in the card to the same bias state if
1940 * they're not ground referenced.
1942 bias = SND_SOC_BIAS_OFF;
1943 list_for_each_entry(d, &card->dapm_list, list)
1944 if (d->target_bias_level > bias)
1945 bias = d->target_bias_level;
1946 list_for_each_entry(d, &card->dapm_list, list)
1947 if (!dapm_idle_bias_off(d))
1948 d->target_bias_level = bias;
1950 trace_snd_soc_dapm_walk_done(card);
1952 /* Run card bias changes at first */
1953 dapm_pre_sequence_async(&card->dapm, 0);
1954 /* Run other bias changes in parallel */
1955 list_for_each_entry(d, &card->dapm_list, list) {
1956 if (d != &card->dapm)
1957 async_schedule_domain(dapm_pre_sequence_async, d,
1960 async_synchronize_full_domain(&async_domain);
1962 list_for_each_entry(w, &down_list, power_list) {
1963 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1966 list_for_each_entry(w, &up_list, power_list) {
1967 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1970 /* Power down widgets first; try to avoid amplifying pops. */
1971 dapm_seq_run(card, &down_list, event, false);
1973 dapm_widget_update(card);
1976 dapm_seq_run(card, &up_list, event, true);
1978 /* Run all the bias changes in parallel */
1979 list_for_each_entry(d, &card->dapm_list, list) {
1980 if (d != &card->dapm)
1981 async_schedule_domain(dapm_post_sequence_async, d,
1984 async_synchronize_full_domain(&async_domain);
1985 /* Run card bias changes at last */
1986 dapm_post_sequence_async(&card->dapm, 0);
1988 /* do we need to notify any clients that DAPM event is complete */
1989 list_for_each_entry(d, &card->dapm_list, list) {
1990 if (d->stream_event)
1991 d->stream_event(d, event);
1994 pop_dbg(card->dev, card->pop_time,
1995 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1996 pop_wait(card->pop_time);
1998 trace_snd_soc_dapm_done(card);
2003 #ifdef CONFIG_DEBUG_FS
2004 static ssize_t dapm_widget_power_read_file(struct file *file,
2005 char __user *user_buf,
2006 size_t count, loff_t *ppos)
2008 struct snd_soc_dapm_widget *w = file->private_data;
2009 struct snd_soc_card *card = w->dapm->card;
2010 enum snd_soc_dapm_direction dir, rdir;
2014 struct snd_soc_dapm_path *p = NULL;
2016 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2020 mutex_lock(&card->dapm_mutex);
2022 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2027 in = is_connected_input_ep(w, NULL, NULL);
2028 out = is_connected_output_ep(w, NULL, NULL);
2031 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2032 w->name, w->power ? "On" : "Off",
2033 w->force ? " (forced)" : "", in, out);
2036 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2037 " - R%d(0x%x) mask 0x%x",
2038 w->reg, w->reg, w->mask << w->shift);
2040 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
2043 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2045 w->active ? "active" : "inactive");
2047 snd_soc_dapm_for_each_direction(dir) {
2048 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2049 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2050 if (p->connected && !p->connected(p->source, p->sink))
2056 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2057 " %s \"%s\" \"%s\"\n",
2058 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2059 p->name ? p->name : "static",
2060 p->node[rdir]->name);
2064 mutex_unlock(&card->dapm_mutex);
2066 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2072 static const struct file_operations dapm_widget_power_fops = {
2073 .open = simple_open,
2074 .read = dapm_widget_power_read_file,
2075 .llseek = default_llseek,
2078 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2079 size_t count, loff_t *ppos)
2081 struct snd_soc_dapm_context *dapm = file->private_data;
2084 switch (dapm->bias_level) {
2085 case SND_SOC_BIAS_ON:
2088 case SND_SOC_BIAS_PREPARE:
2089 level = "Prepare\n";
2091 case SND_SOC_BIAS_STANDBY:
2092 level = "Standby\n";
2094 case SND_SOC_BIAS_OFF:
2098 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2099 level = "Unknown\n";
2103 return simple_read_from_buffer(user_buf, count, ppos, level,
2107 static const struct file_operations dapm_bias_fops = {
2108 .open = simple_open,
2109 .read = dapm_bias_read_file,
2110 .llseek = default_llseek,
2113 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2114 struct dentry *parent)
2121 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2123 if (!dapm->debugfs_dapm) {
2125 "ASoC: Failed to create DAPM debugfs directory\n");
2129 d = debugfs_create_file("bias_level", 0444,
2130 dapm->debugfs_dapm, dapm,
2134 "ASoC: Failed to create bias level debugfs file\n");
2137 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2139 struct snd_soc_dapm_context *dapm = w->dapm;
2142 if (!dapm->debugfs_dapm || !w->name)
2145 d = debugfs_create_file(w->name, 0444,
2146 dapm->debugfs_dapm, w,
2147 &dapm_widget_power_fops);
2149 dev_warn(w->dapm->dev,
2150 "ASoC: Failed to create %s debugfs file\n",
2154 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2156 debugfs_remove_recursive(dapm->debugfs_dapm);
2160 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2161 struct dentry *parent)
2165 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2169 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2176 * soc_dapm_connect_path() - Connects or disconnects a path
2177 * @path: The path to update
2178 * @connect: The new connect state of the path. True if the path is connected,
2179 * false if it is disconnected.
2180 * @reason: The reason why the path changed (for debugging only)
2182 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2183 bool connect, const char *reason)
2185 if (path->connect == connect)
2188 path->connect = connect;
2189 dapm_mark_dirty(path->source, reason);
2190 dapm_mark_dirty(path->sink, reason);
2191 dapm_path_invalidate(path);
2194 /* test and update the power status of a mux widget */
2195 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2196 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2198 struct snd_soc_dapm_path *path;
2202 lockdep_assert_held(&card->dapm_mutex);
2204 /* find dapm widget path assoc with kcontrol */
2205 dapm_kcontrol_for_each_path(path, kcontrol) {
2207 /* we now need to match the string in the enum to the path */
2208 if (!(strcmp(path->name, e->texts[mux])))
2213 soc_dapm_connect_path(path, connect, "mux update");
2217 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2222 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2223 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2224 struct snd_soc_dapm_update *update)
2226 struct snd_soc_card *card = dapm->card;
2229 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2230 card->update = update;
2231 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2232 card->update = NULL;
2233 mutex_unlock(&card->dapm_mutex);
2235 soc_dpcm_runtime_update(card);
2238 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2240 /* test and update the power status of a mixer or switch widget */
2241 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2242 struct snd_kcontrol *kcontrol,
2243 int connect, int rconnect)
2245 struct snd_soc_dapm_path *path;
2248 lockdep_assert_held(&card->dapm_mutex);
2250 /* find dapm widget path assoc with kcontrol */
2251 dapm_kcontrol_for_each_path(path, kcontrol) {
2253 * Ideally this function should support any number of
2254 * paths and channels. But since kcontrols only come
2255 * in mono and stereo variants, we are limited to 2
2258 * The following code assumes for stereo controls the
2259 * first path (when 'found == 0') is the left channel,
2260 * and all remaining paths (when 'found == 1') are the
2263 * A stereo control is signified by a valid 'rconnect'
2264 * value, either 0 for unconnected, or >= 0 for connected.
2265 * This is chosen instead of using snd_soc_volsw_is_stereo,
2266 * so that the behavior of snd_soc_dapm_mixer_update_power
2267 * doesn't change even when the kcontrol passed in is
2270 * It passes 'connect' as the path connect status for
2271 * the left channel, and 'rconnect' for the right
2274 if (found && rconnect >= 0)
2275 soc_dapm_connect_path(path, rconnect, "mixer update");
2277 soc_dapm_connect_path(path, connect, "mixer update");
2282 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2287 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2288 struct snd_kcontrol *kcontrol, int connect,
2289 struct snd_soc_dapm_update *update)
2291 struct snd_soc_card *card = dapm->card;
2294 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2295 card->update = update;
2296 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2297 card->update = NULL;
2298 mutex_unlock(&card->dapm_mutex);
2300 soc_dpcm_runtime_update(card);
2303 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2305 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2308 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2309 struct snd_soc_dapm_widget *w;
2311 char *state = "not set";
2313 /* card won't be set for the dummy component, as a spot fix
2314 * we're checking for that case specifically here but in future
2315 * we will ensure that the dummy component looks like others.
2320 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2321 if (w->dapm != dapm)
2324 /* only display widgets that burn power */
2326 case snd_soc_dapm_hp:
2327 case snd_soc_dapm_mic:
2328 case snd_soc_dapm_spk:
2329 case snd_soc_dapm_line:
2330 case snd_soc_dapm_micbias:
2331 case snd_soc_dapm_dac:
2332 case snd_soc_dapm_adc:
2333 case snd_soc_dapm_pga:
2334 case snd_soc_dapm_out_drv:
2335 case snd_soc_dapm_mixer:
2336 case snd_soc_dapm_mixer_named_ctl:
2337 case snd_soc_dapm_supply:
2338 case snd_soc_dapm_regulator_supply:
2339 case snd_soc_dapm_pinctrl:
2340 case snd_soc_dapm_clock_supply:
2342 count += sprintf(buf + count, "%s: %s\n",
2343 w->name, w->power ? "On":"Off");
2350 switch (snd_soc_dapm_get_bias_level(dapm)) {
2351 case SND_SOC_BIAS_ON:
2354 case SND_SOC_BIAS_PREPARE:
2357 case SND_SOC_BIAS_STANDBY:
2360 case SND_SOC_BIAS_OFF:
2364 count += sprintf(buf + count, "PM State: %s\n", state);
2369 /* show dapm widget status in sys fs */
2370 static ssize_t dapm_widget_show(struct device *dev,
2371 struct device_attribute *attr, char *buf)
2373 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2376 mutex_lock(&rtd->card->dapm_mutex);
2378 for (i = 0; i < rtd->num_codecs; i++) {
2379 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2381 count += dapm_widget_show_component(cmpnt, buf + count);
2384 mutex_unlock(&rtd->card->dapm_mutex);
2389 static DEVICE_ATTR_RO(dapm_widget);
2391 struct attribute *soc_dapm_dev_attrs[] = {
2392 &dev_attr_dapm_widget.attr,
2396 static void dapm_free_path(struct snd_soc_dapm_path *path)
2398 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2399 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2400 list_del(&path->list_kcontrol);
2401 list_del(&path->list);
2405 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2407 struct snd_soc_dapm_path *p, *next_p;
2408 enum snd_soc_dapm_direction dir;
2412 * remove source and sink paths associated to this widget.
2413 * While removing the path, remove reference to it from both
2414 * source and sink widgets so that path is removed only once.
2416 snd_soc_dapm_for_each_direction(dir) {
2417 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2421 kfree(w->kcontrols);
2422 kfree_const(w->name);
2426 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2428 dapm->path_sink_cache.widget = NULL;
2429 dapm->path_source_cache.widget = NULL;
2432 /* free all dapm widgets and resources */
2433 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2435 struct snd_soc_dapm_widget *w, *next_w;
2437 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2438 if (w->dapm != dapm)
2440 snd_soc_dapm_free_widget(w);
2442 snd_soc_dapm_reset_cache(dapm);
2445 static struct snd_soc_dapm_widget *dapm_find_widget(
2446 struct snd_soc_dapm_context *dapm, const char *pin,
2447 bool search_other_contexts)
2449 struct snd_soc_dapm_widget *w;
2450 struct snd_soc_dapm_widget *fallback = NULL;
2452 list_for_each_entry(w, &dapm->card->widgets, list) {
2453 if (!strcmp(w->name, pin)) {
2454 if (w->dapm == dapm)
2461 if (search_other_contexts)
2467 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2468 const char *pin, int status)
2470 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2472 dapm_assert_locked(dapm);
2475 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2479 if (w->connected != status) {
2480 dapm_mark_dirty(w, "pin configuration");
2481 dapm_widget_invalidate_input_paths(w);
2482 dapm_widget_invalidate_output_paths(w);
2485 w->connected = status;
2493 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2494 * @dapm: DAPM context
2496 * Walks all dapm audio paths and powers widgets according to their
2497 * stream or path usage.
2499 * Requires external locking.
2501 * Returns 0 for success.
2503 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2506 * Suppress early reports (eg, jacks syncing their state) to avoid
2507 * silly DAPM runs during card startup.
2509 if (!dapm->card || !dapm->card->instantiated)
2512 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2514 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2517 * snd_soc_dapm_sync - scan and power dapm paths
2518 * @dapm: DAPM context
2520 * Walks all dapm audio paths and powers widgets according to their
2521 * stream or path usage.
2523 * Returns 0 for success.
2525 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2529 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2530 ret = snd_soc_dapm_sync_unlocked(dapm);
2531 mutex_unlock(&dapm->card->dapm_mutex);
2534 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2537 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2538 * @w: The widget for which to update the flags
2540 * Some widgets have a dynamic category which depends on which neighbors they
2541 * are connected to. This function update the category for these widgets.
2543 * This function must be called whenever a path is added or removed to a widget.
2545 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2547 enum snd_soc_dapm_direction dir;
2548 struct snd_soc_dapm_path *p;
2552 case snd_soc_dapm_input:
2553 /* On a fully routed card an input is never a source */
2554 if (w->dapm->card->fully_routed)
2556 ep = SND_SOC_DAPM_EP_SOURCE;
2557 snd_soc_dapm_widget_for_each_source_path(w, p) {
2558 if (p->source->id == snd_soc_dapm_micbias ||
2559 p->source->id == snd_soc_dapm_mic ||
2560 p->source->id == snd_soc_dapm_line ||
2561 p->source->id == snd_soc_dapm_output) {
2567 case snd_soc_dapm_output:
2568 /* On a fully routed card a output is never a sink */
2569 if (w->dapm->card->fully_routed)
2571 ep = SND_SOC_DAPM_EP_SINK;
2572 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2573 if (p->sink->id == snd_soc_dapm_spk ||
2574 p->sink->id == snd_soc_dapm_hp ||
2575 p->sink->id == snd_soc_dapm_line ||
2576 p->sink->id == snd_soc_dapm_input) {
2582 case snd_soc_dapm_line:
2584 snd_soc_dapm_for_each_direction(dir) {
2585 if (!list_empty(&w->edges[dir]))
2586 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2596 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2597 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2598 const char *control)
2600 bool dynamic_source = false;
2601 bool dynamic_sink = false;
2606 switch (source->id) {
2607 case snd_soc_dapm_demux:
2608 dynamic_source = true;
2615 case snd_soc_dapm_mux:
2616 case snd_soc_dapm_switch:
2617 case snd_soc_dapm_mixer:
2618 case snd_soc_dapm_mixer_named_ctl:
2619 dynamic_sink = true;
2625 if (dynamic_source && dynamic_sink) {
2627 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2628 source->name, control, sink->name);
2630 } else if (!dynamic_source && !dynamic_sink) {
2632 "Control not supported for path %s -> [%s] -> %s\n",
2633 source->name, control, sink->name);
2640 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2641 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2642 const char *control,
2643 int (*connected)(struct snd_soc_dapm_widget *source,
2644 struct snd_soc_dapm_widget *sink))
2646 struct snd_soc_dapm_widget *widgets[2];
2647 enum snd_soc_dapm_direction dir;
2648 struct snd_soc_dapm_path *path;
2651 if (wsink->is_supply && !wsource->is_supply) {
2653 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2654 wsource->name, wsink->name);
2658 if (connected && !wsource->is_supply) {
2660 "connected() callback only supported for supply widgets (%s -> %s)\n",
2661 wsource->name, wsink->name);
2665 if (wsource->is_supply && control) {
2667 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2668 wsource->name, control, wsink->name);
2672 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2676 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2680 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2681 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2682 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2683 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2685 path->connected = connected;
2686 INIT_LIST_HEAD(&path->list);
2687 INIT_LIST_HEAD(&path->list_kcontrol);
2689 if (wsource->is_supply || wsink->is_supply)
2690 path->is_supply = 1;
2692 /* connect static paths */
2693 if (control == NULL) {
2696 switch (wsource->id) {
2697 case snd_soc_dapm_demux:
2698 ret = dapm_connect_mux(dapm, path, control, wsource);
2706 switch (wsink->id) {
2707 case snd_soc_dapm_mux:
2708 ret = dapm_connect_mux(dapm, path, control, wsink);
2712 case snd_soc_dapm_switch:
2713 case snd_soc_dapm_mixer:
2714 case snd_soc_dapm_mixer_named_ctl:
2715 ret = dapm_connect_mixer(dapm, path, control);
2724 list_add(&path->list, &dapm->card->paths);
2725 snd_soc_dapm_for_each_direction(dir)
2726 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2728 snd_soc_dapm_for_each_direction(dir) {
2729 dapm_update_widget_flags(widgets[dir]);
2730 dapm_mark_dirty(widgets[dir], "Route added");
2733 if (dapm->card->instantiated && path->connect)
2734 dapm_path_invalidate(path);
2742 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2743 const struct snd_soc_dapm_route *route)
2745 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2746 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2749 char prefixed_sink[80];
2750 char prefixed_source[80];
2754 prefix = soc_dapm_prefix(dapm);
2756 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2757 prefix, route->sink);
2758 sink = prefixed_sink;
2759 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2760 prefix, route->source);
2761 source = prefixed_source;
2764 source = route->source;
2767 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2768 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2770 if (wsink && wsource)
2774 * find src and dest widgets over all widgets but favor a widget from
2775 * current DAPM context
2777 list_for_each_entry(w, &dapm->card->widgets, list) {
2778 if (!wsink && !(strcmp(w->name, sink))) {
2780 if (w->dapm == dapm) {
2787 if (!wsource && !(strcmp(w->name, source))) {
2789 if (w->dapm == dapm) {
2796 /* use widget from another DAPM context if not found from this */
2802 if (wsource == NULL) {
2803 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2807 if (wsink == NULL) {
2808 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2814 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2815 dapm_wcache_update(&dapm->path_source_cache, wsource);
2817 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2824 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2825 source, route->control, sink);
2829 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2830 const struct snd_soc_dapm_route *route)
2832 struct snd_soc_dapm_widget *wsource, *wsink;
2833 struct snd_soc_dapm_path *path, *p;
2836 char prefixed_sink[80];
2837 char prefixed_source[80];
2840 if (route->control) {
2842 "ASoC: Removal of routes with controls not supported\n");
2846 prefix = soc_dapm_prefix(dapm);
2848 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2849 prefix, route->sink);
2850 sink = prefixed_sink;
2851 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2852 prefix, route->source);
2853 source = prefixed_source;
2856 source = route->source;
2860 list_for_each_entry(p, &dapm->card->paths, list) {
2861 if (strcmp(p->source->name, source) != 0)
2863 if (strcmp(p->sink->name, sink) != 0)
2870 wsource = path->source;
2873 dapm_mark_dirty(wsource, "Route removed");
2874 dapm_mark_dirty(wsink, "Route removed");
2876 dapm_path_invalidate(path);
2878 dapm_free_path(path);
2880 /* Update any path related flags */
2881 dapm_update_widget_flags(wsource);
2882 dapm_update_widget_flags(wsink);
2884 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2892 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2893 * @dapm: DAPM context
2894 * @route: audio routes
2895 * @num: number of routes
2897 * Connects 2 dapm widgets together via a named audio path. The sink is
2898 * the widget receiving the audio signal, whilst the source is the sender
2899 * of the audio signal.
2901 * Returns 0 for success else error. On error all resources can be freed
2902 * with a call to snd_soc_card_free().
2904 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2905 const struct snd_soc_dapm_route *route, int num)
2909 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2910 for (i = 0; i < num; i++) {
2911 r = snd_soc_dapm_add_route(dapm, route);
2913 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2915 route->control ? route->control : "direct",
2921 mutex_unlock(&dapm->card->dapm_mutex);
2925 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2928 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2929 * @dapm: DAPM context
2930 * @route: audio routes
2931 * @num: number of routes
2933 * Removes routes from the DAPM context.
2935 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2936 const struct snd_soc_dapm_route *route, int num)
2940 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2941 for (i = 0; i < num; i++) {
2942 snd_soc_dapm_del_route(dapm, route);
2945 mutex_unlock(&dapm->card->dapm_mutex);
2949 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2951 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2952 const struct snd_soc_dapm_route *route)
2954 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2957 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2960 struct snd_soc_dapm_path *path;
2964 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2970 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2975 if (route->control || route->connected)
2976 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2977 route->source, route->sink);
2979 snd_soc_dapm_widget_for_each_sink_path(source, path) {
2980 if (path->sink == sink) {
2987 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2988 route->source, route->sink);
2990 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2991 count, route->source, route->sink);
2997 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2998 * @dapm: DAPM context
2999 * @route: audio routes
3000 * @num: number of routes
3002 * Mark existing routes matching those specified in the passed array
3003 * as being weak, meaning that they are ignored for the purpose of
3004 * power decisions. The main intended use case is for sidetone paths
3005 * which couple audio between other independent paths if they are both
3006 * active in order to make the combination work better at the user
3007 * level but which aren't intended to be "used".
3009 * Note that CODEC drivers should not use this as sidetone type paths
3010 * can frequently also be used as bypass paths.
3012 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3013 const struct snd_soc_dapm_route *route, int num)
3018 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3019 for (i = 0; i < num; i++) {
3020 err = snd_soc_dapm_weak_route(dapm, route);
3025 mutex_unlock(&dapm->card->dapm_mutex);
3029 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3032 * snd_soc_dapm_new_widgets - add new dapm widgets
3033 * @card: card to be checked for new dapm widgets
3035 * Checks the codec for any new dapm widgets and creates them if found.
3037 * Returns 0 for success.
3039 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3041 struct snd_soc_dapm_widget *w;
3044 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3046 list_for_each_entry(w, &card->widgets, list)
3051 if (w->num_kcontrols) {
3052 w->kcontrols = kcalloc(w->num_kcontrols,
3053 sizeof(struct snd_kcontrol *),
3055 if (!w->kcontrols) {
3056 mutex_unlock(&card->dapm_mutex);
3062 case snd_soc_dapm_switch:
3063 case snd_soc_dapm_mixer:
3064 case snd_soc_dapm_mixer_named_ctl:
3067 case snd_soc_dapm_mux:
3068 case snd_soc_dapm_demux:
3071 case snd_soc_dapm_pga:
3072 case snd_soc_dapm_out_drv:
3075 case snd_soc_dapm_dai_link:
3076 dapm_new_dai_link(w);
3082 /* Read the initial power state from the device */
3084 soc_dapm_read(w->dapm, w->reg, &val);
3085 val = val >> w->shift;
3087 if (val == w->on_val)
3093 dapm_mark_dirty(w, "new widget");
3094 dapm_debugfs_add_widget(w);
3097 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3098 mutex_unlock(&card->dapm_mutex);
3101 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3104 * snd_soc_dapm_get_volsw - dapm mixer get callback
3105 * @kcontrol: mixer control
3106 * @ucontrol: control element information
3108 * Callback to get the value of a dapm mixer control.
3110 * Returns 0 for success.
3112 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3113 struct snd_ctl_elem_value *ucontrol)
3115 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3116 struct snd_soc_card *card = dapm->card;
3117 struct soc_mixer_control *mc =
3118 (struct soc_mixer_control *)kcontrol->private_value;
3120 unsigned int shift = mc->shift;
3122 unsigned int width = fls(max);
3123 unsigned int mask = (1 << fls(max)) - 1;
3124 unsigned int invert = mc->invert;
3125 unsigned int reg_val, val, rval = 0;
3128 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3129 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3130 ret = soc_dapm_read(dapm, reg, ®_val);
3131 val = (reg_val >> shift) & mask;
3133 if (ret == 0 && reg != mc->rreg)
3134 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3136 if (snd_soc_volsw_is_stereo(mc))
3137 rval = (reg_val >> mc->rshift) & mask;
3139 reg_val = dapm_kcontrol_get_value(kcontrol);
3140 val = reg_val & mask;
3142 if (snd_soc_volsw_is_stereo(mc))
3143 rval = (reg_val >> width) & mask;
3145 mutex_unlock(&card->dapm_mutex);
3151 ucontrol->value.integer.value[0] = max - val;
3153 ucontrol->value.integer.value[0] = val;
3155 if (snd_soc_volsw_is_stereo(mc)) {
3157 ucontrol->value.integer.value[1] = max - rval;
3159 ucontrol->value.integer.value[1] = rval;
3164 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3167 * snd_soc_dapm_put_volsw - dapm mixer set callback
3168 * @kcontrol: mixer control
3169 * @ucontrol: control element information
3171 * Callback to set the value of a dapm mixer control.
3173 * Returns 0 for success.
3175 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3176 struct snd_ctl_elem_value *ucontrol)
3178 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3179 struct snd_soc_card *card = dapm->card;
3180 struct soc_mixer_control *mc =
3181 (struct soc_mixer_control *)kcontrol->private_value;
3183 unsigned int shift = mc->shift;
3185 unsigned int width = fls(max);
3186 unsigned int mask = (1 << width) - 1;
3187 unsigned int invert = mc->invert;
3188 unsigned int val, rval = 0;
3189 int connect, rconnect = -1, change, reg_change = 0;
3190 struct snd_soc_dapm_update update = {};
3193 val = (ucontrol->value.integer.value[0] & mask);
3199 if (snd_soc_volsw_is_stereo(mc)) {
3200 rval = (ucontrol->value.integer.value[1] & mask);
3206 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3208 /* This assumes field width < (bits in unsigned int / 2) */
3209 if (width > sizeof(unsigned int) * 8 / 2)
3211 "ASoC: control %s field width limit exceeded\n",
3213 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3215 if (reg != SND_SOC_NOPM) {
3217 rval = rval << mc->rshift;
3219 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3221 if (snd_soc_volsw_is_stereo(mc))
3222 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3227 if (change || reg_change) {
3229 if (snd_soc_volsw_is_stereo(mc)) {
3230 update.has_second_set = true;
3231 update.reg2 = mc->rreg;
3232 update.mask2 = mask << mc->rshift;
3235 update.kcontrol = kcontrol;
3237 update.mask = mask << shift;
3239 card->update = &update;
3241 change |= reg_change;
3243 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3246 card->update = NULL;
3249 mutex_unlock(&card->dapm_mutex);
3252 soc_dpcm_runtime_update(card);
3256 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3259 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3260 * @kcontrol: mixer control
3261 * @ucontrol: control element information
3263 * Callback to get the value of a dapm enumerated double mixer control.
3265 * Returns 0 for success.
3267 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3268 struct snd_ctl_elem_value *ucontrol)
3270 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3271 struct snd_soc_card *card = dapm->card;
3272 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3273 unsigned int reg_val, val;
3275 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3276 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3277 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3279 mutex_unlock(&card->dapm_mutex);
3283 reg_val = dapm_kcontrol_get_value(kcontrol);
3285 mutex_unlock(&card->dapm_mutex);
3287 val = (reg_val >> e->shift_l) & e->mask;
3288 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3289 if (e->shift_l != e->shift_r) {
3290 val = (reg_val >> e->shift_r) & e->mask;
3291 val = snd_soc_enum_val_to_item(e, val);
3292 ucontrol->value.enumerated.item[1] = val;
3297 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3300 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3301 * @kcontrol: mixer control
3302 * @ucontrol: control element information
3304 * Callback to set the value of a dapm enumerated double mixer control.
3306 * Returns 0 for success.
3308 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3309 struct snd_ctl_elem_value *ucontrol)
3311 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3312 struct snd_soc_card *card = dapm->card;
3313 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3314 unsigned int *item = ucontrol->value.enumerated.item;
3315 unsigned int val, change, reg_change = 0;
3317 struct snd_soc_dapm_update update = {};
3320 if (item[0] >= e->items)
3323 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3324 mask = e->mask << e->shift_l;
3325 if (e->shift_l != e->shift_r) {
3326 if (item[1] > e->items)
3328 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3329 mask |= e->mask << e->shift_r;
3332 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3334 change = dapm_kcontrol_set_value(kcontrol, val);
3336 if (e->reg != SND_SOC_NOPM)
3337 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3339 if (change || reg_change) {
3341 update.kcontrol = kcontrol;
3342 update.reg = e->reg;
3345 card->update = &update;
3347 change |= reg_change;
3349 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3351 card->update = NULL;
3354 mutex_unlock(&card->dapm_mutex);
3357 soc_dpcm_runtime_update(card);
3361 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3364 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3366 * @kcontrol: mixer control
3367 * @uinfo: control element information
3369 * Callback to provide information about a pin switch control.
3371 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3372 struct snd_ctl_elem_info *uinfo)
3374 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3376 uinfo->value.integer.min = 0;
3377 uinfo->value.integer.max = 1;
3381 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3384 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3386 * @kcontrol: mixer control
3389 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3390 struct snd_ctl_elem_value *ucontrol)
3392 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3393 const char *pin = (const char *)kcontrol->private_value;
3395 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3397 ucontrol->value.integer.value[0] =
3398 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3400 mutex_unlock(&card->dapm_mutex);
3404 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3407 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3409 * @kcontrol: mixer control
3412 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3413 struct snd_ctl_elem_value *ucontrol)
3415 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3416 const char *pin = (const char *)kcontrol->private_value;
3418 if (ucontrol->value.integer.value[0])
3419 snd_soc_dapm_enable_pin(&card->dapm, pin);
3421 snd_soc_dapm_disable_pin(&card->dapm, pin);
3423 snd_soc_dapm_sync(&card->dapm);
3426 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3428 struct snd_soc_dapm_widget *
3429 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3430 const struct snd_soc_dapm_widget *widget)
3432 struct snd_soc_dapm_widget *w;
3434 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3435 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3436 /* Do not nag about probe deferrals */
3438 int ret = PTR_ERR(w);
3440 if (ret != -EPROBE_DEFER)
3442 "ASoC: Failed to create DAPM control %s (%d)\n",
3448 "ASoC: Failed to create DAPM control %s\n",
3452 mutex_unlock(&dapm->card->dapm_mutex);
3455 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3457 struct snd_soc_dapm_widget *
3458 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3459 const struct snd_soc_dapm_widget *widget)
3461 enum snd_soc_dapm_direction dir;
3462 struct snd_soc_dapm_widget *w;
3466 if ((w = dapm_cnew_widget(widget)) == NULL)
3470 case snd_soc_dapm_regulator_supply:
3471 w->regulator = devm_regulator_get(dapm->dev, w->name);
3472 if (IS_ERR(w->regulator)) {
3473 ret = PTR_ERR(w->regulator);
3474 if (ret == -EPROBE_DEFER)
3475 return ERR_PTR(ret);
3476 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3481 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3482 ret = regulator_allow_bypass(w->regulator, true);
3484 dev_warn(w->dapm->dev,
3485 "ASoC: Failed to bypass %s: %d\n",
3489 case snd_soc_dapm_pinctrl:
3490 w->pinctrl = devm_pinctrl_get(dapm->dev);
3491 if (IS_ERR_OR_NULL(w->pinctrl)) {
3492 ret = PTR_ERR(w->pinctrl);
3493 if (ret == -EPROBE_DEFER)
3494 return ERR_PTR(ret);
3495 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3500 case snd_soc_dapm_clock_supply:
3501 #ifdef CONFIG_CLKDEV_LOOKUP
3502 w->clk = devm_clk_get(dapm->dev, w->name);
3503 if (IS_ERR(w->clk)) {
3504 ret = PTR_ERR(w->clk);
3505 if (ret == -EPROBE_DEFER)
3506 return ERR_PTR(ret);
3507 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3519 prefix = soc_dapm_prefix(dapm);
3521 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3523 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3524 if (w->name == NULL) {
3530 case snd_soc_dapm_mic:
3531 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3532 w->power_check = dapm_generic_check_power;
3534 case snd_soc_dapm_input:
3535 if (!dapm->card->fully_routed)
3536 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3537 w->power_check = dapm_generic_check_power;
3539 case snd_soc_dapm_spk:
3540 case snd_soc_dapm_hp:
3541 w->is_ep = SND_SOC_DAPM_EP_SINK;
3542 w->power_check = dapm_generic_check_power;
3544 case snd_soc_dapm_output:
3545 if (!dapm->card->fully_routed)
3546 w->is_ep = SND_SOC_DAPM_EP_SINK;
3547 w->power_check = dapm_generic_check_power;
3549 case snd_soc_dapm_vmid:
3550 case snd_soc_dapm_siggen:
3551 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3552 w->power_check = dapm_always_on_check_power;
3554 case snd_soc_dapm_sink:
3555 w->is_ep = SND_SOC_DAPM_EP_SINK;
3556 w->power_check = dapm_always_on_check_power;
3559 case snd_soc_dapm_mux:
3560 case snd_soc_dapm_demux:
3561 case snd_soc_dapm_switch:
3562 case snd_soc_dapm_mixer:
3563 case snd_soc_dapm_mixer_named_ctl:
3564 case snd_soc_dapm_adc:
3565 case snd_soc_dapm_aif_out:
3566 case snd_soc_dapm_dac:
3567 case snd_soc_dapm_aif_in:
3568 case snd_soc_dapm_pga:
3569 case snd_soc_dapm_out_drv:
3570 case snd_soc_dapm_micbias:
3571 case snd_soc_dapm_line:
3572 case snd_soc_dapm_dai_link:
3573 case snd_soc_dapm_dai_out:
3574 case snd_soc_dapm_dai_in:
3575 w->power_check = dapm_generic_check_power;
3577 case snd_soc_dapm_supply:
3578 case snd_soc_dapm_regulator_supply:
3579 case snd_soc_dapm_pinctrl:
3580 case snd_soc_dapm_clock_supply:
3581 case snd_soc_dapm_kcontrol:
3583 w->power_check = dapm_supply_check_power;
3586 w->power_check = dapm_always_on_check_power;
3591 INIT_LIST_HEAD(&w->list);
3592 INIT_LIST_HEAD(&w->dirty);
3593 list_add_tail(&w->list, &dapm->card->widgets);
3595 snd_soc_dapm_for_each_direction(dir) {
3596 INIT_LIST_HEAD(&w->edges[dir]);
3597 w->endpoints[dir] = -1;
3600 /* machine layer sets up unconnected pins and insertions */
3606 * snd_soc_dapm_new_controls - create new dapm controls
3607 * @dapm: DAPM context
3608 * @widget: widget array
3609 * @num: number of widgets
3611 * Creates new DAPM controls based upon the templates.
3613 * Returns 0 for success else error.
3615 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3616 const struct snd_soc_dapm_widget *widget,
3619 struct snd_soc_dapm_widget *w;
3623 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3624 for (i = 0; i < num; i++) {
3625 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3628 /* Do not nag about probe deferrals */
3629 if (ret == -EPROBE_DEFER)
3632 "ASoC: Failed to create DAPM control %s (%d)\n",
3638 "ASoC: Failed to create DAPM control %s\n",
3645 mutex_unlock(&dapm->card->dapm_mutex);
3648 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3650 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3651 struct snd_kcontrol *kcontrol, int event)
3653 struct snd_soc_dapm_path *source_p, *sink_p;
3654 struct snd_soc_dai *source, *sink;
3655 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
3656 struct snd_pcm_substream substream;
3657 struct snd_pcm_hw_params *params = NULL;
3658 struct snd_pcm_runtime *runtime = NULL;
3662 if (WARN_ON(!config) ||
3663 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3664 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3667 /* We only support a single source and sink, pick the first */
3668 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3669 struct snd_soc_dapm_path,
3670 list_node[SND_SOC_DAPM_DIR_OUT]);
3671 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3672 struct snd_soc_dapm_path,
3673 list_node[SND_SOC_DAPM_DIR_IN]);
3675 source = source_p->source->priv;
3676 sink = sink_p->sink->priv;
3678 /* Be a little careful as we don't want to overflow the mask array */
3679 if (config->formats) {
3680 fmt = ffs(config->formats) - 1;
3682 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3687 /* Currently very limited parameter selection */
3688 params = kzalloc(sizeof(*params), GFP_KERNEL);
3693 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3695 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3697 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3700 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3701 = config->channels_min;
3702 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3703 = config->channels_max;
3705 memset(&substream, 0, sizeof(substream));
3707 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3708 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3713 substream.runtime = runtime;
3716 case SND_SOC_DAPM_PRE_PMU:
3717 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3718 if (source->driver->ops->startup) {
3719 ret = source->driver->ops->startup(&substream, source);
3721 dev_err(source->dev,
3722 "ASoC: startup() failed: %d\n", ret);
3727 ret = soc_dai_hw_params(&substream, params, source);
3731 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3732 if (sink->driver->ops->startup) {
3733 ret = sink->driver->ops->startup(&substream, sink);
3736 "ASoC: startup() failed: %d\n", ret);
3741 ret = soc_dai_hw_params(&substream, params, sink);
3746 case SND_SOC_DAPM_POST_PMU:
3747 ret = snd_soc_dai_digital_mute(sink, 0,
3748 SNDRV_PCM_STREAM_PLAYBACK);
3749 if (ret != 0 && ret != -ENOTSUPP)
3750 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3754 case SND_SOC_DAPM_PRE_PMD:
3755 ret = snd_soc_dai_digital_mute(sink, 1,
3756 SNDRV_PCM_STREAM_PLAYBACK);
3757 if (ret != 0 && ret != -ENOTSUPP)
3758 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3762 if (source->driver->ops->shutdown) {
3763 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3764 source->driver->ops->shutdown(&substream, source);
3768 if (sink->driver->ops->shutdown) {
3769 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3770 sink->driver->ops->shutdown(&substream, sink);
3775 WARN(1, "Unknown event %d\n", event);
3785 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3786 struct snd_ctl_elem_value *ucontrol)
3788 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3790 ucontrol->value.enumerated.item[0] = w->params_select;
3795 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3796 struct snd_ctl_elem_value *ucontrol)
3798 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3800 /* Can't change the config when widget is already powered */
3804 if (ucontrol->value.enumerated.item[0] == w->params_select)
3807 if (ucontrol->value.enumerated.item[0] >= w->num_params)
3810 w->params_select = ucontrol->value.enumerated.item[0];
3816 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3817 unsigned long *private_value,
3819 const char **w_param_text)
3823 devm_kfree(card->dev, (void *)*private_value);
3824 for (count = 0 ; count < num_params; count++)
3825 devm_kfree(card->dev, (void *)w_param_text[count]);
3826 devm_kfree(card->dev, w_param_text);
3829 static struct snd_kcontrol_new *
3830 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3832 const struct snd_soc_pcm_stream *params,
3833 int num_params, const char **w_param_text,
3834 unsigned long *private_value)
3836 struct soc_enum w_param_enum[] = {
3837 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3839 struct snd_kcontrol_new kcontrol_dai_link[] = {
3840 SOC_ENUM_EXT(NULL, w_param_enum[0],
3841 snd_soc_dapm_dai_link_get,
3842 snd_soc_dapm_dai_link_put),
3844 struct snd_kcontrol_new *kcontrol_news;
3845 const struct snd_soc_pcm_stream *config = params;
3848 for (count = 0 ; count < num_params; count++) {
3849 if (!config->stream_name) {
3850 dev_warn(card->dapm.dev,
3851 "ASoC: anonymous config %d for dai link %s\n",
3853 w_param_text[count] =
3854 devm_kasprintf(card->dev, GFP_KERNEL,
3855 "Anonymous Configuration %d",
3858 w_param_text[count] = devm_kmemdup(card->dev,
3859 config->stream_name,
3860 strlen(config->stream_name) + 1,
3863 if (!w_param_text[count])
3864 goto outfree_w_param;
3868 w_param_enum[0].items = num_params;
3869 w_param_enum[0].texts = w_param_text;
3872 (unsigned long) devm_kmemdup(card->dev,
3873 (void *)(kcontrol_dai_link[0].private_value),
3874 sizeof(struct soc_enum), GFP_KERNEL);
3875 if (!*private_value) {
3876 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3878 goto outfree_w_param;
3880 kcontrol_dai_link[0].private_value = *private_value;
3881 /* duplicate kcontrol_dai_link on heap so that memory persists */
3882 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3883 sizeof(struct snd_kcontrol_new),
3885 if (!kcontrol_news) {
3886 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3888 goto outfree_w_param;
3890 return kcontrol_news;
3893 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
3897 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3898 const struct snd_soc_pcm_stream *params,
3899 unsigned int num_params,
3900 struct snd_soc_dapm_widget *source,
3901 struct snd_soc_dapm_widget *sink)
3903 struct snd_soc_dapm_widget template;
3904 struct snd_soc_dapm_widget *w;
3905 const char **w_param_text;
3906 unsigned long private_value;
3910 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3911 source->name, sink->name);
3915 memset(&template, 0, sizeof(template));
3916 template.reg = SND_SOC_NOPM;
3917 template.id = snd_soc_dapm_dai_link;
3918 template.name = link_name;
3919 template.event = snd_soc_dai_link_event;
3920 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3921 SND_SOC_DAPM_PRE_PMD;
3922 template.kcontrol_news = NULL;
3924 /* allocate memory for control, only in case of multiple configs */
3925 if (num_params > 1) {
3926 w_param_text = devm_kcalloc(card->dev, num_params,
3927 sizeof(char *), GFP_KERNEL);
3928 if (!w_param_text) {
3933 template.num_kcontrols = 1;
3934 template.kcontrol_news =
3935 snd_soc_dapm_alloc_kcontrol(card,
3936 link_name, params, num_params,
3937 w_param_text, &private_value);
3938 if (!template.kcontrol_news) {
3943 w_param_text = NULL;
3945 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3947 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
3950 /* Do not nag about probe deferrals */
3951 if (ret != -EPROBE_DEFER)
3953 "ASoC: Failed to create %s widget (%d)\n",
3955 goto outfree_kcontrol_news;
3958 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3961 goto outfree_kcontrol_news;
3965 w->num_params = num_params;
3967 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3970 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3973 devm_kfree(card->dev, w);
3974 outfree_kcontrol_news:
3975 devm_kfree(card->dev, (void *)template.kcontrol_news);
3976 snd_soc_dapm_free_kcontrol(card, &private_value, num_params, w_param_text);
3978 devm_kfree(card->dev, link_name);
3982 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3983 struct snd_soc_dai *dai)
3985 struct snd_soc_dapm_widget template;
3986 struct snd_soc_dapm_widget *w;
3988 WARN_ON(dapm->dev != dai->dev);
3990 memset(&template, 0, sizeof(template));
3991 template.reg = SND_SOC_NOPM;
3993 if (dai->driver->playback.stream_name) {
3994 template.id = snd_soc_dapm_dai_in;
3995 template.name = dai->driver->playback.stream_name;
3996 template.sname = dai->driver->playback.stream_name;
3998 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4001 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4003 int ret = PTR_ERR(w);
4005 /* Do not nag about probe deferrals */
4006 if (ret != -EPROBE_DEFER)
4008 "ASoC: Failed to create %s widget (%d)\n",
4009 dai->driver->playback.stream_name, ret);
4013 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4014 dai->driver->playback.stream_name);
4019 dai->playback_widget = w;
4022 if (dai->driver->capture.stream_name) {
4023 template.id = snd_soc_dapm_dai_out;
4024 template.name = dai->driver->capture.stream_name;
4025 template.sname = dai->driver->capture.stream_name;
4027 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4030 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4032 int ret = PTR_ERR(w);
4034 /* Do not nag about probe deferrals */
4035 if (ret != -EPROBE_DEFER)
4037 "ASoC: Failed to create %s widget (%d)\n",
4038 dai->driver->playback.stream_name, ret);
4042 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4043 dai->driver->capture.stream_name);
4048 dai->capture_widget = w;
4054 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4056 struct snd_soc_dapm_widget *dai_w, *w;
4057 struct snd_soc_dapm_widget *src, *sink;
4058 struct snd_soc_dai *dai;
4060 /* For each DAI widget... */
4061 list_for_each_entry(dai_w, &card->widgets, list) {
4062 switch (dai_w->id) {
4063 case snd_soc_dapm_dai_in:
4064 case snd_soc_dapm_dai_out:
4070 /* let users know there is no DAI to link */
4072 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4079 /* ...find all widgets with the same stream and link them */
4080 list_for_each_entry(w, &card->widgets, list) {
4081 if (w->dapm != dai_w->dapm)
4085 case snd_soc_dapm_dai_in:
4086 case snd_soc_dapm_dai_out:
4092 if (!w->sname || !strstr(w->sname, dai_w->sname))
4095 if (dai_w->id == snd_soc_dapm_dai_in) {
4102 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4103 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4110 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4111 struct snd_soc_pcm_runtime *rtd)
4113 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4114 struct snd_soc_dapm_widget *sink, *source;
4117 for (i = 0; i < rtd->num_codecs; i++) {
4118 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
4120 /* connect BE DAI playback if widgets are valid */
4121 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
4122 source = cpu_dai->playback_widget;
4123 sink = codec_dai->playback_widget;
4124 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4125 cpu_dai->component->name, source->name,
4126 codec_dai->component->name, sink->name);
4128 snd_soc_dapm_add_path(&card->dapm, source, sink,
4132 /* connect BE DAI capture if widgets are valid */
4133 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
4134 source = codec_dai->capture_widget;
4135 sink = cpu_dai->capture_widget;
4136 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4137 codec_dai->component->name, source->name,
4138 cpu_dai->component->name, sink->name);
4140 snd_soc_dapm_add_path(&card->dapm, source, sink,
4146 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4149 struct snd_soc_dapm_widget *w;
4152 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4153 w = dai->playback_widget;
4155 w = dai->capture_widget;
4158 dapm_mark_dirty(w, "stream event");
4160 if (w->id == snd_soc_dapm_dai_in) {
4161 ep = SND_SOC_DAPM_EP_SOURCE;
4162 dapm_widget_invalidate_input_paths(w);
4164 ep = SND_SOC_DAPM_EP_SINK;
4165 dapm_widget_invalidate_output_paths(w);
4169 case SND_SOC_DAPM_STREAM_START:
4173 case SND_SOC_DAPM_STREAM_STOP:
4177 case SND_SOC_DAPM_STREAM_SUSPEND:
4178 case SND_SOC_DAPM_STREAM_RESUME:
4179 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4180 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4186 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4188 struct snd_soc_pcm_runtime *rtd;
4190 /* for each BE DAI link... */
4191 list_for_each_entry(rtd, &card->rtd_list, list) {
4193 * dynamic FE links have no fixed DAI mapping.
4194 * CODEC<->CODEC links have no direct connection.
4196 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4199 dapm_connect_dai_link_widgets(card, rtd);
4203 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4208 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4209 for (i = 0; i < rtd->num_codecs; i++)
4210 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
4212 dapm_power_widgets(rtd->card, event);
4216 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4217 * @rtd: PCM runtime data
4218 * @stream: stream name
4219 * @event: stream event
4221 * Sends a stream event to the dapm core. The core then makes any
4222 * necessary widget power changes.
4224 * Returns 0 for success else error.
4226 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4229 struct snd_soc_card *card = rtd->card;
4231 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4232 soc_dapm_stream_event(rtd, stream, event);
4233 mutex_unlock(&card->dapm_mutex);
4237 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4238 * @dapm: DAPM context
4241 * Enables input/output pin and its parents or children widgets iff there is
4242 * a valid audio route and active audio stream.
4244 * Requires external locking.
4246 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4247 * do any widget power switching.
4249 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4252 return snd_soc_dapm_set_pin(dapm, pin, 1);
4254 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4257 * snd_soc_dapm_enable_pin - enable pin.
4258 * @dapm: DAPM context
4261 * Enables input/output pin and its parents or children widgets iff there is
4262 * a valid audio route and active audio stream.
4264 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4265 * do any widget power switching.
4267 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4271 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4273 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4275 mutex_unlock(&dapm->card->dapm_mutex);
4279 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4282 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4283 * @dapm: DAPM context
4286 * Enables input/output pin regardless of any other state. This is
4287 * intended for use with microphone bias supplies used in microphone
4290 * Requires external locking.
4292 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4293 * do any widget power switching.
4295 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4298 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4301 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4305 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4306 if (!w->connected) {
4308 * w->force does not affect the number of input or output paths,
4309 * so we only have to recheck if w->connected is changed
4311 dapm_widget_invalidate_input_paths(w);
4312 dapm_widget_invalidate_output_paths(w);
4316 dapm_mark_dirty(w, "force enable");
4320 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4323 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4324 * @dapm: DAPM context
4327 * Enables input/output pin regardless of any other state. This is
4328 * intended for use with microphone bias supplies used in microphone
4331 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4332 * do any widget power switching.
4334 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4339 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4341 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4343 mutex_unlock(&dapm->card->dapm_mutex);
4347 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4350 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4351 * @dapm: DAPM context
4354 * Disables input/output pin and its parents or children widgets.
4356 * Requires external locking.
4358 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4359 * do any widget power switching.
4361 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4364 return snd_soc_dapm_set_pin(dapm, pin, 0);
4366 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4369 * snd_soc_dapm_disable_pin - disable pin.
4370 * @dapm: DAPM context
4373 * Disables input/output pin and its parents or children widgets.
4375 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4376 * do any widget power switching.
4378 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4383 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4385 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4387 mutex_unlock(&dapm->card->dapm_mutex);
4391 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4394 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4395 * @dapm: DAPM context
4398 * Marks the specified pin as being not connected, disabling it along
4399 * any parent or child widgets. At present this is identical to
4400 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4401 * additional things such as disabling controls which only affect
4402 * paths through the pin.
4404 * Requires external locking.
4406 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4407 * do any widget power switching.
4409 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4412 return snd_soc_dapm_set_pin(dapm, pin, 0);
4414 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4417 * snd_soc_dapm_nc_pin - permanently disable pin.
4418 * @dapm: DAPM context
4421 * Marks the specified pin as being not connected, disabling it along
4422 * any parent or child widgets. At present this is identical to
4423 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4424 * additional things such as disabling controls which only affect
4425 * paths through the pin.
4427 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4428 * do any widget power switching.
4430 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4434 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4436 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4438 mutex_unlock(&dapm->card->dapm_mutex);
4442 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4445 * snd_soc_dapm_get_pin_status - get audio pin status
4446 * @dapm: DAPM context
4447 * @pin: audio signal pin endpoint (or start point)
4449 * Get audio pin status - connected or disconnected.
4451 * Returns 1 for connected otherwise 0.
4453 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4456 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4459 return w->connected;
4463 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4466 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4467 * @dapm: DAPM context
4468 * @pin: audio signal pin endpoint (or start point)
4470 * Mark the given endpoint or pin as ignoring suspend. When the
4471 * system is disabled a path between two endpoints flagged as ignoring
4472 * suspend will not be disabled. The path must already be enabled via
4473 * normal means at suspend time, it will not be turned on if it was not
4476 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4479 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4482 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4486 w->ignore_suspend = 1;
4490 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4493 * snd_soc_dapm_free - free dapm resources
4494 * @dapm: DAPM context
4496 * Free all dapm widgets and resources.
4498 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4500 dapm_debugfs_cleanup(dapm);
4501 dapm_free_widgets(dapm);
4502 list_del(&dapm->list);
4504 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4506 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4508 struct snd_soc_card *card = dapm->card;
4509 struct snd_soc_dapm_widget *w;
4510 LIST_HEAD(down_list);
4513 mutex_lock(&card->dapm_mutex);
4515 list_for_each_entry(w, &dapm->card->widgets, list) {
4516 if (w->dapm != dapm)
4519 dapm_seq_insert(w, &down_list, false);
4525 /* If there were no widgets to power down we're already in
4529 if (dapm->bias_level == SND_SOC_BIAS_ON)
4530 snd_soc_dapm_set_bias_level(dapm,
4531 SND_SOC_BIAS_PREPARE);
4532 dapm_seq_run(card, &down_list, 0, false);
4533 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4534 snd_soc_dapm_set_bias_level(dapm,
4535 SND_SOC_BIAS_STANDBY);
4538 mutex_unlock(&card->dapm_mutex);
4542 * snd_soc_dapm_shutdown - callback for system shutdown
4544 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4546 struct snd_soc_dapm_context *dapm;
4548 list_for_each_entry(dapm, &card->dapm_list, list) {
4549 if (dapm != &card->dapm) {
4550 soc_dapm_shutdown_dapm(dapm);
4551 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4552 snd_soc_dapm_set_bias_level(dapm,
4557 soc_dapm_shutdown_dapm(&card->dapm);
4558 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4559 snd_soc_dapm_set_bias_level(&card->dapm,
4563 /* Module information */
4564 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4565 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4566 MODULE_LICENSE("GPL");