2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/headphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed power down of audio subsystem to reduce pops between a quick
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/clk.h>
39 #include <linux/slab.h>
40 #include <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44 #include <sound/initval.h>
46 #include <trace/events/asoc.h>
48 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
50 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
51 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
53 #define snd_soc_dapm_for_each_direction(dir) \
54 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
57 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
58 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
60 int (*connected)(struct snd_soc_dapm_widget *source,
61 struct snd_soc_dapm_widget *sink));
63 struct snd_soc_dapm_widget *
64 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
65 const struct snd_soc_dapm_widget *widget);
67 struct snd_soc_dapm_widget *
68 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
69 const struct snd_soc_dapm_widget *widget);
71 /* dapm power sequences - make this per codec in the future */
72 static int dapm_up_seq[] = {
73 [snd_soc_dapm_pre] = 0,
74 [snd_soc_dapm_regulator_supply] = 1,
75 [snd_soc_dapm_clock_supply] = 1,
76 [snd_soc_dapm_supply] = 2,
77 [snd_soc_dapm_micbias] = 3,
78 [snd_soc_dapm_vmid] = 3,
79 [snd_soc_dapm_dai_link] = 2,
80 [snd_soc_dapm_dai_in] = 4,
81 [snd_soc_dapm_dai_out] = 4,
82 [snd_soc_dapm_aif_in] = 4,
83 [snd_soc_dapm_aif_out] = 4,
84 [snd_soc_dapm_mic] = 5,
85 [snd_soc_dapm_siggen] = 5,
86 [snd_soc_dapm_input] = 5,
87 [snd_soc_dapm_output] = 5,
88 [snd_soc_dapm_mux] = 6,
89 [snd_soc_dapm_demux] = 6,
90 [snd_soc_dapm_dac] = 7,
91 [snd_soc_dapm_switch] = 8,
92 [snd_soc_dapm_mixer] = 8,
93 [snd_soc_dapm_mixer_named_ctl] = 8,
94 [snd_soc_dapm_pga] = 9,
95 [snd_soc_dapm_buffer] = 9,
96 [snd_soc_dapm_scheduler] = 9,
97 [snd_soc_dapm_effect] = 9,
98 [snd_soc_dapm_src] = 9,
99 [snd_soc_dapm_asrc] = 9,
100 [snd_soc_dapm_encoder] = 9,
101 [snd_soc_dapm_decoder] = 9,
102 [snd_soc_dapm_adc] = 10,
103 [snd_soc_dapm_out_drv] = 11,
104 [snd_soc_dapm_hp] = 11,
105 [snd_soc_dapm_spk] = 11,
106 [snd_soc_dapm_line] = 11,
107 [snd_soc_dapm_sink] = 11,
108 [snd_soc_dapm_kcontrol] = 12,
109 [snd_soc_dapm_post] = 13,
112 static int dapm_down_seq[] = {
113 [snd_soc_dapm_pre] = 0,
114 [snd_soc_dapm_kcontrol] = 1,
115 [snd_soc_dapm_adc] = 2,
116 [snd_soc_dapm_hp] = 3,
117 [snd_soc_dapm_spk] = 3,
118 [snd_soc_dapm_line] = 3,
119 [snd_soc_dapm_out_drv] = 3,
120 [snd_soc_dapm_sink] = 3,
121 [snd_soc_dapm_pga] = 4,
122 [snd_soc_dapm_buffer] = 4,
123 [snd_soc_dapm_scheduler] = 4,
124 [snd_soc_dapm_effect] = 4,
125 [snd_soc_dapm_src] = 4,
126 [snd_soc_dapm_asrc] = 4,
127 [snd_soc_dapm_encoder] = 4,
128 [snd_soc_dapm_decoder] = 4,
129 [snd_soc_dapm_switch] = 5,
130 [snd_soc_dapm_mixer_named_ctl] = 5,
131 [snd_soc_dapm_mixer] = 5,
132 [snd_soc_dapm_dac] = 6,
133 [snd_soc_dapm_mic] = 7,
134 [snd_soc_dapm_siggen] = 7,
135 [snd_soc_dapm_input] = 7,
136 [snd_soc_dapm_output] = 7,
137 [snd_soc_dapm_micbias] = 8,
138 [snd_soc_dapm_vmid] = 8,
139 [snd_soc_dapm_mux] = 9,
140 [snd_soc_dapm_demux] = 9,
141 [snd_soc_dapm_aif_in] = 10,
142 [snd_soc_dapm_aif_out] = 10,
143 [snd_soc_dapm_dai_in] = 10,
144 [snd_soc_dapm_dai_out] = 10,
145 [snd_soc_dapm_dai_link] = 11,
146 [snd_soc_dapm_supply] = 12,
147 [snd_soc_dapm_clock_supply] = 13,
148 [snd_soc_dapm_regulator_supply] = 13,
149 [snd_soc_dapm_post] = 14,
152 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
154 if (dapm->card && dapm->card->instantiated)
155 lockdep_assert_held(&dapm->card->dapm_mutex);
158 static void pop_wait(u32 pop_time)
161 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
164 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
172 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
177 vsnprintf(buf, PAGE_SIZE, fmt, args);
178 dev_info(dev, "%s", buf);
184 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
186 return !list_empty(&w->dirty);
189 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
191 dapm_assert_locked(w->dapm);
193 if (!dapm_dirty_widget(w)) {
194 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
196 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
201 * Common implementation for dapm_widget_invalidate_input_paths() and
202 * dapm_widget_invalidate_output_paths(). The function is inlined since the
203 * combined size of the two specialized functions is only marginally larger then
204 * the size of the generic function and at the same time the fast path of the
205 * specialized functions is significantly smaller than the generic function.
207 static __always_inline void dapm_widget_invalidate_paths(
208 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
210 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
211 struct snd_soc_dapm_widget *node;
212 struct snd_soc_dapm_path *p;
215 dapm_assert_locked(w->dapm);
217 if (w->endpoints[dir] == -1)
220 list_add_tail(&w->work_list, &list);
221 w->endpoints[dir] = -1;
223 list_for_each_entry(w, &list, work_list) {
224 snd_soc_dapm_widget_for_each_path(w, dir, p) {
225 if (p->is_supply || p->weak || !p->connect)
227 node = p->node[rdir];
228 if (node->endpoints[dir] != -1) {
229 node->endpoints[dir] = -1;
230 list_add_tail(&node->work_list, &list);
237 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
239 * @w: The widget for which to invalidate the cached number of input paths
241 * Resets the cached number of inputs for the specified widget and all widgets
242 * that can be reached via outcoming paths from the widget.
244 * This function must be called if the number of output paths for a widget might
245 * have changed. E.g. if the source state of a widget changes or a path is added
246 * or activated with the widget as the sink.
248 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
250 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
254 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
256 * @w: The widget for which to invalidate the cached number of output paths
258 * Resets the cached number of outputs for the specified widget and all widgets
259 * that can be reached via incoming paths from the widget.
261 * This function must be called if the number of output paths for a widget might
262 * have changed. E.g. if the sink state of a widget changes or a path is added
263 * or activated with the widget as the source.
265 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
267 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
271 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
272 * for the widgets connected to a path
273 * @p: The path to invalidate
275 * Resets the cached number of inputs for the sink of the path and the cached
276 * number of outputs for the source of the path.
278 * This function must be called when a path is added, removed or the connected
281 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
284 * Weak paths or supply paths do not influence the number of input or
285 * output paths of their neighbors.
287 if (p->weak || p->is_supply)
291 * The number of connected endpoints is the sum of the number of
292 * connected endpoints of all neighbors. If a node with 0 connected
293 * endpoints is either connected or disconnected that sum won't change,
294 * so there is no need to re-check the path.
296 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
297 dapm_widget_invalidate_input_paths(p->sink);
298 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
299 dapm_widget_invalidate_output_paths(p->source);
302 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
304 struct snd_soc_dapm_widget *w;
306 mutex_lock(&card->dapm_mutex);
308 list_for_each_entry(w, &card->widgets, list) {
310 dapm_mark_dirty(w, "Rechecking endpoints");
311 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
312 dapm_widget_invalidate_output_paths(w);
313 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
314 dapm_widget_invalidate_input_paths(w);
318 mutex_unlock(&card->dapm_mutex);
320 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
322 /* create a new dapm widget */
323 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
324 const struct snd_soc_dapm_widget *_widget)
326 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
329 struct dapm_kcontrol_data {
331 struct snd_soc_dapm_widget *widget;
332 struct list_head paths;
333 struct snd_soc_dapm_widget_list *wlist;
336 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
337 struct snd_kcontrol *kcontrol, const char *ctrl_name)
339 struct dapm_kcontrol_data *data;
340 struct soc_mixer_control *mc;
345 data = kzalloc(sizeof(*data), GFP_KERNEL);
349 INIT_LIST_HEAD(&data->paths);
351 switch (widget->id) {
352 case snd_soc_dapm_switch:
353 case snd_soc_dapm_mixer:
354 case snd_soc_dapm_mixer_named_ctl:
355 mc = (struct soc_mixer_control *)kcontrol->private_value;
357 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
358 dev_warn(widget->dapm->dev,
359 "ASoC: Unsupported stereo autodisable control '%s'\n",
362 if (mc->autodisable) {
363 struct snd_soc_dapm_widget template;
365 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
372 memset(&template, 0, sizeof(template));
373 template.reg = mc->reg;
374 template.mask = (1 << fls(mc->max)) - 1;
375 template.shift = mc->shift;
377 template.off_val = mc->max;
379 template.off_val = 0;
380 template.on_val = template.off_val;
381 template.id = snd_soc_dapm_kcontrol;
382 template.name = name;
384 data->value = template.on_val;
387 snd_soc_dapm_new_control_unlocked(widget->dapm,
390 if (IS_ERR(data->widget)) {
391 ret = PTR_ERR(data->widget);
400 case snd_soc_dapm_demux:
401 case snd_soc_dapm_mux:
402 e = (struct soc_enum *)kcontrol->private_value;
404 if (e->autodisable) {
405 struct snd_soc_dapm_widget template;
407 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
414 memset(&template, 0, sizeof(template));
415 template.reg = e->reg;
416 template.mask = e->mask;
417 template.shift = e->shift_l;
418 template.off_val = snd_soc_enum_item_to_val(e, 0);
419 template.on_val = template.off_val;
420 template.id = snd_soc_dapm_kcontrol;
421 template.name = name;
423 data->value = template.on_val;
425 data->widget = snd_soc_dapm_new_control_unlocked(
426 widget->dapm, &template);
428 if (IS_ERR(data->widget)) {
429 ret = PTR_ERR(data->widget);
437 snd_soc_dapm_add_path(widget->dapm, data->widget,
445 kcontrol->private_data = data;
454 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
456 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
458 list_del(&data->paths);
463 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
464 const struct snd_kcontrol *kcontrol)
466 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
471 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
472 struct snd_soc_dapm_widget *widget)
474 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
475 struct snd_soc_dapm_widget_list *new_wlist;
479 n = data->wlist->num_widgets + 1;
483 new_wlist = krealloc(data->wlist,
484 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
488 new_wlist->widgets[n - 1] = widget;
489 new_wlist->num_widgets = n;
491 data->wlist = new_wlist;
496 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
497 struct snd_soc_dapm_path *path)
499 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
501 list_add_tail(&path->list_kcontrol, &data->paths);
504 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
506 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
511 return data->widget->power;
514 static struct list_head *dapm_kcontrol_get_path_list(
515 const struct snd_kcontrol *kcontrol)
517 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
522 #define dapm_kcontrol_for_each_path(path, kcontrol) \
523 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
526 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
528 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
532 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
534 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
537 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
539 if (data->value == value)
543 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
544 case snd_soc_dapm_switch:
545 case snd_soc_dapm_mixer:
546 case snd_soc_dapm_mixer_named_ctl:
547 data->widget->on_val = value & data->widget->mask;
549 case snd_soc_dapm_demux:
550 case snd_soc_dapm_mux:
551 data->widget->on_val = value >> data->widget->shift;
554 data->widget->on_val = value;
565 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
567 * @kcontrol: The kcontrol
569 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
570 struct snd_kcontrol *kcontrol)
572 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
574 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
577 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
579 * @kcontrol: The kcontrol
581 * Note: This function must only be used on kcontrols that are known to have
582 * been registered for a CODEC. Otherwise the behaviour is undefined.
584 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
585 struct snd_kcontrol *kcontrol)
587 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
589 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
591 static void dapm_reset(struct snd_soc_card *card)
593 struct snd_soc_dapm_widget *w;
595 lockdep_assert_held(&card->dapm_mutex);
597 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
599 list_for_each_entry(w, &card->widgets, list) {
600 w->new_power = w->power;
601 w->power_checked = false;
605 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
607 if (!dapm->component)
609 return dapm->component->name_prefix;
612 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
615 if (!dapm->component)
617 return snd_soc_component_read(dapm->component, reg, value);
620 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
621 int reg, unsigned int mask, unsigned int value)
623 if (!dapm->component)
625 return snd_soc_component_update_bits(dapm->component, reg,
629 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
630 int reg, unsigned int mask, unsigned int value)
632 if (!dapm->component)
634 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
637 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
640 snd_soc_component_async_complete(dapm->component);
643 static struct snd_soc_dapm_widget *
644 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
646 struct snd_soc_dapm_widget *w = wcache->widget;
647 struct list_head *wlist;
652 wlist = &w->dapm->card->widgets;
654 list_for_each_entry_from(w, wlist, list) {
655 if (!strcmp(name, w->name))
666 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
667 struct snd_soc_dapm_widget *w)
673 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
674 * @dapm: The DAPM context for which to set the level
675 * @level: The level to set
677 * Forces the DAPM bias level to a specific state. It will call the bias level
678 * callback of DAPM context with the specified level. This will even happen if
679 * the context is already at the same level. Furthermore it will not go through
680 * the normal bias level sequencing, meaning any intermediate states between the
681 * current and the target state will not be entered.
683 * Note that the change in bias level is only temporary and the next time
684 * snd_soc_dapm_sync() is called the state will be set to the level as
685 * determined by the DAPM core. The function is mainly intended to be used to
686 * used during probe or resume from suspend to power up the device so
687 * initialization can be done, before the DAPM core takes over.
689 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
690 enum snd_soc_bias_level level)
694 if (dapm->set_bias_level)
695 ret = dapm->set_bias_level(dapm, level);
698 dapm->bias_level = level;
702 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
705 * snd_soc_dapm_set_bias_level - set the bias level for the system
706 * @dapm: DAPM context
707 * @level: level to configure
709 * Configure the bias (power) levels for the SoC audio device.
711 * Returns 0 for success else error.
713 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
714 enum snd_soc_bias_level level)
716 struct snd_soc_card *card = dapm->card;
719 trace_snd_soc_bias_level_start(card, level);
721 if (card && card->set_bias_level)
722 ret = card->set_bias_level(card, dapm, level);
726 if (!card || dapm != &card->dapm)
727 ret = snd_soc_dapm_force_bias_level(dapm, level);
732 if (card && card->set_bias_level_post)
733 ret = card->set_bias_level_post(card, dapm, level);
735 trace_snd_soc_bias_level_done(card, level);
740 /* connect mux widget to its interconnecting audio paths */
741 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
742 struct snd_soc_dapm_path *path, const char *control_name,
743 struct snd_soc_dapm_widget *w)
745 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
746 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
747 unsigned int val, item;
750 if (e->reg != SND_SOC_NOPM) {
751 soc_dapm_read(dapm, e->reg, &val);
752 val = (val >> e->shift_l) & e->mask;
753 item = snd_soc_enum_val_to_item(e, val);
755 /* since a virtual mux has no backing registers to
756 * decide which path to connect, it will try to match
757 * with the first enumeration. This is to ensure
758 * that the default mux choice (the first) will be
759 * correctly powered up during initialization.
764 for (i = 0; i < e->items; i++) {
765 if (!(strcmp(control_name, e->texts[i]))) {
766 path->name = e->texts[i];
778 /* set up initial codec paths */
779 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
782 struct soc_mixer_control *mc = (struct soc_mixer_control *)
783 p->sink->kcontrol_news[i].private_value;
784 unsigned int reg = mc->reg;
785 unsigned int shift = mc->shift;
786 unsigned int max = mc->max;
787 unsigned int mask = (1 << fls(max)) - 1;
788 unsigned int invert = mc->invert;
791 if (reg != SND_SOC_NOPM) {
792 soc_dapm_read(p->sink->dapm, reg, &val);
794 * The nth_path argument allows this function to know
795 * which path of a kcontrol it is setting the initial
796 * status for. Ideally this would support any number
797 * of paths and channels. But since kcontrols only come
798 * in mono and stereo variants, we are limited to 2
801 * The following code assumes for stereo controls the
802 * first path is the left channel, and all remaining
803 * paths are the right channel.
805 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
807 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
808 val = (val >> mc->rshift) & mask;
810 val = (val >> shift) & mask;
816 /* since a virtual mixer has no backing registers to
817 * decide which path to connect, it will try to match
818 * with initial state. This is to ensure
819 * that the default mixer choice will be
820 * correctly powered up during initialization.
826 /* connect mixer widget to its interconnecting audio paths */
827 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
828 struct snd_soc_dapm_path *path, const char *control_name)
832 /* search for mixer kcontrol */
833 for (i = 0; i < path->sink->num_kcontrols; i++) {
834 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
835 path->name = path->sink->kcontrol_news[i].name;
836 dapm_set_mixer_path_status(path, i, nth_path++);
843 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
844 struct snd_soc_dapm_widget *kcontrolw,
845 const struct snd_kcontrol_new *kcontrol_new,
846 struct snd_kcontrol **kcontrol)
848 struct snd_soc_dapm_widget *w;
853 list_for_each_entry(w, &dapm->card->widgets, list) {
854 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
856 for (i = 0; i < w->num_kcontrols; i++) {
857 if (&w->kcontrol_news[i] == kcontrol_new) {
859 *kcontrol = w->kcontrols[i];
869 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
870 * create it. Either way, add the widget into the control's widget list
872 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
875 struct snd_soc_dapm_context *dapm = w->dapm;
876 struct snd_card *card = dapm->card->snd_card;
880 struct snd_kcontrol *kcontrol;
881 bool wname_in_long_name, kcname_in_long_name;
882 char *long_name = NULL;
886 prefix = soc_dapm_prefix(dapm);
888 prefix_len = strlen(prefix) + 1;
892 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
897 wname_in_long_name = false;
898 kcname_in_long_name = true;
901 case snd_soc_dapm_switch:
902 case snd_soc_dapm_mixer:
903 case snd_soc_dapm_pga:
904 case snd_soc_dapm_out_drv:
905 wname_in_long_name = true;
906 kcname_in_long_name = true;
908 case snd_soc_dapm_mixer_named_ctl:
909 wname_in_long_name = false;
910 kcname_in_long_name = true;
912 case snd_soc_dapm_demux:
913 case snd_soc_dapm_mux:
914 wname_in_long_name = true;
915 kcname_in_long_name = false;
922 if (wname_in_long_name && kcname_in_long_name) {
924 * The control will get a prefix from the control
925 * creation process but we're also using the same
926 * prefix for widgets so cut the prefix off the
927 * front of the widget name.
929 long_name = kasprintf(GFP_KERNEL, "%s %s",
930 w->name + prefix_len,
931 w->kcontrol_news[kci].name);
932 if (long_name == NULL)
936 } else if (wname_in_long_name) {
938 name = w->name + prefix_len;
941 name = w->kcontrol_news[kci].name;
944 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
951 kcontrol->private_free = dapm_kcontrol_free;
953 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
955 snd_ctl_free_one(kcontrol);
959 ret = snd_ctl_add(card, kcontrol);
962 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
968 ret = dapm_kcontrol_add_widget(kcontrol, w);
970 w->kcontrols[kci] = kcontrol;
978 /* create new dapm mixer control */
979 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
982 struct snd_soc_dapm_path *path;
983 struct dapm_kcontrol_data *data;
986 for (i = 0; i < w->num_kcontrols; i++) {
988 snd_soc_dapm_widget_for_each_source_path(w, path) {
989 /* mixer/mux paths name must match control name */
990 if (path->name != (char *)w->kcontrol_news[i].name)
993 if (!w->kcontrols[i]) {
994 ret = dapm_create_or_share_kcontrol(w, i);
999 dapm_kcontrol_add_path(w->kcontrols[i], path);
1001 data = snd_kcontrol_chip(w->kcontrols[i]);
1003 snd_soc_dapm_add_path(data->widget->dapm,
1013 /* create new dapm mux control */
1014 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1016 struct snd_soc_dapm_context *dapm = w->dapm;
1017 enum snd_soc_dapm_direction dir;
1018 struct snd_soc_dapm_path *path;
1023 case snd_soc_dapm_mux:
1024 dir = SND_SOC_DAPM_DIR_OUT;
1027 case snd_soc_dapm_demux:
1028 dir = SND_SOC_DAPM_DIR_IN;
1035 if (w->num_kcontrols != 1) {
1037 "ASoC: %s %s has incorrect number of controls\n", type,
1042 if (list_empty(&w->edges[dir])) {
1043 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1047 ret = dapm_create_or_share_kcontrol(w, 0);
1051 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1053 dapm_kcontrol_add_path(w->kcontrols[0], path);
1059 /* create new dapm volume control */
1060 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1064 for (i = 0; i < w->num_kcontrols; i++) {
1065 ret = dapm_create_or_share_kcontrol(w, i);
1073 /* create new dapm dai link control */
1074 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1077 struct snd_kcontrol *kcontrol;
1078 struct snd_soc_dapm_context *dapm = w->dapm;
1079 struct snd_card *card = dapm->card->snd_card;
1081 /* create control for links with > 1 config */
1082 if (w->num_params <= 1)
1086 for (i = 0; i < w->num_kcontrols; i++) {
1087 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1089 ret = snd_ctl_add(card, kcontrol);
1092 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1093 w->name, w->kcontrol_news[i].name, ret);
1096 kcontrol->private_data = w;
1097 w->kcontrols[i] = kcontrol;
1103 /* We implement power down on suspend by checking the power state of
1104 * the ALSA card - when we are suspending the ALSA state for the card
1107 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1109 int level = snd_power_get_state(widget->dapm->card->snd_card);
1112 case SNDRV_CTL_POWER_D3hot:
1113 case SNDRV_CTL_POWER_D3cold:
1114 if (widget->ignore_suspend)
1115 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1117 return widget->ignore_suspend;
1123 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1124 struct list_head *widgets)
1126 struct snd_soc_dapm_widget *w;
1127 struct list_head *it;
1128 unsigned int size = 0;
1131 list_for_each(it, widgets)
1134 *list = kzalloc(sizeof(**list) + size * sizeof(*w), GFP_KERNEL);
1138 list_for_each_entry(w, widgets, work_list)
1139 (*list)->widgets[i++] = w;
1141 (*list)->num_widgets = i;
1147 * Common implementation for is_connected_output_ep() and
1148 * is_connected_input_ep(). The function is inlined since the combined size of
1149 * the two specialized functions is only marginally larger then the size of the
1150 * generic function and at the same time the fast path of the specialized
1151 * functions is significantly smaller than the generic function.
1153 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1154 struct list_head *list, enum snd_soc_dapm_direction dir,
1155 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1156 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1157 enum snd_soc_dapm_direction)),
1158 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1159 enum snd_soc_dapm_direction))
1161 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1162 struct snd_soc_dapm_path *path;
1165 if (widget->endpoints[dir] >= 0)
1166 return widget->endpoints[dir];
1168 DAPM_UPDATE_STAT(widget, path_checks);
1170 /* do we need to add this widget to the list ? */
1172 list_add_tail(&widget->work_list, list);
1174 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1176 custom_stop_condition = NULL;
1179 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1180 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1181 return widget->endpoints[dir];
1184 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1185 DAPM_UPDATE_STAT(widget, neighbour_checks);
1187 if (path->weak || path->is_supply)
1193 trace_snd_soc_dapm_path(widget, dir, path);
1195 if (path->connect) {
1197 con += fn(path->node[dir], list, custom_stop_condition);
1202 widget->endpoints[dir] = con;
1208 * Recursively check for a completed path to an active or physically connected
1209 * output widget. Returns number of complete paths.
1211 * Optionally, can be supplied with a function acting as a stopping condition.
1212 * This function takes the dapm widget currently being examined and the walk
1213 * direction as an arguments, it should return true if widgets from that point
1214 * in the graph onwards should not be added to the widget list.
1216 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1217 struct list_head *list,
1218 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1219 enum snd_soc_dapm_direction))
1221 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1222 is_connected_output_ep, custom_stop_condition);
1226 * Recursively check for a completed path to an active or physically connected
1227 * input widget. Returns number of complete paths.
1229 * Optionally, can be supplied with a function acting as a stopping condition.
1230 * This function takes the dapm widget currently being examined and the walk
1231 * direction as an arguments, it should return true if the walk should be
1232 * stopped and false otherwise.
1234 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1235 struct list_head *list,
1236 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1237 enum snd_soc_dapm_direction))
1239 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1240 is_connected_input_ep, custom_stop_condition);
1244 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1245 * @dai: the soc DAI.
1246 * @stream: stream direction.
1247 * @list: list of active widgets for this stream.
1248 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1249 * walk based on custom logic.
1251 * Queries DAPM graph as to whether a valid audio stream path exists for
1252 * the initial stream specified by name. This takes into account
1253 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1255 * Optionally, can be supplied with a function acting as a stopping condition.
1256 * This function takes the dapm widget currently being examined and the walk
1257 * direction as an arguments, it should return true if the walk should be
1258 * stopped and false otherwise.
1260 * Returns the number of valid paths or negative error.
1262 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1263 struct snd_soc_dapm_widget_list **list,
1264 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1265 enum snd_soc_dapm_direction))
1267 struct snd_soc_card *card = dai->component->card;
1268 struct snd_soc_dapm_widget *w;
1273 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1276 * For is_connected_{output,input}_ep fully discover the graph we need
1277 * to reset the cached number of inputs and outputs.
1279 list_for_each_entry(w, &card->widgets, list) {
1280 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1281 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1284 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1285 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1286 custom_stop_condition);
1288 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1289 custom_stop_condition);
1291 /* Drop starting point */
1292 list_del(widgets.next);
1294 ret = dapm_widget_list_create(list, &widgets);
1298 trace_snd_soc_dapm_connected(paths, stream);
1299 mutex_unlock(&card->dapm_mutex);
1305 * Handler for regulator supply widget.
1307 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1308 struct snd_kcontrol *kcontrol, int event)
1312 soc_dapm_async_complete(w->dapm);
1314 if (SND_SOC_DAPM_EVENT_ON(event)) {
1315 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1316 ret = regulator_allow_bypass(w->regulator, false);
1318 dev_warn(w->dapm->dev,
1319 "ASoC: Failed to unbypass %s: %d\n",
1323 return regulator_enable(w->regulator);
1325 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1326 ret = regulator_allow_bypass(w->regulator, true);
1328 dev_warn(w->dapm->dev,
1329 "ASoC: Failed to bypass %s: %d\n",
1333 return regulator_disable_deferred(w->regulator, w->shift);
1336 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1339 * Handler for clock supply widget.
1341 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1342 struct snd_kcontrol *kcontrol, int event)
1347 soc_dapm_async_complete(w->dapm);
1349 #ifdef CONFIG_HAVE_CLK
1350 if (SND_SOC_DAPM_EVENT_ON(event)) {
1351 return clk_prepare_enable(w->clk);
1353 clk_disable_unprepare(w->clk);
1359 EXPORT_SYMBOL_GPL(dapm_clock_event);
1361 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1363 if (w->power_checked)
1364 return w->new_power;
1369 w->new_power = w->power_check(w);
1371 w->power_checked = true;
1373 return w->new_power;
1376 /* Generic check to see if a widget should be powered. */
1377 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1381 DAPM_UPDATE_STAT(w, power_checks);
1383 in = is_connected_input_ep(w, NULL, NULL);
1384 out = is_connected_output_ep(w, NULL, NULL);
1385 return out != 0 && in != 0;
1388 /* Check to see if a power supply is needed */
1389 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1391 struct snd_soc_dapm_path *path;
1393 DAPM_UPDATE_STAT(w, power_checks);
1395 /* Check if one of our outputs is connected */
1396 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1397 DAPM_UPDATE_STAT(w, neighbour_checks);
1402 if (path->connected &&
1403 !path->connected(path->source, path->sink))
1406 if (dapm_widget_power_check(path->sink))
1413 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1415 return w->connected;
1418 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1419 struct snd_soc_dapm_widget *b,
1427 sort = dapm_down_seq;
1429 if (sort[a->id] != sort[b->id])
1430 return sort[a->id] - sort[b->id];
1431 if (a->subseq != b->subseq) {
1433 return a->subseq - b->subseq;
1435 return b->subseq - a->subseq;
1437 if (a->reg != b->reg)
1438 return a->reg - b->reg;
1439 if (a->dapm != b->dapm)
1440 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1445 /* Insert a widget in order into a DAPM power sequence. */
1446 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1447 struct list_head *list,
1450 struct snd_soc_dapm_widget *w;
1452 list_for_each_entry(w, list, power_list)
1453 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1454 list_add_tail(&new_widget->power_list, &w->power_list);
1458 list_add_tail(&new_widget->power_list, list);
1461 static void dapm_seq_check_event(struct snd_soc_card *card,
1462 struct snd_soc_dapm_widget *w, int event)
1464 const char *ev_name;
1468 case SND_SOC_DAPM_PRE_PMU:
1469 ev_name = "PRE_PMU";
1472 case SND_SOC_DAPM_POST_PMU:
1473 ev_name = "POST_PMU";
1476 case SND_SOC_DAPM_PRE_PMD:
1477 ev_name = "PRE_PMD";
1480 case SND_SOC_DAPM_POST_PMD:
1481 ev_name = "POST_PMD";
1484 case SND_SOC_DAPM_WILL_PMU:
1485 ev_name = "WILL_PMU";
1488 case SND_SOC_DAPM_WILL_PMD:
1489 ev_name = "WILL_PMD";
1493 WARN(1, "Unknown event %d\n", event);
1497 if (w->new_power != power)
1500 if (w->event && (w->event_flags & event)) {
1501 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1503 soc_dapm_async_complete(w->dapm);
1504 trace_snd_soc_dapm_widget_event_start(w, event);
1505 ret = w->event(w, NULL, event);
1506 trace_snd_soc_dapm_widget_event_done(w, event);
1508 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1509 ev_name, w->name, ret);
1513 /* Apply the coalesced changes from a DAPM sequence */
1514 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1515 struct list_head *pending)
1517 struct snd_soc_dapm_context *dapm;
1518 struct snd_soc_dapm_widget *w;
1520 unsigned int value = 0;
1521 unsigned int mask = 0;
1523 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1527 list_for_each_entry(w, pending, power_list) {
1528 WARN_ON(reg != w->reg || dapm != w->dapm);
1529 w->power = w->new_power;
1531 mask |= w->mask << w->shift;
1533 value |= w->on_val << w->shift;
1535 value |= w->off_val << w->shift;
1537 pop_dbg(dapm->dev, card->pop_time,
1538 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1539 w->name, reg, value, mask);
1541 /* Check for events */
1542 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1543 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1547 /* Any widget will do, they should all be updating the
1551 pop_dbg(dapm->dev, card->pop_time,
1552 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1553 value, mask, reg, card->pop_time);
1554 pop_wait(card->pop_time);
1555 soc_dapm_update_bits(dapm, reg, mask, value);
1558 list_for_each_entry(w, pending, power_list) {
1559 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1560 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1564 /* Apply a DAPM power sequence.
1566 * We walk over a pre-sorted list of widgets to apply power to. In
1567 * order to minimise the number of writes to the device required
1568 * multiple widgets will be updated in a single write where possible.
1569 * Currently anything that requires more than a single write is not
1572 static void dapm_seq_run(struct snd_soc_card *card,
1573 struct list_head *list, int event, bool power_up)
1575 struct snd_soc_dapm_widget *w, *n;
1576 struct snd_soc_dapm_context *d;
1579 int cur_subseq = -1;
1580 int cur_reg = SND_SOC_NOPM;
1581 struct snd_soc_dapm_context *cur_dapm = NULL;
1588 sort = dapm_down_seq;
1590 list_for_each_entry_safe(w, n, list, power_list) {
1593 /* Do we need to apply any queued changes? */
1594 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1595 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1596 if (!list_empty(&pending))
1597 dapm_seq_run_coalesced(card, &pending);
1599 if (cur_dapm && cur_dapm->seq_notifier) {
1600 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1601 if (sort[i] == cur_sort)
1602 cur_dapm->seq_notifier(cur_dapm,
1607 if (cur_dapm && w->dapm != cur_dapm)
1608 soc_dapm_async_complete(cur_dapm);
1610 INIT_LIST_HEAD(&pending);
1612 cur_subseq = INT_MIN;
1613 cur_reg = SND_SOC_NOPM;
1618 case snd_soc_dapm_pre:
1622 if (event == SND_SOC_DAPM_STREAM_START)
1624 NULL, SND_SOC_DAPM_PRE_PMU);
1625 else if (event == SND_SOC_DAPM_STREAM_STOP)
1627 NULL, SND_SOC_DAPM_PRE_PMD);
1630 case snd_soc_dapm_post:
1634 if (event == SND_SOC_DAPM_STREAM_START)
1636 NULL, SND_SOC_DAPM_POST_PMU);
1637 else if (event == SND_SOC_DAPM_STREAM_STOP)
1639 NULL, SND_SOC_DAPM_POST_PMD);
1643 /* Queue it up for application */
1644 cur_sort = sort[w->id];
1645 cur_subseq = w->subseq;
1648 list_move(&w->power_list, &pending);
1653 dev_err(w->dapm->dev,
1654 "ASoC: Failed to apply widget power: %d\n", ret);
1657 if (!list_empty(&pending))
1658 dapm_seq_run_coalesced(card, &pending);
1660 if (cur_dapm && cur_dapm->seq_notifier) {
1661 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1662 if (sort[i] == cur_sort)
1663 cur_dapm->seq_notifier(cur_dapm,
1667 list_for_each_entry(d, &card->dapm_list, list) {
1668 soc_dapm_async_complete(d);
1672 static void dapm_widget_update(struct snd_soc_card *card)
1674 struct snd_soc_dapm_update *update = card->update;
1675 struct snd_soc_dapm_widget_list *wlist;
1676 struct snd_soc_dapm_widget *w = NULL;
1680 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1683 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1685 for (wi = 0; wi < wlist->num_widgets; wi++) {
1686 w = wlist->widgets[wi];
1688 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1689 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1691 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1699 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1702 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1705 if (update->has_second_set) {
1706 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1707 update->mask2, update->val2);
1709 dev_err(w->dapm->dev,
1710 "ASoC: %s DAPM update failed: %d\n",
1714 for (wi = 0; wi < wlist->num_widgets; wi++) {
1715 w = wlist->widgets[wi];
1717 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1718 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1720 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1726 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1727 * they're changing state.
1729 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1731 struct snd_soc_dapm_context *d = data;
1734 /* If we're off and we're not supposed to go into STANDBY */
1735 if (d->bias_level == SND_SOC_BIAS_OFF &&
1736 d->target_bias_level != SND_SOC_BIAS_OFF) {
1738 pm_runtime_get_sync(d->dev);
1740 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1743 "ASoC: Failed to turn on bias: %d\n", ret);
1746 /* Prepare for a transition to ON or away from ON */
1747 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1748 d->bias_level != SND_SOC_BIAS_ON) ||
1749 (d->target_bias_level != SND_SOC_BIAS_ON &&
1750 d->bias_level == SND_SOC_BIAS_ON)) {
1751 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1754 "ASoC: Failed to prepare bias: %d\n", ret);
1758 /* Async callback run prior to DAPM sequences - brings to their final
1761 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1763 struct snd_soc_dapm_context *d = data;
1766 /* If we just powered the last thing off drop to standby bias */
1767 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1768 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1769 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1770 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1772 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1776 /* If we're in standby and can support bias off then do that */
1777 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1778 d->target_bias_level == SND_SOC_BIAS_OFF) {
1779 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1781 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1785 pm_runtime_put(d->dev);
1788 /* If we just powered up then move to active bias */
1789 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1790 d->target_bias_level == SND_SOC_BIAS_ON) {
1791 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1793 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1798 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1799 bool power, bool connect)
1801 /* If a connection is being made or broken then that update
1802 * will have marked the peer dirty, otherwise the widgets are
1803 * not connected and this update has no impact. */
1807 /* If the peer is already in the state we're moving to then we
1808 * won't have an impact on it. */
1809 if (power != peer->power)
1810 dapm_mark_dirty(peer, "peer state change");
1813 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1814 struct list_head *up_list,
1815 struct list_head *down_list)
1817 struct snd_soc_dapm_path *path;
1819 if (w->power == power)
1822 trace_snd_soc_dapm_widget_power(w, power);
1824 /* If we changed our power state perhaps our neigbours changed
1827 snd_soc_dapm_widget_for_each_source_path(w, path)
1828 dapm_widget_set_peer_power(path->source, power, path->connect);
1830 /* Supplies can't affect their outputs, only their inputs */
1831 if (!w->is_supply) {
1832 snd_soc_dapm_widget_for_each_sink_path(w, path)
1833 dapm_widget_set_peer_power(path->sink, power,
1838 dapm_seq_insert(w, up_list, true);
1840 dapm_seq_insert(w, down_list, false);
1843 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1844 struct list_head *up_list,
1845 struct list_head *down_list)
1850 case snd_soc_dapm_pre:
1851 dapm_seq_insert(w, down_list, false);
1853 case snd_soc_dapm_post:
1854 dapm_seq_insert(w, up_list, true);
1858 power = dapm_widget_power_check(w);
1860 dapm_widget_set_power(w, power, up_list, down_list);
1865 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1867 if (dapm->idle_bias_off)
1870 switch (snd_power_get_state(dapm->card->snd_card)) {
1871 case SNDRV_CTL_POWER_D3hot:
1872 case SNDRV_CTL_POWER_D3cold:
1873 return dapm->suspend_bias_off;
1882 * Scan each dapm widget for complete audio path.
1883 * A complete path is a route that has valid endpoints i.e.:-
1885 * o DAC to output pin.
1886 * o Input pin to ADC.
1887 * o Input pin to Output pin (bypass, sidetone)
1888 * o DAC to ADC (loopback).
1890 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1892 struct snd_soc_dapm_widget *w;
1893 struct snd_soc_dapm_context *d;
1895 LIST_HEAD(down_list);
1896 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1897 enum snd_soc_bias_level bias;
1899 lockdep_assert_held(&card->dapm_mutex);
1901 trace_snd_soc_dapm_start(card);
1903 list_for_each_entry(d, &card->dapm_list, list) {
1904 if (dapm_idle_bias_off(d))
1905 d->target_bias_level = SND_SOC_BIAS_OFF;
1907 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1912 /* Check which widgets we need to power and store them in
1913 * lists indicating if they should be powered up or down. We
1914 * only check widgets that have been flagged as dirty but note
1915 * that new widgets may be added to the dirty list while we
1918 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1919 dapm_power_one_widget(w, &up_list, &down_list);
1922 list_for_each_entry(w, &card->widgets, list) {
1924 case snd_soc_dapm_pre:
1925 case snd_soc_dapm_post:
1926 /* These widgets always need to be powered */
1929 list_del_init(&w->dirty);
1936 /* Supplies and micbiases only bring the
1937 * context up to STANDBY as unless something
1938 * else is active and passing audio they
1939 * generally don't require full power. Signal
1940 * generators are virtual pins and have no
1941 * power impact themselves.
1944 case snd_soc_dapm_siggen:
1945 case snd_soc_dapm_vmid:
1947 case snd_soc_dapm_supply:
1948 case snd_soc_dapm_regulator_supply:
1949 case snd_soc_dapm_clock_supply:
1950 case snd_soc_dapm_micbias:
1951 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1952 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1955 d->target_bias_level = SND_SOC_BIAS_ON;
1962 /* Force all contexts in the card to the same bias state if
1963 * they're not ground referenced.
1965 bias = SND_SOC_BIAS_OFF;
1966 list_for_each_entry(d, &card->dapm_list, list)
1967 if (d->target_bias_level > bias)
1968 bias = d->target_bias_level;
1969 list_for_each_entry(d, &card->dapm_list, list)
1970 if (!dapm_idle_bias_off(d))
1971 d->target_bias_level = bias;
1973 trace_snd_soc_dapm_walk_done(card);
1975 /* Run card bias changes at first */
1976 dapm_pre_sequence_async(&card->dapm, 0);
1977 /* Run other bias changes in parallel */
1978 list_for_each_entry(d, &card->dapm_list, list) {
1979 if (d != &card->dapm)
1980 async_schedule_domain(dapm_pre_sequence_async, d,
1983 async_synchronize_full_domain(&async_domain);
1985 list_for_each_entry(w, &down_list, power_list) {
1986 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1989 list_for_each_entry(w, &up_list, power_list) {
1990 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1993 /* Power down widgets first; try to avoid amplifying pops. */
1994 dapm_seq_run(card, &down_list, event, false);
1996 dapm_widget_update(card);
1999 dapm_seq_run(card, &up_list, event, true);
2001 /* Run all the bias changes in parallel */
2002 list_for_each_entry(d, &card->dapm_list, list) {
2003 if (d != &card->dapm)
2004 async_schedule_domain(dapm_post_sequence_async, d,
2007 async_synchronize_full_domain(&async_domain);
2008 /* Run card bias changes at last */
2009 dapm_post_sequence_async(&card->dapm, 0);
2011 /* do we need to notify any clients that DAPM event is complete */
2012 list_for_each_entry(d, &card->dapm_list, list) {
2013 if (d->stream_event)
2014 d->stream_event(d, event);
2017 pop_dbg(card->dev, card->pop_time,
2018 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2019 pop_wait(card->pop_time);
2021 trace_snd_soc_dapm_done(card);
2026 #ifdef CONFIG_DEBUG_FS
2027 static ssize_t dapm_widget_power_read_file(struct file *file,
2028 char __user *user_buf,
2029 size_t count, loff_t *ppos)
2031 struct snd_soc_dapm_widget *w = file->private_data;
2032 struct snd_soc_card *card = w->dapm->card;
2033 enum snd_soc_dapm_direction dir, rdir;
2037 struct snd_soc_dapm_path *p = NULL;
2039 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2043 mutex_lock(&card->dapm_mutex);
2045 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2050 in = is_connected_input_ep(w, NULL, NULL);
2051 out = is_connected_output_ep(w, NULL, NULL);
2054 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2055 w->name, w->power ? "On" : "Off",
2056 w->force ? " (forced)" : "", in, out);
2059 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2060 " - R%d(0x%x) mask 0x%x",
2061 w->reg, w->reg, w->mask << w->shift);
2063 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2066 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2068 w->active ? "active" : "inactive");
2070 snd_soc_dapm_for_each_direction(dir) {
2071 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2072 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2073 if (p->connected && !p->connected(w, p->node[rdir]))
2079 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2080 " %s \"%s\" \"%s\"\n",
2081 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2082 p->name ? p->name : "static",
2083 p->node[rdir]->name);
2087 mutex_unlock(&card->dapm_mutex);
2089 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2095 static const struct file_operations dapm_widget_power_fops = {
2096 .open = simple_open,
2097 .read = dapm_widget_power_read_file,
2098 .llseek = default_llseek,
2101 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2102 size_t count, loff_t *ppos)
2104 struct snd_soc_dapm_context *dapm = file->private_data;
2107 switch (dapm->bias_level) {
2108 case SND_SOC_BIAS_ON:
2111 case SND_SOC_BIAS_PREPARE:
2112 level = "Prepare\n";
2114 case SND_SOC_BIAS_STANDBY:
2115 level = "Standby\n";
2117 case SND_SOC_BIAS_OFF:
2121 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2122 level = "Unknown\n";
2126 return simple_read_from_buffer(user_buf, count, ppos, level,
2130 static const struct file_operations dapm_bias_fops = {
2131 .open = simple_open,
2132 .read = dapm_bias_read_file,
2133 .llseek = default_llseek,
2136 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2137 struct dentry *parent)
2141 if (!parent || IS_ERR(parent))
2144 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2146 if (IS_ERR(dapm->debugfs_dapm)) {
2148 "ASoC: Failed to create DAPM debugfs directory %ld\n",
2149 PTR_ERR(dapm->debugfs_dapm));
2153 d = debugfs_create_file("bias_level", 0444,
2154 dapm->debugfs_dapm, dapm,
2158 "ASoC: Failed to create bias level debugfs file: %ld\n",
2162 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2164 struct snd_soc_dapm_context *dapm = w->dapm;
2167 if (!dapm->debugfs_dapm || !w->name)
2170 d = debugfs_create_file(w->name, 0444,
2171 dapm->debugfs_dapm, w,
2172 &dapm_widget_power_fops);
2174 dev_warn(w->dapm->dev,
2175 "ASoC: Failed to create %s debugfs file: %ld\n",
2176 w->name, PTR_ERR(d));
2179 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2181 debugfs_remove_recursive(dapm->debugfs_dapm);
2185 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2186 struct dentry *parent)
2190 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2194 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2201 * soc_dapm_connect_path() - Connects or disconnects a path
2202 * @path: The path to update
2203 * @connect: The new connect state of the path. True if the path is connected,
2204 * false if it is disconnected.
2205 * @reason: The reason why the path changed (for debugging only)
2207 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2208 bool connect, const char *reason)
2210 if (path->connect == connect)
2213 path->connect = connect;
2214 dapm_mark_dirty(path->source, reason);
2215 dapm_mark_dirty(path->sink, reason);
2216 dapm_path_invalidate(path);
2219 /* test and update the power status of a mux widget */
2220 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2221 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2223 struct snd_soc_dapm_path *path;
2227 lockdep_assert_held(&card->dapm_mutex);
2229 /* find dapm widget path assoc with kcontrol */
2230 dapm_kcontrol_for_each_path(path, kcontrol) {
2232 /* we now need to match the string in the enum to the path */
2233 if (!(strcmp(path->name, e->texts[mux])))
2238 soc_dapm_connect_path(path, connect, "mux update");
2242 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2247 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2248 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2249 struct snd_soc_dapm_update *update)
2251 struct snd_soc_card *card = dapm->card;
2254 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2255 card->update = update;
2256 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2257 card->update = NULL;
2258 mutex_unlock(&card->dapm_mutex);
2260 soc_dpcm_runtime_update(card);
2263 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2265 /* test and update the power status of a mixer or switch widget */
2266 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2267 struct snd_kcontrol *kcontrol,
2268 int connect, int rconnect)
2270 struct snd_soc_dapm_path *path;
2273 lockdep_assert_held(&card->dapm_mutex);
2275 /* find dapm widget path assoc with kcontrol */
2276 dapm_kcontrol_for_each_path(path, kcontrol) {
2278 * Ideally this function should support any number of
2279 * paths and channels. But since kcontrols only come
2280 * in mono and stereo variants, we are limited to 2
2283 * The following code assumes for stereo controls the
2284 * first path (when 'found == 0') is the left channel,
2285 * and all remaining paths (when 'found == 1') are the
2288 * A stereo control is signified by a valid 'rconnect'
2289 * value, either 0 for unconnected, or >= 0 for connected.
2290 * This is chosen instead of using snd_soc_volsw_is_stereo,
2291 * so that the behavior of snd_soc_dapm_mixer_update_power
2292 * doesn't change even when the kcontrol passed in is
2295 * It passes 'connect' as the path connect status for
2296 * the left channel, and 'rconnect' for the right
2299 if (found && rconnect >= 0)
2300 soc_dapm_connect_path(path, rconnect, "mixer update");
2302 soc_dapm_connect_path(path, connect, "mixer update");
2307 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2312 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2313 struct snd_kcontrol *kcontrol, int connect,
2314 struct snd_soc_dapm_update *update)
2316 struct snd_soc_card *card = dapm->card;
2319 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2320 card->update = update;
2321 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2322 card->update = NULL;
2323 mutex_unlock(&card->dapm_mutex);
2325 soc_dpcm_runtime_update(card);
2328 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2330 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2333 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2334 struct snd_soc_dapm_widget *w;
2336 char *state = "not set";
2338 /* card won't be set for the dummy component, as a spot fix
2339 * we're checking for that case specifically here but in future
2340 * we will ensure that the dummy component looks like others.
2345 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2346 if (w->dapm != dapm)
2349 /* only display widgets that burn power */
2351 case snd_soc_dapm_hp:
2352 case snd_soc_dapm_mic:
2353 case snd_soc_dapm_spk:
2354 case snd_soc_dapm_line:
2355 case snd_soc_dapm_micbias:
2356 case snd_soc_dapm_dac:
2357 case snd_soc_dapm_adc:
2358 case snd_soc_dapm_pga:
2359 case snd_soc_dapm_out_drv:
2360 case snd_soc_dapm_mixer:
2361 case snd_soc_dapm_mixer_named_ctl:
2362 case snd_soc_dapm_supply:
2363 case snd_soc_dapm_regulator_supply:
2364 case snd_soc_dapm_clock_supply:
2366 count += sprintf(buf + count, "%s: %s\n",
2367 w->name, w->power ? "On":"Off");
2374 switch (snd_soc_dapm_get_bias_level(dapm)) {
2375 case SND_SOC_BIAS_ON:
2378 case SND_SOC_BIAS_PREPARE:
2381 case SND_SOC_BIAS_STANDBY:
2384 case SND_SOC_BIAS_OFF:
2388 count += sprintf(buf + count, "PM State: %s\n", state);
2393 /* show dapm widget status in sys fs */
2394 static ssize_t dapm_widget_show(struct device *dev,
2395 struct device_attribute *attr, char *buf)
2397 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2400 mutex_lock(&rtd->card->dapm_mutex);
2402 for (i = 0; i < rtd->num_codecs; i++) {
2403 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2405 count += dapm_widget_show_component(cmpnt, buf + count);
2408 mutex_unlock(&rtd->card->dapm_mutex);
2413 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2415 struct attribute *soc_dapm_dev_attrs[] = {
2416 &dev_attr_dapm_widget.attr,
2420 static void dapm_free_path(struct snd_soc_dapm_path *path)
2422 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2423 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2424 list_del(&path->list_kcontrol);
2425 list_del(&path->list);
2429 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2431 struct snd_soc_dapm_path *p, *next_p;
2432 enum snd_soc_dapm_direction dir;
2435 list_del(&w->dirty);
2437 * remove source and sink paths associated to this widget.
2438 * While removing the path, remove reference to it from both
2439 * source and sink widgets so that path is removed only once.
2441 snd_soc_dapm_for_each_direction(dir) {
2442 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2446 kfree(w->kcontrols);
2447 kfree_const(w->name);
2451 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2453 dapm->path_sink_cache.widget = NULL;
2454 dapm->path_source_cache.widget = NULL;
2457 /* free all dapm widgets and resources */
2458 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2460 struct snd_soc_dapm_widget *w, *next_w;
2462 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2463 if (w->dapm != dapm)
2465 snd_soc_dapm_free_widget(w);
2467 snd_soc_dapm_reset_cache(dapm);
2470 static struct snd_soc_dapm_widget *dapm_find_widget(
2471 struct snd_soc_dapm_context *dapm, const char *pin,
2472 bool search_other_contexts)
2474 struct snd_soc_dapm_widget *w;
2475 struct snd_soc_dapm_widget *fallback = NULL;
2477 list_for_each_entry(w, &dapm->card->widgets, list) {
2478 if (!strcmp(w->name, pin)) {
2479 if (w->dapm == dapm)
2486 if (search_other_contexts)
2493 * set the DAPM pin status:
2494 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2495 * error code; called from kcontrol put callback
2497 static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2498 const char *pin, int status)
2500 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2503 dapm_assert_locked(dapm);
2506 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2510 if (w->connected != status) {
2511 dapm_mark_dirty(w, "pin configuration");
2512 dapm_widget_invalidate_input_paths(w);
2513 dapm_widget_invalidate_output_paths(w);
2517 w->connected = status;
2525 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2526 * called from several API functions below
2528 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2529 const char *pin, int status)
2531 int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2533 return ret < 0 ? ret : 0;
2537 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2538 * @dapm: DAPM context
2540 * Walks all dapm audio paths and powers widgets according to their
2541 * stream or path usage.
2543 * Requires external locking.
2545 * Returns 0 for success.
2547 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2550 * Suppress early reports (eg, jacks syncing their state) to avoid
2551 * silly DAPM runs during card startup.
2553 if (!dapm->card || !dapm->card->instantiated)
2556 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2558 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2561 * snd_soc_dapm_sync - scan and power dapm paths
2562 * @dapm: DAPM context
2564 * Walks all dapm audio paths and powers widgets according to their
2565 * stream or path usage.
2567 * Returns 0 for success.
2569 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2573 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2574 ret = snd_soc_dapm_sync_unlocked(dapm);
2575 mutex_unlock(&dapm->card->dapm_mutex);
2578 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2581 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2582 * @w: The widget for which to update the flags
2584 * Some widgets have a dynamic category which depends on which neighbors they
2585 * are connected to. This function update the category for these widgets.
2587 * This function must be called whenever a path is added or removed to a widget.
2589 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2591 enum snd_soc_dapm_direction dir;
2592 struct snd_soc_dapm_path *p;
2596 case snd_soc_dapm_input:
2597 /* On a fully routed card an input is never a source */
2598 if (w->dapm->card->fully_routed)
2600 ep = SND_SOC_DAPM_EP_SOURCE;
2601 snd_soc_dapm_widget_for_each_source_path(w, p) {
2602 if (p->source->id == snd_soc_dapm_micbias ||
2603 p->source->id == snd_soc_dapm_mic ||
2604 p->source->id == snd_soc_dapm_line ||
2605 p->source->id == snd_soc_dapm_output) {
2611 case snd_soc_dapm_output:
2612 /* On a fully routed card a output is never a sink */
2613 if (w->dapm->card->fully_routed)
2615 ep = SND_SOC_DAPM_EP_SINK;
2616 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2617 if (p->sink->id == snd_soc_dapm_spk ||
2618 p->sink->id == snd_soc_dapm_hp ||
2619 p->sink->id == snd_soc_dapm_line ||
2620 p->sink->id == snd_soc_dapm_input) {
2626 case snd_soc_dapm_line:
2628 snd_soc_dapm_for_each_direction(dir) {
2629 if (!list_empty(&w->edges[dir]))
2630 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2640 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2641 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2642 const char *control)
2644 bool dynamic_source = false;
2645 bool dynamic_sink = false;
2650 switch (source->id) {
2651 case snd_soc_dapm_demux:
2652 dynamic_source = true;
2659 case snd_soc_dapm_mux:
2660 case snd_soc_dapm_switch:
2661 case snd_soc_dapm_mixer:
2662 case snd_soc_dapm_mixer_named_ctl:
2663 dynamic_sink = true;
2669 if (dynamic_source && dynamic_sink) {
2671 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2672 source->name, control, sink->name);
2674 } else if (!dynamic_source && !dynamic_sink) {
2676 "Control not supported for path %s -> [%s] -> %s\n",
2677 source->name, control, sink->name);
2684 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2685 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2686 const char *control,
2687 int (*connected)(struct snd_soc_dapm_widget *source,
2688 struct snd_soc_dapm_widget *sink))
2690 struct snd_soc_dapm_widget *widgets[2];
2691 enum snd_soc_dapm_direction dir;
2692 struct snd_soc_dapm_path *path;
2695 if (wsink->is_supply && !wsource->is_supply) {
2697 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2698 wsource->name, wsink->name);
2702 if (connected && !wsource->is_supply) {
2704 "connected() callback only supported for supply widgets (%s -> %s)\n",
2705 wsource->name, wsink->name);
2709 if (wsource->is_supply && control) {
2711 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2712 wsource->name, control, wsink->name);
2716 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2720 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2724 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2725 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2726 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2727 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2729 path->connected = connected;
2730 INIT_LIST_HEAD(&path->list);
2731 INIT_LIST_HEAD(&path->list_kcontrol);
2733 if (wsource->is_supply || wsink->is_supply)
2734 path->is_supply = 1;
2736 /* connect static paths */
2737 if (control == NULL) {
2740 switch (wsource->id) {
2741 case snd_soc_dapm_demux:
2742 ret = dapm_connect_mux(dapm, path, control, wsource);
2750 switch (wsink->id) {
2751 case snd_soc_dapm_mux:
2752 ret = dapm_connect_mux(dapm, path, control, wsink);
2756 case snd_soc_dapm_switch:
2757 case snd_soc_dapm_mixer:
2758 case snd_soc_dapm_mixer_named_ctl:
2759 ret = dapm_connect_mixer(dapm, path, control);
2768 list_add(&path->list, &dapm->card->paths);
2769 snd_soc_dapm_for_each_direction(dir)
2770 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2772 snd_soc_dapm_for_each_direction(dir) {
2773 dapm_update_widget_flags(widgets[dir]);
2774 dapm_mark_dirty(widgets[dir], "Route added");
2777 if (dapm->card->instantiated && path->connect)
2778 dapm_path_invalidate(path);
2786 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2787 const struct snd_soc_dapm_route *route)
2789 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2790 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2793 char prefixed_sink[80];
2794 char prefixed_source[80];
2798 prefix = soc_dapm_prefix(dapm);
2800 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2801 prefix, route->sink);
2802 sink = prefixed_sink;
2803 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2804 prefix, route->source);
2805 source = prefixed_source;
2808 source = route->source;
2811 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2812 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2814 if (wsink && wsource)
2818 * find src and dest widgets over all widgets but favor a widget from
2819 * current DAPM context
2821 list_for_each_entry(w, &dapm->card->widgets, list) {
2822 if (!wsink && !(strcmp(w->name, sink))) {
2824 if (w->dapm == dapm) {
2831 if (!wsource && !(strcmp(w->name, source))) {
2833 if (w->dapm == dapm) {
2840 /* use widget from another DAPM context if not found from this */
2846 if (wsource == NULL) {
2847 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2851 if (wsink == NULL) {
2852 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2858 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2859 dapm_wcache_update(&dapm->path_source_cache, wsource);
2861 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2868 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2869 source, route->control, sink);
2873 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2874 const struct snd_soc_dapm_route *route)
2876 struct snd_soc_dapm_widget *wsource, *wsink;
2877 struct snd_soc_dapm_path *path, *p;
2880 char prefixed_sink[80];
2881 char prefixed_source[80];
2884 if (route->control) {
2886 "ASoC: Removal of routes with controls not supported\n");
2890 prefix = soc_dapm_prefix(dapm);
2892 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2893 prefix, route->sink);
2894 sink = prefixed_sink;
2895 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2896 prefix, route->source);
2897 source = prefixed_source;
2900 source = route->source;
2904 list_for_each_entry(p, &dapm->card->paths, list) {
2905 if (strcmp(p->source->name, source) != 0)
2907 if (strcmp(p->sink->name, sink) != 0)
2914 wsource = path->source;
2917 dapm_mark_dirty(wsource, "Route removed");
2918 dapm_mark_dirty(wsink, "Route removed");
2920 dapm_path_invalidate(path);
2922 dapm_free_path(path);
2924 /* Update any path related flags */
2925 dapm_update_widget_flags(wsource);
2926 dapm_update_widget_flags(wsink);
2928 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2936 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2937 * @dapm: DAPM context
2938 * @route: audio routes
2939 * @num: number of routes
2941 * Connects 2 dapm widgets together via a named audio path. The sink is
2942 * the widget receiving the audio signal, whilst the source is the sender
2943 * of the audio signal.
2945 * Returns 0 for success else error. On error all resources can be freed
2946 * with a call to snd_soc_card_free().
2948 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2949 const struct snd_soc_dapm_route *route, int num)
2953 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2954 for (i = 0; i < num; i++) {
2955 r = snd_soc_dapm_add_route(dapm, route);
2957 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2959 route->control ? route->control : "direct",
2965 mutex_unlock(&dapm->card->dapm_mutex);
2969 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2972 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2973 * @dapm: DAPM context
2974 * @route: audio routes
2975 * @num: number of routes
2977 * Removes routes from the DAPM context.
2979 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2980 const struct snd_soc_dapm_route *route, int num)
2984 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2985 for (i = 0; i < num; i++) {
2986 snd_soc_dapm_del_route(dapm, route);
2989 mutex_unlock(&dapm->card->dapm_mutex);
2993 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2995 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2996 const struct snd_soc_dapm_route *route)
2998 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3001 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3004 struct snd_soc_dapm_path *path;
3008 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3014 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3019 if (route->control || route->connected)
3020 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3021 route->source, route->sink);
3023 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3024 if (path->sink == sink) {
3031 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3032 route->source, route->sink);
3034 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3035 count, route->source, route->sink);
3041 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3042 * @dapm: DAPM context
3043 * @route: audio routes
3044 * @num: number of routes
3046 * Mark existing routes matching those specified in the passed array
3047 * as being weak, meaning that they are ignored for the purpose of
3048 * power decisions. The main intended use case is for sidetone paths
3049 * which couple audio between other independent paths if they are both
3050 * active in order to make the combination work better at the user
3051 * level but which aren't intended to be "used".
3053 * Note that CODEC drivers should not use this as sidetone type paths
3054 * can frequently also be used as bypass paths.
3056 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3057 const struct snd_soc_dapm_route *route, int num)
3062 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3063 for (i = 0; i < num; i++) {
3064 err = snd_soc_dapm_weak_route(dapm, route);
3069 mutex_unlock(&dapm->card->dapm_mutex);
3073 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3076 * snd_soc_dapm_new_widgets - add new dapm widgets
3077 * @card: card to be checked for new dapm widgets
3079 * Checks the codec for any new dapm widgets and creates them if found.
3081 * Returns 0 for success.
3083 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3085 struct snd_soc_dapm_widget *w;
3088 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3090 list_for_each_entry(w, &card->widgets, list)
3095 if (w->num_kcontrols) {
3096 w->kcontrols = kzalloc(w->num_kcontrols *
3097 sizeof(struct snd_kcontrol *),
3099 if (!w->kcontrols) {
3100 mutex_unlock(&card->dapm_mutex);
3106 case snd_soc_dapm_switch:
3107 case snd_soc_dapm_mixer:
3108 case snd_soc_dapm_mixer_named_ctl:
3111 case snd_soc_dapm_mux:
3112 case snd_soc_dapm_demux:
3115 case snd_soc_dapm_pga:
3116 case snd_soc_dapm_out_drv:
3119 case snd_soc_dapm_dai_link:
3120 dapm_new_dai_link(w);
3126 /* Read the initial power state from the device */
3128 soc_dapm_read(w->dapm, w->reg, &val);
3129 val = val >> w->shift;
3131 if (val == w->on_val)
3137 dapm_mark_dirty(w, "new widget");
3138 dapm_debugfs_add_widget(w);
3141 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3142 mutex_unlock(&card->dapm_mutex);
3145 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3148 * snd_soc_dapm_get_volsw - dapm mixer get callback
3149 * @kcontrol: mixer control
3150 * @ucontrol: control element information
3152 * Callback to get the value of a dapm mixer control.
3154 * Returns 0 for success.
3156 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3157 struct snd_ctl_elem_value *ucontrol)
3159 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3160 struct snd_soc_card *card = dapm->card;
3161 struct soc_mixer_control *mc =
3162 (struct soc_mixer_control *)kcontrol->private_value;
3164 unsigned int shift = mc->shift;
3166 unsigned int width = fls(max);
3167 unsigned int mask = (1 << fls(max)) - 1;
3168 unsigned int invert = mc->invert;
3169 unsigned int reg_val, val, rval = 0;
3172 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3173 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3174 ret = soc_dapm_read(dapm, reg, ®_val);
3175 val = (reg_val >> shift) & mask;
3177 if (ret == 0 && reg != mc->rreg)
3178 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3180 if (snd_soc_volsw_is_stereo(mc))
3181 rval = (reg_val >> mc->rshift) & mask;
3183 reg_val = dapm_kcontrol_get_value(kcontrol);
3184 val = reg_val & mask;
3186 if (snd_soc_volsw_is_stereo(mc))
3187 rval = (reg_val >> width) & mask;
3189 mutex_unlock(&card->dapm_mutex);
3195 ucontrol->value.integer.value[0] = max - val;
3197 ucontrol->value.integer.value[0] = val;
3199 if (snd_soc_volsw_is_stereo(mc)) {
3201 ucontrol->value.integer.value[1] = max - rval;
3203 ucontrol->value.integer.value[1] = rval;
3208 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3211 * snd_soc_dapm_put_volsw - dapm mixer set callback
3212 * @kcontrol: mixer control
3213 * @ucontrol: control element information
3215 * Callback to set the value of a dapm mixer control.
3217 * Returns 0 for success.
3219 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3220 struct snd_ctl_elem_value *ucontrol)
3222 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3223 struct snd_soc_card *card = dapm->card;
3224 struct soc_mixer_control *mc =
3225 (struct soc_mixer_control *)kcontrol->private_value;
3227 unsigned int shift = mc->shift;
3229 unsigned int width = fls(max);
3230 unsigned int mask = (1 << width) - 1;
3231 unsigned int invert = mc->invert;
3232 unsigned int val, rval = 0;
3233 int connect, rconnect = -1, change, reg_change = 0;
3234 struct snd_soc_dapm_update update = { NULL };
3237 val = (ucontrol->value.integer.value[0] & mask);
3243 if (snd_soc_volsw_is_stereo(mc)) {
3244 rval = (ucontrol->value.integer.value[1] & mask);
3250 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3252 /* This assumes field width < (bits in unsigned int / 2) */
3253 if (width > sizeof(unsigned int) * 8 / 2)
3255 "ASoC: control %s field width limit exceeded\n",
3257 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3259 if (reg != SND_SOC_NOPM) {
3261 rval = rval << mc->rshift;
3263 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3265 if (snd_soc_volsw_is_stereo(mc))
3266 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3271 if (change || reg_change) {
3273 if (snd_soc_volsw_is_stereo(mc)) {
3274 update.has_second_set = true;
3275 update.reg2 = mc->rreg;
3276 update.mask2 = mask << mc->rshift;
3279 update.kcontrol = kcontrol;
3281 update.mask = mask << shift;
3283 card->update = &update;
3286 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3289 card->update = NULL;
3292 mutex_unlock(&card->dapm_mutex);
3295 soc_dpcm_runtime_update(card);
3299 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3302 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3303 * @kcontrol: mixer control
3304 * @ucontrol: control element information
3306 * Callback to get the value of a dapm enumerated double mixer control.
3308 * Returns 0 for success.
3310 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3311 struct snd_ctl_elem_value *ucontrol)
3313 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3314 struct snd_soc_card *card = dapm->card;
3315 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3316 unsigned int reg_val, val;
3318 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3319 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3320 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3322 mutex_unlock(&card->dapm_mutex);
3326 reg_val = dapm_kcontrol_get_value(kcontrol);
3328 mutex_unlock(&card->dapm_mutex);
3330 val = (reg_val >> e->shift_l) & e->mask;
3331 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3332 if (e->shift_l != e->shift_r) {
3333 val = (reg_val >> e->shift_r) & e->mask;
3334 val = snd_soc_enum_val_to_item(e, val);
3335 ucontrol->value.enumerated.item[1] = val;
3340 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3343 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3344 * @kcontrol: mixer control
3345 * @ucontrol: control element information
3347 * Callback to set the value of a dapm enumerated double mixer control.
3349 * Returns 0 for success.
3351 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3352 struct snd_ctl_elem_value *ucontrol)
3354 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3355 struct snd_soc_card *card = dapm->card;
3356 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3357 unsigned int *item = ucontrol->value.enumerated.item;
3358 unsigned int val, change, reg_change = 0;
3360 struct snd_soc_dapm_update update = { NULL };
3363 if (item[0] >= e->items)
3366 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3367 mask = e->mask << e->shift_l;
3368 if (e->shift_l != e->shift_r) {
3369 if (item[1] > e->items)
3371 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3372 mask |= e->mask << e->shift_r;
3375 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3377 change = dapm_kcontrol_set_value(kcontrol, val);
3379 if (e->reg != SND_SOC_NOPM)
3380 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3382 if (change || reg_change) {
3384 update.kcontrol = kcontrol;
3385 update.reg = e->reg;
3388 card->update = &update;
3391 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3393 card->update = NULL;
3396 mutex_unlock(&card->dapm_mutex);
3399 soc_dpcm_runtime_update(card);
3403 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3406 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3408 * @kcontrol: mixer control
3409 * @uinfo: control element information
3411 * Callback to provide information about a pin switch control.
3413 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3414 struct snd_ctl_elem_info *uinfo)
3416 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3418 uinfo->value.integer.min = 0;
3419 uinfo->value.integer.max = 1;
3423 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3426 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3428 * @kcontrol: mixer control
3431 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3432 struct snd_ctl_elem_value *ucontrol)
3434 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3435 const char *pin = (const char *)kcontrol->private_value;
3437 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3439 ucontrol->value.integer.value[0] =
3440 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3442 mutex_unlock(&card->dapm_mutex);
3446 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3449 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3451 * @kcontrol: mixer control
3454 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3455 struct snd_ctl_elem_value *ucontrol)
3457 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3458 const char *pin = (const char *)kcontrol->private_value;
3461 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3462 ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3463 !!ucontrol->value.integer.value[0]);
3464 mutex_unlock(&card->dapm_mutex);
3466 snd_soc_dapm_sync(&card->dapm);
3469 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3471 struct snd_soc_dapm_widget *
3472 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3473 const struct snd_soc_dapm_widget *widget)
3475 struct snd_soc_dapm_widget *w;
3477 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3478 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3479 /* Do not nag about probe deferrals */
3481 int ret = PTR_ERR(w);
3483 if (ret != -EPROBE_DEFER)
3485 "ASoC: Failed to create DAPM control %s (%d)\n",
3491 "ASoC: Failed to create DAPM control %s\n",
3495 mutex_unlock(&dapm->card->dapm_mutex);
3498 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3500 struct snd_soc_dapm_widget *
3501 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3502 const struct snd_soc_dapm_widget *widget)
3504 enum snd_soc_dapm_direction dir;
3505 struct snd_soc_dapm_widget *w;
3509 if ((w = dapm_cnew_widget(widget)) == NULL)
3513 case snd_soc_dapm_regulator_supply:
3514 w->regulator = devm_regulator_get(dapm->dev, w->name);
3515 if (IS_ERR(w->regulator)) {
3516 ret = PTR_ERR(w->regulator);
3517 if (ret == -EPROBE_DEFER)
3518 return ERR_PTR(ret);
3519 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3524 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3525 ret = regulator_allow_bypass(w->regulator, true);
3527 dev_warn(w->dapm->dev,
3528 "ASoC: Failed to bypass %s: %d\n",
3532 case snd_soc_dapm_clock_supply:
3533 #ifdef CONFIG_CLKDEV_LOOKUP
3534 w->clk = devm_clk_get(dapm->dev, w->name);
3535 if (IS_ERR(w->clk)) {
3536 ret = PTR_ERR(w->clk);
3537 if (ret == -EPROBE_DEFER)
3538 return ERR_PTR(ret);
3539 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3551 prefix = soc_dapm_prefix(dapm);
3553 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3555 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3556 if (w->name == NULL) {
3562 case snd_soc_dapm_mic:
3563 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3564 w->power_check = dapm_generic_check_power;
3566 case snd_soc_dapm_input:
3567 if (!dapm->card->fully_routed)
3568 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3569 w->power_check = dapm_generic_check_power;
3571 case snd_soc_dapm_spk:
3572 case snd_soc_dapm_hp:
3573 w->is_ep = SND_SOC_DAPM_EP_SINK;
3574 w->power_check = dapm_generic_check_power;
3576 case snd_soc_dapm_output:
3577 if (!dapm->card->fully_routed)
3578 w->is_ep = SND_SOC_DAPM_EP_SINK;
3579 w->power_check = dapm_generic_check_power;
3581 case snd_soc_dapm_vmid:
3582 case snd_soc_dapm_siggen:
3583 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3584 w->power_check = dapm_always_on_check_power;
3586 case snd_soc_dapm_sink:
3587 w->is_ep = SND_SOC_DAPM_EP_SINK;
3588 w->power_check = dapm_always_on_check_power;
3591 case snd_soc_dapm_mux:
3592 case snd_soc_dapm_demux:
3593 case snd_soc_dapm_switch:
3594 case snd_soc_dapm_mixer:
3595 case snd_soc_dapm_mixer_named_ctl:
3596 case snd_soc_dapm_adc:
3597 case snd_soc_dapm_aif_out:
3598 case snd_soc_dapm_dac:
3599 case snd_soc_dapm_aif_in:
3600 case snd_soc_dapm_pga:
3601 case snd_soc_dapm_out_drv:
3602 case snd_soc_dapm_micbias:
3603 case snd_soc_dapm_line:
3604 case snd_soc_dapm_dai_link:
3605 case snd_soc_dapm_dai_out:
3606 case snd_soc_dapm_dai_in:
3607 w->power_check = dapm_generic_check_power;
3609 case snd_soc_dapm_supply:
3610 case snd_soc_dapm_regulator_supply:
3611 case snd_soc_dapm_clock_supply:
3612 case snd_soc_dapm_kcontrol:
3614 w->power_check = dapm_supply_check_power;
3617 w->power_check = dapm_always_on_check_power;
3622 INIT_LIST_HEAD(&w->list);
3623 INIT_LIST_HEAD(&w->dirty);
3624 list_add_tail(&w->list, &dapm->card->widgets);
3626 snd_soc_dapm_for_each_direction(dir) {
3627 INIT_LIST_HEAD(&w->edges[dir]);
3628 w->endpoints[dir] = -1;
3631 /* machine layer sets up unconnected pins and insertions */
3637 * snd_soc_dapm_new_controls - create new dapm controls
3638 * @dapm: DAPM context
3639 * @widget: widget array
3640 * @num: number of widgets
3642 * Creates new DAPM controls based upon the templates.
3644 * Returns 0 for success else error.
3646 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3647 const struct snd_soc_dapm_widget *widget,
3650 struct snd_soc_dapm_widget *w;
3654 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3655 for (i = 0; i < num; i++) {
3656 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3659 /* Do not nag about probe deferrals */
3660 if (ret == -EPROBE_DEFER)
3663 "ASoC: Failed to create DAPM control %s (%d)\n",
3669 "ASoC: Failed to create DAPM control %s\n",
3676 mutex_unlock(&dapm->card->dapm_mutex);
3679 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3681 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3682 struct snd_kcontrol *kcontrol, int event)
3684 struct snd_soc_dapm_path *source_p, *sink_p;
3685 struct snd_soc_dai *source, *sink;
3686 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
3687 struct snd_pcm_substream substream;
3688 struct snd_pcm_hw_params *params = NULL;
3689 struct snd_pcm_runtime *runtime = NULL;
3693 if (WARN_ON(!config) ||
3694 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3695 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3698 /* We only support a single source and sink, pick the first */
3699 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3700 struct snd_soc_dapm_path,
3701 list_node[SND_SOC_DAPM_DIR_OUT]);
3702 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3703 struct snd_soc_dapm_path,
3704 list_node[SND_SOC_DAPM_DIR_IN]);
3706 source = source_p->source->priv;
3707 sink = sink_p->sink->priv;
3709 /* Be a little careful as we don't want to overflow the mask array */
3710 if (config->formats) {
3711 fmt = ffs(config->formats) - 1;
3713 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3718 /* Currently very limited parameter selection */
3719 params = kzalloc(sizeof(*params), GFP_KERNEL);
3724 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3726 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3728 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3731 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3732 = config->channels_min;
3733 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3734 = config->channels_max;
3736 memset(&substream, 0, sizeof(substream));
3738 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3739 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3744 substream.runtime = runtime;
3747 case SND_SOC_DAPM_PRE_PMU:
3748 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3749 if (source->driver->ops && source->driver->ops->startup) {
3750 ret = source->driver->ops->startup(&substream, source);
3752 dev_err(source->dev,
3753 "ASoC: startup() failed: %d\n", ret);
3758 ret = soc_dai_hw_params(&substream, params, source);
3762 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3763 if (sink->driver->ops && sink->driver->ops->startup) {
3764 ret = sink->driver->ops->startup(&substream, sink);
3767 "ASoC: startup() failed: %d\n", ret);
3772 ret = soc_dai_hw_params(&substream, params, sink);
3777 case SND_SOC_DAPM_POST_PMU:
3778 ret = snd_soc_dai_digital_mute(sink, 0,
3779 SNDRV_PCM_STREAM_PLAYBACK);
3780 if (ret != 0 && ret != -ENOTSUPP)
3781 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3785 case SND_SOC_DAPM_PRE_PMD:
3786 ret = snd_soc_dai_digital_mute(sink, 1,
3787 SNDRV_PCM_STREAM_PLAYBACK);
3788 if (ret != 0 && ret != -ENOTSUPP)
3789 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3793 if (source->driver->ops && source->driver->ops->shutdown) {
3794 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3795 source->driver->ops->shutdown(&substream, source);
3799 if (sink->driver->ops && sink->driver->ops->shutdown) {
3800 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3801 sink->driver->ops->shutdown(&substream, sink);
3806 WARN(1, "Unknown event %d\n", event);
3816 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3817 struct snd_ctl_elem_value *ucontrol)
3819 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3821 ucontrol->value.enumerated.item[0] = w->params_select;
3826 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3827 struct snd_ctl_elem_value *ucontrol)
3829 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3831 /* Can't change the config when widget is already powered */
3835 if (ucontrol->value.enumerated.item[0] == w->params_select)
3838 if (ucontrol->value.enumerated.item[0] >= w->num_params)
3841 w->params_select = ucontrol->value.enumerated.item[0];
3846 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3847 const struct snd_soc_pcm_stream *params,
3848 unsigned int num_params,
3849 struct snd_soc_dapm_widget *source,
3850 struct snd_soc_dapm_widget *sink)
3852 struct snd_soc_dapm_widget template;
3853 struct snd_soc_dapm_widget *w;
3856 unsigned long private_value;
3857 const char **w_param_text;
3858 struct soc_enum w_param_enum[] = {
3859 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3861 struct snd_kcontrol_new kcontrol_dai_link[] = {
3862 SOC_ENUM_EXT(NULL, w_param_enum[0],
3863 snd_soc_dapm_dai_link_get,
3864 snd_soc_dapm_dai_link_put),
3866 const struct snd_soc_pcm_stream *config = params;
3868 w_param_text = devm_kcalloc(card->dev, num_params,
3869 sizeof(char *), GFP_KERNEL);
3873 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3874 source->name, sink->name);
3877 goto outfree_w_param;
3880 for (count = 0 ; count < num_params; count++) {
3881 if (!config->stream_name) {
3882 dev_warn(card->dapm.dev,
3883 "ASoC: anonymous config %d for dai link %s\n",
3885 w_param_text[count] =
3886 devm_kasprintf(card->dev, GFP_KERNEL,
3887 "Anonymous Configuration %d",
3889 if (!w_param_text[count]) {
3891 goto outfree_link_name;
3894 w_param_text[count] = devm_kmemdup(card->dev,
3895 config->stream_name,
3896 strlen(config->stream_name) + 1,
3898 if (!w_param_text[count]) {
3900 goto outfree_link_name;
3905 w_param_enum[0].items = num_params;
3906 w_param_enum[0].texts = w_param_text;
3908 memset(&template, 0, sizeof(template));
3909 template.reg = SND_SOC_NOPM;
3910 template.id = snd_soc_dapm_dai_link;
3911 template.name = link_name;
3912 template.event = snd_soc_dai_link_event;
3913 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3914 SND_SOC_DAPM_PRE_PMD;
3915 template.num_kcontrols = 1;
3916 /* duplicate w_param_enum on heap so that memory persists */
3918 (unsigned long) devm_kmemdup(card->dev,
3919 (void *)(kcontrol_dai_link[0].private_value),
3920 sizeof(struct soc_enum), GFP_KERNEL);
3921 if (!private_value) {
3922 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3925 goto outfree_link_name;
3927 kcontrol_dai_link[0].private_value = private_value;
3928 /* duplicate kcontrol_dai_link on heap so that memory persists */
3929 template.kcontrol_news =
3930 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3931 sizeof(struct snd_kcontrol_new),
3933 if (!template.kcontrol_news) {
3934 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3937 goto outfree_private_value;
3940 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3942 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
3945 /* Do not nag about probe deferrals */
3946 if (ret != -EPROBE_DEFER)
3948 "ASoC: Failed to create %s widget (%d)\n",
3950 goto outfree_kcontrol_news;
3953 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3956 goto outfree_kcontrol_news;
3960 w->num_params = num_params;
3962 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3965 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3968 devm_kfree(card->dev, w);
3969 outfree_kcontrol_news:
3970 devm_kfree(card->dev, (void *)template.kcontrol_news);
3971 outfree_private_value:
3972 devm_kfree(card->dev, (void *)private_value);
3974 devm_kfree(card->dev, link_name);
3976 for (count = 0 ; count < num_params; count++)
3977 devm_kfree(card->dev, (void *)w_param_text[count]);
3978 devm_kfree(card->dev, w_param_text);
3983 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3984 struct snd_soc_dai *dai)
3986 struct snd_soc_dapm_widget template;
3987 struct snd_soc_dapm_widget *w;
3989 WARN_ON(dapm->dev != dai->dev);
3991 memset(&template, 0, sizeof(template));
3992 template.reg = SND_SOC_NOPM;
3994 if (dai->driver->playback.stream_name) {
3995 template.id = snd_soc_dapm_dai_in;
3996 template.name = dai->driver->playback.stream_name;
3997 template.sname = dai->driver->playback.stream_name;
3999 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4002 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4004 int ret = PTR_ERR(w);
4006 /* Do not nag about probe deferrals */
4007 if (ret != -EPROBE_DEFER)
4009 "ASoC: Failed to create %s widget (%d)\n",
4010 dai->driver->playback.stream_name, ret);
4014 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4015 dai->driver->playback.stream_name);
4020 dai->playback_widget = w;
4023 if (dai->driver->capture.stream_name) {
4024 template.id = snd_soc_dapm_dai_out;
4025 template.name = dai->driver->capture.stream_name;
4026 template.sname = dai->driver->capture.stream_name;
4028 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4031 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4033 int ret = PTR_ERR(w);
4035 /* Do not nag about probe deferrals */
4036 if (ret != -EPROBE_DEFER)
4038 "ASoC: Failed to create %s widget (%d)\n",
4039 dai->driver->playback.stream_name, ret);
4043 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4044 dai->driver->capture.stream_name);
4049 dai->capture_widget = w;
4055 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4057 struct snd_soc_dapm_widget *dai_w, *w;
4058 struct snd_soc_dapm_widget *src, *sink;
4059 struct snd_soc_dai *dai;
4061 /* For each DAI widget... */
4062 list_for_each_entry(dai_w, &card->widgets, list) {
4063 switch (dai_w->id) {
4064 case snd_soc_dapm_dai_in:
4065 case snd_soc_dapm_dai_out:
4071 /* let users know there is no DAI to link */
4073 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4080 /* ...find all widgets with the same stream and link them */
4081 list_for_each_entry(w, &card->widgets, list) {
4082 if (w->dapm != dai_w->dapm)
4086 case snd_soc_dapm_dai_in:
4087 case snd_soc_dapm_dai_out:
4093 if (!w->sname || !strstr(w->sname, dai_w->sname))
4096 if (dai_w->id == snd_soc_dapm_dai_in) {
4103 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4104 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4111 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4112 struct snd_soc_pcm_runtime *rtd)
4114 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4115 struct snd_soc_dapm_widget *sink, *source;
4118 for (i = 0; i < rtd->num_codecs; i++) {
4119 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
4121 /* connect BE DAI playback if widgets are valid */
4122 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
4123 source = cpu_dai->playback_widget;
4124 sink = codec_dai->playback_widget;
4125 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4126 cpu_dai->component->name, source->name,
4127 codec_dai->component->name, sink->name);
4129 snd_soc_dapm_add_path(&card->dapm, source, sink,
4133 /* connect BE DAI capture if widgets are valid */
4134 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
4135 source = codec_dai->capture_widget;
4136 sink = cpu_dai->capture_widget;
4137 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4138 codec_dai->component->name, source->name,
4139 cpu_dai->component->name, sink->name);
4141 snd_soc_dapm_add_path(&card->dapm, source, sink,
4147 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4150 struct snd_soc_dapm_widget *w;
4153 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4154 w = dai->playback_widget;
4156 w = dai->capture_widget;
4159 dapm_mark_dirty(w, "stream event");
4161 if (w->id == snd_soc_dapm_dai_in) {
4162 ep = SND_SOC_DAPM_EP_SOURCE;
4163 dapm_widget_invalidate_input_paths(w);
4165 ep = SND_SOC_DAPM_EP_SINK;
4166 dapm_widget_invalidate_output_paths(w);
4170 case SND_SOC_DAPM_STREAM_START:
4174 case SND_SOC_DAPM_STREAM_STOP:
4178 case SND_SOC_DAPM_STREAM_SUSPEND:
4179 case SND_SOC_DAPM_STREAM_RESUME:
4180 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4181 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4187 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4189 struct snd_soc_pcm_runtime *rtd;
4191 /* for each BE DAI link... */
4192 list_for_each_entry(rtd, &card->rtd_list, list) {
4194 * dynamic FE links have no fixed DAI mapping.
4195 * CODEC<->CODEC links have no direct connection.
4197 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4200 dapm_connect_dai_link_widgets(card, rtd);
4204 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4209 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4210 for (i = 0; i < rtd->num_codecs; i++)
4211 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
4213 dapm_power_widgets(rtd->card, event);
4217 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4218 * @rtd: PCM runtime data
4219 * @stream: stream name
4220 * @event: stream event
4222 * Sends a stream event to the dapm core. The core then makes any
4223 * necessary widget power changes.
4225 * Returns 0 for success else error.
4227 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4230 struct snd_soc_card *card = rtd->card;
4232 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4233 soc_dapm_stream_event(rtd, stream, event);
4234 mutex_unlock(&card->dapm_mutex);
4238 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4239 * @dapm: DAPM context
4242 * Enables input/output pin and its parents or children widgets iff there is
4243 * a valid audio route and active audio stream.
4245 * Requires external locking.
4247 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4248 * do any widget power switching.
4250 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4253 return snd_soc_dapm_set_pin(dapm, pin, 1);
4255 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4258 * snd_soc_dapm_enable_pin - enable pin.
4259 * @dapm: DAPM context
4262 * Enables input/output pin and its parents or children widgets iff there is
4263 * a valid audio route and active audio stream.
4265 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4266 * do any widget power switching.
4268 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4272 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4274 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4276 mutex_unlock(&dapm->card->dapm_mutex);
4280 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4283 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4284 * @dapm: DAPM context
4287 * Enables input/output pin regardless of any other state. This is
4288 * intended for use with microphone bias supplies used in microphone
4291 * Requires external locking.
4293 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4294 * do any widget power switching.
4296 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4299 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4302 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4306 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4307 if (!w->connected) {
4309 * w->force does not affect the number of input or output paths,
4310 * so we only have to recheck if w->connected is changed
4312 dapm_widget_invalidate_input_paths(w);
4313 dapm_widget_invalidate_output_paths(w);
4317 dapm_mark_dirty(w, "force enable");
4321 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4324 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4325 * @dapm: DAPM context
4328 * Enables input/output pin regardless of any other state. This is
4329 * intended for use with microphone bias supplies used in microphone
4332 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4333 * do any widget power switching.
4335 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4340 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4342 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4344 mutex_unlock(&dapm->card->dapm_mutex);
4348 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4351 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4352 * @dapm: DAPM context
4355 * Disables input/output pin and its parents or children widgets.
4357 * Requires external locking.
4359 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4360 * do any widget power switching.
4362 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4365 return snd_soc_dapm_set_pin(dapm, pin, 0);
4367 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4370 * snd_soc_dapm_disable_pin - disable pin.
4371 * @dapm: DAPM context
4374 * Disables input/output pin and its parents or children widgets.
4376 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4377 * do any widget power switching.
4379 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4384 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4386 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4388 mutex_unlock(&dapm->card->dapm_mutex);
4392 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4395 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4396 * @dapm: DAPM context
4399 * Marks the specified pin as being not connected, disabling it along
4400 * any parent or child widgets. At present this is identical to
4401 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4402 * additional things such as disabling controls which only affect
4403 * paths through the pin.
4405 * Requires external locking.
4407 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4408 * do any widget power switching.
4410 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4413 return snd_soc_dapm_set_pin(dapm, pin, 0);
4415 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4418 * snd_soc_dapm_nc_pin - permanently disable pin.
4419 * @dapm: DAPM context
4422 * Marks the specified pin as being not connected, disabling it along
4423 * any parent or child widgets. At present this is identical to
4424 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4425 * additional things such as disabling controls which only affect
4426 * paths through the pin.
4428 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4429 * do any widget power switching.
4431 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4435 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4437 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4439 mutex_unlock(&dapm->card->dapm_mutex);
4443 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4446 * snd_soc_dapm_get_pin_status - get audio pin status
4447 * @dapm: DAPM context
4448 * @pin: audio signal pin endpoint (or start point)
4450 * Get audio pin status - connected or disconnected.
4452 * Returns 1 for connected otherwise 0.
4454 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4457 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4460 return w->connected;
4464 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4467 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4468 * @dapm: DAPM context
4469 * @pin: audio signal pin endpoint (or start point)
4471 * Mark the given endpoint or pin as ignoring suspend. When the
4472 * system is disabled a path between two endpoints flagged as ignoring
4473 * suspend will not be disabled. The path must already be enabled via
4474 * normal means at suspend time, it will not be turned on if it was not
4477 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4480 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4483 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4487 w->ignore_suspend = 1;
4491 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4494 * snd_soc_dapm_free - free dapm resources
4495 * @dapm: DAPM context
4497 * Free all dapm widgets and resources.
4499 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4501 dapm_debugfs_cleanup(dapm);
4502 dapm_free_widgets(dapm);
4503 list_del(&dapm->list);
4505 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4507 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4509 struct snd_soc_card *card = dapm->card;
4510 struct snd_soc_dapm_widget *w;
4511 LIST_HEAD(down_list);
4514 mutex_lock(&card->dapm_mutex);
4516 list_for_each_entry(w, &dapm->card->widgets, list) {
4517 if (w->dapm != dapm)
4520 dapm_seq_insert(w, &down_list, false);
4526 /* If there were no widgets to power down we're already in
4530 if (dapm->bias_level == SND_SOC_BIAS_ON)
4531 snd_soc_dapm_set_bias_level(dapm,
4532 SND_SOC_BIAS_PREPARE);
4533 dapm_seq_run(card, &down_list, 0, false);
4534 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4535 snd_soc_dapm_set_bias_level(dapm,
4536 SND_SOC_BIAS_STANDBY);
4539 mutex_unlock(&card->dapm_mutex);
4543 * snd_soc_dapm_shutdown - callback for system shutdown
4545 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4547 struct snd_soc_dapm_context *dapm;
4549 list_for_each_entry(dapm, &card->dapm_list, list) {
4550 if (dapm != &card->dapm) {
4551 soc_dapm_shutdown_dapm(dapm);
4552 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4553 snd_soc_dapm_set_bias_level(dapm,
4558 soc_dapm_shutdown_dapm(&card->dapm);
4559 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4560 snd_soc_dapm_set_bias_level(&card->dapm,
4564 /* Module information */
4565 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4566 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4567 MODULE_LICENSE("GPL");