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/init.h>
22 #include <linux/async.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/platform_device.h>
27 #include <linux/jiffies.h>
28 #include <linux/debugfs.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/clk.h>
33 #include <linux/slab.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/initval.h>
40 #include <trace/events/asoc.h>
42 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
44 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
45 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
47 #define snd_soc_dapm_for_each_direction(dir) \
48 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
51 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
52 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
54 int (*connected)(struct snd_soc_dapm_widget *source,
55 struct snd_soc_dapm_widget *sink));
57 struct snd_soc_dapm_widget *
58 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
59 const struct snd_soc_dapm_widget *widget);
61 struct snd_soc_dapm_widget *
62 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
63 const struct snd_soc_dapm_widget *widget);
65 /* dapm power sequences - make this per codec in the future */
66 static int dapm_up_seq[] = {
67 [snd_soc_dapm_pre] = 0,
68 [snd_soc_dapm_regulator_supply] = 1,
69 [snd_soc_dapm_pinctrl] = 1,
70 [snd_soc_dapm_clock_supply] = 1,
71 [snd_soc_dapm_supply] = 2,
72 [snd_soc_dapm_micbias] = 3,
73 [snd_soc_dapm_dai_link] = 2,
74 [snd_soc_dapm_dai_in] = 4,
75 [snd_soc_dapm_dai_out] = 4,
76 [snd_soc_dapm_aif_in] = 4,
77 [snd_soc_dapm_aif_out] = 4,
78 [snd_soc_dapm_mic] = 5,
79 [snd_soc_dapm_mux] = 6,
80 [snd_soc_dapm_demux] = 6,
81 [snd_soc_dapm_dac] = 7,
82 [snd_soc_dapm_switch] = 8,
83 [snd_soc_dapm_mixer] = 8,
84 [snd_soc_dapm_mixer_named_ctl] = 8,
85 [snd_soc_dapm_pga] = 9,
86 [snd_soc_dapm_adc] = 10,
87 [snd_soc_dapm_out_drv] = 11,
88 [snd_soc_dapm_hp] = 11,
89 [snd_soc_dapm_spk] = 11,
90 [snd_soc_dapm_line] = 11,
91 [snd_soc_dapm_kcontrol] = 12,
92 [snd_soc_dapm_post] = 13,
95 static int dapm_down_seq[] = {
96 [snd_soc_dapm_pre] = 0,
97 [snd_soc_dapm_kcontrol] = 1,
98 [snd_soc_dapm_adc] = 2,
99 [snd_soc_dapm_hp] = 3,
100 [snd_soc_dapm_spk] = 3,
101 [snd_soc_dapm_line] = 3,
102 [snd_soc_dapm_out_drv] = 3,
103 [snd_soc_dapm_pga] = 4,
104 [snd_soc_dapm_switch] = 5,
105 [snd_soc_dapm_mixer_named_ctl] = 5,
106 [snd_soc_dapm_mixer] = 5,
107 [snd_soc_dapm_dac] = 6,
108 [snd_soc_dapm_mic] = 7,
109 [snd_soc_dapm_micbias] = 8,
110 [snd_soc_dapm_mux] = 9,
111 [snd_soc_dapm_demux] = 9,
112 [snd_soc_dapm_aif_in] = 10,
113 [snd_soc_dapm_aif_out] = 10,
114 [snd_soc_dapm_dai_in] = 10,
115 [snd_soc_dapm_dai_out] = 10,
116 [snd_soc_dapm_dai_link] = 11,
117 [snd_soc_dapm_supply] = 12,
118 [snd_soc_dapm_clock_supply] = 13,
119 [snd_soc_dapm_pinctrl] = 13,
120 [snd_soc_dapm_regulator_supply] = 13,
121 [snd_soc_dapm_post] = 14,
124 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
126 if (dapm->card && dapm->card->instantiated)
127 lockdep_assert_held(&dapm->card->dapm_mutex);
130 static void pop_wait(u32 pop_time)
133 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
136 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
144 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
149 vsnprintf(buf, PAGE_SIZE, fmt, args);
150 dev_info(dev, "%s", buf);
156 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
158 return !list_empty(&w->dirty);
161 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
163 dapm_assert_locked(w->dapm);
165 if (!dapm_dirty_widget(w)) {
166 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
168 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
173 * Common implementation for dapm_widget_invalidate_input_paths() and
174 * dapm_widget_invalidate_output_paths(). The function is inlined since the
175 * combined size of the two specialized functions is only marginally larger then
176 * the size of the generic function and at the same time the fast path of the
177 * specialized functions is significantly smaller than the generic function.
179 static __always_inline void dapm_widget_invalidate_paths(
180 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
182 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
183 struct snd_soc_dapm_widget *node;
184 struct snd_soc_dapm_path *p;
187 dapm_assert_locked(w->dapm);
189 if (w->endpoints[dir] == -1)
192 list_add_tail(&w->work_list, &list);
193 w->endpoints[dir] = -1;
195 list_for_each_entry(w, &list, work_list) {
196 snd_soc_dapm_widget_for_each_path(w, dir, p) {
197 if (p->is_supply || p->weak || !p->connect)
199 node = p->node[rdir];
200 if (node->endpoints[dir] != -1) {
201 node->endpoints[dir] = -1;
202 list_add_tail(&node->work_list, &list);
209 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
211 * @w: The widget for which to invalidate the cached number of input paths
213 * Resets the cached number of inputs for the specified widget and all widgets
214 * that can be reached via outcoming paths from the widget.
216 * This function must be called if the number of output paths for a widget might
217 * have changed. E.g. if the source state of a widget changes or a path is added
218 * or activated with the widget as the sink.
220 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
222 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
226 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
228 * @w: The widget for which to invalidate the cached number of output paths
230 * Resets the cached number of outputs for the specified widget and all widgets
231 * that can be reached via incoming paths from the widget.
233 * This function must be called if the number of output paths for a widget might
234 * have changed. E.g. if the sink state of a widget changes or a path is added
235 * or activated with the widget as the source.
237 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
239 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
243 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
244 * for the widgets connected to a path
245 * @p: The path to invalidate
247 * Resets the cached number of inputs for the sink of the path and the cached
248 * number of outputs for the source of the path.
250 * This function must be called when a path is added, removed or the connected
253 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
256 * Weak paths or supply paths do not influence the number of input or
257 * output paths of their neighbors.
259 if (p->weak || p->is_supply)
263 * The number of connected endpoints is the sum of the number of
264 * connected endpoints of all neighbors. If a node with 0 connected
265 * endpoints is either connected or disconnected that sum won't change,
266 * so there is no need to re-check the path.
268 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
269 dapm_widget_invalidate_input_paths(p->sink);
270 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
271 dapm_widget_invalidate_output_paths(p->source);
274 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
276 struct snd_soc_dapm_widget *w;
278 mutex_lock(&card->dapm_mutex);
280 list_for_each_entry(w, &card->widgets, list) {
282 dapm_mark_dirty(w, "Rechecking endpoints");
283 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
284 dapm_widget_invalidate_output_paths(w);
285 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
286 dapm_widget_invalidate_input_paths(w);
290 mutex_unlock(&card->dapm_mutex);
292 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
294 /* create a new dapm widget */
295 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
296 const struct snd_soc_dapm_widget *_widget)
298 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
301 struct dapm_kcontrol_data {
303 struct snd_soc_dapm_widget *widget;
304 struct list_head paths;
305 struct snd_soc_dapm_widget_list *wlist;
308 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
309 struct snd_kcontrol *kcontrol, const char *ctrl_name)
311 struct dapm_kcontrol_data *data;
312 struct soc_mixer_control *mc;
317 data = kzalloc(sizeof(*data), GFP_KERNEL);
321 INIT_LIST_HEAD(&data->paths);
323 switch (widget->id) {
324 case snd_soc_dapm_switch:
325 case snd_soc_dapm_mixer:
326 case snd_soc_dapm_mixer_named_ctl:
327 mc = (struct soc_mixer_control *)kcontrol->private_value;
329 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
330 dev_warn(widget->dapm->dev,
331 "ASoC: Unsupported stereo autodisable control '%s'\n",
334 if (mc->autodisable) {
335 struct snd_soc_dapm_widget template;
337 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
344 memset(&template, 0, sizeof(template));
345 template.reg = mc->reg;
346 template.mask = (1 << fls(mc->max)) - 1;
347 template.shift = mc->shift;
349 template.off_val = mc->max;
351 template.off_val = 0;
352 template.on_val = template.off_val;
353 template.id = snd_soc_dapm_kcontrol;
354 template.name = name;
356 data->value = template.on_val;
359 snd_soc_dapm_new_control_unlocked(widget->dapm,
362 if (IS_ERR(data->widget)) {
363 ret = PTR_ERR(data->widget);
368 case snd_soc_dapm_demux:
369 case snd_soc_dapm_mux:
370 e = (struct soc_enum *)kcontrol->private_value;
372 if (e->autodisable) {
373 struct snd_soc_dapm_widget template;
375 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
382 memset(&template, 0, sizeof(template));
383 template.reg = e->reg;
384 template.mask = e->mask << e->shift_l;
385 template.shift = e->shift_l;
386 template.off_val = snd_soc_enum_item_to_val(e, 0);
387 template.on_val = template.off_val;
388 template.id = snd_soc_dapm_kcontrol;
389 template.name = name;
391 data->value = template.on_val;
393 data->widget = snd_soc_dapm_new_control_unlocked(
394 widget->dapm, &template);
396 if (IS_ERR(data->widget)) {
397 ret = PTR_ERR(data->widget);
401 snd_soc_dapm_add_path(widget->dapm, data->widget,
409 kcontrol->private_data = data;
418 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
420 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
422 list_del(&data->paths);
427 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
428 const struct snd_kcontrol *kcontrol)
430 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
435 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
436 struct snd_soc_dapm_widget *widget)
438 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
439 struct snd_soc_dapm_widget_list *new_wlist;
443 n = data->wlist->num_widgets + 1;
447 new_wlist = krealloc(data->wlist,
448 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
452 new_wlist->widgets[n - 1] = widget;
453 new_wlist->num_widgets = n;
455 data->wlist = new_wlist;
460 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
461 struct snd_soc_dapm_path *path)
463 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
465 list_add_tail(&path->list_kcontrol, &data->paths);
468 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
470 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
475 return data->widget->power;
478 static struct list_head *dapm_kcontrol_get_path_list(
479 const struct snd_kcontrol *kcontrol)
481 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
486 #define dapm_kcontrol_for_each_path(path, kcontrol) \
487 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
490 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
492 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
496 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
498 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
501 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
503 if (data->value == value)
507 data->widget->on_val = value;
515 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
517 * @kcontrol: The kcontrol
519 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
520 struct snd_kcontrol *kcontrol)
522 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
524 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
527 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
529 * @kcontrol: The kcontrol
531 * Note: This function must only be used on kcontrols that are known to have
532 * been registered for a CODEC. Otherwise the behaviour is undefined.
534 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
535 struct snd_kcontrol *kcontrol)
537 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
539 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
541 static void dapm_reset(struct snd_soc_card *card)
543 struct snd_soc_dapm_widget *w;
545 lockdep_assert_held(&card->dapm_mutex);
547 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
549 list_for_each_entry(w, &card->widgets, list) {
550 w->new_power = w->power;
551 w->power_checked = false;
555 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
557 if (!dapm->component)
559 return dapm->component->name_prefix;
562 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
565 if (!dapm->component)
567 return snd_soc_component_read(dapm->component, reg, value);
570 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
571 int reg, unsigned int mask, unsigned int value)
573 if (!dapm->component)
575 return snd_soc_component_update_bits(dapm->component, reg,
579 static int soc_dapm_test_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_test_bits(dapm->component, reg, mask, value);
587 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
590 snd_soc_component_async_complete(dapm->component);
593 static struct snd_soc_dapm_widget *
594 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
596 struct snd_soc_dapm_widget *w = wcache->widget;
597 struct list_head *wlist;
602 wlist = &w->dapm->card->widgets;
604 list_for_each_entry_from(w, wlist, list) {
605 if (!strcmp(name, w->name))
616 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
617 struct snd_soc_dapm_widget *w)
623 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
624 * @dapm: The DAPM context for which to set the level
625 * @level: The level to set
627 * Forces the DAPM bias level to a specific state. It will call the bias level
628 * callback of DAPM context with the specified level. This will even happen if
629 * the context is already at the same level. Furthermore it will not go through
630 * the normal bias level sequencing, meaning any intermediate states between the
631 * current and the target state will not be entered.
633 * Note that the change in bias level is only temporary and the next time
634 * snd_soc_dapm_sync() is called the state will be set to the level as
635 * determined by the DAPM core. The function is mainly intended to be used to
636 * used during probe or resume from suspend to power up the device so
637 * initialization can be done, before the DAPM core takes over.
639 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
640 enum snd_soc_bias_level level)
644 if (dapm->set_bias_level)
645 ret = dapm->set_bias_level(dapm, level);
648 dapm->bias_level = level;
652 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
655 * snd_soc_dapm_set_bias_level - set the bias level for the system
656 * @dapm: DAPM context
657 * @level: level to configure
659 * Configure the bias (power) levels for the SoC audio device.
661 * Returns 0 for success else error.
663 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
664 enum snd_soc_bias_level level)
666 struct snd_soc_card *card = dapm->card;
669 trace_snd_soc_bias_level_start(card, level);
671 if (card && card->set_bias_level)
672 ret = card->set_bias_level(card, dapm, level);
676 if (!card || dapm != &card->dapm)
677 ret = snd_soc_dapm_force_bias_level(dapm, level);
682 if (card && card->set_bias_level_post)
683 ret = card->set_bias_level_post(card, dapm, level);
685 trace_snd_soc_bias_level_done(card, level);
690 /* connect mux widget to its interconnecting audio paths */
691 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
692 struct snd_soc_dapm_path *path, const char *control_name,
693 struct snd_soc_dapm_widget *w)
695 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
696 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
697 unsigned int val, item;
700 if (e->reg != SND_SOC_NOPM) {
701 soc_dapm_read(dapm, e->reg, &val);
702 val = (val >> e->shift_l) & e->mask;
703 item = snd_soc_enum_val_to_item(e, val);
705 /* since a virtual mux has no backing registers to
706 * decide which path to connect, it will try to match
707 * with the first enumeration. This is to ensure
708 * that the default mux choice (the first) will be
709 * correctly powered up during initialization.
714 i = match_string(e->texts, e->items, control_name);
718 path->name = e->texts[i];
719 path->connect = (i == item);
724 /* set up initial codec paths */
725 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
728 struct soc_mixer_control *mc = (struct soc_mixer_control *)
729 p->sink->kcontrol_news[i].private_value;
730 unsigned int reg = mc->reg;
731 unsigned int shift = mc->shift;
732 unsigned int max = mc->max;
733 unsigned int mask = (1 << fls(max)) - 1;
734 unsigned int invert = mc->invert;
737 if (reg != SND_SOC_NOPM) {
738 soc_dapm_read(p->sink->dapm, reg, &val);
740 * The nth_path argument allows this function to know
741 * which path of a kcontrol it is setting the initial
742 * status for. Ideally this would support any number
743 * of paths and channels. But since kcontrols only come
744 * in mono and stereo variants, we are limited to 2
747 * The following code assumes for stereo controls the
748 * first path is the left channel, and all remaining
749 * paths are the right channel.
751 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
753 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
754 val = (val >> mc->rshift) & mask;
756 val = (val >> shift) & mask;
766 /* connect mixer widget to its interconnecting audio paths */
767 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
768 struct snd_soc_dapm_path *path, const char *control_name)
772 /* search for mixer kcontrol */
773 for (i = 0; i < path->sink->num_kcontrols; i++) {
774 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
775 path->name = path->sink->kcontrol_news[i].name;
776 dapm_set_mixer_path_status(path, i, nth_path++);
783 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
784 struct snd_soc_dapm_widget *kcontrolw,
785 const struct snd_kcontrol_new *kcontrol_new,
786 struct snd_kcontrol **kcontrol)
788 struct snd_soc_dapm_widget *w;
793 list_for_each_entry(w, &dapm->card->widgets, list) {
794 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
796 for (i = 0; i < w->num_kcontrols; i++) {
797 if (&w->kcontrol_news[i] == kcontrol_new) {
799 *kcontrol = w->kcontrols[i];
809 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
810 * create it. Either way, add the widget into the control's widget list
812 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
815 struct snd_soc_dapm_context *dapm = w->dapm;
816 struct snd_card *card = dapm->card->snd_card;
820 struct snd_kcontrol *kcontrol;
821 bool wname_in_long_name, kcname_in_long_name;
822 char *long_name = NULL;
826 prefix = soc_dapm_prefix(dapm);
828 prefix_len = strlen(prefix) + 1;
832 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
837 wname_in_long_name = false;
838 kcname_in_long_name = true;
841 case snd_soc_dapm_switch:
842 case snd_soc_dapm_mixer:
843 case snd_soc_dapm_pga:
844 case snd_soc_dapm_out_drv:
845 wname_in_long_name = true;
846 kcname_in_long_name = true;
848 case snd_soc_dapm_mixer_named_ctl:
849 wname_in_long_name = false;
850 kcname_in_long_name = true;
852 case snd_soc_dapm_demux:
853 case snd_soc_dapm_mux:
854 wname_in_long_name = true;
855 kcname_in_long_name = false;
862 if (wname_in_long_name && kcname_in_long_name) {
864 * The control will get a prefix from the control
865 * creation process but we're also using the same
866 * prefix for widgets so cut the prefix off the
867 * front of the widget name.
869 long_name = kasprintf(GFP_KERNEL, "%s %s",
870 w->name + prefix_len,
871 w->kcontrol_news[kci].name);
872 if (long_name == NULL)
876 } else if (wname_in_long_name) {
878 name = w->name + prefix_len;
881 name = w->kcontrol_news[kci].name;
884 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
891 kcontrol->private_free = dapm_kcontrol_free;
893 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
895 snd_ctl_free_one(kcontrol);
899 ret = snd_ctl_add(card, kcontrol);
902 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
908 ret = dapm_kcontrol_add_widget(kcontrol, w);
910 w->kcontrols[kci] = kcontrol;
918 /* create new dapm mixer control */
919 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
922 struct snd_soc_dapm_path *path;
923 struct dapm_kcontrol_data *data;
926 for (i = 0; i < w->num_kcontrols; i++) {
928 snd_soc_dapm_widget_for_each_source_path(w, path) {
929 /* mixer/mux paths name must match control name */
930 if (path->name != (char *)w->kcontrol_news[i].name)
933 if (!w->kcontrols[i]) {
934 ret = dapm_create_or_share_kcontrol(w, i);
939 dapm_kcontrol_add_path(w->kcontrols[i], path);
941 data = snd_kcontrol_chip(w->kcontrols[i]);
943 snd_soc_dapm_add_path(data->widget->dapm,
953 /* create new dapm mux control */
954 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
956 struct snd_soc_dapm_context *dapm = w->dapm;
957 enum snd_soc_dapm_direction dir;
958 struct snd_soc_dapm_path *path;
963 case snd_soc_dapm_mux:
964 dir = SND_SOC_DAPM_DIR_OUT;
967 case snd_soc_dapm_demux:
968 dir = SND_SOC_DAPM_DIR_IN;
975 if (w->num_kcontrols != 1) {
977 "ASoC: %s %s has incorrect number of controls\n", type,
982 if (list_empty(&w->edges[dir])) {
983 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
987 ret = dapm_create_or_share_kcontrol(w, 0);
991 snd_soc_dapm_widget_for_each_path(w, dir, path) {
993 dapm_kcontrol_add_path(w->kcontrols[0], path);
999 /* create new dapm volume control */
1000 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1004 for (i = 0; i < w->num_kcontrols; i++) {
1005 ret = dapm_create_or_share_kcontrol(w, i);
1013 /* create new dapm dai link control */
1014 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1017 struct snd_kcontrol *kcontrol;
1018 struct snd_soc_dapm_context *dapm = w->dapm;
1019 struct snd_card *card = dapm->card->snd_card;
1020 struct snd_soc_pcm_runtime *rtd = w->priv;
1022 /* create control for links with > 1 config */
1023 if (rtd->dai_link->num_params <= 1)
1027 for (i = 0; i < w->num_kcontrols; i++) {
1028 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1030 ret = snd_ctl_add(card, kcontrol);
1033 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1034 w->name, w->kcontrol_news[i].name, ret);
1037 kcontrol->private_data = w;
1038 w->kcontrols[i] = kcontrol;
1044 /* We implement power down on suspend by checking the power state of
1045 * the ALSA card - when we are suspending the ALSA state for the card
1048 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1050 int level = snd_power_get_state(widget->dapm->card->snd_card);
1053 case SNDRV_CTL_POWER_D3hot:
1054 case SNDRV_CTL_POWER_D3cold:
1055 if (widget->ignore_suspend)
1056 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1058 return widget->ignore_suspend;
1064 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1065 struct list_head *widgets)
1067 struct snd_soc_dapm_widget *w;
1068 struct list_head *it;
1069 unsigned int size = 0;
1072 list_for_each(it, widgets)
1075 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1079 list_for_each_entry(w, widgets, work_list)
1080 (*list)->widgets[i++] = w;
1082 (*list)->num_widgets = i;
1088 * Common implementation for is_connected_output_ep() and
1089 * is_connected_input_ep(). The function is inlined since the combined size of
1090 * the two specialized functions is only marginally larger then the size of the
1091 * generic function and at the same time the fast path of the specialized
1092 * functions is significantly smaller than the generic function.
1094 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1095 struct list_head *list, enum snd_soc_dapm_direction dir,
1096 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1097 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1098 enum snd_soc_dapm_direction)),
1099 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1100 enum snd_soc_dapm_direction))
1102 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1103 struct snd_soc_dapm_path *path;
1106 if (widget->endpoints[dir] >= 0)
1107 return widget->endpoints[dir];
1109 DAPM_UPDATE_STAT(widget, path_checks);
1111 /* do we need to add this widget to the list ? */
1113 list_add_tail(&widget->work_list, list);
1115 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1116 widget->endpoints[dir] = 1;
1117 return widget->endpoints[dir];
1120 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1121 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1122 return widget->endpoints[dir];
1125 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1126 DAPM_UPDATE_STAT(widget, neighbour_checks);
1128 if (path->weak || path->is_supply)
1134 trace_snd_soc_dapm_path(widget, dir, path);
1136 if (path->connect) {
1138 con += fn(path->node[dir], list, custom_stop_condition);
1143 widget->endpoints[dir] = con;
1149 * Recursively check for a completed path to an active or physically connected
1150 * output widget. Returns number of complete paths.
1152 * Optionally, can be supplied with a function acting as a stopping condition.
1153 * This function takes the dapm widget currently being examined and the walk
1154 * direction as an arguments, it should return true if the walk should be
1155 * stopped and false otherwise.
1157 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1158 struct list_head *list,
1159 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1160 enum snd_soc_dapm_direction))
1162 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1163 is_connected_output_ep, custom_stop_condition);
1167 * Recursively check for a completed path to an active or physically connected
1168 * input widget. Returns number of complete paths.
1170 * Optionally, can be supplied with a function acting as a stopping condition.
1171 * This function takes the dapm widget currently being examined and the walk
1172 * direction as an arguments, it should return true if the walk should be
1173 * stopped and false otherwise.
1175 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1176 struct list_head *list,
1177 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1178 enum snd_soc_dapm_direction))
1180 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1181 is_connected_input_ep, custom_stop_condition);
1185 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1186 * @dai: the soc DAI.
1187 * @stream: stream direction.
1188 * @list: list of active widgets for this stream.
1189 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1190 * walk based on custom logic.
1192 * Queries DAPM graph as to whether a valid audio stream path exists for
1193 * the initial stream specified by name. This takes into account
1194 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1196 * Optionally, can be supplied with a function acting as a stopping condition.
1197 * This function takes the dapm widget currently being examined and the walk
1198 * direction as an arguments, it should return true if the walk should be
1199 * stopped and false otherwise.
1201 * Returns the number of valid paths or negative error.
1203 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1204 struct snd_soc_dapm_widget_list **list,
1205 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1206 enum snd_soc_dapm_direction))
1208 struct snd_soc_card *card = dai->component->card;
1209 struct snd_soc_dapm_widget *w;
1214 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1217 * For is_connected_{output,input}_ep fully discover the graph we need
1218 * to reset the cached number of inputs and outputs.
1220 list_for_each_entry(w, &card->widgets, list) {
1221 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1222 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1225 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1226 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1227 custom_stop_condition);
1229 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1230 custom_stop_condition);
1232 /* Drop starting point */
1233 list_del(widgets.next);
1235 ret = dapm_widget_list_create(list, &widgets);
1239 trace_snd_soc_dapm_connected(paths, stream);
1240 mutex_unlock(&card->dapm_mutex);
1246 * Handler for regulator supply widget.
1248 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1249 struct snd_kcontrol *kcontrol, int event)
1253 soc_dapm_async_complete(w->dapm);
1255 if (SND_SOC_DAPM_EVENT_ON(event)) {
1256 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1257 ret = regulator_allow_bypass(w->regulator, false);
1259 dev_warn(w->dapm->dev,
1260 "ASoC: Failed to unbypass %s: %d\n",
1264 return regulator_enable(w->regulator);
1266 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1267 ret = regulator_allow_bypass(w->regulator, true);
1269 dev_warn(w->dapm->dev,
1270 "ASoC: Failed to bypass %s: %d\n",
1274 return regulator_disable_deferred(w->regulator, w->shift);
1277 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1280 * Handler for pinctrl widget.
1282 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1283 struct snd_kcontrol *kcontrol, int event)
1285 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1286 struct pinctrl *p = w->pinctrl;
1287 struct pinctrl_state *s;
1292 if (SND_SOC_DAPM_EVENT_ON(event))
1293 s = pinctrl_lookup_state(p, priv->active_state);
1295 s = pinctrl_lookup_state(p, priv->sleep_state);
1300 return pinctrl_select_state(p, s);
1302 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1305 * Handler for clock supply widget.
1307 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1308 struct snd_kcontrol *kcontrol, int event)
1313 soc_dapm_async_complete(w->dapm);
1315 if (SND_SOC_DAPM_EVENT_ON(event)) {
1316 return clk_prepare_enable(w->clk);
1318 clk_disable_unprepare(w->clk);
1324 EXPORT_SYMBOL_GPL(dapm_clock_event);
1326 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1328 if (w->power_checked)
1329 return w->new_power;
1334 w->new_power = w->power_check(w);
1336 w->power_checked = true;
1338 return w->new_power;
1341 /* Generic check to see if a widget should be powered. */
1342 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1346 DAPM_UPDATE_STAT(w, power_checks);
1348 in = is_connected_input_ep(w, NULL, NULL);
1349 out = is_connected_output_ep(w, NULL, NULL);
1350 return out != 0 && in != 0;
1353 /* Check to see if a power supply is needed */
1354 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1356 struct snd_soc_dapm_path *path;
1358 DAPM_UPDATE_STAT(w, power_checks);
1360 /* Check if one of our outputs is connected */
1361 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1362 DAPM_UPDATE_STAT(w, neighbour_checks);
1367 if (path->connected &&
1368 !path->connected(path->source, path->sink))
1371 if (dapm_widget_power_check(path->sink))
1378 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1380 return w->connected;
1383 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1384 struct snd_soc_dapm_widget *b,
1392 sort = dapm_down_seq;
1394 if (sort[a->id] != sort[b->id])
1395 return sort[a->id] - sort[b->id];
1396 if (a->subseq != b->subseq) {
1398 return a->subseq - b->subseq;
1400 return b->subseq - a->subseq;
1402 if (a->reg != b->reg)
1403 return a->reg - b->reg;
1404 if (a->dapm != b->dapm)
1405 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1410 /* Insert a widget in order into a DAPM power sequence. */
1411 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1412 struct list_head *list,
1415 struct snd_soc_dapm_widget *w;
1417 list_for_each_entry(w, list, power_list)
1418 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1419 list_add_tail(&new_widget->power_list, &w->power_list);
1423 list_add_tail(&new_widget->power_list, list);
1426 static void dapm_seq_check_event(struct snd_soc_card *card,
1427 struct snd_soc_dapm_widget *w, int event)
1429 const char *ev_name;
1433 case SND_SOC_DAPM_PRE_PMU:
1434 ev_name = "PRE_PMU";
1437 case SND_SOC_DAPM_POST_PMU:
1438 ev_name = "POST_PMU";
1441 case SND_SOC_DAPM_PRE_PMD:
1442 ev_name = "PRE_PMD";
1445 case SND_SOC_DAPM_POST_PMD:
1446 ev_name = "POST_PMD";
1449 case SND_SOC_DAPM_WILL_PMU:
1450 ev_name = "WILL_PMU";
1453 case SND_SOC_DAPM_WILL_PMD:
1454 ev_name = "WILL_PMD";
1458 WARN(1, "Unknown event %d\n", event);
1462 if (w->new_power != power)
1465 if (w->event && (w->event_flags & event)) {
1466 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1468 soc_dapm_async_complete(w->dapm);
1469 trace_snd_soc_dapm_widget_event_start(w, event);
1470 ret = w->event(w, NULL, event);
1471 trace_snd_soc_dapm_widget_event_done(w, event);
1473 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1474 ev_name, w->name, ret);
1478 /* Apply the coalesced changes from a DAPM sequence */
1479 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1480 struct list_head *pending)
1482 struct snd_soc_dapm_context *dapm;
1483 struct snd_soc_dapm_widget *w;
1485 unsigned int value = 0;
1486 unsigned int mask = 0;
1488 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1492 list_for_each_entry(w, pending, power_list) {
1493 WARN_ON(reg != w->reg || dapm != w->dapm);
1494 w->power = w->new_power;
1496 mask |= w->mask << w->shift;
1498 value |= w->on_val << w->shift;
1500 value |= w->off_val << w->shift;
1502 pop_dbg(dapm->dev, card->pop_time,
1503 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1504 w->name, reg, value, mask);
1506 /* Check for events */
1507 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1508 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1512 /* Any widget will do, they should all be updating the
1516 pop_dbg(dapm->dev, card->pop_time,
1517 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1518 value, mask, reg, card->pop_time);
1519 pop_wait(card->pop_time);
1520 soc_dapm_update_bits(dapm, reg, mask, value);
1523 list_for_each_entry(w, pending, power_list) {
1524 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1525 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1529 /* Apply a DAPM power sequence.
1531 * We walk over a pre-sorted list of widgets to apply power to. In
1532 * order to minimise the number of writes to the device required
1533 * multiple widgets will be updated in a single write where possible.
1534 * Currently anything that requires more than a single write is not
1537 static void dapm_seq_run(struct snd_soc_card *card,
1538 struct list_head *list, int event, bool power_up)
1540 struct snd_soc_dapm_widget *w, *n;
1541 struct snd_soc_dapm_context *d;
1544 int cur_subseq = -1;
1545 int cur_reg = SND_SOC_NOPM;
1546 struct snd_soc_dapm_context *cur_dapm = NULL;
1553 sort = dapm_down_seq;
1555 list_for_each_entry_safe(w, n, list, power_list) {
1558 /* Do we need to apply any queued changes? */
1559 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1560 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1561 if (!list_empty(&pending))
1562 dapm_seq_run_coalesced(card, &pending);
1564 if (cur_dapm && cur_dapm->seq_notifier) {
1565 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1566 if (sort[i] == cur_sort)
1567 cur_dapm->seq_notifier(cur_dapm,
1572 if (cur_dapm && w->dapm != cur_dapm)
1573 soc_dapm_async_complete(cur_dapm);
1575 INIT_LIST_HEAD(&pending);
1577 cur_subseq = INT_MIN;
1578 cur_reg = SND_SOC_NOPM;
1583 case snd_soc_dapm_pre:
1585 list_for_each_entry_safe_continue(w, n, list,
1588 if (event == SND_SOC_DAPM_STREAM_START)
1590 NULL, SND_SOC_DAPM_PRE_PMU);
1591 else if (event == SND_SOC_DAPM_STREAM_STOP)
1593 NULL, SND_SOC_DAPM_PRE_PMD);
1596 case snd_soc_dapm_post:
1598 list_for_each_entry_safe_continue(w, n, list,
1601 if (event == SND_SOC_DAPM_STREAM_START)
1603 NULL, SND_SOC_DAPM_POST_PMU);
1604 else if (event == SND_SOC_DAPM_STREAM_STOP)
1606 NULL, SND_SOC_DAPM_POST_PMD);
1610 /* Queue it up for application */
1611 cur_sort = sort[w->id];
1612 cur_subseq = w->subseq;
1615 list_move(&w->power_list, &pending);
1620 dev_err(w->dapm->dev,
1621 "ASoC: Failed to apply widget power: %d\n", ret);
1624 if (!list_empty(&pending))
1625 dapm_seq_run_coalesced(card, &pending);
1627 if (cur_dapm && cur_dapm->seq_notifier) {
1628 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1629 if (sort[i] == cur_sort)
1630 cur_dapm->seq_notifier(cur_dapm,
1634 list_for_each_entry(d, &card->dapm_list, list) {
1635 soc_dapm_async_complete(d);
1639 static void dapm_widget_update(struct snd_soc_card *card)
1641 struct snd_soc_dapm_update *update = card->update;
1642 struct snd_soc_dapm_widget_list *wlist;
1643 struct snd_soc_dapm_widget *w = NULL;
1647 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1650 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1652 for (wi = 0; wi < wlist->num_widgets; wi++) {
1653 w = wlist->widgets[wi];
1655 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1656 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1658 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1666 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1669 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1672 if (update->has_second_set) {
1673 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1674 update->mask2, update->val2);
1676 dev_err(w->dapm->dev,
1677 "ASoC: %s DAPM update failed: %d\n",
1681 for (wi = 0; wi < wlist->num_widgets; wi++) {
1682 w = wlist->widgets[wi];
1684 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1685 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1687 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1693 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1694 * they're changing state.
1696 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1698 struct snd_soc_dapm_context *d = data;
1701 /* If we're off and we're not supposed to go into STANDBY */
1702 if (d->bias_level == SND_SOC_BIAS_OFF &&
1703 d->target_bias_level != SND_SOC_BIAS_OFF) {
1705 pm_runtime_get_sync(d->dev);
1707 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1710 "ASoC: Failed to turn on bias: %d\n", ret);
1713 /* Prepare for a transition to ON or away from ON */
1714 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1715 d->bias_level != SND_SOC_BIAS_ON) ||
1716 (d->target_bias_level != SND_SOC_BIAS_ON &&
1717 d->bias_level == SND_SOC_BIAS_ON)) {
1718 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1721 "ASoC: Failed to prepare bias: %d\n", ret);
1725 /* Async callback run prior to DAPM sequences - brings to their final
1728 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1730 struct snd_soc_dapm_context *d = data;
1733 /* If we just powered the last thing off drop to standby bias */
1734 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1735 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1736 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1737 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1739 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1743 /* If we're in standby and can support bias off then do that */
1744 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1745 d->target_bias_level == SND_SOC_BIAS_OFF) {
1746 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1748 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1752 pm_runtime_put(d->dev);
1755 /* If we just powered up then move to active bias */
1756 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1757 d->target_bias_level == SND_SOC_BIAS_ON) {
1758 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1760 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1765 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1766 bool power, bool connect)
1768 /* If a connection is being made or broken then that update
1769 * will have marked the peer dirty, otherwise the widgets are
1770 * not connected and this update has no impact. */
1774 /* If the peer is already in the state we're moving to then we
1775 * won't have an impact on it. */
1776 if (power != peer->power)
1777 dapm_mark_dirty(peer, "peer state change");
1780 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1781 struct list_head *up_list,
1782 struct list_head *down_list)
1784 struct snd_soc_dapm_path *path;
1786 if (w->power == power)
1789 trace_snd_soc_dapm_widget_power(w, power);
1791 /* If we changed our power state perhaps our neigbours changed
1794 snd_soc_dapm_widget_for_each_source_path(w, path)
1795 dapm_widget_set_peer_power(path->source, power, path->connect);
1797 /* Supplies can't affect their outputs, only their inputs */
1798 if (!w->is_supply) {
1799 snd_soc_dapm_widget_for_each_sink_path(w, path)
1800 dapm_widget_set_peer_power(path->sink, power,
1805 dapm_seq_insert(w, up_list, true);
1807 dapm_seq_insert(w, down_list, false);
1810 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1811 struct list_head *up_list,
1812 struct list_head *down_list)
1817 case snd_soc_dapm_pre:
1818 dapm_seq_insert(w, down_list, false);
1820 case snd_soc_dapm_post:
1821 dapm_seq_insert(w, up_list, true);
1825 power = dapm_widget_power_check(w);
1827 dapm_widget_set_power(w, power, up_list, down_list);
1832 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1834 if (dapm->idle_bias_off)
1837 switch (snd_power_get_state(dapm->card->snd_card)) {
1838 case SNDRV_CTL_POWER_D3hot:
1839 case SNDRV_CTL_POWER_D3cold:
1840 return dapm->suspend_bias_off;
1849 * Scan each dapm widget for complete audio path.
1850 * A complete path is a route that has valid endpoints i.e.:-
1852 * o DAC to output pin.
1853 * o Input pin to ADC.
1854 * o Input pin to Output pin (bypass, sidetone)
1855 * o DAC to ADC (loopback).
1857 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1859 struct snd_soc_dapm_widget *w;
1860 struct snd_soc_dapm_context *d;
1862 LIST_HEAD(down_list);
1863 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1864 enum snd_soc_bias_level bias;
1866 lockdep_assert_held(&card->dapm_mutex);
1868 trace_snd_soc_dapm_start(card);
1870 list_for_each_entry(d, &card->dapm_list, list) {
1871 if (dapm_idle_bias_off(d))
1872 d->target_bias_level = SND_SOC_BIAS_OFF;
1874 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1879 /* Check which widgets we need to power and store them in
1880 * lists indicating if they should be powered up or down. We
1881 * only check widgets that have been flagged as dirty but note
1882 * that new widgets may be added to the dirty list while we
1885 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1886 dapm_power_one_widget(w, &up_list, &down_list);
1889 list_for_each_entry(w, &card->widgets, list) {
1891 case snd_soc_dapm_pre:
1892 case snd_soc_dapm_post:
1893 /* These widgets always need to be powered */
1896 list_del_init(&w->dirty);
1903 /* Supplies and micbiases only bring the
1904 * context up to STANDBY as unless something
1905 * else is active and passing audio they
1906 * generally don't require full power. Signal
1907 * generators are virtual pins and have no
1908 * power impact themselves.
1911 case snd_soc_dapm_siggen:
1912 case snd_soc_dapm_vmid:
1914 case snd_soc_dapm_supply:
1915 case snd_soc_dapm_regulator_supply:
1916 case snd_soc_dapm_pinctrl:
1917 case snd_soc_dapm_clock_supply:
1918 case snd_soc_dapm_micbias:
1919 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1920 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1923 d->target_bias_level = SND_SOC_BIAS_ON;
1930 /* Force all contexts in the card to the same bias state if
1931 * they're not ground referenced.
1933 bias = SND_SOC_BIAS_OFF;
1934 list_for_each_entry(d, &card->dapm_list, list)
1935 if (d->target_bias_level > bias)
1936 bias = d->target_bias_level;
1937 list_for_each_entry(d, &card->dapm_list, list)
1938 if (!dapm_idle_bias_off(d))
1939 d->target_bias_level = bias;
1941 trace_snd_soc_dapm_walk_done(card);
1943 /* Run card bias changes at first */
1944 dapm_pre_sequence_async(&card->dapm, 0);
1945 /* Run other bias changes in parallel */
1946 list_for_each_entry(d, &card->dapm_list, list) {
1947 if (d != &card->dapm && d->bias_level != d->target_bias_level)
1948 async_schedule_domain(dapm_pre_sequence_async, d,
1951 async_synchronize_full_domain(&async_domain);
1953 list_for_each_entry(w, &down_list, power_list) {
1954 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1957 list_for_each_entry(w, &up_list, power_list) {
1958 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1961 /* Power down widgets first; try to avoid amplifying pops. */
1962 dapm_seq_run(card, &down_list, event, false);
1964 dapm_widget_update(card);
1967 dapm_seq_run(card, &up_list, event, true);
1969 /* Run all the bias changes in parallel */
1970 list_for_each_entry(d, &card->dapm_list, list) {
1971 if (d != &card->dapm && d->bias_level != d->target_bias_level)
1972 async_schedule_domain(dapm_post_sequence_async, d,
1975 async_synchronize_full_domain(&async_domain);
1976 /* Run card bias changes at last */
1977 dapm_post_sequence_async(&card->dapm, 0);
1979 /* do we need to notify any clients that DAPM event is complete */
1980 list_for_each_entry(d, &card->dapm_list, list) {
1981 if (d->stream_event)
1982 d->stream_event(d, event);
1985 pop_dbg(card->dev, card->pop_time,
1986 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1987 pop_wait(card->pop_time);
1989 trace_snd_soc_dapm_done(card);
1994 #ifdef CONFIG_DEBUG_FS
1995 static ssize_t dapm_widget_power_read_file(struct file *file,
1996 char __user *user_buf,
1997 size_t count, loff_t *ppos)
1999 struct snd_soc_dapm_widget *w = file->private_data;
2000 struct snd_soc_card *card = w->dapm->card;
2001 enum snd_soc_dapm_direction dir, rdir;
2005 struct snd_soc_dapm_path *p = NULL;
2007 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2011 mutex_lock(&card->dapm_mutex);
2013 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2018 in = is_connected_input_ep(w, NULL, NULL);
2019 out = is_connected_output_ep(w, NULL, NULL);
2022 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2023 w->name, w->power ? "On" : "Off",
2024 w->force ? " (forced)" : "", in, out);
2027 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2028 " - R%d(0x%x) mask 0x%x",
2029 w->reg, w->reg, w->mask << w->shift);
2031 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
2034 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2036 w->active ? "active" : "inactive");
2038 snd_soc_dapm_for_each_direction(dir) {
2039 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2040 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2041 if (p->connected && !p->connected(p->source, p->sink))
2047 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2048 " %s \"%s\" \"%s\"\n",
2049 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2050 p->name ? p->name : "static",
2051 p->node[rdir]->name);
2055 mutex_unlock(&card->dapm_mutex);
2057 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2063 static const struct file_operations dapm_widget_power_fops = {
2064 .open = simple_open,
2065 .read = dapm_widget_power_read_file,
2066 .llseek = default_llseek,
2069 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2070 size_t count, loff_t *ppos)
2072 struct snd_soc_dapm_context *dapm = file->private_data;
2075 switch (dapm->bias_level) {
2076 case SND_SOC_BIAS_ON:
2079 case SND_SOC_BIAS_PREPARE:
2080 level = "Prepare\n";
2082 case SND_SOC_BIAS_STANDBY:
2083 level = "Standby\n";
2085 case SND_SOC_BIAS_OFF:
2089 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2090 level = "Unknown\n";
2094 return simple_read_from_buffer(user_buf, count, ppos, level,
2098 static const struct file_operations dapm_bias_fops = {
2099 .open = simple_open,
2100 .read = dapm_bias_read_file,
2101 .llseek = default_llseek,
2104 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2105 struct dentry *parent)
2112 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2114 if (!dapm->debugfs_dapm) {
2116 "ASoC: Failed to create DAPM debugfs directory\n");
2120 d = debugfs_create_file("bias_level", 0444,
2121 dapm->debugfs_dapm, dapm,
2125 "ASoC: Failed to create bias level debugfs file\n");
2128 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2130 struct snd_soc_dapm_context *dapm = w->dapm;
2133 if (!dapm->debugfs_dapm || !w->name)
2136 d = debugfs_create_file(w->name, 0444,
2137 dapm->debugfs_dapm, w,
2138 &dapm_widget_power_fops);
2140 dev_warn(w->dapm->dev,
2141 "ASoC: Failed to create %s debugfs file\n",
2145 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2147 debugfs_remove_recursive(dapm->debugfs_dapm);
2151 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2152 struct dentry *parent)
2156 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2160 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2167 * soc_dapm_connect_path() - Connects or disconnects a path
2168 * @path: The path to update
2169 * @connect: The new connect state of the path. True if the path is connected,
2170 * false if it is disconnected.
2171 * @reason: The reason why the path changed (for debugging only)
2173 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2174 bool connect, const char *reason)
2176 if (path->connect == connect)
2179 path->connect = connect;
2180 dapm_mark_dirty(path->source, reason);
2181 dapm_mark_dirty(path->sink, reason);
2182 dapm_path_invalidate(path);
2185 /* test and update the power status of a mux widget */
2186 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2187 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2189 struct snd_soc_dapm_path *path;
2193 lockdep_assert_held(&card->dapm_mutex);
2195 /* find dapm widget path assoc with kcontrol */
2196 dapm_kcontrol_for_each_path(path, kcontrol) {
2198 /* we now need to match the string in the enum to the path */
2199 if (!(strcmp(path->name, e->texts[mux])))
2204 soc_dapm_connect_path(path, connect, "mux update");
2208 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2213 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2214 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2215 struct snd_soc_dapm_update *update)
2217 struct snd_soc_card *card = dapm->card;
2220 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2221 card->update = update;
2222 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2223 card->update = NULL;
2224 mutex_unlock(&card->dapm_mutex);
2226 soc_dpcm_runtime_update(card);
2229 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2231 /* test and update the power status of a mixer or switch widget */
2232 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2233 struct snd_kcontrol *kcontrol,
2234 int connect, int rconnect)
2236 struct snd_soc_dapm_path *path;
2239 lockdep_assert_held(&card->dapm_mutex);
2241 /* find dapm widget path assoc with kcontrol */
2242 dapm_kcontrol_for_each_path(path, kcontrol) {
2244 * Ideally this function should support any number of
2245 * paths and channels. But since kcontrols only come
2246 * in mono and stereo variants, we are limited to 2
2249 * The following code assumes for stereo controls the
2250 * first path (when 'found == 0') is the left channel,
2251 * and all remaining paths (when 'found == 1') are the
2254 * A stereo control is signified by a valid 'rconnect'
2255 * value, either 0 for unconnected, or >= 0 for connected.
2256 * This is chosen instead of using snd_soc_volsw_is_stereo,
2257 * so that the behavior of snd_soc_dapm_mixer_update_power
2258 * doesn't change even when the kcontrol passed in is
2261 * It passes 'connect' as the path connect status for
2262 * the left channel, and 'rconnect' for the right
2265 if (found && rconnect >= 0)
2266 soc_dapm_connect_path(path, rconnect, "mixer update");
2268 soc_dapm_connect_path(path, connect, "mixer update");
2273 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2278 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2279 struct snd_kcontrol *kcontrol, int connect,
2280 struct snd_soc_dapm_update *update)
2282 struct snd_soc_card *card = dapm->card;
2285 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2286 card->update = update;
2287 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2288 card->update = NULL;
2289 mutex_unlock(&card->dapm_mutex);
2291 soc_dpcm_runtime_update(card);
2294 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2296 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2299 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2300 struct snd_soc_dapm_widget *w;
2302 char *state = "not set";
2304 /* card won't be set for the dummy component, as a spot fix
2305 * we're checking for that case specifically here but in future
2306 * we will ensure that the dummy component looks like others.
2311 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2312 if (w->dapm != dapm)
2315 /* only display widgets that burn power */
2317 case snd_soc_dapm_hp:
2318 case snd_soc_dapm_mic:
2319 case snd_soc_dapm_spk:
2320 case snd_soc_dapm_line:
2321 case snd_soc_dapm_micbias:
2322 case snd_soc_dapm_dac:
2323 case snd_soc_dapm_adc:
2324 case snd_soc_dapm_pga:
2325 case snd_soc_dapm_out_drv:
2326 case snd_soc_dapm_mixer:
2327 case snd_soc_dapm_mixer_named_ctl:
2328 case snd_soc_dapm_supply:
2329 case snd_soc_dapm_regulator_supply:
2330 case snd_soc_dapm_pinctrl:
2331 case snd_soc_dapm_clock_supply:
2333 count += sprintf(buf + count, "%s: %s\n",
2334 w->name, w->power ? "On":"Off");
2341 switch (snd_soc_dapm_get_bias_level(dapm)) {
2342 case SND_SOC_BIAS_ON:
2345 case SND_SOC_BIAS_PREPARE:
2348 case SND_SOC_BIAS_STANDBY:
2351 case SND_SOC_BIAS_OFF:
2355 count += sprintf(buf + count, "PM State: %s\n", state);
2360 /* show dapm widget status in sys fs */
2361 static ssize_t dapm_widget_show(struct device *dev,
2362 struct device_attribute *attr, char *buf)
2364 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2365 struct snd_soc_dai *codec_dai;
2368 mutex_lock(&rtd->card->dapm_mutex);
2370 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2371 struct snd_soc_component *cmpnt = codec_dai->component;
2373 count += dapm_widget_show_component(cmpnt, buf + count);
2376 mutex_unlock(&rtd->card->dapm_mutex);
2381 static DEVICE_ATTR_RO(dapm_widget);
2383 struct attribute *soc_dapm_dev_attrs[] = {
2384 &dev_attr_dapm_widget.attr,
2388 static void dapm_free_path(struct snd_soc_dapm_path *path)
2390 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2391 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2392 list_del(&path->list_kcontrol);
2393 list_del(&path->list);
2397 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2399 struct snd_soc_dapm_path *p, *next_p;
2400 enum snd_soc_dapm_direction dir;
2404 * remove source and sink paths associated to this widget.
2405 * While removing the path, remove reference to it from both
2406 * source and sink widgets so that path is removed only once.
2408 snd_soc_dapm_for_each_direction(dir) {
2409 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2413 kfree(w->kcontrols);
2414 kfree_const(w->name);
2418 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2420 dapm->path_sink_cache.widget = NULL;
2421 dapm->path_source_cache.widget = NULL;
2424 /* free all dapm widgets and resources */
2425 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2427 struct snd_soc_dapm_widget *w, *next_w;
2429 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2430 if (w->dapm != dapm)
2432 snd_soc_dapm_free_widget(w);
2434 snd_soc_dapm_reset_cache(dapm);
2437 static struct snd_soc_dapm_widget *dapm_find_widget(
2438 struct snd_soc_dapm_context *dapm, const char *pin,
2439 bool search_other_contexts)
2441 struct snd_soc_dapm_widget *w;
2442 struct snd_soc_dapm_widget *fallback = NULL;
2444 list_for_each_entry(w, &dapm->card->widgets, list) {
2445 if (!strcmp(w->name, pin)) {
2446 if (w->dapm == dapm)
2453 if (search_other_contexts)
2459 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2460 const char *pin, int status)
2462 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2464 dapm_assert_locked(dapm);
2467 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2471 if (w->connected != status) {
2472 dapm_mark_dirty(w, "pin configuration");
2473 dapm_widget_invalidate_input_paths(w);
2474 dapm_widget_invalidate_output_paths(w);
2477 w->connected = status;
2485 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2486 * @dapm: DAPM context
2488 * Walks all dapm audio paths and powers widgets according to their
2489 * stream or path usage.
2491 * Requires external locking.
2493 * Returns 0 for success.
2495 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2498 * Suppress early reports (eg, jacks syncing their state) to avoid
2499 * silly DAPM runs during card startup.
2501 if (!dapm->card || !dapm->card->instantiated)
2504 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2506 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2509 * snd_soc_dapm_sync - scan and power dapm paths
2510 * @dapm: DAPM context
2512 * Walks all dapm audio paths and powers widgets according to their
2513 * stream or path usage.
2515 * Returns 0 for success.
2517 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2521 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2522 ret = snd_soc_dapm_sync_unlocked(dapm);
2523 mutex_unlock(&dapm->card->dapm_mutex);
2526 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2529 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2530 * @w: The widget for which to update the flags
2532 * Some widgets have a dynamic category which depends on which neighbors they
2533 * are connected to. This function update the category for these widgets.
2535 * This function must be called whenever a path is added or removed to a widget.
2537 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2539 enum snd_soc_dapm_direction dir;
2540 struct snd_soc_dapm_path *p;
2544 case snd_soc_dapm_input:
2545 /* On a fully routed card an input is never a source */
2546 if (w->dapm->card->fully_routed)
2548 ep = SND_SOC_DAPM_EP_SOURCE;
2549 snd_soc_dapm_widget_for_each_source_path(w, p) {
2550 if (p->source->id == snd_soc_dapm_micbias ||
2551 p->source->id == snd_soc_dapm_mic ||
2552 p->source->id == snd_soc_dapm_line ||
2553 p->source->id == snd_soc_dapm_output) {
2559 case snd_soc_dapm_output:
2560 /* On a fully routed card a output is never a sink */
2561 if (w->dapm->card->fully_routed)
2563 ep = SND_SOC_DAPM_EP_SINK;
2564 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2565 if (p->sink->id == snd_soc_dapm_spk ||
2566 p->sink->id == snd_soc_dapm_hp ||
2567 p->sink->id == snd_soc_dapm_line ||
2568 p->sink->id == snd_soc_dapm_input) {
2574 case snd_soc_dapm_line:
2576 snd_soc_dapm_for_each_direction(dir) {
2577 if (!list_empty(&w->edges[dir]))
2578 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2588 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2589 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2590 const char *control)
2592 bool dynamic_source = false;
2593 bool dynamic_sink = false;
2598 switch (source->id) {
2599 case snd_soc_dapm_demux:
2600 dynamic_source = true;
2607 case snd_soc_dapm_mux:
2608 case snd_soc_dapm_switch:
2609 case snd_soc_dapm_mixer:
2610 case snd_soc_dapm_mixer_named_ctl:
2611 dynamic_sink = true;
2617 if (dynamic_source && dynamic_sink) {
2619 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2620 source->name, control, sink->name);
2622 } else if (!dynamic_source && !dynamic_sink) {
2624 "Control not supported for path %s -> [%s] -> %s\n",
2625 source->name, control, sink->name);
2632 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2633 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2634 const char *control,
2635 int (*connected)(struct snd_soc_dapm_widget *source,
2636 struct snd_soc_dapm_widget *sink))
2638 struct snd_soc_dapm_widget *widgets[2];
2639 enum snd_soc_dapm_direction dir;
2640 struct snd_soc_dapm_path *path;
2643 if (wsink->is_supply && !wsource->is_supply) {
2645 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2646 wsource->name, wsink->name);
2650 if (connected && !wsource->is_supply) {
2652 "connected() callback only supported for supply widgets (%s -> %s)\n",
2653 wsource->name, wsink->name);
2657 if (wsource->is_supply && control) {
2659 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2660 wsource->name, control, wsink->name);
2664 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2668 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2672 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2673 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2674 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2675 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2677 path->connected = connected;
2678 INIT_LIST_HEAD(&path->list);
2679 INIT_LIST_HEAD(&path->list_kcontrol);
2681 if (wsource->is_supply || wsink->is_supply)
2682 path->is_supply = 1;
2684 /* connect static paths */
2685 if (control == NULL) {
2688 switch (wsource->id) {
2689 case snd_soc_dapm_demux:
2690 ret = dapm_connect_mux(dapm, path, control, wsource);
2698 switch (wsink->id) {
2699 case snd_soc_dapm_mux:
2700 ret = dapm_connect_mux(dapm, path, control, wsink);
2704 case snd_soc_dapm_switch:
2705 case snd_soc_dapm_mixer:
2706 case snd_soc_dapm_mixer_named_ctl:
2707 ret = dapm_connect_mixer(dapm, path, control);
2716 list_add(&path->list, &dapm->card->paths);
2717 snd_soc_dapm_for_each_direction(dir)
2718 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2720 snd_soc_dapm_for_each_direction(dir) {
2721 dapm_update_widget_flags(widgets[dir]);
2722 dapm_mark_dirty(widgets[dir], "Route added");
2725 if (dapm->card->instantiated && path->connect)
2726 dapm_path_invalidate(path);
2734 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2735 const struct snd_soc_dapm_route *route)
2737 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2738 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2741 char prefixed_sink[80];
2742 char prefixed_source[80];
2746 prefix = soc_dapm_prefix(dapm);
2748 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2749 prefix, route->sink);
2750 sink = prefixed_sink;
2751 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2752 prefix, route->source);
2753 source = prefixed_source;
2756 source = route->source;
2759 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2760 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2762 if (wsink && wsource)
2766 * find src and dest widgets over all widgets but favor a widget from
2767 * current DAPM context
2769 list_for_each_entry(w, &dapm->card->widgets, list) {
2770 if (!wsink && !(strcmp(w->name, sink))) {
2772 if (w->dapm == dapm) {
2779 if (!wsource && !(strcmp(w->name, source))) {
2781 if (w->dapm == dapm) {
2788 /* use widget from another DAPM context if not found from this */
2794 if (wsource == NULL) {
2795 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2799 if (wsink == NULL) {
2800 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2806 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2807 dapm_wcache_update(&dapm->path_source_cache, wsource);
2809 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2816 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2817 source, route->control, sink);
2821 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2822 const struct snd_soc_dapm_route *route)
2824 struct snd_soc_dapm_widget *wsource, *wsink;
2825 struct snd_soc_dapm_path *path, *p;
2828 char prefixed_sink[80];
2829 char prefixed_source[80];
2832 if (route->control) {
2834 "ASoC: Removal of routes with controls not supported\n");
2838 prefix = soc_dapm_prefix(dapm);
2840 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2841 prefix, route->sink);
2842 sink = prefixed_sink;
2843 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2844 prefix, route->source);
2845 source = prefixed_source;
2848 source = route->source;
2852 list_for_each_entry(p, &dapm->card->paths, list) {
2853 if (strcmp(p->source->name, source) != 0)
2855 if (strcmp(p->sink->name, sink) != 0)
2862 wsource = path->source;
2865 dapm_mark_dirty(wsource, "Route removed");
2866 dapm_mark_dirty(wsink, "Route removed");
2868 dapm_path_invalidate(path);
2870 dapm_free_path(path);
2872 /* Update any path related flags */
2873 dapm_update_widget_flags(wsource);
2874 dapm_update_widget_flags(wsink);
2876 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2884 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2885 * @dapm: DAPM context
2886 * @route: audio routes
2887 * @num: number of routes
2889 * Connects 2 dapm widgets together via a named audio path. The sink is
2890 * the widget receiving the audio signal, whilst the source is the sender
2891 * of the audio signal.
2893 * Returns 0 for success else error. On error all resources can be freed
2894 * with a call to snd_soc_card_free().
2896 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2897 const struct snd_soc_dapm_route *route, int num)
2901 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2902 for (i = 0; i < num; i++) {
2903 r = snd_soc_dapm_add_route(dapm, route);
2905 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2907 route->control ? route->control : "direct",
2913 mutex_unlock(&dapm->card->dapm_mutex);
2917 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2920 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2921 * @dapm: DAPM context
2922 * @route: audio routes
2923 * @num: number of routes
2925 * Removes routes from the DAPM context.
2927 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2928 const struct snd_soc_dapm_route *route, int num)
2932 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2933 for (i = 0; i < num; i++) {
2934 snd_soc_dapm_del_route(dapm, route);
2937 mutex_unlock(&dapm->card->dapm_mutex);
2941 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2943 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2944 const struct snd_soc_dapm_route *route)
2946 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2949 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2952 struct snd_soc_dapm_path *path;
2956 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2962 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2967 if (route->control || route->connected)
2968 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2969 route->source, route->sink);
2971 snd_soc_dapm_widget_for_each_sink_path(source, path) {
2972 if (path->sink == sink) {
2979 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2980 route->source, route->sink);
2982 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2983 count, route->source, route->sink);
2989 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2990 * @dapm: DAPM context
2991 * @route: audio routes
2992 * @num: number of routes
2994 * Mark existing routes matching those specified in the passed array
2995 * as being weak, meaning that they are ignored for the purpose of
2996 * power decisions. The main intended use case is for sidetone paths
2997 * which couple audio between other independent paths if they are both
2998 * active in order to make the combination work better at the user
2999 * level but which aren't intended to be "used".
3001 * Note that CODEC drivers should not use this as sidetone type paths
3002 * can frequently also be used as bypass paths.
3004 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3005 const struct snd_soc_dapm_route *route, int num)
3010 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3011 for (i = 0; i < num; i++) {
3012 err = snd_soc_dapm_weak_route(dapm, route);
3017 mutex_unlock(&dapm->card->dapm_mutex);
3021 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3024 * snd_soc_dapm_new_widgets - add new dapm widgets
3025 * @card: card to be checked for new dapm widgets
3027 * Checks the codec for any new dapm widgets and creates them if found.
3029 * Returns 0 for success.
3031 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3033 struct snd_soc_dapm_widget *w;
3036 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3038 list_for_each_entry(w, &card->widgets, list)
3043 if (w->num_kcontrols) {
3044 w->kcontrols = kcalloc(w->num_kcontrols,
3045 sizeof(struct snd_kcontrol *),
3047 if (!w->kcontrols) {
3048 mutex_unlock(&card->dapm_mutex);
3054 case snd_soc_dapm_switch:
3055 case snd_soc_dapm_mixer:
3056 case snd_soc_dapm_mixer_named_ctl:
3059 case snd_soc_dapm_mux:
3060 case snd_soc_dapm_demux:
3063 case snd_soc_dapm_pga:
3064 case snd_soc_dapm_out_drv:
3067 case snd_soc_dapm_dai_link:
3068 dapm_new_dai_link(w);
3074 /* Read the initial power state from the device */
3076 soc_dapm_read(w->dapm, w->reg, &val);
3077 val = val >> w->shift;
3079 if (val == w->on_val)
3085 dapm_mark_dirty(w, "new widget");
3086 dapm_debugfs_add_widget(w);
3089 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3090 mutex_unlock(&card->dapm_mutex);
3093 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3096 * snd_soc_dapm_get_volsw - dapm mixer get callback
3097 * @kcontrol: mixer control
3098 * @ucontrol: control element information
3100 * Callback to get the value of a dapm mixer control.
3102 * Returns 0 for success.
3104 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3105 struct snd_ctl_elem_value *ucontrol)
3107 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3108 struct snd_soc_card *card = dapm->card;
3109 struct soc_mixer_control *mc =
3110 (struct soc_mixer_control *)kcontrol->private_value;
3112 unsigned int shift = mc->shift;
3114 unsigned int width = fls(max);
3115 unsigned int mask = (1 << fls(max)) - 1;
3116 unsigned int invert = mc->invert;
3117 unsigned int reg_val, val, rval = 0;
3120 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3121 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3122 ret = soc_dapm_read(dapm, reg, ®_val);
3123 val = (reg_val >> shift) & mask;
3125 if (ret == 0 && reg != mc->rreg)
3126 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3128 if (snd_soc_volsw_is_stereo(mc))
3129 rval = (reg_val >> mc->rshift) & mask;
3131 reg_val = dapm_kcontrol_get_value(kcontrol);
3132 val = reg_val & mask;
3134 if (snd_soc_volsw_is_stereo(mc))
3135 rval = (reg_val >> width) & mask;
3137 mutex_unlock(&card->dapm_mutex);
3143 ucontrol->value.integer.value[0] = max - val;
3145 ucontrol->value.integer.value[0] = val;
3147 if (snd_soc_volsw_is_stereo(mc)) {
3149 ucontrol->value.integer.value[1] = max - rval;
3151 ucontrol->value.integer.value[1] = rval;
3156 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3159 * snd_soc_dapm_put_volsw - dapm mixer set callback
3160 * @kcontrol: mixer control
3161 * @ucontrol: control element information
3163 * Callback to set the value of a dapm mixer control.
3165 * Returns 0 for success.
3167 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3168 struct snd_ctl_elem_value *ucontrol)
3170 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3171 struct snd_soc_card *card = dapm->card;
3172 struct soc_mixer_control *mc =
3173 (struct soc_mixer_control *)kcontrol->private_value;
3175 unsigned int shift = mc->shift;
3177 unsigned int width = fls(max);
3178 unsigned int mask = (1 << width) - 1;
3179 unsigned int invert = mc->invert;
3180 unsigned int val, rval = 0;
3181 int connect, rconnect = -1, change, reg_change = 0;
3182 struct snd_soc_dapm_update update = {};
3185 val = (ucontrol->value.integer.value[0] & mask);
3191 if (snd_soc_volsw_is_stereo(mc)) {
3192 rval = (ucontrol->value.integer.value[1] & mask);
3198 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3200 /* This assumes field width < (bits in unsigned int / 2) */
3201 if (width > sizeof(unsigned int) * 8 / 2)
3203 "ASoC: control %s field width limit exceeded\n",
3205 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3207 if (reg != SND_SOC_NOPM) {
3209 rval = rval << mc->rshift;
3211 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3213 if (snd_soc_volsw_is_stereo(mc))
3214 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3219 if (change || reg_change) {
3221 if (snd_soc_volsw_is_stereo(mc)) {
3222 update.has_second_set = true;
3223 update.reg2 = mc->rreg;
3224 update.mask2 = mask << mc->rshift;
3227 update.kcontrol = kcontrol;
3229 update.mask = mask << shift;
3231 card->update = &update;
3233 change |= reg_change;
3235 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3238 card->update = NULL;
3241 mutex_unlock(&card->dapm_mutex);
3244 soc_dpcm_runtime_update(card);
3248 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3251 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3252 * @kcontrol: mixer control
3253 * @ucontrol: control element information
3255 * Callback to get the value of a dapm enumerated double mixer control.
3257 * Returns 0 for success.
3259 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3260 struct snd_ctl_elem_value *ucontrol)
3262 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3263 struct snd_soc_card *card = dapm->card;
3264 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3265 unsigned int reg_val, val;
3267 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3268 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3269 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3271 mutex_unlock(&card->dapm_mutex);
3275 reg_val = dapm_kcontrol_get_value(kcontrol);
3277 mutex_unlock(&card->dapm_mutex);
3279 val = (reg_val >> e->shift_l) & e->mask;
3280 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3281 if (e->shift_l != e->shift_r) {
3282 val = (reg_val >> e->shift_r) & e->mask;
3283 val = snd_soc_enum_val_to_item(e, val);
3284 ucontrol->value.enumerated.item[1] = val;
3289 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3292 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3293 * @kcontrol: mixer control
3294 * @ucontrol: control element information
3296 * Callback to set the value of a dapm enumerated double mixer control.
3298 * Returns 0 for success.
3300 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3301 struct snd_ctl_elem_value *ucontrol)
3303 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3304 struct snd_soc_card *card = dapm->card;
3305 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3306 unsigned int *item = ucontrol->value.enumerated.item;
3307 unsigned int val, change, reg_change = 0;
3309 struct snd_soc_dapm_update update = {};
3312 if (item[0] >= e->items)
3315 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3316 mask = e->mask << e->shift_l;
3317 if (e->shift_l != e->shift_r) {
3318 if (item[1] > e->items)
3320 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3321 mask |= e->mask << e->shift_r;
3324 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3326 change = dapm_kcontrol_set_value(kcontrol, val);
3328 if (e->reg != SND_SOC_NOPM)
3329 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3331 if (change || reg_change) {
3333 update.kcontrol = kcontrol;
3334 update.reg = e->reg;
3337 card->update = &update;
3339 change |= reg_change;
3341 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3343 card->update = NULL;
3346 mutex_unlock(&card->dapm_mutex);
3349 soc_dpcm_runtime_update(card);
3353 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3356 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3358 * @kcontrol: mixer control
3359 * @uinfo: control element information
3361 * Callback to provide information about a pin switch control.
3363 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3364 struct snd_ctl_elem_info *uinfo)
3366 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3368 uinfo->value.integer.min = 0;
3369 uinfo->value.integer.max = 1;
3373 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3376 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3378 * @kcontrol: mixer control
3381 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3382 struct snd_ctl_elem_value *ucontrol)
3384 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3385 const char *pin = (const char *)kcontrol->private_value;
3387 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3389 ucontrol->value.integer.value[0] =
3390 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3392 mutex_unlock(&card->dapm_mutex);
3396 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3399 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3401 * @kcontrol: mixer control
3404 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3405 struct snd_ctl_elem_value *ucontrol)
3407 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3408 const char *pin = (const char *)kcontrol->private_value;
3410 if (ucontrol->value.integer.value[0])
3411 snd_soc_dapm_enable_pin(&card->dapm, pin);
3413 snd_soc_dapm_disable_pin(&card->dapm, pin);
3415 snd_soc_dapm_sync(&card->dapm);
3418 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3420 struct snd_soc_dapm_widget *
3421 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3422 const struct snd_soc_dapm_widget *widget)
3424 enum snd_soc_dapm_direction dir;
3425 struct snd_soc_dapm_widget *w;
3429 if ((w = dapm_cnew_widget(widget)) == NULL)
3430 return ERR_PTR(-ENOMEM);
3433 case snd_soc_dapm_regulator_supply:
3434 w->regulator = devm_regulator_get(dapm->dev, w->name);
3435 if (IS_ERR(w->regulator)) {
3436 ret = PTR_ERR(w->regulator);
3437 goto request_failed;
3440 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3441 ret = regulator_allow_bypass(w->regulator, true);
3444 "ASoC: Failed to bypass %s: %d\n",
3448 case snd_soc_dapm_pinctrl:
3449 w->pinctrl = devm_pinctrl_get(dapm->dev);
3450 if (IS_ERR(w->pinctrl)) {
3451 ret = PTR_ERR(w->pinctrl);
3452 goto request_failed;
3455 case snd_soc_dapm_clock_supply:
3456 w->clk = devm_clk_get(dapm->dev, w->name);
3457 if (IS_ERR(w->clk)) {
3458 ret = PTR_ERR(w->clk);
3459 goto request_failed;
3466 prefix = soc_dapm_prefix(dapm);
3468 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3470 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3471 if (w->name == NULL) {
3473 return ERR_PTR(-ENOMEM);
3477 case snd_soc_dapm_mic:
3478 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3479 w->power_check = dapm_generic_check_power;
3481 case snd_soc_dapm_input:
3482 if (!dapm->card->fully_routed)
3483 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3484 w->power_check = dapm_generic_check_power;
3486 case snd_soc_dapm_spk:
3487 case snd_soc_dapm_hp:
3488 w->is_ep = SND_SOC_DAPM_EP_SINK;
3489 w->power_check = dapm_generic_check_power;
3491 case snd_soc_dapm_output:
3492 if (!dapm->card->fully_routed)
3493 w->is_ep = SND_SOC_DAPM_EP_SINK;
3494 w->power_check = dapm_generic_check_power;
3496 case snd_soc_dapm_vmid:
3497 case snd_soc_dapm_siggen:
3498 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3499 w->power_check = dapm_always_on_check_power;
3501 case snd_soc_dapm_sink:
3502 w->is_ep = SND_SOC_DAPM_EP_SINK;
3503 w->power_check = dapm_always_on_check_power;
3506 case snd_soc_dapm_mux:
3507 case snd_soc_dapm_demux:
3508 case snd_soc_dapm_switch:
3509 case snd_soc_dapm_mixer:
3510 case snd_soc_dapm_mixer_named_ctl:
3511 case snd_soc_dapm_adc:
3512 case snd_soc_dapm_aif_out:
3513 case snd_soc_dapm_dac:
3514 case snd_soc_dapm_aif_in:
3515 case snd_soc_dapm_pga:
3516 case snd_soc_dapm_out_drv:
3517 case snd_soc_dapm_micbias:
3518 case snd_soc_dapm_line:
3519 case snd_soc_dapm_dai_link:
3520 case snd_soc_dapm_dai_out:
3521 case snd_soc_dapm_dai_in:
3522 w->power_check = dapm_generic_check_power;
3524 case snd_soc_dapm_supply:
3525 case snd_soc_dapm_regulator_supply:
3526 case snd_soc_dapm_pinctrl:
3527 case snd_soc_dapm_clock_supply:
3528 case snd_soc_dapm_kcontrol:
3530 w->power_check = dapm_supply_check_power;
3533 w->power_check = dapm_always_on_check_power;
3538 INIT_LIST_HEAD(&w->list);
3539 INIT_LIST_HEAD(&w->dirty);
3540 list_add_tail(&w->list, &dapm->card->widgets);
3542 snd_soc_dapm_for_each_direction(dir) {
3543 INIT_LIST_HEAD(&w->edges[dir]);
3544 w->endpoints[dir] = -1;
3547 /* machine layer sets up unconnected pins and insertions */
3552 if (ret != -EPROBE_DEFER)
3553 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3556 return ERR_PTR(ret);
3560 * snd_soc_dapm_new_control - create new dapm control
3561 * @dapm: DAPM context
3562 * @widget: widget template
3564 * Creates new DAPM control based upon a template.
3566 * Returns a widget pointer on success or an error pointer on failure
3568 struct snd_soc_dapm_widget *
3569 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3570 const struct snd_soc_dapm_widget *widget)
3572 struct snd_soc_dapm_widget *w;
3574 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3575 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3576 mutex_unlock(&dapm->card->dapm_mutex);
3580 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3583 * snd_soc_dapm_new_controls - create new dapm controls
3584 * @dapm: DAPM context
3585 * @widget: widget array
3586 * @num: number of widgets
3588 * Creates new DAPM controls based upon the templates.
3590 * Returns 0 for success else error.
3592 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3593 const struct snd_soc_dapm_widget *widget,
3596 struct snd_soc_dapm_widget *w;
3600 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3601 for (i = 0; i < num; i++) {
3602 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3609 mutex_unlock(&dapm->card->dapm_mutex);
3612 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3614 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3615 struct snd_kcontrol *kcontrol, int event)
3617 struct snd_soc_dapm_path *path;
3618 struct snd_soc_dai *source, *sink;
3619 struct snd_soc_pcm_runtime *rtd = w->priv;
3620 const struct snd_soc_pcm_stream *config;
3621 struct snd_pcm_substream substream;
3622 struct snd_pcm_hw_params *params = NULL;
3623 struct snd_pcm_runtime *runtime = NULL;
3627 config = rtd->dai_link->params + rtd->params_select;
3629 if (WARN_ON(!config) ||
3630 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3631 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3634 /* Be a little careful as we don't want to overflow the mask array */
3635 if (config->formats) {
3636 fmt = ffs(config->formats) - 1;
3638 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3643 /* Currently very limited parameter selection */
3644 params = kzalloc(sizeof(*params), GFP_KERNEL);
3649 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3651 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3653 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3656 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3657 = config->channels_min;
3658 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3659 = config->channels_max;
3661 memset(&substream, 0, sizeof(substream));
3663 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
3664 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3669 substream.runtime = runtime;
3670 substream.private_data = rtd;
3673 case SND_SOC_DAPM_PRE_PMU:
3674 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3675 snd_soc_dapm_widget_for_each_source_path(w, path) {
3676 source = path->source->priv;
3678 if (source->driver->ops->startup) {
3679 ret = source->driver->ops->startup(&substream,
3682 dev_err(source->dev,
3683 "ASoC: startup() failed: %d\n",
3689 ret = soc_dai_hw_params(&substream, params, source);
3694 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3695 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3696 sink = path->sink->priv;
3698 if (sink->driver->ops->startup) {
3699 ret = sink->driver->ops->startup(&substream,
3703 "ASoC: startup() failed: %d\n",
3709 ret = soc_dai_hw_params(&substream, params, sink);
3715 case SND_SOC_DAPM_POST_PMU:
3716 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3717 sink = path->sink->priv;
3719 ret = snd_soc_dai_digital_mute(sink, 0,
3720 SNDRV_PCM_STREAM_PLAYBACK);
3721 if (ret != 0 && ret != -ENOTSUPP)
3723 "ASoC: Failed to unmute: %d\n", ret);
3728 case SND_SOC_DAPM_PRE_PMD:
3729 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3730 sink = path->sink->priv;
3732 ret = snd_soc_dai_digital_mute(sink, 1,
3733 SNDRV_PCM_STREAM_PLAYBACK);
3734 if (ret != 0 && ret != -ENOTSUPP)
3736 "ASoC: Failed to mute: %d\n", ret);
3740 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3741 snd_soc_dapm_widget_for_each_source_path(w, path) {
3742 source = path->source->priv;
3744 if (source->driver->ops->hw_free)
3745 source->driver->ops->hw_free(&substream,
3749 if (source->driver->ops->shutdown)
3750 source->driver->ops->shutdown(&substream,
3754 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3755 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3756 sink = path->sink->priv;
3758 if (sink->driver->ops->hw_free)
3759 sink->driver->ops->hw_free(&substream, sink);
3762 if (sink->driver->ops->shutdown)
3763 sink->driver->ops->shutdown(&substream, sink);
3768 WARN(1, "Unknown event %d\n", event);
3778 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3779 struct snd_ctl_elem_value *ucontrol)
3781 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3782 struct snd_soc_pcm_runtime *rtd = w->priv;
3784 ucontrol->value.enumerated.item[0] = rtd->params_select;
3789 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3790 struct snd_ctl_elem_value *ucontrol)
3792 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3793 struct snd_soc_pcm_runtime *rtd = w->priv;
3795 /* Can't change the config when widget is already powered */
3799 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
3802 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
3805 rtd->params_select = ucontrol->value.enumerated.item[0];
3811 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
3812 unsigned long *private_value,
3814 const char **w_param_text)
3818 devm_kfree(card->dev, (void *)*private_value);
3819 for (count = 0 ; count < num_params; count++)
3820 devm_kfree(card->dev, (void *)w_param_text[count]);
3821 devm_kfree(card->dev, w_param_text);
3824 static struct snd_kcontrol_new *
3825 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
3827 const struct snd_soc_pcm_stream *params,
3828 int num_params, const char **w_param_text,
3829 unsigned long *private_value)
3831 struct soc_enum w_param_enum[] = {
3832 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3834 struct snd_kcontrol_new kcontrol_dai_link[] = {
3835 SOC_ENUM_EXT(NULL, w_param_enum[0],
3836 snd_soc_dapm_dai_link_get,
3837 snd_soc_dapm_dai_link_put),
3839 struct snd_kcontrol_new *kcontrol_news;
3840 const struct snd_soc_pcm_stream *config = params;
3843 for (count = 0 ; count < num_params; count++) {
3844 if (!config->stream_name) {
3845 dev_warn(card->dapm.dev,
3846 "ASoC: anonymous config %d for dai link %s\n",
3848 w_param_text[count] =
3849 devm_kasprintf(card->dev, GFP_KERNEL,
3850 "Anonymous Configuration %d",
3853 w_param_text[count] = devm_kmemdup(card->dev,
3854 config->stream_name,
3855 strlen(config->stream_name) + 1,
3858 if (!w_param_text[count])
3859 goto outfree_w_param;
3863 w_param_enum[0].items = num_params;
3864 w_param_enum[0].texts = w_param_text;
3867 (unsigned long) devm_kmemdup(card->dev,
3868 (void *)(kcontrol_dai_link[0].private_value),
3869 sizeof(struct soc_enum), GFP_KERNEL);
3870 if (!*private_value) {
3871 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3873 goto outfree_w_param;
3875 kcontrol_dai_link[0].private_value = *private_value;
3876 /* duplicate kcontrol_dai_link on heap so that memory persists */
3877 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3878 sizeof(struct snd_kcontrol_new),
3880 if (!kcontrol_news) {
3881 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3883 goto outfree_w_param;
3885 return kcontrol_news;
3888 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
3892 static struct snd_soc_dapm_widget *
3893 snd_soc_dapm_new_dai(struct snd_soc_card *card, struct snd_soc_pcm_runtime *rtd,
3894 struct snd_soc_dapm_widget *source,
3895 struct snd_soc_dapm_widget *sink)
3897 struct snd_soc_dapm_widget template;
3898 struct snd_soc_dapm_widget *w;
3899 const char **w_param_text;
3900 unsigned long private_value;
3904 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3905 source->name, sink->name);
3907 return ERR_PTR(-ENOMEM);
3909 memset(&template, 0, sizeof(template));
3910 template.reg = SND_SOC_NOPM;
3911 template.id = snd_soc_dapm_dai_link;
3912 template.name = link_name;
3913 template.event = snd_soc_dai_link_event;
3914 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3915 SND_SOC_DAPM_PRE_PMD;
3916 template.kcontrol_news = NULL;
3918 /* allocate memory for control, only in case of multiple configs */
3919 if (rtd->dai_link->num_params > 1) {
3920 w_param_text = devm_kcalloc(card->dev,
3921 rtd->dai_link->num_params,
3922 sizeof(char *), GFP_KERNEL);
3923 if (!w_param_text) {
3928 template.num_kcontrols = 1;
3929 template.kcontrol_news =
3930 snd_soc_dapm_alloc_kcontrol(card,
3932 rtd->dai_link->params,
3933 rtd->dai_link->num_params,
3934 w_param_text, &private_value);
3935 if (!template.kcontrol_news) {
3940 w_param_text = NULL;
3942 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3944 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
3947 goto outfree_kcontrol_news;
3954 outfree_kcontrol_news:
3955 devm_kfree(card->dev, (void *)template.kcontrol_news);
3956 snd_soc_dapm_free_kcontrol(card, &private_value,
3957 rtd->dai_link->num_params, w_param_text);
3959 devm_kfree(card->dev, link_name);
3960 return ERR_PTR(ret);
3963 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3964 struct snd_soc_dai *dai)
3966 struct snd_soc_dapm_widget template;
3967 struct snd_soc_dapm_widget *w;
3969 WARN_ON(dapm->dev != dai->dev);
3971 memset(&template, 0, sizeof(template));
3972 template.reg = SND_SOC_NOPM;
3974 if (dai->driver->playback.stream_name) {
3975 template.id = snd_soc_dapm_dai_in;
3976 template.name = dai->driver->playback.stream_name;
3977 template.sname = dai->driver->playback.stream_name;
3979 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3982 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
3987 dai->playback_widget = w;
3990 if (dai->driver->capture.stream_name) {
3991 template.id = snd_soc_dapm_dai_out;
3992 template.name = dai->driver->capture.stream_name;
3993 template.sname = dai->driver->capture.stream_name;
3995 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3998 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4003 dai->capture_widget = w;
4009 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4011 struct snd_soc_dapm_widget *dai_w, *w;
4012 struct snd_soc_dapm_widget *src, *sink;
4013 struct snd_soc_dai *dai;
4015 /* For each DAI widget... */
4016 list_for_each_entry(dai_w, &card->widgets, list) {
4017 switch (dai_w->id) {
4018 case snd_soc_dapm_dai_in:
4019 case snd_soc_dapm_dai_out:
4025 /* let users know there is no DAI to link */
4027 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4034 /* ...find all widgets with the same stream and link them */
4035 list_for_each_entry(w, &card->widgets, list) {
4036 if (w->dapm != dai_w->dapm)
4040 case snd_soc_dapm_dai_in:
4041 case snd_soc_dapm_dai_out:
4047 if (!w->sname || !strstr(w->sname, dai_w->sname))
4050 if (dai_w->id == snd_soc_dapm_dai_in) {
4057 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4058 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4065 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4066 struct snd_soc_pcm_runtime *rtd)
4068 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4069 struct snd_soc_dai *codec_dai;
4070 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4071 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
4074 if (rtd->dai_link->params) {
4075 playback_cpu = cpu_dai->capture_widget;
4076 capture_cpu = cpu_dai->playback_widget;
4078 playback = cpu_dai->playback_widget;
4079 capture = cpu_dai->capture_widget;
4080 playback_cpu = playback;
4081 capture_cpu = capture;
4084 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4086 /* connect BE DAI playback if widgets are valid */
4087 codec = codec_dai->playback_widget;
4089 if (playback_cpu && codec) {
4091 playback = snd_soc_dapm_new_dai(card, rtd,
4094 if (IS_ERR(playback)) {
4096 "ASoC: Failed to create DAI %s: %ld\n",
4102 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4103 playback, NULL, NULL);
4106 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4107 cpu_dai->component->name, playback_cpu->name,
4108 codec_dai->component->name, codec->name);
4110 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4115 for_each_rtd_codec_dai(rtd, i, codec_dai) {
4116 /* connect BE DAI capture if widgets are valid */
4117 codec = codec_dai->capture_widget;
4119 if (codec && capture_cpu) {
4121 capture = snd_soc_dapm_new_dai(card, rtd,
4124 if (IS_ERR(capture)) {
4126 "ASoC: Failed to create DAI %s: %ld\n",
4132 snd_soc_dapm_add_path(&card->dapm, capture,
4133 capture_cpu, NULL, NULL);
4136 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4137 codec_dai->component->name, codec->name,
4138 cpu_dai->component->name, capture_cpu->name);
4140 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4146 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4149 struct snd_soc_dapm_widget *w;
4152 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
4153 w = dai->playback_widget;
4155 w = dai->capture_widget;
4158 dapm_mark_dirty(w, "stream event");
4160 if (w->id == snd_soc_dapm_dai_in) {
4161 ep = SND_SOC_DAPM_EP_SOURCE;
4162 dapm_widget_invalidate_input_paths(w);
4164 ep = SND_SOC_DAPM_EP_SINK;
4165 dapm_widget_invalidate_output_paths(w);
4169 case SND_SOC_DAPM_STREAM_START:
4173 case SND_SOC_DAPM_STREAM_STOP:
4177 case SND_SOC_DAPM_STREAM_SUSPEND:
4178 case SND_SOC_DAPM_STREAM_RESUME:
4179 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4180 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4186 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4188 struct snd_soc_pcm_runtime *rtd;
4190 /* for each BE DAI link... */
4191 for_each_card_rtds(card, rtd) {
4193 * dynamic FE links have no fixed DAI mapping.
4194 * CODEC<->CODEC links have no direct connection.
4196 if (rtd->dai_link->dynamic)
4199 dapm_connect_dai_link_widgets(card, rtd);
4203 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4206 struct snd_soc_dai *codec_dai;
4209 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
4210 for_each_rtd_codec_dai(rtd, i, codec_dai)
4211 soc_dapm_dai_stream_event(codec_dai, stream, event);
4213 dapm_power_widgets(rtd->card, event);
4217 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4218 * @rtd: PCM runtime data
4219 * @stream: stream name
4220 * @event: stream event
4222 * Sends a stream event to the dapm core. The core then makes any
4223 * necessary widget power changes.
4225 * Returns 0 for success else error.
4227 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4230 struct snd_soc_card *card = rtd->card;
4232 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4233 soc_dapm_stream_event(rtd, stream, event);
4234 mutex_unlock(&card->dapm_mutex);
4238 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4239 * @dapm: DAPM context
4242 * Enables input/output pin and its parents or children widgets iff there is
4243 * a valid audio route and active audio stream.
4245 * Requires external locking.
4247 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4248 * do any widget power switching.
4250 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4253 return snd_soc_dapm_set_pin(dapm, pin, 1);
4255 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4258 * snd_soc_dapm_enable_pin - enable pin.
4259 * @dapm: DAPM context
4262 * Enables input/output pin and its parents or children widgets iff there is
4263 * a valid audio route and active audio stream.
4265 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4266 * do any widget power switching.
4268 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4272 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4274 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4276 mutex_unlock(&dapm->card->dapm_mutex);
4280 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4283 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4284 * @dapm: DAPM context
4287 * Enables input/output pin regardless of any other state. This is
4288 * intended for use with microphone bias supplies used in microphone
4291 * Requires external locking.
4293 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4294 * do any widget power switching.
4296 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4299 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4302 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4306 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4307 if (!w->connected) {
4309 * w->force does not affect the number of input or output paths,
4310 * so we only have to recheck if w->connected is changed
4312 dapm_widget_invalidate_input_paths(w);
4313 dapm_widget_invalidate_output_paths(w);
4317 dapm_mark_dirty(w, "force enable");
4321 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4324 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4325 * @dapm: DAPM context
4328 * Enables input/output pin regardless of any other state. This is
4329 * intended for use with microphone bias supplies used in microphone
4332 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4333 * do any widget power switching.
4335 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4340 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4342 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4344 mutex_unlock(&dapm->card->dapm_mutex);
4348 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4351 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4352 * @dapm: DAPM context
4355 * Disables input/output pin and its parents or children widgets.
4357 * Requires external locking.
4359 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4360 * do any widget power switching.
4362 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4365 return snd_soc_dapm_set_pin(dapm, pin, 0);
4367 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4370 * snd_soc_dapm_disable_pin - disable pin.
4371 * @dapm: DAPM context
4374 * Disables input/output pin and its parents or children widgets.
4376 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4377 * do any widget power switching.
4379 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4384 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4386 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4388 mutex_unlock(&dapm->card->dapm_mutex);
4392 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4395 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4396 * @dapm: DAPM context
4399 * Marks the specified pin as being not connected, disabling it along
4400 * any parent or child widgets. At present this is identical to
4401 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4402 * additional things such as disabling controls which only affect
4403 * paths through the pin.
4405 * Requires external locking.
4407 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4408 * do any widget power switching.
4410 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4413 return snd_soc_dapm_set_pin(dapm, pin, 0);
4415 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4418 * snd_soc_dapm_nc_pin - permanently disable pin.
4419 * @dapm: DAPM context
4422 * Marks the specified pin as being not connected, disabling it along
4423 * any parent or child widgets. At present this is identical to
4424 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4425 * additional things such as disabling controls which only affect
4426 * paths through the pin.
4428 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4429 * do any widget power switching.
4431 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4435 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4437 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4439 mutex_unlock(&dapm->card->dapm_mutex);
4443 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4446 * snd_soc_dapm_get_pin_status - get audio pin status
4447 * @dapm: DAPM context
4448 * @pin: audio signal pin endpoint (or start point)
4450 * Get audio pin status - connected or disconnected.
4452 * Returns 1 for connected otherwise 0.
4454 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4457 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4460 return w->connected;
4464 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4467 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4468 * @dapm: DAPM context
4469 * @pin: audio signal pin endpoint (or start point)
4471 * Mark the given endpoint or pin as ignoring suspend. When the
4472 * system is disabled a path between two endpoints flagged as ignoring
4473 * suspend will not be disabled. The path must already be enabled via
4474 * normal means at suspend time, it will not be turned on if it was not
4477 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4480 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4483 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4487 w->ignore_suspend = 1;
4491 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4494 * snd_soc_dapm_free - free dapm resources
4495 * @dapm: DAPM context
4497 * Free all dapm widgets and resources.
4499 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4501 dapm_debugfs_cleanup(dapm);
4502 dapm_free_widgets(dapm);
4503 list_del(&dapm->list);
4505 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4507 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4509 struct snd_soc_card *card = dapm->card;
4510 struct snd_soc_dapm_widget *w;
4511 LIST_HEAD(down_list);
4514 mutex_lock(&card->dapm_mutex);
4516 list_for_each_entry(w, &dapm->card->widgets, list) {
4517 if (w->dapm != dapm)
4520 dapm_seq_insert(w, &down_list, false);
4526 /* If there were no widgets to power down we're already in
4530 if (dapm->bias_level == SND_SOC_BIAS_ON)
4531 snd_soc_dapm_set_bias_level(dapm,
4532 SND_SOC_BIAS_PREPARE);
4533 dapm_seq_run(card, &down_list, 0, false);
4534 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4535 snd_soc_dapm_set_bias_level(dapm,
4536 SND_SOC_BIAS_STANDBY);
4539 mutex_unlock(&card->dapm_mutex);
4543 * snd_soc_dapm_shutdown - callback for system shutdown
4545 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4547 struct snd_soc_dapm_context *dapm;
4549 list_for_each_entry(dapm, &card->dapm_list, list) {
4550 if (dapm != &card->dapm) {
4551 soc_dapm_shutdown_dapm(dapm);
4552 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4553 snd_soc_dapm_set_bias_level(dapm,
4558 soc_dapm_shutdown_dapm(&card->dapm);
4559 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4560 snd_soc_dapm_set_bias_level(&card->dapm,
4564 /* Module information */
4565 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4566 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4567 MODULE_LICENSE("GPL");