2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/headphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed power down of audio subsystem to reduce pops between a quick
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/pinctrl/consumer.h>
39 #include <linux/clk.h>
40 #include <linux/slab.h>
41 #include <sound/core.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45 #include <sound/initval.h>
47 #include <trace/events/asoc.h>
49 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
51 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
52 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
54 #define snd_soc_dapm_for_each_direction(dir) \
55 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
58 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
59 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
61 int (*connected)(struct snd_soc_dapm_widget *source,
62 struct snd_soc_dapm_widget *sink));
64 struct snd_soc_dapm_widget *
65 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
66 const struct snd_soc_dapm_widget *widget);
68 struct snd_soc_dapm_widget *
69 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
70 const struct snd_soc_dapm_widget *widget);
72 /* dapm power sequences - make this per codec in the future */
73 static int dapm_up_seq[] = {
74 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_regulator_supply] = 1,
76 [snd_soc_dapm_pinctrl] = 1,
77 [snd_soc_dapm_clock_supply] = 1,
78 [snd_soc_dapm_supply] = 2,
79 [snd_soc_dapm_micbias] = 3,
80 [snd_soc_dapm_dai_link] = 2,
81 [snd_soc_dapm_dai_in] = 4,
82 [snd_soc_dapm_dai_out] = 4,
83 [snd_soc_dapm_aif_in] = 4,
84 [snd_soc_dapm_aif_out] = 4,
85 [snd_soc_dapm_mic] = 5,
86 [snd_soc_dapm_mux] = 6,
87 [snd_soc_dapm_demux] = 6,
88 [snd_soc_dapm_dac] = 7,
89 [snd_soc_dapm_switch] = 8,
90 [snd_soc_dapm_mixer] = 8,
91 [snd_soc_dapm_mixer_named_ctl] = 8,
92 [snd_soc_dapm_pga] = 9,
93 [snd_soc_dapm_adc] = 10,
94 [snd_soc_dapm_out_drv] = 11,
95 [snd_soc_dapm_hp] = 11,
96 [snd_soc_dapm_spk] = 11,
97 [snd_soc_dapm_line] = 11,
98 [snd_soc_dapm_kcontrol] = 12,
99 [snd_soc_dapm_post] = 13,
102 static int dapm_down_seq[] = {
103 [snd_soc_dapm_pre] = 0,
104 [snd_soc_dapm_kcontrol] = 1,
105 [snd_soc_dapm_adc] = 2,
106 [snd_soc_dapm_hp] = 3,
107 [snd_soc_dapm_spk] = 3,
108 [snd_soc_dapm_line] = 3,
109 [snd_soc_dapm_out_drv] = 3,
110 [snd_soc_dapm_pga] = 4,
111 [snd_soc_dapm_switch] = 5,
112 [snd_soc_dapm_mixer_named_ctl] = 5,
113 [snd_soc_dapm_mixer] = 5,
114 [snd_soc_dapm_dac] = 6,
115 [snd_soc_dapm_mic] = 7,
116 [snd_soc_dapm_micbias] = 8,
117 [snd_soc_dapm_mux] = 9,
118 [snd_soc_dapm_demux] = 9,
119 [snd_soc_dapm_aif_in] = 10,
120 [snd_soc_dapm_aif_out] = 10,
121 [snd_soc_dapm_dai_in] = 10,
122 [snd_soc_dapm_dai_out] = 10,
123 [snd_soc_dapm_dai_link] = 11,
124 [snd_soc_dapm_supply] = 12,
125 [snd_soc_dapm_clock_supply] = 13,
126 [snd_soc_dapm_pinctrl] = 13,
127 [snd_soc_dapm_regulator_supply] = 13,
128 [snd_soc_dapm_post] = 14,
131 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
133 if (dapm->card && dapm->card->instantiated)
134 lockdep_assert_held(&dapm->card->dapm_mutex);
137 static void pop_wait(u32 pop_time)
140 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
143 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
151 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
156 vsnprintf(buf, PAGE_SIZE, fmt, args);
157 dev_info(dev, "%s", buf);
163 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
165 return !list_empty(&w->dirty);
168 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
170 dapm_assert_locked(w->dapm);
172 if (!dapm_dirty_widget(w)) {
173 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
175 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
180 * Common implementation for dapm_widget_invalidate_input_paths() and
181 * dapm_widget_invalidate_output_paths(). The function is inlined since the
182 * combined size of the two specialized functions is only marginally larger then
183 * the size of the generic function and at the same time the fast path of the
184 * specialized functions is significantly smaller than the generic function.
186 static __always_inline void dapm_widget_invalidate_paths(
187 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
189 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
190 struct snd_soc_dapm_widget *node;
191 struct snd_soc_dapm_path *p;
194 dapm_assert_locked(w->dapm);
196 if (w->endpoints[dir] == -1)
199 list_add_tail(&w->work_list, &list);
200 w->endpoints[dir] = -1;
202 list_for_each_entry(w, &list, work_list) {
203 snd_soc_dapm_widget_for_each_path(w, dir, p) {
204 if (p->is_supply || p->weak || !p->connect)
206 node = p->node[rdir];
207 if (node->endpoints[dir] != -1) {
208 node->endpoints[dir] = -1;
209 list_add_tail(&node->work_list, &list);
216 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
218 * @w: The widget for which to invalidate the cached number of input paths
220 * Resets the cached number of inputs for the specified widget and all widgets
221 * that can be reached via outcoming paths from the widget.
223 * This function must be called if the number of output paths for a widget might
224 * have changed. E.g. if the source state of a widget changes or a path is added
225 * or activated with the widget as the sink.
227 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
229 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
233 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
235 * @w: The widget for which to invalidate the cached number of output paths
237 * Resets the cached number of outputs for the specified widget and all widgets
238 * that can be reached via incoming paths from the widget.
240 * This function must be called if the number of output paths for a widget might
241 * have changed. E.g. if the sink state of a widget changes or a path is added
242 * or activated with the widget as the source.
244 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
246 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
250 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
251 * for the widgets connected to a path
252 * @p: The path to invalidate
254 * Resets the cached number of inputs for the sink of the path and the cached
255 * number of outputs for the source of the path.
257 * This function must be called when a path is added, removed or the connected
260 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
263 * Weak paths or supply paths do not influence the number of input or
264 * output paths of their neighbors.
266 if (p->weak || p->is_supply)
270 * The number of connected endpoints is the sum of the number of
271 * connected endpoints of all neighbors. If a node with 0 connected
272 * endpoints is either connected or disconnected that sum won't change,
273 * so there is no need to re-check the path.
275 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
276 dapm_widget_invalidate_input_paths(p->sink);
277 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
278 dapm_widget_invalidate_output_paths(p->source);
281 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
283 struct snd_soc_dapm_widget *w;
285 mutex_lock(&card->dapm_mutex);
287 list_for_each_entry(w, &card->widgets, list) {
289 dapm_mark_dirty(w, "Rechecking endpoints");
290 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
291 dapm_widget_invalidate_output_paths(w);
292 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
293 dapm_widget_invalidate_input_paths(w);
297 mutex_unlock(&card->dapm_mutex);
299 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
301 /* create a new dapm widget */
302 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
303 const struct snd_soc_dapm_widget *_widget)
305 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
308 struct dapm_kcontrol_data {
310 struct snd_soc_dapm_widget *widget;
311 struct list_head paths;
312 struct snd_soc_dapm_widget_list *wlist;
315 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
316 struct snd_kcontrol *kcontrol, const char *ctrl_name)
318 struct dapm_kcontrol_data *data;
319 struct soc_mixer_control *mc;
324 data = kzalloc(sizeof(*data), GFP_KERNEL);
328 INIT_LIST_HEAD(&data->paths);
330 switch (widget->id) {
331 case snd_soc_dapm_switch:
332 case snd_soc_dapm_mixer:
333 case snd_soc_dapm_mixer_named_ctl:
334 mc = (struct soc_mixer_control *)kcontrol->private_value;
336 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
337 dev_warn(widget->dapm->dev,
338 "ASoC: Unsupported stereo autodisable control '%s'\n",
341 if (mc->autodisable) {
342 struct snd_soc_dapm_widget template;
344 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
351 memset(&template, 0, sizeof(template));
352 template.reg = mc->reg;
353 template.mask = (1 << fls(mc->max)) - 1;
354 template.shift = mc->shift;
356 template.off_val = mc->max;
358 template.off_val = 0;
359 template.on_val = template.off_val;
360 template.id = snd_soc_dapm_kcontrol;
361 template.name = name;
363 data->value = template.on_val;
366 snd_soc_dapm_new_control_unlocked(widget->dapm,
369 if (IS_ERR(data->widget)) {
370 ret = PTR_ERR(data->widget);
379 case snd_soc_dapm_demux:
380 case snd_soc_dapm_mux:
381 e = (struct soc_enum *)kcontrol->private_value;
383 if (e->autodisable) {
384 struct snd_soc_dapm_widget template;
386 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
393 memset(&template, 0, sizeof(template));
394 template.reg = e->reg;
395 template.mask = e->mask << e->shift_l;
396 template.shift = e->shift_l;
397 template.off_val = snd_soc_enum_item_to_val(e, 0);
398 template.on_val = template.off_val;
399 template.id = snd_soc_dapm_kcontrol;
400 template.name = name;
402 data->value = template.on_val;
404 data->widget = snd_soc_dapm_new_control_unlocked(
405 widget->dapm, &template);
407 if (IS_ERR(data->widget)) {
408 ret = PTR_ERR(data->widget);
416 snd_soc_dapm_add_path(widget->dapm, data->widget,
424 kcontrol->private_data = data;
433 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
435 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
437 list_del(&data->paths);
442 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
443 const struct snd_kcontrol *kcontrol)
445 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
450 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
451 struct snd_soc_dapm_widget *widget)
453 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
454 struct snd_soc_dapm_widget_list *new_wlist;
458 n = data->wlist->num_widgets + 1;
462 new_wlist = krealloc(data->wlist,
463 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
467 new_wlist->widgets[n - 1] = widget;
468 new_wlist->num_widgets = n;
470 data->wlist = new_wlist;
475 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
476 struct snd_soc_dapm_path *path)
478 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
480 list_add_tail(&path->list_kcontrol, &data->paths);
483 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
485 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
490 return data->widget->power;
493 static struct list_head *dapm_kcontrol_get_path_list(
494 const struct snd_kcontrol *kcontrol)
496 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
501 #define dapm_kcontrol_for_each_path(path, kcontrol) \
502 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
505 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
507 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
511 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
513 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
516 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
518 if (data->value == value)
522 data->widget->on_val = value;
530 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
532 * @kcontrol: The kcontrol
534 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
535 struct snd_kcontrol *kcontrol)
537 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
539 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
542 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
544 * @kcontrol: The kcontrol
546 * Note: This function must only be used on kcontrols that are known to have
547 * been registered for a CODEC. Otherwise the behaviour is undefined.
549 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
550 struct snd_kcontrol *kcontrol)
552 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
554 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
556 static void dapm_reset(struct snd_soc_card *card)
558 struct snd_soc_dapm_widget *w;
560 lockdep_assert_held(&card->dapm_mutex);
562 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
564 list_for_each_entry(w, &card->widgets, list) {
565 w->new_power = w->power;
566 w->power_checked = false;
570 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
572 if (!dapm->component)
574 return dapm->component->name_prefix;
577 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
580 if (!dapm->component)
582 return snd_soc_component_read(dapm->component, reg, value);
585 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
586 int reg, unsigned int mask, unsigned int value)
588 if (!dapm->component)
590 return snd_soc_component_update_bits(dapm->component, reg,
594 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
595 int reg, unsigned int mask, unsigned int value)
597 if (!dapm->component)
599 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
602 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
605 snd_soc_component_async_complete(dapm->component);
608 static struct snd_soc_dapm_widget *
609 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
611 struct snd_soc_dapm_widget *w = wcache->widget;
612 struct list_head *wlist;
617 wlist = &w->dapm->card->widgets;
619 list_for_each_entry_from(w, wlist, list) {
620 if (!strcmp(name, w->name))
631 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
632 struct snd_soc_dapm_widget *w)
638 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
639 * @dapm: The DAPM context for which to set the level
640 * @level: The level to set
642 * Forces the DAPM bias level to a specific state. It will call the bias level
643 * callback of DAPM context with the specified level. This will even happen if
644 * the context is already at the same level. Furthermore it will not go through
645 * the normal bias level sequencing, meaning any intermediate states between the
646 * current and the target state will not be entered.
648 * Note that the change in bias level is only temporary and the next time
649 * snd_soc_dapm_sync() is called the state will be set to the level as
650 * determined by the DAPM core. The function is mainly intended to be used to
651 * used during probe or resume from suspend to power up the device so
652 * initialization can be done, before the DAPM core takes over.
654 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
655 enum snd_soc_bias_level level)
659 if (dapm->set_bias_level)
660 ret = dapm->set_bias_level(dapm, level);
663 dapm->bias_level = level;
667 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
670 * snd_soc_dapm_set_bias_level - set the bias level for the system
671 * @dapm: DAPM context
672 * @level: level to configure
674 * Configure the bias (power) levels for the SoC audio device.
676 * Returns 0 for success else error.
678 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
679 enum snd_soc_bias_level level)
681 struct snd_soc_card *card = dapm->card;
684 trace_snd_soc_bias_level_start(card, level);
686 if (card && card->set_bias_level)
687 ret = card->set_bias_level(card, dapm, level);
691 if (!card || dapm != &card->dapm)
692 ret = snd_soc_dapm_force_bias_level(dapm, level);
697 if (card && card->set_bias_level_post)
698 ret = card->set_bias_level_post(card, dapm, level);
700 trace_snd_soc_bias_level_done(card, level);
705 /* connect mux widget to its interconnecting audio paths */
706 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
707 struct snd_soc_dapm_path *path, const char *control_name,
708 struct snd_soc_dapm_widget *w)
710 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
711 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
712 unsigned int val, item;
715 if (e->reg != SND_SOC_NOPM) {
716 soc_dapm_read(dapm, e->reg, &val);
717 val = (val >> e->shift_l) & e->mask;
718 item = snd_soc_enum_val_to_item(e, val);
720 /* since a virtual mux has no backing registers to
721 * decide which path to connect, it will try to match
722 * with the first enumeration. This is to ensure
723 * that the default mux choice (the first) will be
724 * correctly powered up during initialization.
729 i = match_string(e->texts, e->items, control_name);
733 path->name = e->texts[i];
734 path->connect = (i == item);
739 /* set up initial codec paths */
740 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
743 struct soc_mixer_control *mc = (struct soc_mixer_control *)
744 p->sink->kcontrol_news[i].private_value;
745 unsigned int reg = mc->reg;
746 unsigned int shift = mc->shift;
747 unsigned int max = mc->max;
748 unsigned int mask = (1 << fls(max)) - 1;
749 unsigned int invert = mc->invert;
752 if (reg != SND_SOC_NOPM) {
753 soc_dapm_read(p->sink->dapm, reg, &val);
755 * The nth_path argument allows this function to know
756 * which path of a kcontrol it is setting the initial
757 * status for. Ideally this would support any number
758 * of paths and channels. But since kcontrols only come
759 * in mono and stereo variants, we are limited to 2
762 * The following code assumes for stereo controls the
763 * first path is the left channel, and all remaining
764 * paths are the right channel.
766 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
768 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
769 val = (val >> mc->rshift) & mask;
771 val = (val >> shift) & mask;
781 /* connect mixer widget to its interconnecting audio paths */
782 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
783 struct snd_soc_dapm_path *path, const char *control_name)
787 /* search for mixer kcontrol */
788 for (i = 0; i < path->sink->num_kcontrols; i++) {
789 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
790 path->name = path->sink->kcontrol_news[i].name;
791 dapm_set_mixer_path_status(path, i, nth_path++);
798 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
799 struct snd_soc_dapm_widget *kcontrolw,
800 const struct snd_kcontrol_new *kcontrol_new,
801 struct snd_kcontrol **kcontrol)
803 struct snd_soc_dapm_widget *w;
808 list_for_each_entry(w, &dapm->card->widgets, list) {
809 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
811 for (i = 0; i < w->num_kcontrols; i++) {
812 if (&w->kcontrol_news[i] == kcontrol_new) {
814 *kcontrol = w->kcontrols[i];
824 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
825 * create it. Either way, add the widget into the control's widget list
827 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
830 struct snd_soc_dapm_context *dapm = w->dapm;
831 struct snd_card *card = dapm->card->snd_card;
835 struct snd_kcontrol *kcontrol;
836 bool wname_in_long_name, kcname_in_long_name;
837 char *long_name = NULL;
841 prefix = soc_dapm_prefix(dapm);
843 prefix_len = strlen(prefix) + 1;
847 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
852 wname_in_long_name = false;
853 kcname_in_long_name = true;
856 case snd_soc_dapm_switch:
857 case snd_soc_dapm_mixer:
858 case snd_soc_dapm_pga:
859 case snd_soc_dapm_out_drv:
860 wname_in_long_name = true;
861 kcname_in_long_name = true;
863 case snd_soc_dapm_mixer_named_ctl:
864 wname_in_long_name = false;
865 kcname_in_long_name = true;
867 case snd_soc_dapm_demux:
868 case snd_soc_dapm_mux:
869 wname_in_long_name = true;
870 kcname_in_long_name = false;
877 if (wname_in_long_name && kcname_in_long_name) {
879 * The control will get a prefix from the control
880 * creation process but we're also using the same
881 * prefix for widgets so cut the prefix off the
882 * front of the widget name.
884 long_name = kasprintf(GFP_KERNEL, "%s %s",
885 w->name + prefix_len,
886 w->kcontrol_news[kci].name);
887 if (long_name == NULL)
891 } else if (wname_in_long_name) {
893 name = w->name + prefix_len;
896 name = w->kcontrol_news[kci].name;
899 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
906 kcontrol->private_free = dapm_kcontrol_free;
908 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
910 snd_ctl_free_one(kcontrol);
914 ret = snd_ctl_add(card, kcontrol);
917 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
923 ret = dapm_kcontrol_add_widget(kcontrol, w);
925 w->kcontrols[kci] = kcontrol;
933 /* create new dapm mixer control */
934 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
937 struct snd_soc_dapm_path *path;
938 struct dapm_kcontrol_data *data;
941 for (i = 0; i < w->num_kcontrols; i++) {
943 snd_soc_dapm_widget_for_each_source_path(w, path) {
944 /* mixer/mux paths name must match control name */
945 if (path->name != (char *)w->kcontrol_news[i].name)
948 if (!w->kcontrols[i]) {
949 ret = dapm_create_or_share_kcontrol(w, i);
954 dapm_kcontrol_add_path(w->kcontrols[i], path);
956 data = snd_kcontrol_chip(w->kcontrols[i]);
958 snd_soc_dapm_add_path(data->widget->dapm,
968 /* create new dapm mux control */
969 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
971 struct snd_soc_dapm_context *dapm = w->dapm;
972 enum snd_soc_dapm_direction dir;
973 struct snd_soc_dapm_path *path;
978 case snd_soc_dapm_mux:
979 dir = SND_SOC_DAPM_DIR_OUT;
982 case snd_soc_dapm_demux:
983 dir = SND_SOC_DAPM_DIR_IN;
990 if (w->num_kcontrols != 1) {
992 "ASoC: %s %s has incorrect number of controls\n", type,
997 if (list_empty(&w->edges[dir])) {
998 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1002 ret = dapm_create_or_share_kcontrol(w, 0);
1006 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1008 dapm_kcontrol_add_path(w->kcontrols[0], path);
1014 /* create new dapm volume control */
1015 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1019 for (i = 0; i < w->num_kcontrols; i++) {
1020 ret = dapm_create_or_share_kcontrol(w, i);
1028 /* create new dapm dai link control */
1029 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1032 struct snd_kcontrol *kcontrol;
1033 struct snd_soc_dapm_context *dapm = w->dapm;
1034 struct snd_card *card = dapm->card->snd_card;
1036 /* create control for links with > 1 config */
1037 if (w->num_params <= 1)
1041 for (i = 0; i < w->num_kcontrols; i++) {
1042 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1044 ret = snd_ctl_add(card, kcontrol);
1047 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1048 w->name, w->kcontrol_news[i].name, ret);
1051 kcontrol->private_data = w;
1052 w->kcontrols[i] = kcontrol;
1058 /* We implement power down on suspend by checking the power state of
1059 * the ALSA card - when we are suspending the ALSA state for the card
1062 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1064 int level = snd_power_get_state(widget->dapm->card->snd_card);
1067 case SNDRV_CTL_POWER_D3hot:
1068 case SNDRV_CTL_POWER_D3cold:
1069 if (widget->ignore_suspend)
1070 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1072 return widget->ignore_suspend;
1078 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1079 struct list_head *widgets)
1081 struct snd_soc_dapm_widget *w;
1082 struct list_head *it;
1083 unsigned int size = 0;
1086 list_for_each(it, widgets)
1089 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1093 list_for_each_entry(w, widgets, work_list)
1094 (*list)->widgets[i++] = w;
1096 (*list)->num_widgets = i;
1102 * Common implementation for is_connected_output_ep() and
1103 * is_connected_input_ep(). The function is inlined since the combined size of
1104 * the two specialized functions is only marginally larger then the size of the
1105 * generic function and at the same time the fast path of the specialized
1106 * functions is significantly smaller than the generic function.
1108 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1109 struct list_head *list, enum snd_soc_dapm_direction dir,
1110 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1111 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1112 enum snd_soc_dapm_direction)),
1113 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1114 enum snd_soc_dapm_direction))
1116 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1117 struct snd_soc_dapm_path *path;
1120 if (widget->endpoints[dir] >= 0)
1121 return widget->endpoints[dir];
1123 DAPM_UPDATE_STAT(widget, path_checks);
1125 /* do we need to add this widget to the list ? */
1127 list_add_tail(&widget->work_list, list);
1129 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1130 widget->endpoints[dir] = 1;
1131 return widget->endpoints[dir];
1134 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1135 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1136 return widget->endpoints[dir];
1139 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1140 DAPM_UPDATE_STAT(widget, neighbour_checks);
1142 if (path->weak || path->is_supply)
1148 trace_snd_soc_dapm_path(widget, dir, path);
1150 if (path->connect) {
1152 con += fn(path->node[dir], list, custom_stop_condition);
1157 widget->endpoints[dir] = con;
1163 * Recursively check for a completed path to an active or physically connected
1164 * output widget. Returns number of complete paths.
1166 * Optionally, can be supplied with a function acting as a stopping condition.
1167 * This function takes the dapm widget currently being examined and the walk
1168 * direction as an arguments, it should return true if the walk should be
1169 * stopped and false otherwise.
1171 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1172 struct list_head *list,
1173 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1174 enum snd_soc_dapm_direction))
1176 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1177 is_connected_output_ep, custom_stop_condition);
1181 * Recursively check for a completed path to an active or physically connected
1182 * input widget. Returns number of complete paths.
1184 * Optionally, can be supplied with a function acting as a stopping condition.
1185 * This function takes the dapm widget currently being examined and the walk
1186 * direction as an arguments, it should return true if the walk should be
1187 * stopped and false otherwise.
1189 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1190 struct list_head *list,
1191 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1192 enum snd_soc_dapm_direction))
1194 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1195 is_connected_input_ep, custom_stop_condition);
1199 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1200 * @dai: the soc DAI.
1201 * @stream: stream direction.
1202 * @list: list of active widgets for this stream.
1203 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1204 * walk based on custom logic.
1206 * Queries DAPM graph as to whether a valid audio stream path exists for
1207 * the initial stream specified by name. This takes into account
1208 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1210 * Optionally, can be supplied with a function acting as a stopping condition.
1211 * This function takes the dapm widget currently being examined and the walk
1212 * direction as an arguments, it should return true if the walk should be
1213 * stopped and false otherwise.
1215 * Returns the number of valid paths or negative error.
1217 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1218 struct snd_soc_dapm_widget_list **list,
1219 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1220 enum snd_soc_dapm_direction))
1222 struct snd_soc_card *card = dai->component->card;
1223 struct snd_soc_dapm_widget *w;
1228 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1231 * For is_connected_{output,input}_ep fully discover the graph we need
1232 * to reset the cached number of inputs and outputs.
1234 list_for_each_entry(w, &card->widgets, list) {
1235 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
1236 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
1239 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1240 paths = is_connected_output_ep(dai->playback_widget, &widgets,
1241 custom_stop_condition);
1243 paths = is_connected_input_ep(dai->capture_widget, &widgets,
1244 custom_stop_condition);
1246 /* Drop starting point */
1247 list_del(widgets.next);
1249 ret = dapm_widget_list_create(list, &widgets);
1253 trace_snd_soc_dapm_connected(paths, stream);
1254 mutex_unlock(&card->dapm_mutex);
1260 * Handler for regulator supply widget.
1262 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1263 struct snd_kcontrol *kcontrol, int event)
1267 soc_dapm_async_complete(w->dapm);
1269 if (SND_SOC_DAPM_EVENT_ON(event)) {
1270 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1271 ret = regulator_allow_bypass(w->regulator, false);
1273 dev_warn(w->dapm->dev,
1274 "ASoC: Failed to unbypass %s: %d\n",
1278 return regulator_enable(w->regulator);
1280 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1281 ret = regulator_allow_bypass(w->regulator, true);
1283 dev_warn(w->dapm->dev,
1284 "ASoC: Failed to bypass %s: %d\n",
1288 return regulator_disable_deferred(w->regulator, w->shift);
1291 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1294 * Handler for pinctrl widget.
1296 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1297 struct snd_kcontrol *kcontrol, int event)
1299 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1300 struct pinctrl *p = w->pinctrl;
1301 struct pinctrl_state *s;
1306 if (SND_SOC_DAPM_EVENT_ON(event))
1307 s = pinctrl_lookup_state(p, priv->active_state);
1309 s = pinctrl_lookup_state(p, priv->sleep_state);
1314 return pinctrl_select_state(p, s);
1316 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1319 * Handler for clock supply widget.
1321 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1322 struct snd_kcontrol *kcontrol, int event)
1327 soc_dapm_async_complete(w->dapm);
1329 #ifdef CONFIG_HAVE_CLK
1330 if (SND_SOC_DAPM_EVENT_ON(event)) {
1331 return clk_prepare_enable(w->clk);
1333 clk_disable_unprepare(w->clk);
1339 EXPORT_SYMBOL_GPL(dapm_clock_event);
1341 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1343 if (w->power_checked)
1344 return w->new_power;
1349 w->new_power = w->power_check(w);
1351 w->power_checked = true;
1353 return w->new_power;
1356 /* Generic check to see if a widget should be powered. */
1357 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1361 DAPM_UPDATE_STAT(w, power_checks);
1363 in = is_connected_input_ep(w, NULL, NULL);
1364 out = is_connected_output_ep(w, NULL, NULL);
1365 return out != 0 && in != 0;
1368 /* Check to see if a power supply is needed */
1369 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1371 struct snd_soc_dapm_path *path;
1373 DAPM_UPDATE_STAT(w, power_checks);
1375 /* Check if one of our outputs is connected */
1376 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1377 DAPM_UPDATE_STAT(w, neighbour_checks);
1382 if (path->connected &&
1383 !path->connected(path->source, path->sink))
1386 if (dapm_widget_power_check(path->sink))
1393 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1395 return w->connected;
1398 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1399 struct snd_soc_dapm_widget *b,
1407 sort = dapm_down_seq;
1409 if (sort[a->id] != sort[b->id])
1410 return sort[a->id] - sort[b->id];
1411 if (a->subseq != b->subseq) {
1413 return a->subseq - b->subseq;
1415 return b->subseq - a->subseq;
1417 if (a->reg != b->reg)
1418 return a->reg - b->reg;
1419 if (a->dapm != b->dapm)
1420 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1425 /* Insert a widget in order into a DAPM power sequence. */
1426 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1427 struct list_head *list,
1430 struct snd_soc_dapm_widget *w;
1432 list_for_each_entry(w, list, power_list)
1433 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1434 list_add_tail(&new_widget->power_list, &w->power_list);
1438 list_add_tail(&new_widget->power_list, list);
1441 static void dapm_seq_check_event(struct snd_soc_card *card,
1442 struct snd_soc_dapm_widget *w, int event)
1444 const char *ev_name;
1448 case SND_SOC_DAPM_PRE_PMU:
1449 ev_name = "PRE_PMU";
1452 case SND_SOC_DAPM_POST_PMU:
1453 ev_name = "POST_PMU";
1456 case SND_SOC_DAPM_PRE_PMD:
1457 ev_name = "PRE_PMD";
1460 case SND_SOC_DAPM_POST_PMD:
1461 ev_name = "POST_PMD";
1464 case SND_SOC_DAPM_WILL_PMU:
1465 ev_name = "WILL_PMU";
1468 case SND_SOC_DAPM_WILL_PMD:
1469 ev_name = "WILL_PMD";
1473 WARN(1, "Unknown event %d\n", event);
1477 if (w->new_power != power)
1480 if (w->event && (w->event_flags & event)) {
1481 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1483 soc_dapm_async_complete(w->dapm);
1484 trace_snd_soc_dapm_widget_event_start(w, event);
1485 ret = w->event(w, NULL, event);
1486 trace_snd_soc_dapm_widget_event_done(w, event);
1488 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1489 ev_name, w->name, ret);
1493 /* Apply the coalesced changes from a DAPM sequence */
1494 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1495 struct list_head *pending)
1497 struct snd_soc_dapm_context *dapm;
1498 struct snd_soc_dapm_widget *w;
1500 unsigned int value = 0;
1501 unsigned int mask = 0;
1503 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1507 list_for_each_entry(w, pending, power_list) {
1508 WARN_ON(reg != w->reg || dapm != w->dapm);
1509 w->power = w->new_power;
1511 mask |= w->mask << w->shift;
1513 value |= w->on_val << w->shift;
1515 value |= w->off_val << w->shift;
1517 pop_dbg(dapm->dev, card->pop_time,
1518 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1519 w->name, reg, value, mask);
1521 /* Check for events */
1522 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1523 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1527 /* Any widget will do, they should all be updating the
1531 pop_dbg(dapm->dev, card->pop_time,
1532 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1533 value, mask, reg, card->pop_time);
1534 pop_wait(card->pop_time);
1535 soc_dapm_update_bits(dapm, reg, mask, value);
1538 list_for_each_entry(w, pending, power_list) {
1539 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1540 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1544 /* Apply a DAPM power sequence.
1546 * We walk over a pre-sorted list of widgets to apply power to. In
1547 * order to minimise the number of writes to the device required
1548 * multiple widgets will be updated in a single write where possible.
1549 * Currently anything that requires more than a single write is not
1552 static void dapm_seq_run(struct snd_soc_card *card,
1553 struct list_head *list, int event, bool power_up)
1555 struct snd_soc_dapm_widget *w, *n;
1556 struct snd_soc_dapm_context *d;
1559 int cur_subseq = -1;
1560 int cur_reg = SND_SOC_NOPM;
1561 struct snd_soc_dapm_context *cur_dapm = NULL;
1568 sort = dapm_down_seq;
1570 list_for_each_entry_safe(w, n, list, power_list) {
1573 /* Do we need to apply any queued changes? */
1574 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1575 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1576 if (!list_empty(&pending))
1577 dapm_seq_run_coalesced(card, &pending);
1579 if (cur_dapm && cur_dapm->seq_notifier) {
1580 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1581 if (sort[i] == cur_sort)
1582 cur_dapm->seq_notifier(cur_dapm,
1587 if (cur_dapm && w->dapm != cur_dapm)
1588 soc_dapm_async_complete(cur_dapm);
1590 INIT_LIST_HEAD(&pending);
1592 cur_subseq = INT_MIN;
1593 cur_reg = SND_SOC_NOPM;
1598 case snd_soc_dapm_pre:
1600 list_for_each_entry_safe_continue(w, n, list,
1603 if (event == SND_SOC_DAPM_STREAM_START)
1605 NULL, SND_SOC_DAPM_PRE_PMU);
1606 else if (event == SND_SOC_DAPM_STREAM_STOP)
1608 NULL, SND_SOC_DAPM_PRE_PMD);
1611 case snd_soc_dapm_post:
1613 list_for_each_entry_safe_continue(w, n, list,
1616 if (event == SND_SOC_DAPM_STREAM_START)
1618 NULL, SND_SOC_DAPM_POST_PMU);
1619 else if (event == SND_SOC_DAPM_STREAM_STOP)
1621 NULL, SND_SOC_DAPM_POST_PMD);
1625 /* Queue it up for application */
1626 cur_sort = sort[w->id];
1627 cur_subseq = w->subseq;
1630 list_move(&w->power_list, &pending);
1635 dev_err(w->dapm->dev,
1636 "ASoC: Failed to apply widget power: %d\n", ret);
1639 if (!list_empty(&pending))
1640 dapm_seq_run_coalesced(card, &pending);
1642 if (cur_dapm && cur_dapm->seq_notifier) {
1643 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1644 if (sort[i] == cur_sort)
1645 cur_dapm->seq_notifier(cur_dapm,
1649 list_for_each_entry(d, &card->dapm_list, list) {
1650 soc_dapm_async_complete(d);
1654 static void dapm_widget_update(struct snd_soc_card *card)
1656 struct snd_soc_dapm_update *update = card->update;
1657 struct snd_soc_dapm_widget_list *wlist;
1658 struct snd_soc_dapm_widget *w = NULL;
1662 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1665 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1667 for (wi = 0; wi < wlist->num_widgets; wi++) {
1668 w = wlist->widgets[wi];
1670 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1671 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1673 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1681 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1684 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1687 if (update->has_second_set) {
1688 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1689 update->mask2, update->val2);
1691 dev_err(w->dapm->dev,
1692 "ASoC: %s DAPM update failed: %d\n",
1696 for (wi = 0; wi < wlist->num_widgets; wi++) {
1697 w = wlist->widgets[wi];
1699 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1700 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1702 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1708 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1709 * they're changing state.
1711 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1713 struct snd_soc_dapm_context *d = data;
1716 /* If we're off and we're not supposed to go into STANDBY */
1717 if (d->bias_level == SND_SOC_BIAS_OFF &&
1718 d->target_bias_level != SND_SOC_BIAS_OFF) {
1720 pm_runtime_get_sync(d->dev);
1722 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1725 "ASoC: Failed to turn on bias: %d\n", ret);
1728 /* Prepare for a transition to ON or away from ON */
1729 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1730 d->bias_level != SND_SOC_BIAS_ON) ||
1731 (d->target_bias_level != SND_SOC_BIAS_ON &&
1732 d->bias_level == SND_SOC_BIAS_ON)) {
1733 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1736 "ASoC: Failed to prepare bias: %d\n", ret);
1740 /* Async callback run prior to DAPM sequences - brings to their final
1743 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1745 struct snd_soc_dapm_context *d = data;
1748 /* If we just powered the last thing off drop to standby bias */
1749 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1750 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1751 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1752 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1754 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1758 /* If we're in standby and can support bias off then do that */
1759 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1760 d->target_bias_level == SND_SOC_BIAS_OFF) {
1761 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1763 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1767 pm_runtime_put(d->dev);
1770 /* If we just powered up then move to active bias */
1771 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1772 d->target_bias_level == SND_SOC_BIAS_ON) {
1773 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1775 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1780 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1781 bool power, bool connect)
1783 /* If a connection is being made or broken then that update
1784 * will have marked the peer dirty, otherwise the widgets are
1785 * not connected and this update has no impact. */
1789 /* If the peer is already in the state we're moving to then we
1790 * won't have an impact on it. */
1791 if (power != peer->power)
1792 dapm_mark_dirty(peer, "peer state change");
1795 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1796 struct list_head *up_list,
1797 struct list_head *down_list)
1799 struct snd_soc_dapm_path *path;
1801 if (w->power == power)
1804 trace_snd_soc_dapm_widget_power(w, power);
1806 /* If we changed our power state perhaps our neigbours changed
1809 snd_soc_dapm_widget_for_each_source_path(w, path)
1810 dapm_widget_set_peer_power(path->source, power, path->connect);
1812 /* Supplies can't affect their outputs, only their inputs */
1813 if (!w->is_supply) {
1814 snd_soc_dapm_widget_for_each_sink_path(w, path)
1815 dapm_widget_set_peer_power(path->sink, power,
1820 dapm_seq_insert(w, up_list, true);
1822 dapm_seq_insert(w, down_list, false);
1825 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1826 struct list_head *up_list,
1827 struct list_head *down_list)
1832 case snd_soc_dapm_pre:
1833 dapm_seq_insert(w, down_list, false);
1835 case snd_soc_dapm_post:
1836 dapm_seq_insert(w, up_list, true);
1840 power = dapm_widget_power_check(w);
1842 dapm_widget_set_power(w, power, up_list, down_list);
1847 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1849 if (dapm->idle_bias_off)
1852 switch (snd_power_get_state(dapm->card->snd_card)) {
1853 case SNDRV_CTL_POWER_D3hot:
1854 case SNDRV_CTL_POWER_D3cold:
1855 return dapm->suspend_bias_off;
1864 * Scan each dapm widget for complete audio path.
1865 * A complete path is a route that has valid endpoints i.e.:-
1867 * o DAC to output pin.
1868 * o Input pin to ADC.
1869 * o Input pin to Output pin (bypass, sidetone)
1870 * o DAC to ADC (loopback).
1872 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1874 struct snd_soc_dapm_widget *w;
1875 struct snd_soc_dapm_context *d;
1877 LIST_HEAD(down_list);
1878 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1879 enum snd_soc_bias_level bias;
1881 lockdep_assert_held(&card->dapm_mutex);
1883 trace_snd_soc_dapm_start(card);
1885 list_for_each_entry(d, &card->dapm_list, list) {
1886 if (dapm_idle_bias_off(d))
1887 d->target_bias_level = SND_SOC_BIAS_OFF;
1889 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1894 /* Check which widgets we need to power and store them in
1895 * lists indicating if they should be powered up or down. We
1896 * only check widgets that have been flagged as dirty but note
1897 * that new widgets may be added to the dirty list while we
1900 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1901 dapm_power_one_widget(w, &up_list, &down_list);
1904 list_for_each_entry(w, &card->widgets, list) {
1906 case snd_soc_dapm_pre:
1907 case snd_soc_dapm_post:
1908 /* These widgets always need to be powered */
1911 list_del_init(&w->dirty);
1918 /* Supplies and micbiases only bring the
1919 * context up to STANDBY as unless something
1920 * else is active and passing audio they
1921 * generally don't require full power. Signal
1922 * generators are virtual pins and have no
1923 * power impact themselves.
1926 case snd_soc_dapm_siggen:
1927 case snd_soc_dapm_vmid:
1929 case snd_soc_dapm_supply:
1930 case snd_soc_dapm_regulator_supply:
1931 case snd_soc_dapm_pinctrl:
1932 case snd_soc_dapm_clock_supply:
1933 case snd_soc_dapm_micbias:
1934 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1935 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1938 d->target_bias_level = SND_SOC_BIAS_ON;
1945 /* Force all contexts in the card to the same bias state if
1946 * they're not ground referenced.
1948 bias = SND_SOC_BIAS_OFF;
1949 list_for_each_entry(d, &card->dapm_list, list)
1950 if (d->target_bias_level > bias)
1951 bias = d->target_bias_level;
1952 list_for_each_entry(d, &card->dapm_list, list)
1953 if (!dapm_idle_bias_off(d))
1954 d->target_bias_level = bias;
1956 trace_snd_soc_dapm_walk_done(card);
1958 /* Run card bias changes at first */
1959 dapm_pre_sequence_async(&card->dapm, 0);
1960 /* Run other bias changes in parallel */
1961 list_for_each_entry(d, &card->dapm_list, list) {
1962 if (d != &card->dapm)
1963 async_schedule_domain(dapm_pre_sequence_async, d,
1966 async_synchronize_full_domain(&async_domain);
1968 list_for_each_entry(w, &down_list, power_list) {
1969 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1972 list_for_each_entry(w, &up_list, power_list) {
1973 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1976 /* Power down widgets first; try to avoid amplifying pops. */
1977 dapm_seq_run(card, &down_list, event, false);
1979 dapm_widget_update(card);
1982 dapm_seq_run(card, &up_list, event, true);
1984 /* Run all the bias changes in parallel */
1985 list_for_each_entry(d, &card->dapm_list, list) {
1986 if (d != &card->dapm)
1987 async_schedule_domain(dapm_post_sequence_async, d,
1990 async_synchronize_full_domain(&async_domain);
1991 /* Run card bias changes at last */
1992 dapm_post_sequence_async(&card->dapm, 0);
1994 /* do we need to notify any clients that DAPM event is complete */
1995 list_for_each_entry(d, &card->dapm_list, list) {
1996 if (d->stream_event)
1997 d->stream_event(d, event);
2000 pop_dbg(card->dev, card->pop_time,
2001 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2002 pop_wait(card->pop_time);
2004 trace_snd_soc_dapm_done(card);
2009 #ifdef CONFIG_DEBUG_FS
2010 static ssize_t dapm_widget_power_read_file(struct file *file,
2011 char __user *user_buf,
2012 size_t count, loff_t *ppos)
2014 struct snd_soc_dapm_widget *w = file->private_data;
2015 struct snd_soc_card *card = w->dapm->card;
2016 enum snd_soc_dapm_direction dir, rdir;
2020 struct snd_soc_dapm_path *p = NULL;
2022 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2026 mutex_lock(&card->dapm_mutex);
2028 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2033 in = is_connected_input_ep(w, NULL, NULL);
2034 out = is_connected_output_ep(w, NULL, NULL);
2037 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2038 w->name, w->power ? "On" : "Off",
2039 w->force ? " (forced)" : "", in, out);
2042 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2043 " - R%d(0x%x) mask 0x%x",
2044 w->reg, w->reg, w->mask << w->shift);
2046 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
2049 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2051 w->active ? "active" : "inactive");
2053 snd_soc_dapm_for_each_direction(dir) {
2054 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2055 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2056 if (p->connected && !p->connected(p->source, p->sink))
2062 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2063 " %s \"%s\" \"%s\"\n",
2064 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2065 p->name ? p->name : "static",
2066 p->node[rdir]->name);
2070 mutex_unlock(&card->dapm_mutex);
2072 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2078 static const struct file_operations dapm_widget_power_fops = {
2079 .open = simple_open,
2080 .read = dapm_widget_power_read_file,
2081 .llseek = default_llseek,
2084 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2085 size_t count, loff_t *ppos)
2087 struct snd_soc_dapm_context *dapm = file->private_data;
2090 switch (dapm->bias_level) {
2091 case SND_SOC_BIAS_ON:
2094 case SND_SOC_BIAS_PREPARE:
2095 level = "Prepare\n";
2097 case SND_SOC_BIAS_STANDBY:
2098 level = "Standby\n";
2100 case SND_SOC_BIAS_OFF:
2104 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2105 level = "Unknown\n";
2109 return simple_read_from_buffer(user_buf, count, ppos, level,
2113 static const struct file_operations dapm_bias_fops = {
2114 .open = simple_open,
2115 .read = dapm_bias_read_file,
2116 .llseek = default_llseek,
2119 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2120 struct dentry *parent)
2127 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2129 if (!dapm->debugfs_dapm) {
2131 "ASoC: Failed to create DAPM debugfs directory\n");
2135 d = debugfs_create_file("bias_level", 0444,
2136 dapm->debugfs_dapm, dapm,
2140 "ASoC: Failed to create bias level debugfs file\n");
2143 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2145 struct snd_soc_dapm_context *dapm = w->dapm;
2148 if (!dapm->debugfs_dapm || !w->name)
2151 d = debugfs_create_file(w->name, 0444,
2152 dapm->debugfs_dapm, w,
2153 &dapm_widget_power_fops);
2155 dev_warn(w->dapm->dev,
2156 "ASoC: Failed to create %s debugfs file\n",
2160 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2162 debugfs_remove_recursive(dapm->debugfs_dapm);
2166 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2167 struct dentry *parent)
2171 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2175 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2182 * soc_dapm_connect_path() - Connects or disconnects a path
2183 * @path: The path to update
2184 * @connect: The new connect state of the path. True if the path is connected,
2185 * false if it is disconnected.
2186 * @reason: The reason why the path changed (for debugging only)
2188 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2189 bool connect, const char *reason)
2191 if (path->connect == connect)
2194 path->connect = connect;
2195 dapm_mark_dirty(path->source, reason);
2196 dapm_mark_dirty(path->sink, reason);
2197 dapm_path_invalidate(path);
2200 /* test and update the power status of a mux widget */
2201 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2202 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2204 struct snd_soc_dapm_path *path;
2208 lockdep_assert_held(&card->dapm_mutex);
2210 /* find dapm widget path assoc with kcontrol */
2211 dapm_kcontrol_for_each_path(path, kcontrol) {
2213 /* we now need to match the string in the enum to the path */
2214 if (!(strcmp(path->name, e->texts[mux])))
2219 soc_dapm_connect_path(path, connect, "mux update");
2223 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2228 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2229 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2230 struct snd_soc_dapm_update *update)
2232 struct snd_soc_card *card = dapm->card;
2235 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2236 card->update = update;
2237 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2238 card->update = NULL;
2239 mutex_unlock(&card->dapm_mutex);
2241 soc_dpcm_runtime_update(card);
2244 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2246 /* test and update the power status of a mixer or switch widget */
2247 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2248 struct snd_kcontrol *kcontrol,
2249 int connect, int rconnect)
2251 struct snd_soc_dapm_path *path;
2254 lockdep_assert_held(&card->dapm_mutex);
2256 /* find dapm widget path assoc with kcontrol */
2257 dapm_kcontrol_for_each_path(path, kcontrol) {
2259 * Ideally this function should support any number of
2260 * paths and channels. But since kcontrols only come
2261 * in mono and stereo variants, we are limited to 2
2264 * The following code assumes for stereo controls the
2265 * first path (when 'found == 0') is the left channel,
2266 * and all remaining paths (when 'found == 1') are the
2269 * A stereo control is signified by a valid 'rconnect'
2270 * value, either 0 for unconnected, or >= 0 for connected.
2271 * This is chosen instead of using snd_soc_volsw_is_stereo,
2272 * so that the behavior of snd_soc_dapm_mixer_update_power
2273 * doesn't change even when the kcontrol passed in is
2276 * It passes 'connect' as the path connect status for
2277 * the left channel, and 'rconnect' for the right
2280 if (found && rconnect >= 0)
2281 soc_dapm_connect_path(path, rconnect, "mixer update");
2283 soc_dapm_connect_path(path, connect, "mixer update");
2288 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2293 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2294 struct snd_kcontrol *kcontrol, int connect,
2295 struct snd_soc_dapm_update *update)
2297 struct snd_soc_card *card = dapm->card;
2300 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2301 card->update = update;
2302 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2303 card->update = NULL;
2304 mutex_unlock(&card->dapm_mutex);
2306 soc_dpcm_runtime_update(card);
2309 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2311 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2314 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2315 struct snd_soc_dapm_widget *w;
2317 char *state = "not set";
2319 /* card won't be set for the dummy component, as a spot fix
2320 * we're checking for that case specifically here but in future
2321 * we will ensure that the dummy component looks like others.
2326 list_for_each_entry(w, &cmpnt->card->widgets, list) {
2327 if (w->dapm != dapm)
2330 /* only display widgets that burn power */
2332 case snd_soc_dapm_hp:
2333 case snd_soc_dapm_mic:
2334 case snd_soc_dapm_spk:
2335 case snd_soc_dapm_line:
2336 case snd_soc_dapm_micbias:
2337 case snd_soc_dapm_dac:
2338 case snd_soc_dapm_adc:
2339 case snd_soc_dapm_pga:
2340 case snd_soc_dapm_out_drv:
2341 case snd_soc_dapm_mixer:
2342 case snd_soc_dapm_mixer_named_ctl:
2343 case snd_soc_dapm_supply:
2344 case snd_soc_dapm_regulator_supply:
2345 case snd_soc_dapm_pinctrl:
2346 case snd_soc_dapm_clock_supply:
2348 count += sprintf(buf + count, "%s: %s\n",
2349 w->name, w->power ? "On":"Off");
2356 switch (snd_soc_dapm_get_bias_level(dapm)) {
2357 case SND_SOC_BIAS_ON:
2360 case SND_SOC_BIAS_PREPARE:
2363 case SND_SOC_BIAS_STANDBY:
2366 case SND_SOC_BIAS_OFF:
2370 count += sprintf(buf + count, "PM State: %s\n", state);
2375 /* show dapm widget status in sys fs */
2376 static ssize_t dapm_widget_show(struct device *dev,
2377 struct device_attribute *attr, char *buf)
2379 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2382 mutex_lock(&rtd->card->dapm_mutex);
2384 for (i = 0; i < rtd->num_codecs; i++) {
2385 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component;
2387 count += dapm_widget_show_component(cmpnt, buf + count);
2390 mutex_unlock(&rtd->card->dapm_mutex);
2395 static DEVICE_ATTR_RO(dapm_widget);
2397 struct attribute *soc_dapm_dev_attrs[] = {
2398 &dev_attr_dapm_widget.attr,
2402 static void dapm_free_path(struct snd_soc_dapm_path *path)
2404 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2405 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2406 list_del(&path->list_kcontrol);
2407 list_del(&path->list);
2411 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2413 struct snd_soc_dapm_path *p, *next_p;
2414 enum snd_soc_dapm_direction dir;
2418 * remove source and sink paths associated to this widget.
2419 * While removing the path, remove reference to it from both
2420 * source and sink widgets so that path is removed only once.
2422 snd_soc_dapm_for_each_direction(dir) {
2423 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2427 kfree(w->kcontrols);
2428 kfree_const(w->name);
2432 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2434 dapm->path_sink_cache.widget = NULL;
2435 dapm->path_source_cache.widget = NULL;
2438 /* free all dapm widgets and resources */
2439 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2441 struct snd_soc_dapm_widget *w, *next_w;
2443 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2444 if (w->dapm != dapm)
2446 snd_soc_dapm_free_widget(w);
2448 snd_soc_dapm_reset_cache(dapm);
2451 static struct snd_soc_dapm_widget *dapm_find_widget(
2452 struct snd_soc_dapm_context *dapm, const char *pin,
2453 bool search_other_contexts)
2455 struct snd_soc_dapm_widget *w;
2456 struct snd_soc_dapm_widget *fallback = NULL;
2458 list_for_each_entry(w, &dapm->card->widgets, list) {
2459 if (!strcmp(w->name, pin)) {
2460 if (w->dapm == dapm)
2467 if (search_other_contexts)
2473 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2474 const char *pin, int status)
2476 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2478 dapm_assert_locked(dapm);
2481 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2485 if (w->connected != status) {
2486 dapm_mark_dirty(w, "pin configuration");
2487 dapm_widget_invalidate_input_paths(w);
2488 dapm_widget_invalidate_output_paths(w);
2491 w->connected = status;
2499 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2500 * @dapm: DAPM context
2502 * Walks all dapm audio paths and powers widgets according to their
2503 * stream or path usage.
2505 * Requires external locking.
2507 * Returns 0 for success.
2509 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2512 * Suppress early reports (eg, jacks syncing their state) to avoid
2513 * silly DAPM runs during card startup.
2515 if (!dapm->card || !dapm->card->instantiated)
2518 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2520 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2523 * snd_soc_dapm_sync - scan and power dapm paths
2524 * @dapm: DAPM context
2526 * Walks all dapm audio paths and powers widgets according to their
2527 * stream or path usage.
2529 * Returns 0 for success.
2531 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2535 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2536 ret = snd_soc_dapm_sync_unlocked(dapm);
2537 mutex_unlock(&dapm->card->dapm_mutex);
2540 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2543 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2544 * @w: The widget for which to update the flags
2546 * Some widgets have a dynamic category which depends on which neighbors they
2547 * are connected to. This function update the category for these widgets.
2549 * This function must be called whenever a path is added or removed to a widget.
2551 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2553 enum snd_soc_dapm_direction dir;
2554 struct snd_soc_dapm_path *p;
2558 case snd_soc_dapm_input:
2559 /* On a fully routed card an input is never a source */
2560 if (w->dapm->card->fully_routed)
2562 ep = SND_SOC_DAPM_EP_SOURCE;
2563 snd_soc_dapm_widget_for_each_source_path(w, p) {
2564 if (p->source->id == snd_soc_dapm_micbias ||
2565 p->source->id == snd_soc_dapm_mic ||
2566 p->source->id == snd_soc_dapm_line ||
2567 p->source->id == snd_soc_dapm_output) {
2573 case snd_soc_dapm_output:
2574 /* On a fully routed card a output is never a sink */
2575 if (w->dapm->card->fully_routed)
2577 ep = SND_SOC_DAPM_EP_SINK;
2578 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2579 if (p->sink->id == snd_soc_dapm_spk ||
2580 p->sink->id == snd_soc_dapm_hp ||
2581 p->sink->id == snd_soc_dapm_line ||
2582 p->sink->id == snd_soc_dapm_input) {
2588 case snd_soc_dapm_line:
2590 snd_soc_dapm_for_each_direction(dir) {
2591 if (!list_empty(&w->edges[dir]))
2592 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2602 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2603 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2604 const char *control)
2606 bool dynamic_source = false;
2607 bool dynamic_sink = false;
2612 switch (source->id) {
2613 case snd_soc_dapm_demux:
2614 dynamic_source = true;
2621 case snd_soc_dapm_mux:
2622 case snd_soc_dapm_switch:
2623 case snd_soc_dapm_mixer:
2624 case snd_soc_dapm_mixer_named_ctl:
2625 dynamic_sink = true;
2631 if (dynamic_source && dynamic_sink) {
2633 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2634 source->name, control, sink->name);
2636 } else if (!dynamic_source && !dynamic_sink) {
2638 "Control not supported for path %s -> [%s] -> %s\n",
2639 source->name, control, sink->name);
2646 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2647 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2648 const char *control,
2649 int (*connected)(struct snd_soc_dapm_widget *source,
2650 struct snd_soc_dapm_widget *sink))
2652 struct snd_soc_dapm_widget *widgets[2];
2653 enum snd_soc_dapm_direction dir;
2654 struct snd_soc_dapm_path *path;
2657 if (wsink->is_supply && !wsource->is_supply) {
2659 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2660 wsource->name, wsink->name);
2664 if (connected && !wsource->is_supply) {
2666 "connected() callback only supported for supply widgets (%s -> %s)\n",
2667 wsource->name, wsink->name);
2671 if (wsource->is_supply && control) {
2673 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2674 wsource->name, control, wsink->name);
2678 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2682 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2686 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2687 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2688 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2689 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2691 path->connected = connected;
2692 INIT_LIST_HEAD(&path->list);
2693 INIT_LIST_HEAD(&path->list_kcontrol);
2695 if (wsource->is_supply || wsink->is_supply)
2696 path->is_supply = 1;
2698 /* connect static paths */
2699 if (control == NULL) {
2702 switch (wsource->id) {
2703 case snd_soc_dapm_demux:
2704 ret = dapm_connect_mux(dapm, path, control, wsource);
2712 switch (wsink->id) {
2713 case snd_soc_dapm_mux:
2714 ret = dapm_connect_mux(dapm, path, control, wsink);
2718 case snd_soc_dapm_switch:
2719 case snd_soc_dapm_mixer:
2720 case snd_soc_dapm_mixer_named_ctl:
2721 ret = dapm_connect_mixer(dapm, path, control);
2730 list_add(&path->list, &dapm->card->paths);
2731 snd_soc_dapm_for_each_direction(dir)
2732 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2734 snd_soc_dapm_for_each_direction(dir) {
2735 dapm_update_widget_flags(widgets[dir]);
2736 dapm_mark_dirty(widgets[dir], "Route added");
2739 if (dapm->card->instantiated && path->connect)
2740 dapm_path_invalidate(path);
2748 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2749 const struct snd_soc_dapm_route *route)
2751 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2752 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2755 char prefixed_sink[80];
2756 char prefixed_source[80];
2760 prefix = soc_dapm_prefix(dapm);
2762 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2763 prefix, route->sink);
2764 sink = prefixed_sink;
2765 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2766 prefix, route->source);
2767 source = prefixed_source;
2770 source = route->source;
2773 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2774 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2776 if (wsink && wsource)
2780 * find src and dest widgets over all widgets but favor a widget from
2781 * current DAPM context
2783 list_for_each_entry(w, &dapm->card->widgets, list) {
2784 if (!wsink && !(strcmp(w->name, sink))) {
2786 if (w->dapm == dapm) {
2793 if (!wsource && !(strcmp(w->name, source))) {
2795 if (w->dapm == dapm) {
2802 /* use widget from another DAPM context if not found from this */
2808 if (wsource == NULL) {
2809 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2813 if (wsink == NULL) {
2814 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2820 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2821 dapm_wcache_update(&dapm->path_source_cache, wsource);
2823 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2830 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2831 source, route->control, sink);
2835 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2836 const struct snd_soc_dapm_route *route)
2838 struct snd_soc_dapm_widget *wsource, *wsink;
2839 struct snd_soc_dapm_path *path, *p;
2842 char prefixed_sink[80];
2843 char prefixed_source[80];
2846 if (route->control) {
2848 "ASoC: Removal of routes with controls not supported\n");
2852 prefix = soc_dapm_prefix(dapm);
2854 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2855 prefix, route->sink);
2856 sink = prefixed_sink;
2857 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2858 prefix, route->source);
2859 source = prefixed_source;
2862 source = route->source;
2866 list_for_each_entry(p, &dapm->card->paths, list) {
2867 if (strcmp(p->source->name, source) != 0)
2869 if (strcmp(p->sink->name, sink) != 0)
2876 wsource = path->source;
2879 dapm_mark_dirty(wsource, "Route removed");
2880 dapm_mark_dirty(wsink, "Route removed");
2882 dapm_path_invalidate(path);
2884 dapm_free_path(path);
2886 /* Update any path related flags */
2887 dapm_update_widget_flags(wsource);
2888 dapm_update_widget_flags(wsink);
2890 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2898 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2899 * @dapm: DAPM context
2900 * @route: audio routes
2901 * @num: number of routes
2903 * Connects 2 dapm widgets together via a named audio path. The sink is
2904 * the widget receiving the audio signal, whilst the source is the sender
2905 * of the audio signal.
2907 * Returns 0 for success else error. On error all resources can be freed
2908 * with a call to snd_soc_card_free().
2910 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2911 const struct snd_soc_dapm_route *route, int num)
2915 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2916 for (i = 0; i < num; i++) {
2917 r = snd_soc_dapm_add_route(dapm, route);
2919 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2921 route->control ? route->control : "direct",
2927 mutex_unlock(&dapm->card->dapm_mutex);
2931 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2934 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2935 * @dapm: DAPM context
2936 * @route: audio routes
2937 * @num: number of routes
2939 * Removes routes from the DAPM context.
2941 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2942 const struct snd_soc_dapm_route *route, int num)
2946 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2947 for (i = 0; i < num; i++) {
2948 snd_soc_dapm_del_route(dapm, route);
2951 mutex_unlock(&dapm->card->dapm_mutex);
2955 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2957 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2958 const struct snd_soc_dapm_route *route)
2960 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2963 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2966 struct snd_soc_dapm_path *path;
2970 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2976 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2981 if (route->control || route->connected)
2982 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2983 route->source, route->sink);
2985 snd_soc_dapm_widget_for_each_sink_path(source, path) {
2986 if (path->sink == sink) {
2993 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2994 route->source, route->sink);
2996 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2997 count, route->source, route->sink);
3003 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3004 * @dapm: DAPM context
3005 * @route: audio routes
3006 * @num: number of routes
3008 * Mark existing routes matching those specified in the passed array
3009 * as being weak, meaning that they are ignored for the purpose of
3010 * power decisions. The main intended use case is for sidetone paths
3011 * which couple audio between other independent paths if they are both
3012 * active in order to make the combination work better at the user
3013 * level but which aren't intended to be "used".
3015 * Note that CODEC drivers should not use this as sidetone type paths
3016 * can frequently also be used as bypass paths.
3018 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3019 const struct snd_soc_dapm_route *route, int num)
3024 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3025 for (i = 0; i < num; i++) {
3026 err = snd_soc_dapm_weak_route(dapm, route);
3031 mutex_unlock(&dapm->card->dapm_mutex);
3035 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3038 * snd_soc_dapm_new_widgets - add new dapm widgets
3039 * @card: card to be checked for new dapm widgets
3041 * Checks the codec for any new dapm widgets and creates them if found.
3043 * Returns 0 for success.
3045 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3047 struct snd_soc_dapm_widget *w;
3050 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3052 list_for_each_entry(w, &card->widgets, list)
3057 if (w->num_kcontrols) {
3058 w->kcontrols = kcalloc(w->num_kcontrols,
3059 sizeof(struct snd_kcontrol *),
3061 if (!w->kcontrols) {
3062 mutex_unlock(&card->dapm_mutex);
3068 case snd_soc_dapm_switch:
3069 case snd_soc_dapm_mixer:
3070 case snd_soc_dapm_mixer_named_ctl:
3073 case snd_soc_dapm_mux:
3074 case snd_soc_dapm_demux:
3077 case snd_soc_dapm_pga:
3078 case snd_soc_dapm_out_drv:
3081 case snd_soc_dapm_dai_link:
3082 dapm_new_dai_link(w);
3088 /* Read the initial power state from the device */
3090 soc_dapm_read(w->dapm, w->reg, &val);
3091 val = val >> w->shift;
3093 if (val == w->on_val)
3099 dapm_mark_dirty(w, "new widget");
3100 dapm_debugfs_add_widget(w);
3103 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3104 mutex_unlock(&card->dapm_mutex);
3107 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3110 * snd_soc_dapm_get_volsw - dapm mixer get callback
3111 * @kcontrol: mixer control
3112 * @ucontrol: control element information
3114 * Callback to get the value of a dapm mixer control.
3116 * Returns 0 for success.
3118 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3119 struct snd_ctl_elem_value *ucontrol)
3121 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3122 struct snd_soc_card *card = dapm->card;
3123 struct soc_mixer_control *mc =
3124 (struct soc_mixer_control *)kcontrol->private_value;
3126 unsigned int shift = mc->shift;
3128 unsigned int width = fls(max);
3129 unsigned int mask = (1 << fls(max)) - 1;
3130 unsigned int invert = mc->invert;
3131 unsigned int reg_val, val, rval = 0;
3134 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3135 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3136 ret = soc_dapm_read(dapm, reg, ®_val);
3137 val = (reg_val >> shift) & mask;
3139 if (ret == 0 && reg != mc->rreg)
3140 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3142 if (snd_soc_volsw_is_stereo(mc))
3143 rval = (reg_val >> mc->rshift) & mask;
3145 reg_val = dapm_kcontrol_get_value(kcontrol);
3146 val = reg_val & mask;
3148 if (snd_soc_volsw_is_stereo(mc))
3149 rval = (reg_val >> width) & mask;
3151 mutex_unlock(&card->dapm_mutex);
3157 ucontrol->value.integer.value[0] = max - val;
3159 ucontrol->value.integer.value[0] = val;
3161 if (snd_soc_volsw_is_stereo(mc)) {
3163 ucontrol->value.integer.value[1] = max - rval;
3165 ucontrol->value.integer.value[1] = rval;
3170 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3173 * snd_soc_dapm_put_volsw - dapm mixer set callback
3174 * @kcontrol: mixer control
3175 * @ucontrol: control element information
3177 * Callback to set the value of a dapm mixer control.
3179 * Returns 0 for success.
3181 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3182 struct snd_ctl_elem_value *ucontrol)
3184 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3185 struct snd_soc_card *card = dapm->card;
3186 struct soc_mixer_control *mc =
3187 (struct soc_mixer_control *)kcontrol->private_value;
3189 unsigned int shift = mc->shift;
3191 unsigned int width = fls(max);
3192 unsigned int mask = (1 << width) - 1;
3193 unsigned int invert = mc->invert;
3194 unsigned int val, rval = 0;
3195 int connect, rconnect = -1, change, reg_change = 0;
3196 struct snd_soc_dapm_update update = {};
3199 val = (ucontrol->value.integer.value[0] & mask);
3205 if (snd_soc_volsw_is_stereo(mc)) {
3206 rval = (ucontrol->value.integer.value[1] & mask);
3212 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3214 /* This assumes field width < (bits in unsigned int / 2) */
3215 if (width > sizeof(unsigned int) * 8 / 2)
3217 "ASoC: control %s field width limit exceeded\n",
3219 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3221 if (reg != SND_SOC_NOPM) {
3223 rval = rval << mc->rshift;
3225 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3227 if (snd_soc_volsw_is_stereo(mc))
3228 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3233 if (change || reg_change) {
3235 if (snd_soc_volsw_is_stereo(mc)) {
3236 update.has_second_set = true;
3237 update.reg2 = mc->rreg;
3238 update.mask2 = mask << mc->rshift;
3241 update.kcontrol = kcontrol;
3243 update.mask = mask << shift;
3245 card->update = &update;
3247 change |= reg_change;
3249 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3252 card->update = NULL;
3255 mutex_unlock(&card->dapm_mutex);
3258 soc_dpcm_runtime_update(card);
3262 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3265 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3266 * @kcontrol: mixer control
3267 * @ucontrol: control element information
3269 * Callback to get the value of a dapm enumerated double mixer control.
3271 * Returns 0 for success.
3273 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3274 struct snd_ctl_elem_value *ucontrol)
3276 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3277 struct snd_soc_card *card = dapm->card;
3278 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3279 unsigned int reg_val, val;
3281 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3282 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3283 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3285 mutex_unlock(&card->dapm_mutex);
3289 reg_val = dapm_kcontrol_get_value(kcontrol);
3291 mutex_unlock(&card->dapm_mutex);
3293 val = (reg_val >> e->shift_l) & e->mask;
3294 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3295 if (e->shift_l != e->shift_r) {
3296 val = (reg_val >> e->shift_r) & e->mask;
3297 val = snd_soc_enum_val_to_item(e, val);
3298 ucontrol->value.enumerated.item[1] = val;
3303 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3306 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3307 * @kcontrol: mixer control
3308 * @ucontrol: control element information
3310 * Callback to set the value of a dapm enumerated double mixer control.
3312 * Returns 0 for success.
3314 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3315 struct snd_ctl_elem_value *ucontrol)
3317 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3318 struct snd_soc_card *card = dapm->card;
3319 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3320 unsigned int *item = ucontrol->value.enumerated.item;
3321 unsigned int val, change, reg_change = 0;
3323 struct snd_soc_dapm_update update = {};
3326 if (item[0] >= e->items)
3329 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3330 mask = e->mask << e->shift_l;
3331 if (e->shift_l != e->shift_r) {
3332 if (item[1] > e->items)
3334 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3335 mask |= e->mask << e->shift_r;
3338 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3340 change = dapm_kcontrol_set_value(kcontrol, val);
3342 if (e->reg != SND_SOC_NOPM)
3343 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3345 if (change || reg_change) {
3347 update.kcontrol = kcontrol;
3348 update.reg = e->reg;
3351 card->update = &update;
3353 change |= reg_change;
3355 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3357 card->update = NULL;
3360 mutex_unlock(&card->dapm_mutex);
3363 soc_dpcm_runtime_update(card);
3367 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3370 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3372 * @kcontrol: mixer control
3373 * @uinfo: control element information
3375 * Callback to provide information about a pin switch control.
3377 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3378 struct snd_ctl_elem_info *uinfo)
3380 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3382 uinfo->value.integer.min = 0;
3383 uinfo->value.integer.max = 1;
3387 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3390 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3392 * @kcontrol: mixer control
3395 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3396 struct snd_ctl_elem_value *ucontrol)
3398 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3399 const char *pin = (const char *)kcontrol->private_value;
3401 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3403 ucontrol->value.integer.value[0] =
3404 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3406 mutex_unlock(&card->dapm_mutex);
3410 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3413 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3415 * @kcontrol: mixer control
3418 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3419 struct snd_ctl_elem_value *ucontrol)
3421 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3422 const char *pin = (const char *)kcontrol->private_value;