1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
17 // o Delayed power down of audio subsystem to reduce pops between a quick
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/async.h>
24 #include <linux/delay.h>
26 #include <linux/bitops.h>
27 #include <linux/platform_device.h>
28 #include <linux/jiffies.h>
29 #include <linux/debugfs.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/pinctrl/consumer.h>
33 #include <linux/clk.h>
34 #include <linux/slab.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/initval.h>
41 #include <trace/events/asoc.h>
43 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
45 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
46 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
48 #define snd_soc_dapm_for_each_direction(dir) \
49 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
52 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
53 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
55 int (*connected)(struct snd_soc_dapm_widget *source,
56 struct snd_soc_dapm_widget *sink));
58 struct snd_soc_dapm_widget *
59 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
60 const struct snd_soc_dapm_widget *widget);
62 struct snd_soc_dapm_widget *
63 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
64 const struct snd_soc_dapm_widget *widget);
66 /* dapm power sequences - make this per codec in the future */
67 static int dapm_up_seq[] = {
68 [snd_soc_dapm_pre] = 0,
69 [snd_soc_dapm_regulator_supply] = 1,
70 [snd_soc_dapm_pinctrl] = 1,
71 [snd_soc_dapm_clock_supply] = 1,
72 [snd_soc_dapm_supply] = 2,
73 [snd_soc_dapm_micbias] = 3,
74 [snd_soc_dapm_dai_link] = 2,
75 [snd_soc_dapm_dai_in] = 4,
76 [snd_soc_dapm_dai_out] = 4,
77 [snd_soc_dapm_aif_in] = 4,
78 [snd_soc_dapm_aif_out] = 4,
79 [snd_soc_dapm_mic] = 5,
80 [snd_soc_dapm_mux] = 6,
81 [snd_soc_dapm_demux] = 6,
82 [snd_soc_dapm_dac] = 7,
83 [snd_soc_dapm_switch] = 8,
84 [snd_soc_dapm_mixer] = 8,
85 [snd_soc_dapm_mixer_named_ctl] = 8,
86 [snd_soc_dapm_pga] = 9,
87 [snd_soc_dapm_adc] = 10,
88 [snd_soc_dapm_out_drv] = 11,
89 [snd_soc_dapm_hp] = 11,
90 [snd_soc_dapm_spk] = 11,
91 [snd_soc_dapm_line] = 11,
92 [snd_soc_dapm_kcontrol] = 12,
93 [snd_soc_dapm_post] = 13,
96 static int dapm_down_seq[] = {
97 [snd_soc_dapm_pre] = 0,
98 [snd_soc_dapm_kcontrol] = 1,
99 [snd_soc_dapm_adc] = 2,
100 [snd_soc_dapm_hp] = 3,
101 [snd_soc_dapm_spk] = 3,
102 [snd_soc_dapm_line] = 3,
103 [snd_soc_dapm_out_drv] = 3,
104 [snd_soc_dapm_pga] = 4,
105 [snd_soc_dapm_switch] = 5,
106 [snd_soc_dapm_mixer_named_ctl] = 5,
107 [snd_soc_dapm_mixer] = 5,
108 [snd_soc_dapm_dac] = 6,
109 [snd_soc_dapm_mic] = 7,
110 [snd_soc_dapm_micbias] = 8,
111 [snd_soc_dapm_mux] = 9,
112 [snd_soc_dapm_demux] = 9,
113 [snd_soc_dapm_aif_in] = 10,
114 [snd_soc_dapm_aif_out] = 10,
115 [snd_soc_dapm_dai_in] = 10,
116 [snd_soc_dapm_dai_out] = 10,
117 [snd_soc_dapm_dai_link] = 11,
118 [snd_soc_dapm_supply] = 12,
119 [snd_soc_dapm_clock_supply] = 13,
120 [snd_soc_dapm_pinctrl] = 13,
121 [snd_soc_dapm_regulator_supply] = 13,
122 [snd_soc_dapm_post] = 14,
125 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
127 if (dapm->card && dapm->card->instantiated)
128 lockdep_assert_held(&dapm->card->dapm_mutex);
131 static void pop_wait(u32 pop_time)
134 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
137 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
145 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
150 vsnprintf(buf, PAGE_SIZE, fmt, args);
151 dev_info(dev, "%s", buf);
157 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
159 return !list_empty(&w->dirty);
162 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
164 dapm_assert_locked(w->dapm);
166 if (!dapm_dirty_widget(w)) {
167 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
169 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
174 * Common implementation for dapm_widget_invalidate_input_paths() and
175 * dapm_widget_invalidate_output_paths(). The function is inlined since the
176 * combined size of the two specialized functions is only marginally larger then
177 * the size of the generic function and at the same time the fast path of the
178 * specialized functions is significantly smaller than the generic function.
180 static __always_inline void dapm_widget_invalidate_paths(
181 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
183 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
184 struct snd_soc_dapm_widget *node;
185 struct snd_soc_dapm_path *p;
188 dapm_assert_locked(w->dapm);
190 if (w->endpoints[dir] == -1)
193 list_add_tail(&w->work_list, &list);
194 w->endpoints[dir] = -1;
196 list_for_each_entry(w, &list, work_list) {
197 snd_soc_dapm_widget_for_each_path(w, dir, p) {
198 if (p->is_supply || p->weak || !p->connect)
200 node = p->node[rdir];
201 if (node->endpoints[dir] != -1) {
202 node->endpoints[dir] = -1;
203 list_add_tail(&node->work_list, &list);
210 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
212 * @w: The widget for which to invalidate the cached number of input paths
214 * Resets the cached number of inputs for the specified widget and all widgets
215 * that can be reached via outcoming paths from the widget.
217 * This function must be called if the number of output paths for a widget might
218 * have changed. E.g. if the source state of a widget changes or a path is added
219 * or activated with the widget as the sink.
221 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
223 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
227 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
229 * @w: The widget for which to invalidate the cached number of output paths
231 * Resets the cached number of outputs for the specified widget and all widgets
232 * that can be reached via incoming paths from the widget.
234 * This function must be called if the number of output paths for a widget might
235 * have changed. E.g. if the sink state of a widget changes or a path is added
236 * or activated with the widget as the source.
238 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
240 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
244 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
245 * for the widgets connected to a path
246 * @p: The path to invalidate
248 * Resets the cached number of inputs for the sink of the path and the cached
249 * number of outputs for the source of the path.
251 * This function must be called when a path is added, removed or the connected
254 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
257 * Weak paths or supply paths do not influence the number of input or
258 * output paths of their neighbors.
260 if (p->weak || p->is_supply)
264 * The number of connected endpoints is the sum of the number of
265 * connected endpoints of all neighbors. If a node with 0 connected
266 * endpoints is either connected or disconnected that sum won't change,
267 * so there is no need to re-check the path.
269 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
270 dapm_widget_invalidate_input_paths(p->sink);
271 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
272 dapm_widget_invalidate_output_paths(p->source);
275 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
277 struct snd_soc_dapm_widget *w;
279 mutex_lock(&card->dapm_mutex);
281 list_for_each_entry(w, &card->widgets, list) {
283 dapm_mark_dirty(w, "Rechecking endpoints");
284 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
285 dapm_widget_invalidate_output_paths(w);
286 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
287 dapm_widget_invalidate_input_paths(w);
291 mutex_unlock(&card->dapm_mutex);
293 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
295 /* create a new dapm widget */
296 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
297 const struct snd_soc_dapm_widget *_widget)
299 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
302 struct dapm_kcontrol_data {
304 struct snd_soc_dapm_widget *widget;
305 struct list_head paths;
306 struct snd_soc_dapm_widget_list *wlist;
309 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
310 struct snd_kcontrol *kcontrol, const char *ctrl_name)
312 struct dapm_kcontrol_data *data;
313 struct soc_mixer_control *mc;
318 data = kzalloc(sizeof(*data), GFP_KERNEL);
322 INIT_LIST_HEAD(&data->paths);
324 switch (widget->id) {
325 case snd_soc_dapm_switch:
326 case snd_soc_dapm_mixer:
327 case snd_soc_dapm_mixer_named_ctl:
328 mc = (struct soc_mixer_control *)kcontrol->private_value;
330 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
331 dev_warn(widget->dapm->dev,
332 "ASoC: Unsupported stereo autodisable control '%s'\n",
335 if (mc->autodisable) {
336 struct snd_soc_dapm_widget template;
338 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
345 memset(&template, 0, sizeof(template));
346 template.reg = mc->reg;
347 template.mask = (1 << fls(mc->max)) - 1;
348 template.shift = mc->shift;
350 template.off_val = mc->max;
352 template.off_val = 0;
353 template.on_val = template.off_val;
354 template.id = snd_soc_dapm_kcontrol;
355 template.name = name;
357 data->value = template.on_val;
360 snd_soc_dapm_new_control_unlocked(widget->dapm,
363 if (IS_ERR(data->widget)) {
364 ret = PTR_ERR(data->widget);
373 case snd_soc_dapm_demux:
374 case snd_soc_dapm_mux:
375 e = (struct soc_enum *)kcontrol->private_value;
377 if (e->autodisable) {
378 struct snd_soc_dapm_widget template;
380 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
387 memset(&template, 0, sizeof(template));
388 template.reg = e->reg;
389 template.mask = e->mask << e->shift_l;
390 template.shift = e->shift_l;
391 template.off_val = snd_soc_enum_item_to_val(e, 0);
392 template.on_val = template.off_val;
393 template.id = snd_soc_dapm_kcontrol;
394 template.name = name;
396 data->value = template.on_val;
398 data->widget = snd_soc_dapm_new_control_unlocked(
399 widget->dapm, &template);
401 if (IS_ERR(data->widget)) {
402 ret = PTR_ERR(data->widget);
410 snd_soc_dapm_add_path(widget->dapm, data->widget,
418 kcontrol->private_data = data;
427 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
429 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
431 list_del(&data->paths);
436 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
437 const struct snd_kcontrol *kcontrol)
439 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
444 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
445 struct snd_soc_dapm_widget *widget)
447 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
448 struct snd_soc_dapm_widget_list *new_wlist;
452 n = data->wlist->num_widgets + 1;
456 new_wlist = krealloc(data->wlist,
457 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
461 new_wlist->widgets[n - 1] = widget;
462 new_wlist->num_widgets = n;
464 data->wlist = new_wlist;
469 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
470 struct snd_soc_dapm_path *path)
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
474 list_add_tail(&path->list_kcontrol, &data->paths);
477 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
479 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
484 return data->widget->power;
487 static struct list_head *dapm_kcontrol_get_path_list(
488 const struct snd_kcontrol *kcontrol)
490 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
495 #define dapm_kcontrol_for_each_path(path, kcontrol) \
496 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
499 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
501 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
505 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
507 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
510 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
512 if (data->value == value)
516 data->widget->on_val = value;
524 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
526 * @kcontrol: The kcontrol
528 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
529 struct snd_kcontrol *kcontrol)
531 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
533 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
536 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
538 * @kcontrol: The kcontrol
540 * Note: This function must only be used on kcontrols that are known to have
541 * been registered for a CODEC. Otherwise the behaviour is undefined.
543 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
544 struct snd_kcontrol *kcontrol)
546 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
548 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
550 static void dapm_reset(struct snd_soc_card *card)
552 struct snd_soc_dapm_widget *w;
554 lockdep_assert_held(&card->dapm_mutex);
556 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
558 list_for_each_entry(w, &card->widgets, list) {
559 w->new_power = w->power;
560 w->power_checked = false;
564 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
566 if (!dapm->component)
568 return dapm->component->name_prefix;
571 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
574 if (!dapm->component)
576 return snd_soc_component_read(dapm->component, reg, value);
579 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
580 int reg, unsigned int mask, unsigned int value)
582 if (!dapm->component)
584 return snd_soc_component_update_bits(dapm->component, reg,
588 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
589 int reg, unsigned int mask, unsigned int value)
591 if (!dapm->component)
593 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
596 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
599 snd_soc_component_async_complete(dapm->component);
602 static struct snd_soc_dapm_widget *
603 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
605 struct snd_soc_dapm_widget *w = wcache->widget;
606 struct list_head *wlist;
611 wlist = &w->dapm->card->widgets;
613 list_for_each_entry_from(w, wlist, list) {
614 if (!strcmp(name, w->name))
625 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
626 struct snd_soc_dapm_widget *w)
632 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
633 * @dapm: The DAPM context for which to set the level
634 * @level: The level to set
636 * Forces the DAPM bias level to a specific state. It will call the bias level
637 * callback of DAPM context with the specified level. This will even happen if
638 * the context is already at the same level. Furthermore it will not go through
639 * the normal bias level sequencing, meaning any intermediate states between the
640 * current and the target state will not be entered.
642 * Note that the change in bias level is only temporary and the next time
643 * snd_soc_dapm_sync() is called the state will be set to the level as
644 * determined by the DAPM core. The function is mainly intended to be used to
645 * used during probe or resume from suspend to power up the device so
646 * initialization can be done, before the DAPM core takes over.
648 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
649 enum snd_soc_bias_level level)
653 if (dapm->set_bias_level)
654 ret = dapm->set_bias_level(dapm, level);
657 dapm->bias_level = level;
661 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
664 * snd_soc_dapm_set_bias_level - set the bias level for the system
665 * @dapm: DAPM context
666 * @level: level to configure
668 * Configure the bias (power) levels for the SoC audio device.
670 * Returns 0 for success else error.
672 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
673 enum snd_soc_bias_level level)
675 struct snd_soc_card *card = dapm->card;
678 trace_snd_soc_bias_level_start(card, level);
680 if (card && card->set_bias_level)
681 ret = card->set_bias_level(card, dapm, level);
685 if (!card || dapm != &card->dapm)
686 ret = snd_soc_dapm_force_bias_level(dapm, level);
691 if (card && card->set_bias_level_post)
692 ret = card->set_bias_level_post(card, dapm, level);
694 trace_snd_soc_bias_level_done(card, level);
699 /* connect mux widget to its interconnecting audio paths */
700 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
701 struct snd_soc_dapm_path *path, const char *control_name,
702 struct snd_soc_dapm_widget *w)
704 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
705 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
706 unsigned int val, item;
709 if (e->reg != SND_SOC_NOPM) {
710 soc_dapm_read(dapm, e->reg, &val);
711 val = (val >> e->shift_l) & e->mask;
712 item = snd_soc_enum_val_to_item(e, val);
714 /* since a virtual mux has no backing registers to
715 * decide which path to connect, it will try to match
716 * with the first enumeration. This is to ensure
717 * that the default mux choice (the first) will be
718 * correctly powered up during initialization.
723 i = match_string(e->texts, e->items, control_name);
727 path->name = e->texts[i];
728 path->connect = (i == item);
733 /* set up initial codec paths */
734 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
737 struct soc_mixer_control *mc = (struct soc_mixer_control *)
738 p->sink->kcontrol_news[i].private_value;
739 unsigned int reg = mc->reg;
740 unsigned int shift = mc->shift;
741 unsigned int max = mc->max;
742 unsigned int mask = (1 << fls(max)) - 1;
743 unsigned int invert = mc->invert;
746 if (reg != SND_SOC_NOPM) {
747 soc_dapm_read(p->sink->dapm, reg, &val);
749 * The nth_path argument allows this function to know
750 * which path of a kcontrol it is setting the initial
751 * status for. Ideally this would support any number
752 * of paths and channels. But since kcontrols only come
753 * in mono and stereo variants, we are limited to 2
756 * The following code assumes for stereo controls the
757 * first path is the left channel, and all remaining
758 * paths are the right channel.
760 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
762 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
763 val = (val >> mc->rshift) & mask;
765 val = (val >> shift) & mask;
775 /* connect mixer widget to its interconnecting audio paths */
776 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
777 struct snd_soc_dapm_path *path, const char *control_name)
781 /* search for mixer kcontrol */
782 for (i = 0; i < path->sink->num_kcontrols; i++) {
783 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
784 path->name = path->sink->kcontrol_news[i].name;
785 dapm_set_mixer_path_status(path, i, nth_path++);
792 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
793 struct snd_soc_dapm_widget *kcontrolw,
794 const struct snd_kcontrol_new *kcontrol_new,
795 struct snd_kcontrol **kcontrol)
797 struct snd_soc_dapm_widget *w;
802 list_for_each_entry(w, &dapm->card->widgets, list) {
803 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
805 for (i = 0; i < w->num_kcontrols; i++) {
806 if (&w->kcontrol_news[i] == kcontrol_new) {
808 *kcontrol = w->kcontrols[i];
818 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
819 * create it. Either way, add the widget into the control's widget list
821 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
824 struct snd_soc_dapm_context *dapm = w->dapm;
825 struct snd_card *card = dapm->card->snd_card;
829 struct snd_kcontrol *kcontrol;
830 bool wname_in_long_name, kcname_in_long_name;
831 char *long_name = NULL;
835 prefix = soc_dapm_prefix(dapm);
837 prefix_len = strlen(prefix) + 1;
841 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
846 wname_in_long_name = false;
847 kcname_in_long_name = true;
850 case snd_soc_dapm_switch:
851 case snd_soc_dapm_mixer:
852 case snd_soc_dapm_pga:
853 case snd_soc_dapm_out_drv:
854 wname_in_long_name = true;
855 kcname_in_long_name = true;
857 case snd_soc_dapm_mixer_named_ctl:
858 wname_in_long_name = false;
859 kcname_in_long_name = true;
861 case snd_soc_dapm_demux:
862 case snd_soc_dapm_mux:
863 wname_in_long_name = true;
864 kcname_in_long_name = false;
871 if (wname_in_long_name && kcname_in_long_name) {
873 * The control will get a prefix from the control
874 * creation process but we're also using the same
875 * prefix for widgets so cut the prefix off the
876 * front of the widget name.
878 long_name = kasprintf(GFP_KERNEL, "%s %s",
879 w->name + prefix_len,
880 w->kcontrol_news[kci].name);
881 if (long_name == NULL)
885 } else if (wname_in_long_name) {
887 name = w->name + prefix_len;
890 name = w->kcontrol_news[kci].name;
893 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
900 kcontrol->private_free = dapm_kcontrol_free;
902 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
904 snd_ctl_free_one(kcontrol);
908 ret = snd_ctl_add(card, kcontrol);
911 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
917 ret = dapm_kcontrol_add_widget(kcontrol, w);
919 w->kcontrols[kci] = kcontrol;
927 /* create new dapm mixer control */
928 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
931 struct snd_soc_dapm_path *path;
932 struct dapm_kcontrol_data *data;
935 for (i = 0; i < w->num_kcontrols; i++) {
937 snd_soc_dapm_widget_for_each_source_path(w, path) {
938 /* mixer/mux paths name must match control name */
939 if (path->name != (char *)w->kcontrol_news[i].name)
942 if (!w->kcontrols[i]) {
943 ret = dapm_create_or_share_kcontrol(w, i);
948 dapm_kcontrol_add_path(w->kcontrols[i], path);
950 data = snd_kcontrol_chip(w->kcontrols[i]);
952 snd_soc_dapm_add_path(data->widget->dapm,
962 /* create new dapm mux control */
963 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
965 struct snd_soc_dapm_context *dapm = w->dapm;
966 enum snd_soc_dapm_direction dir;
967 struct snd_soc_dapm_path *path;
972 case snd_soc_dapm_mux:
973 dir = SND_SOC_DAPM_DIR_OUT;
976 case snd_soc_dapm_demux:
977 dir = SND_SOC_DAPM_DIR_IN;
984 if (w->num_kcontrols != 1) {
986 "ASoC: %s %s has incorrect number of controls\n", type,
991 if (list_empty(&w->edges[dir])) {
992 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
996 ret = dapm_create_or_share_kcontrol(w, 0);
1000 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1002 dapm_kcontrol_add_path(w->kcontrols[0], path);
1008 /* create new dapm volume control */
1009 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1013 for (i = 0; i < w->num_kcontrols; i++) {
1014 ret = dapm_create_or_share_kcontrol(w, i);
1022 /* create new dapm dai link control */
1023 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1026 struct snd_kcontrol *kcontrol;
1027 struct snd_soc_dapm_context *dapm = w->dapm;
1028 struct snd_card *card = dapm->card->snd_card;
1030 /* create control for links with > 1 config */
1031 if (w->num_params <= 1)
1035 for (i = 0; i < w->num_kcontrols; i++) {
1036 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1038 ret = snd_ctl_add(card, kcontrol);
1041 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1042 w->name, w->kcontrol_news[i].name, ret);
1045 kcontrol->private_data = w;
1046 w->kcontrols[i] = kcontrol;
1052 /* We implement power down on suspend by checking the power state of
1053 * the ALSA card - when we are suspending the ALSA state for the card
1056 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1058 int level = snd_power_get_state(widget->dapm->card->snd_card);
1061 case SNDRV_CTL_POWER_D3hot:
1062 case SNDRV_CTL_POWER_D3cold:
1063 if (widget->ignore_suspend)
1064 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1066 return widget->ignore_suspend;
1072 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1073 struct list_head *widgets)
1075 struct snd_soc_dapm_widget *w;
1076 struct list_head *it;
1077 unsigned int size = 0;
1080 list_for_each(it, widgets)
1083 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1087 list_for_each_entry(w, widgets, work_list)
1088 (*list)->widgets[i++] = w;
1090 (*list)->num_widgets = i;
1096 * Common implementation for is_connected_output_ep() and
1097 * is_connected_input_ep(). The function is inlined since the combined size of
1098 * the two specialized functions is only marginally larger then the size of the
1099 * generic function and at the same time the fast path of the specialized
1100 * functions is significantly smaller than the generic function.
1102 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1103 struct list_head *list, enum snd_soc_dapm_direction dir,
1104 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1105 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1106 enum snd_soc_dapm_direction)),
1107 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1108 enum snd_soc_dapm_direction))
1110 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1111 struct snd_soc_dapm_path *path;
1114 if (widget->endpoints[dir] >= 0)
1115 return widget->endpoints[dir];
1117 DAPM_UPDATE_STAT(widget, path_checks);
1119 /* do we need to add this widget to the list ? */
1121 list_add_tail(&widget->work_list, list);
1123 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1124 widget->endpoints[dir] = 1;
1125 return widget->endpoints[dir];
1128 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1129 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1130 return widget->endpoints[dir];
1133 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1134 DAPM_UPDATE_STAT(widget, neighbour_checks);
1136 if (path->weak || path->is_supply)
1142 trace_snd_soc_dapm_path(widget, dir, path);
1144 if (path->connect) {
1146 con += fn(path->node[dir], list, custom_stop_condition);
1151 widget->endpoints[dir] = con;
1157 * Recursively check for a completed path to an active or physically connected
1158 * output widget. Returns number of complete paths.
1160 * Optionally, can be supplied with a function acting as a stopping condition.
1161 * This function takes the dapm widget currently being examined and the walk
1162 * direction as an arguments, it should return true if the walk should be
1163 * stopped and false otherwise.
1165 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1166 struct list_head *list,
1167 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1168 enum snd_soc_dapm_direction))
1170 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1171 is_connected_output_ep, custom_stop_condition);
1175 * Recursively check for a completed path to an active or physically connected
1176 * input widget. Returns number of complete paths.
1178 * Optionally, can be supplied with a function acting as a stopping condition.
1179 * This function takes the dapm widget currently being examined and the walk
1180 * direction as an arguments, it should return true if the walk should be
1181 * stopped and false otherwise.
1183 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1184 struct list_head *list,
1185 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1186 enum snd_soc_dapm_direction))
1188 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1189 is_connected_input_ep, custom_stop_condition);
1193 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1194 * @dai: the soc DAI.
1195 * @stream: stream direction.
1196 * @list: list of active widgets for this stream.
1197 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1198 * walk based on custom logic.
1200 * Queries DAPM graph as to whether a valid audio stream path exists for
1201 * the initial stream specified by name. This takes into account
1202 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1204 * Optionally, can be supplied with a function acting as a stopping condition.
1205 * This function takes the dapm widget currently being examined and the walk
1206 * direction as an arguments, it should return true if the walk should be
1207 * stopped and false otherwise.
1209 * Returns the number of valid paths or negative error.
1211 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1212 struct snd_soc_dapm_widget_list **list,
1213 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1214 enum snd_soc_dapm_direction))
1216 struct snd_soc_card *card = dai->component->card;
1217 struct snd_soc_dapm_widget *w;
1222 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1225 * For is_connected_{output,input}_ep fully discover the graph we need
1226 * to reset the cached number of inputs and outputs.
1228 list_for_each_entry(w, &card->widgets, list) {
1229 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1230 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1233 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1234 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1235 custom_stop_condition);
1237 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1238 custom_stop_condition);
1240 /* Drop starting point */
1241 list_del(widgets.next);
1243 ret = dapm_widget_list_create(list, &widgets);
1247 trace_snd_soc_dapm_connected(paths, stream);
1248 mutex_unlock(&card->dapm_mutex);
1254 * Handler for regulator supply widget.
1256 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1257 struct snd_kcontrol *kcontrol, int event)
1261 soc_dapm_async_complete(w->dapm);
1263 if (SND_SOC_DAPM_EVENT_ON(event)) {
1264 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1265 ret = regulator_allow_bypass(w->regulator, false);
1267 dev_warn(w->dapm->dev,
1268 "ASoC: Failed to unbypass %s: %d\n",
1272 return regulator_enable(w->regulator);
1274 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1275 ret = regulator_allow_bypass(w->regulator, true);
1277 dev_warn(w->dapm->dev,
1278 "ASoC: Failed to bypass %s: %d\n",
1282 return regulator_disable_deferred(w->regulator, w->shift);
1285 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1288 * Handler for pinctrl widget.
1290 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1291 struct snd_kcontrol *kcontrol, int event)
1293 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1294 struct pinctrl *p = w->pinctrl;
1295 struct pinctrl_state *s;
1300 if (SND_SOC_DAPM_EVENT_ON(event))
1301 s = pinctrl_lookup_state(p, priv->active_state);
1303 s = pinctrl_lookup_state(p, priv->sleep_state);
1308 return pinctrl_select_state(p, s);
1310 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1313 * Handler for clock supply widget.
1315 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1316 struct snd_kcontrol *kcontrol, int event)
1321 soc_dapm_async_complete(w->dapm);
1323 if (SND_SOC_DAPM_EVENT_ON(event)) {
1324 return clk_prepare_enable(w->clk);
1326 clk_disable_unprepare(w->clk);
1332 EXPORT_SYMBOL_GPL(dapm_clock_event);
1334 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1336 if (w->power_checked)
1337 return w->new_power;
1342 w->new_power = w->power_check(w);
1344 w->power_checked = true;
1346 return w->new_power;
1349 /* Generic check to see if a widget should be powered. */
1350 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1354 DAPM_UPDATE_STAT(w, power_checks);
1356 in = is_connected_input_ep(w, NULL, NULL);
1357 out = is_connected_output_ep(w, NULL, NULL);
1358 return out != 0 && in != 0;
1361 /* Check to see if a power supply is needed */
1362 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1364 struct snd_soc_dapm_path *path;
1366 DAPM_UPDATE_STAT(w, power_checks);
1368 /* Check if one of our outputs is connected */
1369 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1370 DAPM_UPDATE_STAT(w, neighbour_checks);
1375 if (path->connected &&
1376 !path->connected(path->source, path->sink))
1379 if (dapm_widget_power_check(path->sink))
1386 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1388 return w->connected;
1391 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1392 struct snd_soc_dapm_widget *b,
1400 sort = dapm_down_seq;
1402 if (sort[a->id] != sort[b->id])
1403 return sort[a->id] - sort[b->id];
1404 if (a->subseq != b->subseq) {
1406 return a->subseq - b->subseq;
1408 return b->subseq - a->subseq;
1410 if (a->reg != b->reg)
1411 return a->reg - b->reg;
1412 if (a->dapm != b->dapm)
1413 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1418 /* Insert a widget in order into a DAPM power sequence. */
1419 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1420 struct list_head *list,
1423 struct snd_soc_dapm_widget *w;
1425 list_for_each_entry(w, list, power_list)
1426 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1427 list_add_tail(&new_widget->power_list, &w->power_list);
1431 list_add_tail(&new_widget->power_list, list);
1434 static void dapm_seq_check_event(struct snd_soc_card *card,
1435 struct snd_soc_dapm_widget *w, int event)
1437 const char *ev_name;
1441 case SND_SOC_DAPM_PRE_PMU:
1442 ev_name = "PRE_PMU";
1445 case SND_SOC_DAPM_POST_PMU:
1446 ev_name = "POST_PMU";
1449 case SND_SOC_DAPM_PRE_PMD:
1450 ev_name = "PRE_PMD";
1453 case SND_SOC_DAPM_POST_PMD:
1454 ev_name = "POST_PMD";
1457 case SND_SOC_DAPM_WILL_PMU:
1458 ev_name = "WILL_PMU";
1461 case SND_SOC_DAPM_WILL_PMD:
1462 ev_name = "WILL_PMD";
1466 WARN(1, "Unknown event %d\n", event);
1470 if (w->new_power != power)
1473 if (w->event && (w->event_flags & event)) {
1474 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1476 soc_dapm_async_complete(w->dapm);
1477 trace_snd_soc_dapm_widget_event_start(w, event);
1478 ret = w->event(w, NULL, event);
1479 trace_snd_soc_dapm_widget_event_done(w, event);
1481 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1482 ev_name, w->name, ret);
1486 /* Apply the coalesced changes from a DAPM sequence */
1487 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1488 struct list_head *pending)
1490 struct snd_soc_dapm_context *dapm;
1491 struct snd_soc_dapm_widget *w;
1493 unsigned int value = 0;
1494 unsigned int mask = 0;
1496 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1500 list_for_each_entry(w, pending, power_list) {
1501 WARN_ON(reg != w->reg || dapm != w->dapm);
1502 w->power = w->new_power;
1504 mask |= w->mask << w->shift;
1506 value |= w->on_val << w->shift;
1508 value |= w->off_val << w->shift;
1510 pop_dbg(dapm->dev, card->pop_time,
1511 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1512 w->name, reg, value, mask);
1514 /* Check for events */
1515 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1516 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1520 /* Any widget will do, they should all be updating the
1524 pop_dbg(dapm->dev, card->pop_time,
1525 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1526 value, mask, reg, card->pop_time);
1527 pop_wait(card->pop_time);
1528 soc_dapm_update_bits(dapm, reg, mask, value);
1531 list_for_each_entry(w, pending, power_list) {
1532 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1533 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1537 /* Apply a DAPM power sequence.
1539 * We walk over a pre-sorted list of widgets to apply power to. In
1540 * order to minimise the number of writes to the device required
1541 * multiple widgets will be updated in a single write where possible.
1542 * Currently anything that requires more than a single write is not
1545 static void dapm_seq_run(struct snd_soc_card *card,
1546 struct list_head *list, int event, bool power_up)
1548 struct snd_soc_dapm_widget *w, *n;
1549 struct snd_soc_dapm_context *d;
1552 int cur_subseq = -1;
1553 int cur_reg = SND_SOC_NOPM;
1554 struct snd_soc_dapm_context *cur_dapm = NULL;
1561 sort = dapm_down_seq;
1563 list_for_each_entry_safe(w, n, list, power_list) {
1566 /* Do we need to apply any queued changes? */
1567 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1568 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1569 if (!list_empty(&pending))
1570 dapm_seq_run_coalesced(card, &pending);
1572 if (cur_dapm && cur_dapm->seq_notifier) {
1573 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1574 if (sort[i] == cur_sort)
1575 cur_dapm->seq_notifier(cur_dapm,
1580 if (cur_dapm && w->dapm != cur_dapm)
1581 soc_dapm_async_complete(cur_dapm);
1583 INIT_LIST_HEAD(&pending);
1585 cur_subseq = INT_MIN;
1586 cur_reg = SND_SOC_NOPM;
1591 case snd_soc_dapm_pre:
1593 list_for_each_entry_safe_continue(w, n, list,
1596 if (event == SND_SOC_DAPM_STREAM_START)
1598 NULL, SND_SOC_DAPM_PRE_PMU);
1599 else if (event == SND_SOC_DAPM_STREAM_STOP)
1601 NULL, SND_SOC_DAPM_PRE_PMD);
1604 case snd_soc_dapm_post:
1606 list_for_each_entry_safe_continue(w, n, list,
1609 if (event == SND_SOC_DAPM_STREAM_START)
1611 NULL, SND_SOC_DAPM_POST_PMU);
1612 else if (event == SND_SOC_DAPM_STREAM_STOP)
1614 NULL, SND_SOC_DAPM_POST_PMD);
1618 /* Queue it up for application */
1619 cur_sort = sort[w->id];
1620 cur_subseq = w->subseq;
1623 list_move(&w->power_list, &pending);
1628 dev_err(w->dapm->dev,
1629 "ASoC: Failed to apply widget power: %d\n", ret);
1632 if (!list_empty(&pending))
1633 dapm_seq_run_coalesced(card, &pending);
1635 if (cur_dapm && cur_dapm->seq_notifier) {
1636 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1637 if (sort[i] == cur_sort)
1638 cur_dapm->seq_notifier(cur_dapm,
1642 list_for_each_entry(d, &card->dapm_list, list) {
1643 soc_dapm_async_complete(d);
1647 static void dapm_widget_update(struct snd_soc_card *card)
1649 struct snd_soc_dapm_update *update = card->update;
1650 struct snd_soc_dapm_widget_list *wlist;
1651 struct snd_soc_dapm_widget *w = NULL;
1655 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1658 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1660 for (wi = 0; wi < wlist->num_widgets; wi++) {
1661 w = wlist->widgets[wi];
1663 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1664 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1666 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1674 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1677 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1680 if (update->has_second_set) {
1681 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1682 update->mask2, update->val2);
1684 dev_err(w->dapm->dev,
1685 "ASoC: %s DAPM update failed: %d\n",
1689 for (wi = 0; wi < wlist->num_widgets; wi++) {
1690 w = wlist->widgets[wi];
1692 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1693 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1695 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1701 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1702 * they're changing state.
1704 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1706 struct snd_soc_dapm_context *d = data;
1709 /* If we're off and we're not supposed to go into STANDBY */
1710 if (d->bias_level == SND_SOC_BIAS_OFF &&
1711 d->target_bias_level != SND_SOC_BIAS_OFF) {
1713 pm_runtime_get_sync(d->dev);
1715 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1718 "ASoC: Failed to turn on bias: %d\n", ret);
1721 /* Prepare for a transition to ON or away from ON */
1722 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1723 d->bias_level != SND_SOC_BIAS_ON) ||
1724 (d->target_bias_level != SND_SOC_BIAS_ON &&
1725 d->bias_level == SND_SOC_BIAS_ON)) {
1726 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1729 "ASoC: Failed to prepare bias: %d\n", ret);
1733 /* Async callback run prior to DAPM sequences - brings to their final
1736 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1738 struct snd_soc_dapm_context *d = data;
1741 /* If we just powered the last thing off drop to standby bias */
1742 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1743 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1744 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1745 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1747 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1751 /* If we're in standby and can support bias off then do that */
1752 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1753 d->target_bias_level == SND_SOC_BIAS_OFF) {
1754 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1756 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1760 pm_runtime_put(d->dev);
1763 /* If we just powered up then move to active bias */
1764 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1765 d->target_bias_level == SND_SOC_BIAS_ON) {
1766 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1768 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1773 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1774 bool power, bool connect)
1776 /* If a connection is being made or broken then that update
1777 * will have marked the peer dirty, otherwise the widgets are
1778 * not connected and this update has no impact. */
1782 /* If the peer is already in the state we're moving to then we
1783 * won't have an impact on it. */
1784 if (power != peer->power)
1785 dapm_mark_dirty(peer, "peer state change");
1788 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1789 struct list_head *up_list,
1790 struct list_head *down_list)
1792 struct snd_soc_dapm_path *path;
1794 if (w->power == power)
1797 trace_snd_soc_dapm_widget_power(w, power);
1799 /* If we changed our power state perhaps our neigbours changed
1802 snd_soc_dapm_widget_for_each_source_path(w, path)
1803 dapm_widget_set_peer_power(path->source, power, path->connect);
1805 /* Supplies can't affect their outputs, only their inputs */
1806 if (!w->is_supply) {
1807 snd_soc_dapm_widget_for_each_sink_path(w, path)
1808 dapm_widget_set_peer_power(path->sink, power,
1813 dapm_seq_insert(w, up_list, true);
1815 dapm_seq_insert(w, down_list, false);
1818 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1819 struct list_head *up_list,
1820 struct list_head *down_list)
1825 case snd_soc_dapm_pre:
1826 dapm_seq_insert(w, down_list, false);
1828 case snd_soc_dapm_post:
1829 dapm_seq_insert(w, up_list, true);
1833 power = dapm_widget_power_check(w);
1835 dapm_widget_set_power(w, power, up_list, down_list);
1840 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1842 if (dapm->idle_bias_off)
1845 switch (snd_power_get_state(dapm->card->snd_card)) {
1846 case SNDRV_CTL_POWER_D3hot:
1847 case SNDRV_CTL_POWER_D3cold:
1848 return dapm->suspend_bias_off;
1857 * Scan each dapm widget for complete audio path.
1858 * A complete path is a route that has valid endpoints i.e.:-
1860 * o DAC to output pin.
1861 * o Input pin to ADC.
1862 * o Input pin to Output pin (bypass, sidetone)
1863 * o DAC to ADC (loopback).
1865 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1867 struct snd_soc_dapm_widget *w;
1868 struct snd_soc_dapm_context *d;
1870 LIST_HEAD(down_list);
1871 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1872 enum snd_soc_bias_level bias;
1874 lockdep_assert_held(&card->dapm_mutex);
1876 trace_snd_soc_dapm_start(card);
1878 list_for_each_entry(d, &card->dapm_list, list) {
1879 if (dapm_idle_bias_off(d))
1880 d->target_bias_level = SND_SOC_BIAS_OFF;
1882 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1887 /* Check which widgets we need to power and store them in
1888 * lists indicating if they should be powered up or down. We
1889 * only check widgets that have been flagged as dirty but note
1890 * that new widgets may be added to the dirty list while we
1893 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1894 dapm_power_one_widget(w, &up_list, &down_list);
1897 list_for_each_entry(w, &card->widgets, list) {
1899 case snd_soc_dapm_pre:
1900 case snd_soc_dapm_post:
1901 /* These widgets always need to be powered */
1904 list_del_init(&w->dirty);
1911 /* Supplies and micbiases only bring the
1912 * context up to STANDBY as unless something
1913 * else is active and passing audio they
1914 * generally don't require full power. Signal
1915 * generators are virtual pins and have no
1916 * power impact themselves.
1919 case snd_soc_dapm_siggen:
1920 case snd_soc_dapm_vmid:
1922 case snd_soc_dapm_supply:
1923 case snd_soc_dapm_regulator_supply:
1924 case snd_soc_dapm_pinctrl:
1925 case snd_soc_dapm_clock_supply:
1926 case snd_soc_dapm_micbias:
1927 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1928 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1931 d->target_bias_level = SND_SOC_BIAS_ON;
1938 /* Force all contexts in the card to the same bias state if
1939 * they're not ground referenced.
1941 bias = SND_SOC_BIAS_OFF;
1942 list_for_each_entry(d, &card->dapm_list, list)
1943 if (d->target_bias_level > bias)
1944 bias = d->target_bias_level;
1945 list_for_each_entry(d, &card->dapm_list, list)
1946 if (!dapm_idle_bias_off(d))
1947 d->target_bias_level = bias;
1949 trace_snd_soc_dapm_walk_done(card);
1951 /* Run card bias changes at first */
1952 dapm_pre_sequence_async(&card->dapm, 0);
1953 /* Run other bias changes in parallel */
1954 list_for_each_entry(d, &card->dapm_list, list) {
1955 if (d != &card->dapm && d->bias_level != d->target_bias_level)
1956 async_schedule_domain(dapm_pre_sequence_async, d,
1959 async_synchronize_full_domain(&async_domain);
1961 list_for_each_entry(w, &down_list, power_list) {
1962 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1965 list_for_each_entry(w, &up_list, power_list) {
1966 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1969 /* Power down widgets first; try to avoid amplifying pops. */
1970 dapm_seq_run(card, &down_list, event, false);
1972 dapm_widget_update(card);
1975 dapm_seq_run(card, &up_list, event, true);
1977 /* Run all the bias changes in parallel */
1978 list_for_each_entry(d, &card->dapm_list, list) {
1979 if (d != &card->dapm && d->bias_level != d->target_bias_level)
1980 async_schedule_domain(dapm_post_sequence_async, d,
1983 async_synchronize_full_domain(&async_domain);
1984 /* Run card bias changes at last */
1985 dapm_post_sequence_async(&card->dapm, 0);
1987 /* do we need to notify any clients that DAPM event is complete */
1988 list_for_each_entry(d, &card->dapm_list, list) {
1989 if (d->stream_event)
1990 d->stream_event(d, event);
1993 pop_dbg(card->dev, card->pop_time,
1994 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1995 pop_wait(card->pop_time);
1997 trace_snd_soc_dapm_done(card);
2002 #ifdef CONFIG_DEBUG_FS
2003 static ssize_t dapm_widget_power_read_file(struct file *file,
2004 char __user *user_buf,
2005 size_t count, loff_t *ppos)
2007 struct snd_soc_dapm_widget *w = file->private_data;
2008 struct snd_soc_card *card = w->dapm->card;
2009 enum snd_soc_dapm_direction dir, rdir;
2013 struct snd_soc_dapm_path *p = NULL;
2015 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2019 mutex_lock(&card->dapm_mutex);
2021 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2026 in = is_connected_input_ep(w, NULL, NULL);
2027 out = is_connected_output_ep(w, NULL, NULL);
2030 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2031 w->name, w->power ? "On" : "Off",
2032 w->force ? " (forced)" : "", in, out);
2035 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2036 " - R%d(0x%x) mask 0x%x",
2037 w->reg, w->reg, w->mask << w->shift);
2039 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
2042 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2044 w->active ? "active" : "inactive");
2046 snd_soc_dapm_for_each_direction(dir) {
2047 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2048 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2049 if (p->connected && !p->connected(p->source, p->sink))
2055 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2056 " %s \"%s\" \"%s\"\n",
2057 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2058 p->name ? p->name : "static",
2059 p->node[rdir]->name);
2063 mutex_unlock(&card->dapm_mutex);
2065 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2071 static const struct file_operations dapm_widget_power_fops = {
2072 .open = simple_open,
2073 .read = dapm_widget_power_read_file,
2074 .llseek = default_llseek,
2077 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2078 size_t count, loff_t *ppos)
2080 struct snd_soc_dapm_context *dapm = file->private_data;
2083 switch (dapm->bias_level) {
2084 case SND_SOC_BIAS_ON:
2087 case SND_SOC_BIAS_PREPARE:
2088 level = "Prepare\n";
2090 case SND_SOC_BIAS_STANDBY:
2091 level = "Standby\n";
2093 case SND_SOC_BIAS_OFF:
2097 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2098 level = "Unknown\n";
2102 return simple_read_from_buffer(user_buf, count, ppos, level,
2106 static const struct file_operations dapm_bias_fops = {
2107 .open = simple_open,
2108 .read = dapm_bias_read_file,
2109 .llseek = default_llseek,
2112 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2113 struct dentry *parent)
2120 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2122 if (!dapm->debugfs_dapm) {
2124 "ASoC: Failed to create DAPM debugfs directory\n");
2128 d = debugfs_create_file("bias_level", 0444,
2129 dapm->debugfs_dapm, dapm,
2133 "ASoC: Failed to create bias level debugfs file\n");
2136 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2138 struct snd_soc_dapm_context *dapm = w->dapm;
2141 if (!dapm->debugfs_dapm || !w->name)
2144 d = debugfs_create_file(w->name, 0444,
2145 dapm->debugfs_dapm, w,
2146 &dapm_widget_power_fops);
2148 dev_warn(w->dapm->dev,
2149 "ASoC: Failed to create %s debugfs file\n",
2153 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2155 debugfs_remove_recursive(dapm->debugfs_dapm);
2159 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2160 struct dentry *parent)
2164 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2168 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2175 * soc_dapm_connect_path() - Connects or disconnects a path
2176 * @path: The path to update
2177 * @connect: The new connect state of the path. True if the path is connected,
2178 * false if it is disconnected.
2179 * @reason: The reason why the path changed (for debugging only)
2181 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2182 bool connect, const char *reason)
2184 if (path->connect == connect)
2187 path->connect = connect;
2188 dapm_mark_dirty(path->source, reason);
2189 dapm_mark_dirty(path->sink, reason);
2190 dapm_path_invalidate(path);
2193 /* test and update the power status of a mux widget */
2194 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2195 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2197 struct snd_soc_dapm_path *path;
2201 lockdep_assert_held(&card->dapm_mutex);
2203 /* find dapm widget path assoc with kcontrol */
2204 dapm_kcontrol_for_each_path(path, kcontrol) {
2206 /* we now need to match the string in the enum to the path */
2207 if (!(strcmp(path->name, e->texts[mux])))
2212 soc_dapm_connect_path(path, connect, "mux update");
2216 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2221 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2222 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2223 struct snd_soc_dapm_update *update)
2225 struct snd_soc_card *card = dapm->card;
2228 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2229 card->update = update;
2230 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2231 card->update = NULL;
2232 mutex_unlock(&card->dapm_mutex);
2234 soc_dpcm_runtime_update(card);
2237 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2239 /* test and update the power status of a mixer or switch widget */
2240 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2241 struct snd_kcontrol *kcontrol,
2242 int connect, int rconnect)
2244 struct snd_soc_dapm_path *path;
2247 lockdep_assert_held(&card->dapm_mutex);
2249 /* find dapm widget path assoc with kcontrol */
2250 dapm_kcontrol_for_each_path(path, kcontrol) {
2252 * Ideally this function should support any number of
2253 * paths and channels. But since kcontrols only come
2254 * in mono and stereo variants, we are limited to 2
2257 * The following code assumes for stereo controls the
2258 * first path (when 'found == 0') is the left channel,
2259 * and all remaining paths (when 'found == 1') are the
2262 * A stereo control is signified by a valid 'rconnect'
2263 * value, either 0 for unconnected, or >= 0 for connected.
2264 * This is chosen instead of using snd_soc_volsw_is_stereo,
2265 * so that the behavior of snd_soc_dapm_mixer_update_power
2266 * doesn't change even when the kcontrol passed in is
2269 * It passes 'connect' as the path connect status for
2270 * the left channel, and 'rconnect' for the right
2273 if (found && rconnect >= 0)
2274 soc_dapm_connect_path(path, rconnect, "mixer update");
2276 soc_dapm_connect_path(path, connect, "mixer update");
2281 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2286 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2287 struct snd_kcontrol *kcontrol, int connect,
2288 struct snd_soc_dapm_update *update)
2290 struct snd_soc_card *card = dapm->card;
2293 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2294 card->update = update;
2295 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2296 card->update = NULL;
2297 mutex_unlock(&card->dapm_mutex);
2299 soc_dpcm_runtime_update(card);
2302 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2304 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2307 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2308 struct snd_soc_dapm_widget *w;
2310 char *state = "not set";
2312 /* card won't be set for the dummy component, as a spot fix
2313 * we're checking for that case specifically here but in future
2314 * we will ensure that the dummy component looks like others.
2319 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2320 if (w->dapm != dapm)
2323 /* only display widgets that burn power */
2325 case snd_soc_dapm_hp:
2326 case snd_soc_dapm_mic:
2327 case snd_soc_dapm_spk:
2328 case snd_soc_dapm_line:
2329 case snd_soc_dapm_micbias:
2330 case snd_soc_dapm_dac:
2331 case snd_soc_dapm_adc:
2332 case snd_soc_dapm_pga:
2333 case snd_soc_dapm_out_drv:
2334 case snd_soc_dapm_mixer:
2335 case snd_soc_dapm_mixer_named_ctl:
2336 case snd_soc_dapm_supply:
2337 case snd_soc_dapm_regulator_supply:
2338 case snd_soc_dapm_pinctrl:
2339 case snd_soc_dapm_clock_supply:
2341 count += sprintf(buf + count, "%s: %s\n",
2342 w->name, w->power ? "On":"Off");
2349 switch (snd_soc_dapm_get_bias_level(dapm)) {
2350 case SND_SOC_BIAS_ON:
2353 case SND_SOC_BIAS_PREPARE:
2356 case SND_SOC_BIAS_STANDBY:
2359 case SND_SOC_BIAS_OFF:
2363 count += sprintf(buf + count, "PM State: %s\n", state);
2368 /* show dapm widget status in sys fs */
2369 static ssize_t dapm_widget_show(struct device *dev,
2370 struct device_attribute *attr, char *buf)
2372 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2375 mutex_lock(&rtd->card->dapm_mutex);
2377 for (i = 0; i < rtd->num_codecs; i++) {
2378 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2380 count += dapm_widget_show_component(cmpnt, buf + count);
2383 mutex_unlock(&rtd->card->dapm_mutex);
2388 static DEVICE_ATTR_RO(dapm_widget);
2390 struct attribute *soc_dapm_dev_attrs[] = {
2391 &dev_attr_dapm_widget.attr,
2395 static void dapm_free_path(struct snd_soc_dapm_path *path)
2397 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2398 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2399 list_del(&path->list_kcontrol);
2400 list_del(&path->list);
2404 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2406 struct snd_soc_dapm_path *p, *next_p;
2407 enum snd_soc_dapm_direction dir;
2411 * remove source and sink paths associated to this widget.
2412 * While removing the path, remove reference to it from both
2413 * source and sink widgets so that path is removed only once.
2415 snd_soc_dapm_for_each_direction(dir) {
2416 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2420 kfree(w->kcontrols);
2421 kfree_const(w->name);
2425 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2427 dapm->path_sink_cache.widget = NULL;
2428 dapm->path_source_cache.widget = NULL;
2431 /* free all dapm widgets and resources */
2432 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2434 struct snd_soc_dapm_widget *w, *next_w;
2436 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2437 if (w->dapm != dapm)
2439 snd_soc_dapm_free_widget(w);
2441 snd_soc_dapm_reset_cache(dapm);
2444 static struct snd_soc_dapm_widget *dapm_find_widget(
2445 struct snd_soc_dapm_context *dapm, const char *pin,
2446 bool search_other_contexts)
2448 struct snd_soc_dapm_widget *w;
2449 struct snd_soc_dapm_widget *fallback = NULL;
2451 list_for_each_entry(w, &dapm->card->widgets, list) {
2452 if (!strcmp(w->name, pin)) {
2453 if (w->dapm == dapm)
2460 if (search_other_contexts)
2466 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2467 const char *pin, int status)
2469 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2471 dapm_assert_locked(dapm);
2474 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2478 if (w->connected != status) {
2479 dapm_mark_dirty(w, "pin configuration");
2480 dapm_widget_invalidate_input_paths(w);
2481 dapm_widget_invalidate_output_paths(w);
2484 w->connected = status;
2492 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2493 * @dapm: DAPM context
2495 * Walks all dapm audio paths and powers widgets according to their
2496 * stream or path usage.
2498 * Requires external locking.
2500 * Returns 0 for success.
2502 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2505 * Suppress early reports (eg, jacks syncing their state) to avoid
2506 * silly DAPM runs during card startup.
2508 if (!dapm->card || !dapm->card->instantiated)
2511 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2513 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2516 * snd_soc_dapm_sync - scan and power dapm paths
2517 * @dapm: DAPM context
2519 * Walks all dapm audio paths and powers widgets according to their
2520 * stream or path usage.
2522 * Returns 0 for success.
2524 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2528 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2529 ret = snd_soc_dapm_sync_unlocked(dapm);
2530 mutex_unlock(&dapm->card->dapm_mutex);
2533 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2536 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2537 * @w: The widget for which to update the flags
2539 * Some widgets have a dynamic category which depends on which neighbors they
2540 * are connected to. This function update the category for these widgets.
2542 * This function must be called whenever a path is added or removed to a widget.
2544 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2546 enum snd_soc_dapm_direction dir;
2547 struct snd_soc_dapm_path *p;
2551 case snd_soc_dapm_input:
2552 /* On a fully routed card an input is never a source */
2553 if (w->dapm->card->fully_routed)
2555 ep = SND_SOC_DAPM_EP_SOURCE;
2556 snd_soc_dapm_widget_for_each_source_path(w, p) {
2557 if (p->source->id == snd_soc_dapm_micbias ||
2558 p->source->id == snd_soc_dapm_mic ||
2559 p->source->id == snd_soc_dapm_line ||
2560 p->source->id == snd_soc_dapm_output) {
2566 case snd_soc_dapm_output:
2567 /* On a fully routed card a output is never a sink */
2568 if (w->dapm->card->fully_routed)
2570 ep = SND_SOC_DAPM_EP_SINK;
2571 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2572 if (p->sink->id == snd_soc_dapm_spk ||
2573 p->sink->id == snd_soc_dapm_hp ||
2574 p->sink->id == snd_soc_dapm_line ||
2575 p->sink->id == snd_soc_dapm_input) {
2581 case snd_soc_dapm_line:
2583 snd_soc_dapm_for_each_direction(dir) {
2584 if (!list_empty(&w->edges[dir]))
2585 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2595 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2596 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2597 const char *control)
2599 bool dynamic_source = false;
2600 bool dynamic_sink = false;
2605 switch (source->id) {
2606 case snd_soc_dapm_demux:
2607 dynamic_source = true;
2614 case snd_soc_dapm_mux:
2615 case snd_soc_dapm_switch:
2616 case snd_soc_dapm_mixer:
2617 case snd_soc_dapm_mixer_named_ctl:
2618 dynamic_sink = true;
2624 if (dynamic_source && dynamic_sink) {
2626 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2627 source->name, control, sink->name);
2629 } else if (!dynamic_source && !dynamic_sink) {
2631 "Control not supported for path %s -> [%s] -> %s\n",
2632 source->name, control, sink->name);
2639 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2640 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2641 const char *control,
2642 int (*connected)(struct snd_soc_dapm_widget *source,
2643 struct snd_soc_dapm_widget *sink))
2645 struct snd_soc_dapm_widget *widgets[2];
2646 enum snd_soc_dapm_direction dir;
2647 struct snd_soc_dapm_path *path;
2650 if (wsink->is_supply && !wsource->is_supply) {
2652 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2653 wsource->name, wsink->name);
2657 if (connected && !wsource->is_supply) {
2659 "connected() callback only supported for supply widgets (%s -> %s)\n",
2660 wsource->name, wsink->name);
2664 if (wsource->is_supply && control) {
2666 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2667 wsource->name, control, wsink->name);
2671 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2675 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2679 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2680 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2681 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2682 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2684 path->connected = connected;
2685 INIT_LIST_HEAD(&path->list);
2686 INIT_LIST_HEAD(&path->list_kcontrol);
2688 if (wsource->is_supply || wsink->is_supply)
2689 path->is_supply = 1;
2691 /* connect static paths */
2692 if (control == NULL) {
2695 switch (wsource->id) {
2696 case snd_soc_dapm_demux:
2697 ret = dapm_connect_mux(dapm, path, control, wsource);
2705 switch (wsink->id) {
2706 case snd_soc_dapm_mux:
2707 ret = dapm_connect_mux(dapm, path, control, wsink);
2711 case snd_soc_dapm_switch:
2712 case snd_soc_dapm_mixer:
2713 case snd_soc_dapm_mixer_named_ctl:
2714 ret = dapm_connect_mixer(dapm, path, control);
2723 list_add(&path->list, &dapm->card->paths);
2724 snd_soc_dapm_for_each_direction(dir)
2725 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2727 snd_soc_dapm_for_each_direction(dir) {
2728 dapm_update_widget_flags(widgets[dir]);
2729 dapm_mark_dirty(widgets[dir], "Route added");
2732 if (dapm->card->instantiated && path->connect)
2733 dapm_path_invalidate(path);
2741 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2742 const struct snd_soc_dapm_route *route)
2744 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2745 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2748 char prefixed_sink[80];
2749 char prefixed_source[80];
2753 prefix = soc_dapm_prefix(dapm);
2755 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2756 prefix, route->sink);
2757 sink = prefixed_sink;
2758 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2759 prefix, route->source);
2760 source = prefixed_source;
2763 source = route->source;
2766 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2767 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2769 if (wsink && wsource)
2773 * find src and dest widgets over all widgets but favor a widget from
2774 * current DAPM context
2776 list_for_each_entry(w, &dapm->card->widgets, list) {
2777 if (!wsink && !(strcmp(w->name, sink))) {
2779 if (w->dapm == dapm) {
2786 if (!wsource && !(strcmp(w->name, source))) {
2788 if (w->dapm == dapm) {
2795 /* use widget from another DAPM context if not found from this */
2801 if (wsource == NULL) {
2802 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2806 if (wsink == NULL) {
2807 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2813 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2814 dapm_wcache_update(&dapm->path_source_cache, wsource);
2816 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2823 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2824 source, route->control, sink);
2828 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2829 const struct snd_soc_dapm_route *route)
2831 struct snd_soc_dapm_widget *wsource, *wsink;
2832 struct snd_soc_dapm_path *path, *p;
2835 char prefixed_sink[80];
2836 char prefixed_source[80];
2839 if (route->control) {
2841 "ASoC: Removal of routes with controls not supported\n");
2845 prefix = soc_dapm_prefix(dapm);
2847 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2848 prefix, route->sink);
2849 sink = prefixed_sink;
2850 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2851 prefix, route->source);
2852 source = prefixed_source;
2855 source = route->source;
2859 list_for_each_entry(p, &dapm->card->paths, list) {
2860 if (strcmp(p->source->name, source) != 0)
2862 if (strcmp(p->sink->name, sink) != 0)
2869 wsource = path->source;
2872 dapm_mark_dirty(wsource, "Route removed");
2873 dapm_mark_dirty(wsink, "Route removed");
2875 dapm_path_invalidate(path);
2877 dapm_free_path(path);
2879 /* Update any path related flags */
2880 dapm_update_widget_flags(wsource);
2881 dapm_update_widget_flags(wsink);
2883 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2891 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2892 * @dapm: DAPM context
2893 * @route: audio routes
2894 * @num: number of routes
2896 * Connects 2 dapm widgets together via a named audio path. The sink is
2897 * the widget receiving the audio signal, whilst the source is the sender
2898 * of the audio signal.
2900 * Returns 0 for success else error. On error all resources can be freed
2901 * with a call to snd_soc_card_free().
2903 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2904 const struct snd_soc_dapm_route *route, int num)
2908 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2909 for (i = 0; i < num; i++) {
2910 r = snd_soc_dapm_add_route(dapm, route);
2912 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2914 route->control ? route->control : "direct",
2920 mutex_unlock(&dapm->card->dapm_mutex);
2924 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2927 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2928 * @dapm: DAPM context
2929 * @route: audio routes
2930 * @num: number of routes
2932 * Removes routes from the DAPM context.
2934 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2935 const struct snd_soc_dapm_route *route, int num)
2939 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2940 for (i = 0; i < num; i++) {
2941 snd_soc_dapm_del_route(dapm, route);
2944 mutex_unlock(&dapm->card->dapm_mutex);
2948 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2950 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2951 const struct snd_soc_dapm_route *route)
2953 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2956 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2959 struct snd_soc_dapm_path *path;
2963 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2969 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2974 if (route->control || route->connected)
2975 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2976 route->source, route->sink);
2978 snd_soc_dapm_widget_for_each_sink_path(source, path) {
2979 if (path->sink == sink) {
2986 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2987 route->source, route->sink);
2989 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2990 count, route->source, route->sink);
2996 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2997 * @dapm: DAPM context
2998 * @route: audio routes
2999 * @num: number of routes
3001 * Mark existing routes matching those specified in the passed array
3002 * as being weak, meaning that they are ignored for the purpose of
3003 * power decisions. The main intended use case is for sidetone paths
3004 * which couple audio between other independent paths if they are both
3005 * active in order to make the combination work better at the user
3006 * level but which aren't intended to be "used".
3008 * Note that CODEC drivers should not use this as sidetone type paths
3009 * can frequently also be used as bypass paths.
3011 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3012 const struct snd_soc_dapm_route *route, int num)
3017 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3018 for (i = 0; i < num; i++) {
3019 err = snd_soc_dapm_weak_route(dapm, route);
3024 mutex_unlock(&dapm->card->dapm_mutex);
3028 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3031 * snd_soc_dapm_new_widgets - add new dapm widgets
3032 * @card: card to be checked for new dapm widgets
3034 * Checks the codec for any new dapm widgets and creates them if found.
3036 * Returns 0 for success.
3038 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3040 struct snd_soc_dapm_widget *w;
3043 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3045 list_for_each_entry(w, &card->widgets, list)
3050 if (w->num_kcontrols) {
3051 w->kcontrols = kcalloc(w->num_kcontrols,
3052 sizeof(struct snd_kcontrol *),
3054 if (!w->kcontrols) {
3055 mutex_unlock(&card->dapm_mutex);
3061 case snd_soc_dapm_switch:
3062 case snd_soc_dapm_mixer:
3063 case snd_soc_dapm_mixer_named_ctl:
3066 case snd_soc_dapm_mux:
3067 case snd_soc_dapm_demux:
3070 case snd_soc_dapm_pga:
3071 case snd_soc_dapm_out_drv:
3074 case snd_soc_dapm_dai_link:
3075 dapm_new_dai_link(w);
3081 /* Read the initial power state from the device */
3083 soc_dapm_read(w->dapm, w->reg, &val);
3084 val = val >> w->shift;
3086 if (val == w->on_val)
3092 dapm_mark_dirty(w, "new widget");
3093 dapm_debugfs_add_widget(w);
3096 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3097 mutex_unlock(&card->dapm_mutex);
3100 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3103 * snd_soc_dapm_get_volsw - dapm mixer get callback
3104 * @kcontrol: mixer control
3105 * @ucontrol: control element information
3107 * Callback to get the value of a dapm mixer control.
3109 * Returns 0 for success.
3111 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3112 struct snd_ctl_elem_value *ucontrol)
3114 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3115 struct snd_soc_card *card = dapm->card;
3116 struct soc_mixer_control *mc =
3117 (struct soc_mixer_control *)kcontrol->private_value;
3119 unsigned int shift = mc->shift;
3121 unsigned int width = fls(max);
3122 unsigned int mask = (1 << fls(max)) - 1;
3123 unsigned int invert = mc->invert;
3124 unsigned int reg_val, val, rval = 0;
3127 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3128 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3129 ret = soc_dapm_read(dapm, reg, ®_val);
3130 val = (reg_val >> shift) & mask;
3132 if (ret == 0 && reg != mc->rreg)
3133 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3135 if (snd_soc_volsw_is_stereo(mc))
3136 rval = (reg_val >> mc->rshift) & mask;
3138 reg_val = dapm_kcontrol_get_value(kcontrol);
3139 val = reg_val & mask;
3141 if (snd_soc_volsw_is_stereo(mc))
3142 rval = (reg_val >> width) & mask;
3144 mutex_unlock(&card->dapm_mutex);
3150 ucontrol->value.integer.value[0] = max - val;
3152 ucontrol->value.integer.value[0] = val;
3154 if (snd_soc_volsw_is_stereo(mc)) {
3156 ucontrol->value.integer.value[1] = max - rval;
3158 ucontrol->value.integer.value[1] = rval;
3163 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3166 * snd_soc_dapm_put_volsw - dapm mixer set callback
3167 * @kcontrol: mixer control
3168 * @ucontrol: control element information
3170 * Callback to set the value of a dapm mixer control.
3172 * Returns 0 for success.
3174 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3175 struct snd_ctl_elem_value *ucontrol)
3177 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3178 struct snd_soc_card *card = dapm->card;
3179 struct soc_mixer_control *mc =
3180 (struct soc_mixer_control *)kcontrol->private_value;
3182 unsigned int shift = mc->shift;
3184 unsigned int width = fls(max);
3185 unsigned int mask = (1 << width) - 1;
3186 unsigned int invert = mc->invert;
3187 unsigned int val, rval = 0;
3188 int connect, rconnect = -1, change, reg_change = 0;
3189 struct snd_soc_dapm_update update = {};
3192 val = (ucontrol->value.integer.value[0] & mask);
3198 if (snd_soc_volsw_is_stereo(mc)) {
3199 rval = (ucontrol->value.integer.value[1] & mask);
3205 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3207 /* This assumes field width < (bits in unsigned int / 2) */
3208 if (width > sizeof(unsigned int) * 8 / 2)
3210 "ASoC: control %s field width limit exceeded\n",
3212 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3214 if (reg != SND_SOC_NOPM) {
3216 rval = rval << mc->rshift;
3218 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3220 if (snd_soc_volsw_is_stereo(mc))
3221 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3226 if (change || reg_change) {
3228 if (snd_soc_volsw_is_stereo(mc)) {
3229 update.has_second_set = true;
3230 update.reg2 = mc->rreg;
3231 update.mask2 = mask << mc->rshift;
3234 update.kcontrol = kcontrol;
3236 update.mask = mask << shift;
3238 card->update = &update;
3240 change |= reg_change;
3242 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3245 card->update = NULL;
3248 mutex_unlock(&card->dapm_mutex);
3251 soc_dpcm_runtime_update(card);
3255 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3258 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3259 * @kcontrol: mixer control
3260 * @ucontrol: control element information
3262 * Callback to get the value of a dapm enumerated double mixer control.
3264 * Returns 0 for success.
3266 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3267 struct snd_ctl_elem_value *ucontrol)
3269 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3270 struct snd_soc_card *card = dapm->card;
3271 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3272 unsigned int reg_val, val;
3274 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3275 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3276 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3278 mutex_unlock(&card->dapm_mutex);
3282 reg_val = dapm_kcontrol_get_value(kcontrol);
3284 mutex_unlock(&card->dapm_mutex);
3286 val = (reg_val >> e->shift_l) & e->mask;
3287 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3288 if (e->shift_l != e->shift_r) {
3289 val = (reg_val >> e->shift_r) & e->mask;
3290 val = snd_soc_enum_val_to_item(e, val);
3291 ucontrol->value.enumerated.item[1] = val;
3296 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3299 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3300 * @kcontrol: mixer control
3301 * @ucontrol: control element information
3303 * Callback to set the value of a dapm enumerated double mixer control.
3305 * Returns 0 for success.
3307 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3308 struct snd_ctl_elem_value *ucontrol)
3310 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3311 struct snd_soc_card *card = dapm->card;
3312 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3313 unsigned int *item = ucontrol->value.enumerated.item;
3314 unsigned int val, change, reg_change = 0;
3316 struct snd_soc_dapm_update update = {};
3319 if (item[0] >= e->items)
3322 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3323 mask = e->mask << e->shift_l;
3324 if (e->shift_l != e->shift_r) {
3325 if (item[1] > e->items)
3327 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3328 mask |= e->mask << e->shift_r;
3331 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3333 change = dapm_kcontrol_set_value(kcontrol, val);
3335 if (e->reg != SND_SOC_NOPM)
3336 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3338 if (change || reg_change) {
3340 update.kcontrol = kcontrol;
3341 update.reg = e->reg;
3344 card->update = &update;
3346 change |= reg_change;
3348 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3350 card->update = NULL;
3353 mutex_unlock(&card->dapm_mutex);
3356 soc_dpcm_runtime_update(card);
3360 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3363 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3365 * @kcontrol: mixer control
3366 * @uinfo: control element information
3368 * Callback to provide information about a pin switch control.
3370 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3371 struct snd_ctl_elem_info *uinfo)
3373 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3375 uinfo->value.integer.min = 0;
3376 uinfo->value.integer.max = 1;
3380 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3383 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3385 * @kcontrol: mixer control
3388 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3389 struct snd_ctl_elem_value *ucontrol)
3391 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3392 const char *pin = (const char *)kcontrol->private_value;
3394 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3396 ucontrol->value.integer.value[0] =
3397 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3399 mutex_unlock(&card->dapm_mutex);
3403 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3406 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3408 * @kcontrol: mixer control
3411 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3412 struct snd_ctl_elem_value *ucontrol)
3414 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3415 const char *pin = (const char *)kcontrol->private_value;
3417 if (ucontrol->value.integer.value[0])
3418 snd_soc_dapm_enable_pin(&card->dapm, pin);
3420 snd_soc_dapm_disable_pin(&card->dapm, pin);
3422 snd_soc_dapm_sync(&card->dapm);
3425 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3427 struct snd_soc_dapm_widget *
3428 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3429 const struct snd_soc_dapm_widget *widget)
3431 struct snd_soc_dapm_widget *w;
3433 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3434 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3435 /* Do not nag about probe deferrals */
3437 int ret = PTR_ERR(w);
3439 if (ret != -EPROBE_DEFER)
3441 "ASoC: Failed to create DAPM control %s (%d)\n",
3447 "ASoC: Failed to create DAPM control %s\n",
3451 mutex_unlock(&dapm->card->dapm_mutex);
3454 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3456 struct snd_soc_dapm_widget *
3457 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3458 const struct snd_soc_dapm_widget *widget)
3460 enum snd_soc_dapm_direction dir;
3461 struct snd_soc_dapm_widget *w;
3465 if ((w = dapm_cnew_widget(widget)) == NULL)
3469 case snd_soc_dapm_regulator_supply:
3470 w->regulator = devm_regulator_get(dapm->dev, w->name);
3471 if (IS_ERR(w->regulator)) {
3472 ret = PTR_ERR(w->regulator);
3473 if (ret == -EPROBE_DEFER)
3474 return ERR_PTR(ret);
3475 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3480 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3481 ret = regulator_allow_bypass(w->regulator, true);
3483 dev_warn(w->dapm->dev,
3484 "ASoC: Failed to bypass %s: %d\n",
3488 case snd_soc_dapm_pinctrl:
3489 w->pinctrl = devm_pinctrl_get(dapm->dev);
3490 if (IS_ERR(w->pinctrl)) {
3491 ret = PTR_ERR(w->pinctrl);
3492 if (ret == -EPROBE_DEFER)
3493 return ERR_PTR(ret);
3494 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3499 case snd_soc_dapm_clock_supply:
3500 w->clk = devm_clk_get(dapm->dev, w->name);
3501 if (IS_ERR(w->clk)) {
3502 ret = PTR_ERR(w->clk);
3503 if (ret == -EPROBE_DEFER)
3504 return ERR_PTR(ret);
3505 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3514 prefix = soc_dapm_prefix(dapm);
3516 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3518 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3519 if (w->name == NULL) {
3525 case snd_soc_dapm_mic:
3526 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3527 w->power_check = dapm_generic_check_power;
3529 case snd_soc_dapm_input:
3530 if (!dapm->card->fully_routed)
3531 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3532 w->power_check = dapm_generic_check_power;
3534 case snd_soc_dapm_spk:
3535 case snd_soc_dapm_hp:
3536 w->is_ep = SND_SOC_DAPM_EP_SINK;
3537 w->power_check = dapm_generic_check_power;
3539 case snd_soc_dapm_output:
3540 if (!dapm->card->fully_routed)
3541 w->is_ep = SND_SOC_DAPM_EP_SINK;
3542 w->power_check = dapm_generic_check_power;
3544 case snd_soc_dapm_vmid:
3545 case snd_soc_dapm_siggen:
3546 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3547 w->power_check = dapm_always_on_check_power;
3549 case snd_soc_dapm_sink:
3550 w->is_ep = SND_SOC_DAPM_EP_SINK;
3551 w->power_check = dapm_always_on_check_power;
3554 case snd_soc_dapm_mux:
3555 case snd_soc_dapm_demux:
3556 case snd_soc_dapm_switch:
3557 case snd_soc_dapm_mixer:
3558 case snd_soc_dapm_mixer_named_ctl:
3559 case snd_soc_dapm_adc:
3560 case snd_soc_dapm_aif_out:
3561 case snd_soc_dapm_dac:
3562 case snd_soc_dapm_aif_in:
3563 case snd_soc_dapm_pga:
3564 case snd_soc_dapm_out_drv:
3565 case snd_soc_dapm_micbias:
3566 case snd_soc_dapm_line:
3567 case snd_soc_dapm_dai_link:
3568 case snd_soc_dapm_dai_out:
3569 case snd_soc_dapm_dai_in:
3570 w->power_check = dapm_generic_check_power;
3572 case snd_soc_dapm_supply:
3573 case snd_soc_dapm_regulator_supply:
3574 case snd_soc_dapm_pinctrl:
3575 case snd_soc_dapm_clock_supply:
3576 case snd_soc_dapm_kcontrol:
3578 w->power_check = dapm_supply_check_power;
3581 w->power_check = dapm_always_on_check_power;
3586 INIT_LIST_HEAD(&w->list);
3587 INIT_LIST_HEAD(&w->dirty);
3588 list_add_tail(&w->list, &dapm->card->widgets);
3590 snd_soc_dapm_for_each_direction(dir) {
3591 INIT_LIST_HEAD(&w->edges[dir]);
3592 w->endpoints[dir] = -1;
3595 /* machine layer sets up unconnected pins and insertions */
3601 * snd_soc_dapm_new_controls - create new dapm controls
3602 * @dapm: DAPM context
3603 * @widget: widget array
3604 * @num: number of widgets
3606 * Creates new DAPM controls based upon the templates.
3608 * Returns 0 for success else error.
3610 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3611 const struct snd_soc_dapm_widget *widget,
3614 struct snd_soc_dapm_widget *w;
3618 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3619 for (i = 0; i < num; i++) {
3620 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3623 /* Do not nag about probe deferrals */
3624 if (ret == -EPROBE_DEFER)
3627 "ASoC: Failed to create DAPM control %s (%d)\n",
3633 "ASoC: Failed to create DAPM control %s\n",
3640 mutex_unlock(&dapm->card->dapm_mutex);
3643 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3645 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3646 struct snd_kcontrol *kcontrol, int event)
3648 struct snd_soc_dapm_path *source_p, *sink_p;
3649 struct snd_soc_dai *source, *sink;
3650 struct snd_soc_pcm_runtime *rtd = w->priv;
3651 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
3652 struct snd_pcm_substream substream;
3653 struct snd_pcm_hw_params *params = NULL;
3654 struct snd_pcm_runtime *runtime = NULL;
3658 if (WARN_ON(!config) ||
3659 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3660 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3663 /* We only support a single source and sink, pick the first */
3664 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT],
3665 struct snd_soc_dapm_path,
3666 list_node[SND_SOC_DAPM_DIR_OUT]);
3667 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN],
3668 struct snd_soc_dapm_path,
3669 list_node[SND_SOC_DAPM_DIR_IN]);
3671 source = source_p->source->priv;
3672 sink = sink_p->sink->priv;
3674 /* Be a little careful as we don't want to overflow the mask array */
3675 if (config->formats) {
3676 fmt = ffs(config->formats) - 1;
3678 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3683 /* Currently very limited parameter selection */
3684 params = kzalloc(sizeof(*params), GFP_KERNEL);
3689 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3691 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3693 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3696 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3697 = config->channels_min;
3698 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3699 = config->channels_max;
3701 memset(&substream, 0, sizeof(substream));
3703 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3704 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3709 substream.runtime = runtime;
3710 substream.private_data = rtd;
3713 case SND_SOC_DAPM_PRE_PMU:
3714 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3715 if (source->driver->ops->startup) {
3716 ret = source->driver->ops->startup(&substream, source);
3718 dev_err(source->dev,
3719 "ASoC: startup() failed: %d\n", ret);
3724 ret = soc_dai_hw_params(&substream, params, source);
3728 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3729 if (sink->driver->ops->startup) {
3730 ret = sink->driver->ops->startup(&substream, sink);
3733 "ASoC: startup() failed: %d\n", ret);
3738 ret = soc_dai_hw_params(&substream, params, sink);
3743 case SND_SOC_DAPM_POST_PMU:
3744 ret = snd_soc_dai_digital_mute(sink, 0,
3745 SNDRV_PCM_STREAM_PLAYBACK);
3746 if (ret != 0 && ret != -ENOTSUPP)
3747 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3751 case SND_SOC_DAPM_PRE_PMD:
3752 ret = snd_soc_dai_digital_mute(sink, 1,
3753 SNDRV_PCM_STREAM_PLAYBACK);
3754 if (ret != 0 && ret != -ENOTSUPP)
3755 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3759 if (source->driver->ops->shutdown) {
3760 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3761 source->driver->ops->shutdown(&substream, source);
3765 if (sink->driver->ops->shutdown) {
3766 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3767 sink->driver->ops->shutdown(&substream, sink);
3772 WARN(1, "Unknown event %d\n", event);
3782 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3783 struct snd_ctl_elem_value *ucontrol)
3785 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3787 ucontrol->value.enumerated.item[0] = w->params_select;
3792 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3793 struct snd_ctl_elem_value *ucontrol)
3795 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3797 /* Can't change the config when widget is already powered */
3801 if (ucontrol->value.enumerated.item[0] == w->params_select)
3804 if (ucontrol->value.enumerated.item[0] >= w->num_params)
3807 w->params_select = ucontrol->value.enumerated.item[0];
3813 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3814 unsigned long *private_value,
3816 const char **w_param_text)
3820 devm_kfree(card->dev, (void *)*private_value);
3821 for (count = 0 ; count < num_params; count++)
3822 devm_kfree(card->dev, (void *)w_param_text[count]);
3823 devm_kfree(card->dev, w_param_text);
3826 static struct snd_kcontrol_new *
3827 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3829 const struct snd_soc_pcm_stream *params,
3830 int num_params, const char **w_param_text,
3831 unsigned long *private_value)
3833 struct soc_enum w_param_enum[] = {
3834 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3836 struct snd_kcontrol_new kcontrol_dai_link[] = {
3837 SOC_ENUM_EXT(NULL, w_param_enum[0],
3838 snd_soc_dapm_dai_link_get,
3839 snd_soc_dapm_dai_link_put),
3841 struct snd_kcontrol_new *kcontrol_news;
3842 const struct snd_soc_pcm_stream *config = params;
3845 for (count = 0 ; count < num_params; count++) {
3846 if (!config->stream_name) {
3847 dev_warn(card->dapm.dev,
3848 "ASoC: anonymous config %d for dai link %s\n",
3850 w_param_text[count] =
3851 devm_kasprintf(card->dev, GFP_KERNEL,
3852 "Anonymous Configuration %d",
3855 w_param_text[count] = devm_kmemdup(card->dev,
3856 config->stream_name,
3857 strlen(config->stream_name) + 1,
3860 if (!w_param_text[count])
3861 goto outfree_w_param;
3865 w_param_enum[0].items = num_params;
3866 w_param_enum[0].texts = w_param_text;
3869 (unsigned long) devm_kmemdup(card->dev,
3870 (void *)(kcontrol_dai_link[0].private_value),
3871 sizeof(struct soc_enum), GFP_KERNEL);
3872 if (!*private_value) {
3873 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3875 goto outfree_w_param;
3877 kcontrol_dai_link[0].private_value = *private_value;
3878 /* duplicate kcontrol_dai_link on heap so that memory persists */
3879 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3880 sizeof(struct snd_kcontrol_new),
3882 if (!kcontrol_news) {
3883 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3885 goto outfree_w_param;
3887 return kcontrol_news;
3890 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
3894 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3895 struct snd_soc_pcm_runtime *rtd,
3896 const struct snd_soc_pcm_stream *params,
3897 unsigned int num_params,
3898 struct snd_soc_dapm_widget *source,
3899 struct snd_soc_dapm_widget *sink)
3901 struct snd_soc_dapm_widget template;
3902 struct snd_soc_dapm_widget *w;
3903 const char **w_param_text;
3904 unsigned long private_value;
3908 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3909 source->name, sink->name);
3913 memset(&template, 0, sizeof(template));
3914 template.reg = SND_SOC_NOPM;
3915 template.id = snd_soc_dapm_dai_link;
3916 template.name = link_name;
3917 template.event = snd_soc_dai_link_event;
3918 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3919 SND_SOC_DAPM_PRE_PMD;
3920 template.kcontrol_news = NULL;
3922 /* allocate memory for control, only in case of multiple configs */
3923 if (num_params > 1) {
3924 w_param_text = devm_kcalloc(card->dev, num_params,
3925 sizeof(char *), GFP_KERNEL);
3926 if (!w_param_text) {
3931 template.num_kcontrols = 1;
3932 template.kcontrol_news =
3933 snd_soc_dapm_alloc_kcontrol(card,
3934 link_name, params, num_params,
3935 w_param_text, &private_value);
3936 if (!template.kcontrol_news) {
3941 w_param_text = NULL;
3943 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3945 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
3948 /* Do not nag about probe deferrals */
3949 if (ret != -EPROBE_DEFER)
3951 "ASoC: Failed to create %s widget (%d)\n",
3953 goto outfree_kcontrol_news;
3956 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3959 goto outfree_kcontrol_news;
3963 w->num_params = num_params;
3966 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3969 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3972 devm_kfree(card->dev, w);
3973 outfree_kcontrol_news:
3974 devm_kfree(card->dev, (void *)template.kcontrol_news);
3975 snd_soc_dapm_free_kcontrol(card, &private_value, num_params, w_param_text);
3977 devm_kfree(card->dev, link_name);
3981 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3982 struct snd_soc_dai *dai)
3984 struct snd_soc_dapm_widget template;
3985 struct snd_soc_dapm_widget *w;
3987 WARN_ON(dapm->dev != dai->dev);
3989 memset(&template, 0, sizeof(template));
3990 template.reg = SND_SOC_NOPM;
3992 if (dai->driver->playback.stream_name) {
3993 template.id = snd_soc_dapm_dai_in;
3994 template.name = dai->driver->playback.stream_name;
3995 template.sname = dai->driver->playback.stream_name;
3997 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4000 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4002 int ret = PTR_ERR(w);
4004 /* Do not nag about probe deferrals */
4005 if (ret != -EPROBE_DEFER)
4007 "ASoC: Failed to create %s widget (%d)\n",
4008 dai->driver->playback.stream_name, ret);
4012 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4013 dai->driver->playback.stream_name);
4018 dai->playback_widget = w;
4021 if (dai->driver->capture.stream_name) {
4022 template.id = snd_soc_dapm_dai_out;
4023 template.name = dai->driver->capture.stream_name;
4024 template.sname = dai->driver->capture.stream_name;
4026 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4029 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4031 int ret = PTR_ERR(w);
4033 /* Do not nag about probe deferrals */
4034 if (ret != -EPROBE_DEFER)
4036 "ASoC: Failed to create %s widget (%d)\n",
4037 dai->driver->playback.stream_name, ret);
4041 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
4042 dai->driver->capture.stream_name);
4047 dai->capture_widget = w;
4053 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4055 struct snd_soc_dapm_widget *dai_w, *w;
4056 struct snd_soc_dapm_widget *src, *sink;
4057 struct snd_soc_dai *dai;
4059 /* For each DAI widget... */
4060 list_for_each_entry(dai_w, &card->widgets, list) {
4061 switch (dai_w->id) {
4062 case snd_soc_dapm_dai_in:
4063 case snd_soc_dapm_dai_out:
4069 /* let users know there is no DAI to link */
4071 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4078 /* ...find all widgets with the same stream and link them */
4079 list_for_each_entry(w, &card->widgets, list) {
4080 if (w->dapm != dai_w->dapm)
4084 case snd_soc_dapm_dai_in:
4085 case snd_soc_dapm_dai_out:
4091 if (!w->sname || !strstr(w->sname, dai_w->sname))
4094 if (dai_w->id == snd_soc_dapm_dai_in) {
4101 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4102 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4109 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4110 struct snd_soc_pcm_runtime *rtd)
4112 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4113 struct snd_soc_dapm_widget *sink, *source;
4116 for (i = 0; i < rtd->num_codecs; i++) {
4117 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
4119 /* connect BE DAI playback if widgets are valid */
4120 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
4121 source = cpu_dai->playback_widget;
4122 sink = codec_dai->playback_widget;
4123 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4124 cpu_dai->component->name, source->name,
4125 codec_dai->component->name, sink->name);
4127 snd_soc_dapm_add_path(&card->dapm, source, sink,
4131 /* connect BE DAI capture if widgets are valid */
4132 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
4133 source = codec_dai->capture_widget;
4134 sink = cpu_dai->capture_widget;
4135 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4136 codec_dai->component->name, source->name,
4137 cpu_dai->component->name, sink->name);
4139 snd_soc_dapm_add_path(&card->dapm, source, sink,
4145 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4148 struct snd_soc_dapm_widget *w;
4151 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4152 w = dai->playback_widget;
4154 w = dai->capture_widget;
4157 dapm_mark_dirty(w, "stream event");
4159 if (w->id == snd_soc_dapm_dai_in) {
4160 ep = SND_SOC_DAPM_EP_SOURCE;
4161 dapm_widget_invalidate_input_paths(w);
4163 ep = SND_SOC_DAPM_EP_SINK;
4164 dapm_widget_invalidate_output_paths(w);
4168 case SND_SOC_DAPM_STREAM_START:
4172 case SND_SOC_DAPM_STREAM_STOP:
4176 case SND_SOC_DAPM_STREAM_SUSPEND:
4177 case SND_SOC_DAPM_STREAM_RESUME:
4178 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4179 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4185 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4187 struct snd_soc_pcm_runtime *rtd;
4189 /* for each BE DAI link... */
4190 list_for_each_entry(rtd, &card->rtd_list, list) {
4192 * dynamic FE links have no fixed DAI mapping.
4193 * CODEC<->CODEC links have no direct connection.
4195 if (rtd->dai_link->dynamic || rtd->dai_link->params)
4198 dapm_connect_dai_link_widgets(card, rtd);
4202 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4207 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4208 for (i = 0; i < rtd->num_codecs; i++)
4209 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
4211 dapm_power_widgets(rtd->card, event);
4215 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4216 * @rtd: PCM runtime data
4217 * @stream: stream name
4218 * @event: stream event
4220 * Sends a stream event to the dapm core. The core then makes any
4221 * necessary widget power changes.
4223 * Returns 0 for success else error.
4225 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4228 struct snd_soc_card *card = rtd->card;
4230 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4231 soc_dapm_stream_event(rtd, stream, event);
4232 mutex_unlock(&card->dapm_mutex);
4236 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4237 * @dapm: DAPM context
4240 * Enables input/output pin and its parents or children widgets iff there is
4241 * a valid audio route and active audio stream.
4243 * Requires external locking.
4245 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4246 * do any widget power switching.
4248 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4251 return snd_soc_dapm_set_pin(dapm, pin, 1);
4253 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4256 * snd_soc_dapm_enable_pin - enable pin.
4257 * @dapm: DAPM context
4260 * Enables input/output pin and its parents or children widgets iff there is
4261 * a valid audio route and active audio stream.
4263 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4264 * do any widget power switching.
4266 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4270 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4272 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4274 mutex_unlock(&dapm->card->dapm_mutex);
4278 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4281 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4282 * @dapm: DAPM context
4285 * Enables input/output pin regardless of any other state. This is
4286 * intended for use with microphone bias supplies used in microphone
4289 * Requires external locking.
4291 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4292 * do any widget power switching.
4294 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4297 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4300 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4304 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4305 if (!w->connected) {
4307 * w->force does not affect the number of input or output paths,
4308 * so we only have to recheck if w->connected is changed
4310 dapm_widget_invalidate_input_paths(w);
4311 dapm_widget_invalidate_output_paths(w);
4315 dapm_mark_dirty(w, "force enable");
4319 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4322 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4323 * @dapm: DAPM context
4326 * Enables input/output pin regardless of any other state. This is
4327 * intended for use with microphone bias supplies used in microphone
4330 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4331 * do any widget power switching.
4333 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4338 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4340 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4342 mutex_unlock(&dapm->card->dapm_mutex);
4346 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4349 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4350 * @dapm: DAPM context
4353 * Disables input/output pin and its parents or children widgets.
4355 * Requires external locking.
4357 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4358 * do any widget power switching.
4360 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4363 return snd_soc_dapm_set_pin(dapm, pin, 0);
4365 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4368 * snd_soc_dapm_disable_pin - disable pin.
4369 * @dapm: DAPM context
4372 * Disables input/output pin and its parents or children widgets.
4374 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4375 * do any widget power switching.
4377 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4382 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4384 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4386 mutex_unlock(&dapm->card->dapm_mutex);
4390 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4393 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4394 * @dapm: DAPM context
4397 * Marks the specified pin as being not connected, disabling it along
4398 * any parent or child widgets. At present this is identical to
4399 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4400 * additional things such as disabling controls which only affect
4401 * paths through the pin.
4403 * Requires external locking.
4405 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4406 * do any widget power switching.
4408 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4411 return snd_soc_dapm_set_pin(dapm, pin, 0);
4413 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4416 * snd_soc_dapm_nc_pin - permanently disable pin.
4417 * @dapm: DAPM context
4420 * Marks the specified pin as being not connected, disabling it along
4421 * any parent or child widgets. At present this is identical to
4422 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4423 * additional things such as disabling controls which only affect
4424 * paths through the pin.
4426 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4427 * do any widget power switching.
4429 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4433 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4435 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4437 mutex_unlock(&dapm->card->dapm_mutex);
4441 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4444 * snd_soc_dapm_get_pin_status - get audio pin status
4445 * @dapm: DAPM context
4446 * @pin: audio signal pin endpoint (or start point)
4448 * Get audio pin status - connected or disconnected.
4450 * Returns 1 for connected otherwise 0.
4452 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4455 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4458 return w->connected;
4462 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4465 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4466 * @dapm: DAPM context
4467 * @pin: audio signal pin endpoint (or start point)
4469 * Mark the given endpoint or pin as ignoring suspend. When the
4470 * system is disabled a path between two endpoints flagged as ignoring
4471 * suspend will not be disabled. The path must already be enabled via
4472 * normal means at suspend time, it will not be turned on if it was not
4475 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4478 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4481 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4485 w->ignore_suspend = 1;
4489 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4492 * snd_soc_dapm_free - free dapm resources
4493 * @dapm: DAPM context
4495 * Free all dapm widgets and resources.
4497 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4499 dapm_debugfs_cleanup(dapm);
4500 dapm_free_widgets(dapm);
4501 list_del(&dapm->list);
4503 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4505 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4507 struct snd_soc_card *card = dapm->card;
4508 struct snd_soc_dapm_widget *w;
4509 LIST_HEAD(down_list);
4512 mutex_lock(&card->dapm_mutex);
4514 list_for_each_entry(w, &dapm->card->widgets, list) {
4515 if (w->dapm != dapm)
4518 dapm_seq_insert(w, &down_list, false);
4524 /* If there were no widgets to power down we're already in
4528 if (dapm->bias_level == SND_SOC_BIAS_ON)
4529 snd_soc_dapm_set_bias_level(dapm,
4530 SND_SOC_BIAS_PREPARE);
4531 dapm_seq_run(card, &down_list, 0, false);
4532 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4533 snd_soc_dapm_set_bias_level(dapm,
4534 SND_SOC_BIAS_STANDBY);
4537 mutex_unlock(&card->dapm_mutex);
4541 * snd_soc_dapm_shutdown - callback for system shutdown
4543 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4545 struct snd_soc_dapm_context *dapm;
4547 list_for_each_entry(dapm, &card->dapm_list, list) {
4548 if (dapm != &card->dapm) {
4549 soc_dapm_shutdown_dapm(dapm);
4550 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4551 snd_soc_dapm_set_bias_level(dapm,
4556 soc_dapm_shutdown_dapm(&card->dapm);
4557 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4558 snd_soc_dapm_set_bias_level(&card->dapm,
4562 /* Module information */
4563 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4564 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4565 MODULE_LICENSE("GPL");