2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/headphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed power down of audio subsystem to reduce pops between a quick
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/clk.h>
39 #include <linux/slab.h>
40 #include <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44 #include <sound/initval.h>
46 #include <trace/events/asoc.h>
48 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
50 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
51 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
53 int (*connected)(struct snd_soc_dapm_widget *source,
54 struct snd_soc_dapm_widget *sink));
55 struct snd_soc_dapm_widget *
56 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
57 const struct snd_soc_dapm_widget *widget);
59 /* dapm power sequences - make this per codec in the future */
60 static int dapm_up_seq[] = {
61 [snd_soc_dapm_pre] = 0,
62 [snd_soc_dapm_regulator_supply] = 1,
63 [snd_soc_dapm_clock_supply] = 1,
64 [snd_soc_dapm_supply] = 2,
65 [snd_soc_dapm_micbias] = 3,
66 [snd_soc_dapm_dai_link] = 2,
67 [snd_soc_dapm_dai_in] = 4,
68 [snd_soc_dapm_dai_out] = 4,
69 [snd_soc_dapm_aif_in] = 4,
70 [snd_soc_dapm_aif_out] = 4,
71 [snd_soc_dapm_mic] = 5,
72 [snd_soc_dapm_mux] = 6,
73 [snd_soc_dapm_demux] = 6,
74 [snd_soc_dapm_dac] = 7,
75 [snd_soc_dapm_switch] = 8,
76 [snd_soc_dapm_mixer] = 8,
77 [snd_soc_dapm_mixer_named_ctl] = 8,
78 [snd_soc_dapm_pga] = 9,
79 [snd_soc_dapm_adc] = 10,
80 [snd_soc_dapm_out_drv] = 11,
81 [snd_soc_dapm_hp] = 11,
82 [snd_soc_dapm_spk] = 11,
83 [snd_soc_dapm_line] = 11,
84 [snd_soc_dapm_kcontrol] = 12,
85 [snd_soc_dapm_post] = 13,
88 static int dapm_down_seq[] = {
89 [snd_soc_dapm_pre] = 0,
90 [snd_soc_dapm_kcontrol] = 1,
91 [snd_soc_dapm_adc] = 2,
92 [snd_soc_dapm_hp] = 3,
93 [snd_soc_dapm_spk] = 3,
94 [snd_soc_dapm_line] = 3,
95 [snd_soc_dapm_out_drv] = 3,
96 [snd_soc_dapm_pga] = 4,
97 [snd_soc_dapm_switch] = 5,
98 [snd_soc_dapm_mixer_named_ctl] = 5,
99 [snd_soc_dapm_mixer] = 5,
100 [snd_soc_dapm_dac] = 6,
101 [snd_soc_dapm_mic] = 7,
102 [snd_soc_dapm_micbias] = 8,
103 [snd_soc_dapm_mux] = 9,
104 [snd_soc_dapm_demux] = 9,
105 [snd_soc_dapm_aif_in] = 10,
106 [snd_soc_dapm_aif_out] = 10,
107 [snd_soc_dapm_dai_in] = 10,
108 [snd_soc_dapm_dai_out] = 10,
109 [snd_soc_dapm_dai_link] = 11,
110 [snd_soc_dapm_supply] = 12,
111 [snd_soc_dapm_clock_supply] = 13,
112 [snd_soc_dapm_regulator_supply] = 13,
113 [snd_soc_dapm_post] = 14,
116 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
118 if (dapm->card && dapm->card->instantiated)
119 lockdep_assert_held(&dapm->card->dapm_mutex);
122 static void pop_wait(u32 pop_time)
125 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
128 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
136 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
141 vsnprintf(buf, PAGE_SIZE, fmt, args);
142 dev_info(dev, "%s", buf);
148 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
150 return !list_empty(&w->dirty);
153 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
155 dapm_assert_locked(w->dapm);
157 if (!dapm_dirty_widget(w)) {
158 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
160 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
165 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of input
167 * @w: The widget for which to invalidate the cached number of input paths
169 * The function resets the cached number of inputs for the specified widget and
170 * all widgets that can be reached via outgoing paths from the widget.
172 * This function must be called if the number of input paths for a widget might
173 * have changed. E.g. if the source state of a widget changes or a path is added
174 * or activated with the widget as the sink.
176 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
178 struct snd_soc_dapm_widget *sink;
179 struct snd_soc_dapm_path *p;
182 dapm_assert_locked(w->dapm);
188 list_add_tail(&w->work_list, &list);
190 list_for_each_entry(w, &list, work_list) {
191 list_for_each_entry(p, &w->sinks, list_source) {
192 if (p->is_supply || p->weak || !p->connect)
195 if (sink->inputs != -1) {
197 list_add_tail(&sink->work_list, &list);
204 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
206 * @w: The widget for which to invalidate the cached number of output paths
208 * Resets the cached number of outputs for the specified widget and all widgets
209 * that can be reached via incoming paths from the widget.
211 * This function must be called if the number of output paths for a widget might
212 * have changed. E.g. if the sink state of a widget changes or a path is added
213 * or activated with the widget as the source.
215 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
217 struct snd_soc_dapm_widget *source;
218 struct snd_soc_dapm_path *p;
221 dapm_assert_locked(w->dapm);
223 if (w->outputs == -1)
227 list_add_tail(&w->work_list, &list);
229 list_for_each_entry(w, &list, work_list) {
230 list_for_each_entry(p, &w->sources, list_sink) {
231 if (p->is_supply || p->weak || !p->connect)
234 if (source->outputs != -1) {
235 source->outputs = -1;
236 list_add_tail(&source->work_list, &list);
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->inputs != 0)
269 dapm_widget_invalidate_input_paths(p->sink);
270 if (p->sink->outputs != 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) {
281 if (w->is_sink || w->is_source) {
282 dapm_mark_dirty(w, "Rechecking endpoints");
284 dapm_widget_invalidate_output_paths(w);
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)
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) {
330 struct snd_soc_dapm_widget template;
332 name = kasprintf(GFP_KERNEL, "%s %s", kcontrol->id.name,
339 memset(&template, 0, sizeof(template));
340 template.reg = mc->reg;
341 template.mask = (1 << fls(mc->max)) - 1;
342 template.shift = mc->shift;
344 template.off_val = mc->max;
346 template.off_val = 0;
347 template.on_val = template.off_val;
348 template.id = snd_soc_dapm_kcontrol;
349 template.name = name;
351 data->value = template.on_val;
354 snd_soc_dapm_new_control_unlocked(widget->dapm,
362 case snd_soc_dapm_demux:
363 case snd_soc_dapm_mux:
364 e = (struct soc_enum *)kcontrol->private_value;
366 if (e->autodisable) {
367 struct snd_soc_dapm_widget template;
369 name = kasprintf(GFP_KERNEL, "%s %s", kcontrol->id.name,
376 memset(&template, 0, sizeof(template));
377 template.reg = e->reg;
378 template.mask = e->mask << e->shift_l;
379 template.shift = e->shift_l;
380 template.off_val = snd_soc_enum_item_to_val(e, 0);
381 template.on_val = template.off_val;
382 template.id = snd_soc_dapm_kcontrol;
383 template.name = name;
385 data->value = template.on_val;
387 data->widget = snd_soc_dapm_new_control(widget->dapm,
394 snd_soc_dapm_add_path(widget->dapm, data->widget,
402 kcontrol->private_data = data;
413 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
415 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
417 kfree(data->widget->name);
422 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
423 const struct snd_kcontrol *kcontrol)
425 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
430 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
431 struct snd_soc_dapm_widget *widget)
433 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
434 struct snd_soc_dapm_widget_list *new_wlist;
438 n = data->wlist->num_widgets + 1;
442 new_wlist = krealloc(data->wlist,
443 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
447 new_wlist->widgets[n - 1] = widget;
448 new_wlist->num_widgets = n;
450 data->wlist = new_wlist;
455 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
456 struct snd_soc_dapm_path *path)
458 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
460 list_add_tail(&path->list_kcontrol, &data->paths);
463 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
465 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
470 return data->widget->power;
473 static struct list_head *dapm_kcontrol_get_path_list(
474 const struct snd_kcontrol *kcontrol)
476 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
481 #define dapm_kcontrol_for_each_path(path, kcontrol) \
482 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
485 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
487 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
491 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
493 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
496 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
498 if (data->value == value)
502 data->widget->on_val = value;
510 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
512 * @kcontrol: The kcontrol
514 * Note: This function must only be used on kcontrols that are known to have
515 * been registered for a CODEC. Otherwise the behaviour is undefined.
517 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
518 struct snd_kcontrol *kcontrol)
520 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
522 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
524 static void dapm_reset(struct snd_soc_card *card)
526 struct snd_soc_dapm_widget *w;
528 lockdep_assert_held(&card->dapm_mutex);
530 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
532 list_for_each_entry(w, &card->widgets, list) {
533 w->new_power = w->power;
534 w->power_checked = false;
538 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
540 if (!dapm->component)
542 return dapm->component->name_prefix;
545 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
548 if (!dapm->component)
550 return snd_soc_component_read(dapm->component, reg, value);
553 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
554 int reg, unsigned int mask, unsigned int value)
556 if (!dapm->component)
558 return snd_soc_component_update_bits(dapm->component, reg,
562 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
563 int reg, unsigned int mask, unsigned int value)
565 if (!dapm->component)
567 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
570 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
573 snd_soc_component_async_complete(dapm->component);
576 static struct snd_soc_dapm_widget *
577 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
579 struct snd_soc_dapm_widget *w = wcache->widget;
580 struct list_head *wlist;
585 wlist = &w->dapm->card->widgets;
587 list_for_each_entry_from(w, wlist, list) {
588 if (!strcmp(name, w->name))
599 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
600 struct snd_soc_dapm_widget *w)
606 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
607 * @dapm: The DAPM context for which to set the level
608 * @level: The level to set
610 * Forces the DAPM bias level to a specific state. It will call the bias level
611 * callback of DAPM context with the specified level. This will even happen if
612 * the context is already at the same level. Furthermore it will not go through
613 * the normal bias level sequencing, meaning any intermediate states between the
614 * current and the target state will not be entered.
616 * Note that the change in bias level is only temporary and the next time
617 * snd_soc_dapm_sync() is called the state will be set to the level as
618 * determined by the DAPM core. The function is mainly intended to be used to
619 * used during probe or resume from suspend to power up the device so
620 * initialization can be done, before the DAPM core takes over.
622 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
623 enum snd_soc_bias_level level)
627 if (dapm->set_bias_level)
628 ret = dapm->set_bias_level(dapm, level);
631 dapm->bias_level = level;
635 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
638 * snd_soc_dapm_set_bias_level - set the bias level for the system
639 * @dapm: DAPM context
640 * @level: level to configure
642 * Configure the bias (power) levels for the SoC audio device.
644 * Returns 0 for success else error.
646 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
647 enum snd_soc_bias_level level)
649 struct snd_soc_card *card = dapm->card;
652 trace_snd_soc_bias_level_start(card, level);
654 if (card && card->set_bias_level)
655 ret = card->set_bias_level(card, dapm, level);
659 if (!card || dapm != &card->dapm)
660 ret = snd_soc_dapm_force_bias_level(dapm, level);
665 if (card && card->set_bias_level_post)
666 ret = card->set_bias_level_post(card, dapm, level);
668 trace_snd_soc_bias_level_done(card, level);
673 /* connect mux widget to its interconnecting audio paths */
674 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
675 struct snd_soc_dapm_path *path, const char *control_name,
676 struct snd_soc_dapm_widget *w)
678 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
679 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
680 unsigned int val, item;
683 if (e->reg != SND_SOC_NOPM) {
684 soc_dapm_read(dapm, e->reg, &val);
685 val = (val >> e->shift_l) & e->mask;
686 item = snd_soc_enum_val_to_item(e, val);
688 /* since a virtual mux has no backing registers to
689 * decide which path to connect, it will try to match
690 * with the first enumeration. This is to ensure
691 * that the default mux choice (the first) will be
692 * correctly powered up during initialization.
697 for (i = 0; i < e->items; i++) {
698 if (!(strcmp(control_name, e->texts[i]))) {
699 path->name = e->texts[i];
711 /* set up initial codec paths */
712 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
714 struct soc_mixer_control *mc = (struct soc_mixer_control *)
715 p->sink->kcontrol_news[i].private_value;
716 unsigned int reg = mc->reg;
717 unsigned int shift = mc->shift;
718 unsigned int max = mc->max;
719 unsigned int mask = (1 << fls(max)) - 1;
720 unsigned int invert = mc->invert;
723 if (reg != SND_SOC_NOPM) {
724 soc_dapm_read(p->sink->dapm, reg, &val);
725 val = (val >> shift) & mask;
734 /* connect mixer widget to its interconnecting audio paths */
735 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
736 struct snd_soc_dapm_path *path, const char *control_name)
740 /* search for mixer kcontrol */
741 for (i = 0; i < path->sink->num_kcontrols; i++) {
742 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
743 path->name = path->sink->kcontrol_news[i].name;
744 dapm_set_mixer_path_status(path, i);
751 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
752 struct snd_soc_dapm_widget *kcontrolw,
753 const struct snd_kcontrol_new *kcontrol_new,
754 struct snd_kcontrol **kcontrol)
756 struct snd_soc_dapm_widget *w;
761 list_for_each_entry(w, &dapm->card->widgets, list) {
762 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
764 for (i = 0; i < w->num_kcontrols; i++) {
765 if (&w->kcontrol_news[i] == kcontrol_new) {
767 *kcontrol = w->kcontrols[i];
777 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
778 * create it. Either way, add the widget into the control's widget list
780 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
783 struct snd_soc_dapm_context *dapm = w->dapm;
784 struct snd_card *card = dapm->card->snd_card;
788 struct snd_kcontrol *kcontrol;
789 bool wname_in_long_name, kcname_in_long_name;
790 char *long_name = NULL;
794 prefix = soc_dapm_prefix(dapm);
796 prefix_len = strlen(prefix) + 1;
800 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
805 wname_in_long_name = false;
806 kcname_in_long_name = true;
809 case snd_soc_dapm_switch:
810 case snd_soc_dapm_mixer:
811 wname_in_long_name = true;
812 kcname_in_long_name = true;
814 case snd_soc_dapm_mixer_named_ctl:
815 wname_in_long_name = false;
816 kcname_in_long_name = true;
818 case snd_soc_dapm_demux:
819 case snd_soc_dapm_mux:
820 wname_in_long_name = true;
821 kcname_in_long_name = false;
828 if (wname_in_long_name && kcname_in_long_name) {
830 * The control will get a prefix from the control
831 * creation process but we're also using the same
832 * prefix for widgets so cut the prefix off the
833 * front of the widget name.
835 long_name = kasprintf(GFP_KERNEL, "%s %s",
836 w->name + prefix_len,
837 w->kcontrol_news[kci].name);
838 if (long_name == NULL)
842 } else if (wname_in_long_name) {
844 name = w->name + prefix_len;
847 name = w->kcontrol_news[kci].name;
850 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
857 kcontrol->private_free = dapm_kcontrol_free;
859 ret = dapm_kcontrol_data_alloc(w, kcontrol);
861 snd_ctl_free_one(kcontrol);
865 ret = snd_ctl_add(card, kcontrol);
868 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
874 ret = dapm_kcontrol_add_widget(kcontrol, w);
876 w->kcontrols[kci] = kcontrol;
884 /* create new dapm mixer control */
885 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
888 struct snd_soc_dapm_path *path;
889 struct dapm_kcontrol_data *data;
892 for (i = 0; i < w->num_kcontrols; i++) {
894 list_for_each_entry(path, &w->sources, list_sink) {
895 /* mixer/mux paths name must match control name */
896 if (path->name != (char *)w->kcontrol_news[i].name)
899 if (!w->kcontrols[i]) {
900 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
905 dapm_kcontrol_add_path(w->kcontrols[i], path);
907 data = snd_kcontrol_chip(w->kcontrols[i]);
909 snd_soc_dapm_add_path(data->widget->dapm,
919 /* create new dapm mux control */
920 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
922 struct snd_soc_dapm_context *dapm = w->dapm;
923 struct snd_soc_dapm_path *path;
924 struct list_head *paths;
929 case snd_soc_dapm_mux:
933 case snd_soc_dapm_demux:
941 if (w->num_kcontrols != 1) {
943 "ASoC: %s %s has incorrect number of controls\n", type,
948 if (list_empty(paths)) {
949 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
953 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
957 if (w->id == snd_soc_dapm_mux) {
958 list_for_each_entry(path, &w->sources, list_sink) {
960 dapm_kcontrol_add_path(w->kcontrols[0], path);
963 list_for_each_entry(path, &w->sinks, list_source) {
965 dapm_kcontrol_add_path(w->kcontrols[0], path);
972 /* create new dapm volume control */
973 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
975 if (w->num_kcontrols)
976 dev_err(w->dapm->dev,
977 "ASoC: PGA controls not supported: '%s'\n", w->name);
982 /* create new dapm dai link control */
983 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
986 struct snd_kcontrol *kcontrol;
987 struct snd_soc_dapm_context *dapm = w->dapm;
988 struct snd_card *card = dapm->card->snd_card;
990 /* create control for links with > 1 config */
991 if (w->num_params <= 1)
995 for (i = 0; i < w->num_kcontrols; i++) {
996 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
998 ret = snd_ctl_add(card, kcontrol);
1001 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1002 w->name, w->kcontrol_news[i].name, ret);
1005 kcontrol->private_data = w;
1006 w->kcontrols[i] = kcontrol;
1012 /* We implement power down on suspend by checking the power state of
1013 * the ALSA card - when we are suspending the ALSA state for the card
1016 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1018 int level = snd_power_get_state(widget->dapm->card->snd_card);
1021 case SNDRV_CTL_POWER_D3hot:
1022 case SNDRV_CTL_POWER_D3cold:
1023 if (widget->ignore_suspend)
1024 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1026 return widget->ignore_suspend;
1032 /* add widget to list if it's not already in the list */
1033 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
1034 struct snd_soc_dapm_widget *w)
1036 struct snd_soc_dapm_widget_list *wlist;
1037 int wlistsize, wlistentries, i;
1044 /* is this widget already in the list */
1045 for (i = 0; i < wlist->num_widgets; i++) {
1046 if (wlist->widgets[i] == w)
1050 /* allocate some new space */
1051 wlistentries = wlist->num_widgets + 1;
1052 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
1053 wlistentries * sizeof(struct snd_soc_dapm_widget *);
1054 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
1055 if (*list == NULL) {
1056 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
1062 /* insert the widget */
1063 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
1064 w->name, wlist->num_widgets);
1066 wlist->widgets[wlist->num_widgets] = w;
1067 wlist->num_widgets++;
1072 * Recursively check for a completed path to an active or physically connected
1073 * output widget. Returns number of complete paths.
1075 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1076 struct snd_soc_dapm_widget_list **list)
1078 struct snd_soc_dapm_path *path;
1081 if (widget->outputs >= 0)
1082 return widget->outputs;
1084 DAPM_UPDATE_STAT(widget, path_checks);
1086 if (widget->is_sink && widget->connected) {
1087 widget->outputs = snd_soc_dapm_suspend_check(widget);
1088 return widget->outputs;
1091 list_for_each_entry(path, &widget->sinks, list_source) {
1092 DAPM_UPDATE_STAT(widget, neighbour_checks);
1094 if (path->weak || path->is_supply)
1100 trace_snd_soc_dapm_output_path(widget, path);
1102 if (path->connect) {
1105 /* do we need to add this widget to the list ? */
1108 err = dapm_list_add_widget(list, path->sink);
1110 dev_err(widget->dapm->dev,
1111 "ASoC: could not add widget %s\n",
1118 con += is_connected_output_ep(path->sink, list);
1124 widget->outputs = con;
1130 * Recursively check for a completed path to an active or physically connected
1131 * input widget. Returns number of complete paths.
1133 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1134 struct snd_soc_dapm_widget_list **list)
1136 struct snd_soc_dapm_path *path;
1139 if (widget->inputs >= 0)
1140 return widget->inputs;
1142 DAPM_UPDATE_STAT(widget, path_checks);
1144 if (widget->is_source && widget->connected) {
1145 widget->inputs = snd_soc_dapm_suspend_check(widget);
1146 return widget->inputs;
1149 list_for_each_entry(path, &widget->sources, list_sink) {
1150 DAPM_UPDATE_STAT(widget, neighbour_checks);
1152 if (path->weak || path->is_supply)
1158 trace_snd_soc_dapm_input_path(widget, path);
1160 if (path->connect) {
1163 /* do we need to add this widget to the list ? */
1166 err = dapm_list_add_widget(list, path->source);
1168 dev_err(widget->dapm->dev,
1169 "ASoC: could not add widget %s\n",
1176 con += is_connected_input_ep(path->source, list);
1182 widget->inputs = con;
1188 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1189 * @dai: the soc DAI.
1190 * @stream: stream direction.
1191 * @list: list of active widgets for this stream.
1193 * Queries DAPM graph as to whether an valid audio stream path exists for
1194 * the initial stream specified by name. This takes into account
1195 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1197 * Returns the number of valid paths or negative error.
1199 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1200 struct snd_soc_dapm_widget_list **list)
1202 struct snd_soc_card *card = dai->component->card;
1203 struct snd_soc_dapm_widget *w;
1206 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1209 * For is_connected_{output,input}_ep fully discover the graph we need
1210 * to reset the cached number of inputs and outputs.
1212 list_for_each_entry(w, &card->widgets, list) {
1217 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1218 paths = is_connected_output_ep(dai->playback_widget, list);
1220 paths = is_connected_input_ep(dai->capture_widget, list);
1222 trace_snd_soc_dapm_connected(paths, stream);
1223 mutex_unlock(&card->dapm_mutex);
1229 * Handler for regulator supply widget.
1231 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1232 struct snd_kcontrol *kcontrol, int event)
1236 soc_dapm_async_complete(w->dapm);
1238 if (SND_SOC_DAPM_EVENT_ON(event)) {
1239 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1240 ret = regulator_allow_bypass(w->regulator, false);
1242 dev_warn(w->dapm->dev,
1243 "ASoC: Failed to unbypass %s: %d\n",
1247 return regulator_enable(w->regulator);
1249 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1250 ret = regulator_allow_bypass(w->regulator, true);
1252 dev_warn(w->dapm->dev,
1253 "ASoC: Failed to bypass %s: %d\n",
1257 return regulator_disable_deferred(w->regulator, w->shift);
1260 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1263 * Handler for clock supply widget.
1265 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1266 struct snd_kcontrol *kcontrol, int event)
1271 soc_dapm_async_complete(w->dapm);
1273 #ifdef CONFIG_HAVE_CLK
1274 if (SND_SOC_DAPM_EVENT_ON(event)) {
1275 return clk_prepare_enable(w->clk);
1277 clk_disable_unprepare(w->clk);
1283 EXPORT_SYMBOL_GPL(dapm_clock_event);
1285 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1287 if (w->power_checked)
1288 return w->new_power;
1293 w->new_power = w->power_check(w);
1295 w->power_checked = true;
1297 return w->new_power;
1300 /* Generic check to see if a widget should be powered.
1302 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1306 DAPM_UPDATE_STAT(w, power_checks);
1308 in = is_connected_input_ep(w, NULL);
1309 out = is_connected_output_ep(w, NULL);
1310 return out != 0 && in != 0;
1313 /* Check to see if a power supply is needed */
1314 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1316 struct snd_soc_dapm_path *path;
1318 DAPM_UPDATE_STAT(w, power_checks);
1320 /* Check if one of our outputs is connected */
1321 list_for_each_entry(path, &w->sinks, list_source) {
1322 DAPM_UPDATE_STAT(w, neighbour_checks);
1327 if (path->connected &&
1328 !path->connected(path->source, path->sink))
1331 if (dapm_widget_power_check(path->sink))
1338 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1343 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1344 struct snd_soc_dapm_widget *b,
1352 sort = dapm_down_seq;
1354 if (sort[a->id] != sort[b->id])
1355 return sort[a->id] - sort[b->id];
1356 if (a->subseq != b->subseq) {
1358 return a->subseq - b->subseq;
1360 return b->subseq - a->subseq;
1362 if (a->reg != b->reg)
1363 return a->reg - b->reg;
1364 if (a->dapm != b->dapm)
1365 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1370 /* Insert a widget in order into a DAPM power sequence. */
1371 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1372 struct list_head *list,
1375 struct snd_soc_dapm_widget *w;
1377 list_for_each_entry(w, list, power_list)
1378 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1379 list_add_tail(&new_widget->power_list, &w->power_list);
1383 list_add_tail(&new_widget->power_list, list);
1386 static void dapm_seq_check_event(struct snd_soc_card *card,
1387 struct snd_soc_dapm_widget *w, int event)
1389 const char *ev_name;
1393 case SND_SOC_DAPM_PRE_PMU:
1394 ev_name = "PRE_PMU";
1397 case SND_SOC_DAPM_POST_PMU:
1398 ev_name = "POST_PMU";
1401 case SND_SOC_DAPM_PRE_PMD:
1402 ev_name = "PRE_PMD";
1405 case SND_SOC_DAPM_POST_PMD:
1406 ev_name = "POST_PMD";
1409 case SND_SOC_DAPM_WILL_PMU:
1410 ev_name = "WILL_PMU";
1413 case SND_SOC_DAPM_WILL_PMD:
1414 ev_name = "WILL_PMD";
1418 WARN(1, "Unknown event %d\n", event);
1422 if (w->new_power != power)
1425 if (w->event && (w->event_flags & event)) {
1426 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1428 soc_dapm_async_complete(w->dapm);
1429 trace_snd_soc_dapm_widget_event_start(w, event);
1430 ret = w->event(w, NULL, event);
1431 trace_snd_soc_dapm_widget_event_done(w, event);
1433 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1434 ev_name, w->name, ret);
1438 /* Apply the coalesced changes from a DAPM sequence */
1439 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1440 struct list_head *pending)
1442 struct snd_soc_dapm_context *dapm;
1443 struct snd_soc_dapm_widget *w;
1445 unsigned int value = 0;
1446 unsigned int mask = 0;
1448 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1452 list_for_each_entry(w, pending, power_list) {
1453 WARN_ON(reg != w->reg || dapm != w->dapm);
1454 w->power = w->new_power;
1456 mask |= w->mask << w->shift;
1458 value |= w->on_val << w->shift;
1460 value |= w->off_val << w->shift;
1462 pop_dbg(dapm->dev, card->pop_time,
1463 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1464 w->name, reg, value, mask);
1466 /* Check for events */
1467 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1468 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1472 /* Any widget will do, they should all be updating the
1476 pop_dbg(dapm->dev, card->pop_time,
1477 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1478 value, mask, reg, card->pop_time);
1479 pop_wait(card->pop_time);
1480 soc_dapm_update_bits(dapm, reg, mask, value);
1483 list_for_each_entry(w, pending, power_list) {
1484 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1485 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1489 /* Apply a DAPM power sequence.
1491 * We walk over a pre-sorted list of widgets to apply power to. In
1492 * order to minimise the number of writes to the device required
1493 * multiple widgets will be updated in a single write where possible.
1494 * Currently anything that requires more than a single write is not
1497 static void dapm_seq_run(struct snd_soc_card *card,
1498 struct list_head *list, int event, bool power_up)
1500 struct snd_soc_dapm_widget *w, *n;
1501 struct snd_soc_dapm_context *d;
1504 int cur_subseq = -1;
1505 int cur_reg = SND_SOC_NOPM;
1506 struct snd_soc_dapm_context *cur_dapm = NULL;
1513 sort = dapm_down_seq;
1515 list_for_each_entry_safe(w, n, list, power_list) {
1518 /* Do we need to apply any queued changes? */
1519 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1520 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1521 if (!list_empty(&pending))
1522 dapm_seq_run_coalesced(card, &pending);
1524 if (cur_dapm && cur_dapm->seq_notifier) {
1525 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1526 if (sort[i] == cur_sort)
1527 cur_dapm->seq_notifier(cur_dapm,
1532 if (cur_dapm && w->dapm != cur_dapm)
1533 soc_dapm_async_complete(cur_dapm);
1535 INIT_LIST_HEAD(&pending);
1537 cur_subseq = INT_MIN;
1538 cur_reg = SND_SOC_NOPM;
1543 case snd_soc_dapm_pre:
1545 list_for_each_entry_safe_continue(w, n, list,
1548 if (event == SND_SOC_DAPM_STREAM_START)
1550 NULL, SND_SOC_DAPM_PRE_PMU);
1551 else if (event == SND_SOC_DAPM_STREAM_STOP)
1553 NULL, SND_SOC_DAPM_PRE_PMD);
1556 case snd_soc_dapm_post:
1558 list_for_each_entry_safe_continue(w, n, list,
1561 if (event == SND_SOC_DAPM_STREAM_START)
1563 NULL, SND_SOC_DAPM_POST_PMU);
1564 else if (event == SND_SOC_DAPM_STREAM_STOP)
1566 NULL, SND_SOC_DAPM_POST_PMD);
1570 /* Queue it up for application */
1571 cur_sort = sort[w->id];
1572 cur_subseq = w->subseq;
1575 list_move(&w->power_list, &pending);
1580 dev_err(w->dapm->dev,
1581 "ASoC: Failed to apply widget power: %d\n", ret);
1584 if (!list_empty(&pending))
1585 dapm_seq_run_coalesced(card, &pending);
1587 if (cur_dapm && cur_dapm->seq_notifier) {
1588 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1589 if (sort[i] == cur_sort)
1590 cur_dapm->seq_notifier(cur_dapm,
1594 list_for_each_entry(d, &card->dapm_list, list) {
1595 soc_dapm_async_complete(d);
1599 static void dapm_widget_update(struct snd_soc_card *card)
1601 struct snd_soc_dapm_update *update = card->update;
1602 struct snd_soc_dapm_widget_list *wlist;
1603 struct snd_soc_dapm_widget *w = NULL;
1607 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1610 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1612 for (wi = 0; wi < wlist->num_widgets; wi++) {
1613 w = wlist->widgets[wi];
1615 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1616 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1618 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1626 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1629 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1632 for (wi = 0; wi < wlist->num_widgets; wi++) {
1633 w = wlist->widgets[wi];
1635 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1636 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1638 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1644 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1645 * they're changing state.
1647 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1649 struct snd_soc_dapm_context *d = data;
1652 /* If we're off and we're not supposed to be go into STANDBY */
1653 if (d->bias_level == SND_SOC_BIAS_OFF &&
1654 d->target_bias_level != SND_SOC_BIAS_OFF) {
1656 pm_runtime_get_sync(d->dev);
1658 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1661 "ASoC: Failed to turn on bias: %d\n", ret);
1664 /* Prepare for a transition to ON or away from ON */
1665 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1666 d->bias_level != SND_SOC_BIAS_ON) ||
1667 (d->target_bias_level != SND_SOC_BIAS_ON &&
1668 d->bias_level == SND_SOC_BIAS_ON)) {
1669 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1672 "ASoC: Failed to prepare bias: %d\n", ret);
1676 /* Async callback run prior to DAPM sequences - brings to their final
1679 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1681 struct snd_soc_dapm_context *d = data;
1684 /* If we just powered the last thing off drop to standby bias */
1685 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1686 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1687 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1688 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1690 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1694 /* If we're in standby and can support bias off then do that */
1695 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1696 d->target_bias_level == SND_SOC_BIAS_OFF) {
1697 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1699 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1703 pm_runtime_put(d->dev);
1706 /* If we just powered up then move to active bias */
1707 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1708 d->target_bias_level == SND_SOC_BIAS_ON) {
1709 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1711 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1716 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1717 bool power, bool connect)
1719 /* If a connection is being made or broken then that update
1720 * will have marked the peer dirty, otherwise the widgets are
1721 * not connected and this update has no impact. */
1725 /* If the peer is already in the state we're moving to then we
1726 * won't have an impact on it. */
1727 if (power != peer->power)
1728 dapm_mark_dirty(peer, "peer state change");
1731 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1732 struct list_head *up_list,
1733 struct list_head *down_list)
1735 struct snd_soc_dapm_path *path;
1737 if (w->power == power)
1740 trace_snd_soc_dapm_widget_power(w, power);
1742 /* If we changed our power state perhaps our neigbours changed
1745 list_for_each_entry(path, &w->sources, list_sink)
1746 dapm_widget_set_peer_power(path->source, power, path->connect);
1748 /* Supplies can't affect their outputs, only their inputs */
1749 if (!w->is_supply) {
1750 list_for_each_entry(path, &w->sinks, list_source)
1751 dapm_widget_set_peer_power(path->sink, power,
1756 dapm_seq_insert(w, up_list, true);
1758 dapm_seq_insert(w, down_list, false);
1761 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1762 struct list_head *up_list,
1763 struct list_head *down_list)
1768 case snd_soc_dapm_pre:
1769 dapm_seq_insert(w, down_list, false);
1771 case snd_soc_dapm_post:
1772 dapm_seq_insert(w, up_list, true);
1776 power = dapm_widget_power_check(w);
1778 dapm_widget_set_power(w, power, up_list, down_list);
1783 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1785 if (dapm->idle_bias_off)
1788 switch (snd_power_get_state(dapm->card->snd_card)) {
1789 case SNDRV_CTL_POWER_D3hot:
1790 case SNDRV_CTL_POWER_D3cold:
1791 return dapm->suspend_bias_off;
1800 * Scan each dapm widget for complete audio path.
1801 * A complete path is a route that has valid endpoints i.e.:-
1803 * o DAC to output pin.
1804 * o Input Pin to ADC.
1805 * o Input pin to Output pin (bypass, sidetone)
1806 * o DAC to ADC (loopback).
1808 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1810 struct snd_soc_dapm_widget *w;
1811 struct snd_soc_dapm_context *d;
1813 LIST_HEAD(down_list);
1814 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1815 enum snd_soc_bias_level bias;
1817 lockdep_assert_held(&card->dapm_mutex);
1819 trace_snd_soc_dapm_start(card);
1821 list_for_each_entry(d, &card->dapm_list, list) {
1822 if (dapm_idle_bias_off(d))
1823 d->target_bias_level = SND_SOC_BIAS_OFF;
1825 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1830 /* Check which widgets we need to power and store them in
1831 * lists indicating if they should be powered up or down. We
1832 * only check widgets that have been flagged as dirty but note
1833 * that new widgets may be added to the dirty list while we
1836 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1837 dapm_power_one_widget(w, &up_list, &down_list);
1840 list_for_each_entry(w, &card->widgets, list) {
1842 case snd_soc_dapm_pre:
1843 case snd_soc_dapm_post:
1844 /* These widgets always need to be powered */
1847 list_del_init(&w->dirty);
1854 /* Supplies and micbiases only bring the
1855 * context up to STANDBY as unless something
1856 * else is active and passing audio they
1857 * generally don't require full power. Signal
1858 * generators are virtual pins and have no
1859 * power impact themselves.
1862 case snd_soc_dapm_siggen:
1863 case snd_soc_dapm_vmid:
1865 case snd_soc_dapm_supply:
1866 case snd_soc_dapm_regulator_supply:
1867 case snd_soc_dapm_clock_supply:
1868 case snd_soc_dapm_micbias:
1869 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1870 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1873 d->target_bias_level = SND_SOC_BIAS_ON;
1880 /* Force all contexts in the card to the same bias state if
1881 * they're not ground referenced.
1883 bias = SND_SOC_BIAS_OFF;
1884 list_for_each_entry(d, &card->dapm_list, list)
1885 if (d->target_bias_level > bias)
1886 bias = d->target_bias_level;
1887 list_for_each_entry(d, &card->dapm_list, list)
1888 if (!dapm_idle_bias_off(d))
1889 d->target_bias_level = bias;
1891 trace_snd_soc_dapm_walk_done(card);
1893 /* Run card bias changes at first */
1894 dapm_pre_sequence_async(&card->dapm, 0);
1895 /* Run other bias changes in parallel */
1896 list_for_each_entry(d, &card->dapm_list, list) {
1897 if (d != &card->dapm)
1898 async_schedule_domain(dapm_pre_sequence_async, d,
1901 async_synchronize_full_domain(&async_domain);
1903 list_for_each_entry(w, &down_list, power_list) {
1904 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1907 list_for_each_entry(w, &up_list, power_list) {
1908 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1911 /* Power down widgets first; try to avoid amplifying pops. */
1912 dapm_seq_run(card, &down_list, event, false);
1914 dapm_widget_update(card);
1917 dapm_seq_run(card, &up_list, event, true);
1919 /* Run all the bias changes in parallel */
1920 list_for_each_entry(d, &card->dapm_list, list) {
1921 if (d != &card->dapm)
1922 async_schedule_domain(dapm_post_sequence_async, d,
1925 async_synchronize_full_domain(&async_domain);
1926 /* Run card bias changes at last */
1927 dapm_post_sequence_async(&card->dapm, 0);
1929 /* do we need to notify any clients that DAPM event is complete */
1930 list_for_each_entry(d, &card->dapm_list, list) {
1931 if (d->stream_event)
1932 d->stream_event(d, event);
1935 pop_dbg(card->dev, card->pop_time,
1936 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1937 pop_wait(card->pop_time);
1939 trace_snd_soc_dapm_done(card);
1944 #ifdef CONFIG_DEBUG_FS
1945 static ssize_t dapm_widget_power_read_file(struct file *file,
1946 char __user *user_buf,
1947 size_t count, loff_t *ppos)
1949 struct snd_soc_dapm_widget *w = file->private_data;
1953 struct snd_soc_dapm_path *p = NULL;
1955 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1959 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1964 in = is_connected_input_ep(w, NULL);
1965 out = is_connected_output_ep(w, NULL);
1968 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1969 w->name, w->power ? "On" : "Off",
1970 w->force ? " (forced)" : "", in, out);
1973 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1974 " - R%d(0x%x) mask 0x%x",
1975 w->reg, w->reg, w->mask << w->shift);
1977 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1980 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1982 w->active ? "active" : "inactive");
1984 list_for_each_entry(p, &w->sources, list_sink) {
1985 if (p->connected && !p->connected(w, p->source))
1989 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1990 " in \"%s\" \"%s\"\n",
1991 p->name ? p->name : "static",
1994 list_for_each_entry(p, &w->sinks, list_source) {
1995 if (p->connected && !p->connected(w, p->sink))
1999 ret += snprintf(buf + ret, PAGE_SIZE - ret,
2000 " out \"%s\" \"%s\"\n",
2001 p->name ? p->name : "static",
2005 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2011 static const struct file_operations dapm_widget_power_fops = {
2012 .open = simple_open,
2013 .read = dapm_widget_power_read_file,
2014 .llseek = default_llseek,
2017 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2018 size_t count, loff_t *ppos)
2020 struct snd_soc_dapm_context *dapm = file->private_data;
2023 switch (dapm->bias_level) {
2024 case SND_SOC_BIAS_ON:
2027 case SND_SOC_BIAS_PREPARE:
2028 level = "Prepare\n";
2030 case SND_SOC_BIAS_STANDBY:
2031 level = "Standby\n";
2033 case SND_SOC_BIAS_OFF:
2037 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2038 level = "Unknown\n";
2042 return simple_read_from_buffer(user_buf, count, ppos, level,
2046 static const struct file_operations dapm_bias_fops = {
2047 .open = simple_open,
2048 .read = dapm_bias_read_file,
2049 .llseek = default_llseek,
2052 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2053 struct dentry *parent)
2060 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2062 if (!dapm->debugfs_dapm) {
2064 "ASoC: Failed to create DAPM debugfs directory\n");
2068 d = debugfs_create_file("bias_level", 0444,
2069 dapm->debugfs_dapm, dapm,
2073 "ASoC: Failed to create bias level debugfs file\n");
2076 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2078 struct snd_soc_dapm_context *dapm = w->dapm;
2081 if (!dapm->debugfs_dapm || !w->name)
2084 d = debugfs_create_file(w->name, 0444,
2085 dapm->debugfs_dapm, w,
2086 &dapm_widget_power_fops);
2088 dev_warn(w->dapm->dev,
2089 "ASoC: Failed to create %s debugfs file\n",
2093 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2095 debugfs_remove_recursive(dapm->debugfs_dapm);
2099 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2100 struct dentry *parent)
2104 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2108 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2115 * soc_dapm_connect_path() - Connects or disconnects a path
2116 * @path: The path to update
2117 * @connect: The new connect state of the path. True if the path is connected,
2118 * false if it is disconneted.
2119 * @reason: The reason why the path changed (for debugging only)
2121 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2122 bool connect, const char *reason)
2124 if (path->connect == connect)
2127 path->connect = connect;
2128 dapm_mark_dirty(path->source, reason);
2129 dapm_mark_dirty(path->sink, reason);
2130 dapm_path_invalidate(path);
2133 /* test and update the power status of a mux widget */
2134 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2135 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2137 struct snd_soc_dapm_path *path;
2141 lockdep_assert_held(&card->dapm_mutex);
2143 /* find dapm widget path assoc with kcontrol */
2144 dapm_kcontrol_for_each_path(path, kcontrol) {
2146 /* we now need to match the string in the enum to the path */
2147 if (!(strcmp(path->name, e->texts[mux])))
2152 soc_dapm_connect_path(path, connect, "mux update");
2156 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2161 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2162 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2163 struct snd_soc_dapm_update *update)
2165 struct snd_soc_card *card = dapm->card;
2168 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2169 card->update = update;
2170 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2171 card->update = NULL;
2172 mutex_unlock(&card->dapm_mutex);
2174 soc_dpcm_runtime_update(card);
2177 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2179 /* test and update the power status of a mixer or switch widget */
2180 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2181 struct snd_kcontrol *kcontrol, int connect)
2183 struct snd_soc_dapm_path *path;
2186 lockdep_assert_held(&card->dapm_mutex);
2188 /* find dapm widget path assoc with kcontrol */
2189 dapm_kcontrol_for_each_path(path, kcontrol) {
2191 soc_dapm_connect_path(path, connect, "mixer update");
2195 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2200 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2201 struct snd_kcontrol *kcontrol, int connect,
2202 struct snd_soc_dapm_update *update)
2204 struct snd_soc_card *card = dapm->card;
2207 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2208 card->update = update;
2209 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2210 card->update = NULL;
2211 mutex_unlock(&card->dapm_mutex);
2213 soc_dpcm_runtime_update(card);
2216 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2218 static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
2220 struct snd_soc_dapm_widget *w;
2222 char *state = "not set";
2224 list_for_each_entry(w, &codec->component.card->widgets, list) {
2225 if (w->dapm != &codec->dapm)
2228 /* only display widgets that burnm power */
2230 case snd_soc_dapm_hp:
2231 case snd_soc_dapm_mic:
2232 case snd_soc_dapm_spk:
2233 case snd_soc_dapm_line:
2234 case snd_soc_dapm_micbias:
2235 case snd_soc_dapm_dac:
2236 case snd_soc_dapm_adc:
2237 case snd_soc_dapm_pga:
2238 case snd_soc_dapm_out_drv:
2239 case snd_soc_dapm_mixer:
2240 case snd_soc_dapm_mixer_named_ctl:
2241 case snd_soc_dapm_supply:
2242 case snd_soc_dapm_regulator_supply:
2243 case snd_soc_dapm_clock_supply:
2245 count += sprintf(buf + count, "%s: %s\n",
2246 w->name, w->power ? "On":"Off");
2253 switch (codec->dapm.bias_level) {
2254 case SND_SOC_BIAS_ON:
2257 case SND_SOC_BIAS_PREPARE:
2260 case SND_SOC_BIAS_STANDBY:
2263 case SND_SOC_BIAS_OFF:
2267 count += sprintf(buf + count, "PM State: %s\n", state);
2272 /* show dapm widget status in sys fs */
2273 static ssize_t dapm_widget_show(struct device *dev,
2274 struct device_attribute *attr, char *buf)
2276 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2279 for (i = 0; i < rtd->num_codecs; i++) {
2280 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2281 count += dapm_widget_show_codec(codec, buf + count);
2287 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2289 struct attribute *soc_dapm_dev_attrs[] = {
2290 &dev_attr_dapm_widget.attr,
2294 static void dapm_free_path(struct snd_soc_dapm_path *path)
2296 list_del(&path->list_sink);
2297 list_del(&path->list_source);
2298 list_del(&path->list_kcontrol);
2299 list_del(&path->list);
2303 /* free all dapm widgets and resources */
2304 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2306 struct snd_soc_dapm_widget *w, *next_w;
2307 struct snd_soc_dapm_path *p, *next_p;
2309 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2310 if (w->dapm != dapm)
2314 * remove source and sink paths associated to this widget.
2315 * While removing the path, remove reference to it from both
2316 * source and sink widgets so that path is removed only once.
2318 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2321 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2324 kfree(w->kcontrols);
2330 static struct snd_soc_dapm_widget *dapm_find_widget(
2331 struct snd_soc_dapm_context *dapm, const char *pin,
2332 bool search_other_contexts)
2334 struct snd_soc_dapm_widget *w;
2335 struct snd_soc_dapm_widget *fallback = NULL;
2337 list_for_each_entry(w, &dapm->card->widgets, list) {
2338 if (!strcmp(w->name, pin)) {
2339 if (w->dapm == dapm)
2346 if (search_other_contexts)
2352 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2353 const char *pin, int status)
2355 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2357 dapm_assert_locked(dapm);
2360 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2364 if (w->connected != status) {
2365 dapm_mark_dirty(w, "pin configuration");
2366 dapm_widget_invalidate_input_paths(w);
2367 dapm_widget_invalidate_output_paths(w);
2370 w->connected = status;
2378 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2379 * @dapm: DAPM context
2381 * Walks all dapm audio paths and powers widgets according to their
2382 * stream or path usage.
2384 * Requires external locking.
2386 * Returns 0 for success.
2388 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2391 * Suppress early reports (eg, jacks syncing their state) to avoid
2392 * silly DAPM runs during card startup.
2394 if (!dapm->card || !dapm->card->instantiated)
2397 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2399 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2402 * snd_soc_dapm_sync - scan and power dapm paths
2403 * @dapm: DAPM context
2405 * Walks all dapm audio paths and powers widgets according to their
2406 * stream or path usage.
2408 * Returns 0 for success.
2410 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2414 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2415 ret = snd_soc_dapm_sync_unlocked(dapm);
2416 mutex_unlock(&dapm->card->dapm_mutex);
2419 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2422 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2423 * @w: The widget for which to update the flags
2425 * Some widgets have a dynamic category which depends on which neighbors they
2426 * are connected to. This function update the category for these widgets.
2428 * This function must be called whenever a path is added or removed to a widget.
2430 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2432 struct snd_soc_dapm_path *p;
2435 case snd_soc_dapm_input:
2436 /* On a fully routed card a input is never a source */
2437 if (w->dapm->card->fully_routed)
2440 list_for_each_entry(p, &w->sources, list_sink) {
2441 if (p->source->id == snd_soc_dapm_micbias ||
2442 p->source->id == snd_soc_dapm_mic ||
2443 p->source->id == snd_soc_dapm_line ||
2444 p->source->id == snd_soc_dapm_output) {
2450 case snd_soc_dapm_output:
2451 /* On a fully routed card a output is never a sink */
2452 if (w->dapm->card->fully_routed)
2455 list_for_each_entry(p, &w->sinks, list_source) {
2456 if (p->sink->id == snd_soc_dapm_spk ||
2457 p->sink->id == snd_soc_dapm_hp ||
2458 p->sink->id == snd_soc_dapm_line ||
2459 p->sink->id == snd_soc_dapm_input) {
2465 case snd_soc_dapm_line:
2466 w->is_sink = !list_empty(&w->sources);
2467 w->is_source = !list_empty(&w->sinks);
2474 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2475 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2476 const char *control)
2478 bool dynamic_source = false;
2479 bool dynamic_sink = false;
2484 switch (source->id) {
2485 case snd_soc_dapm_demux:
2486 dynamic_source = true;
2493 case snd_soc_dapm_mux:
2494 case snd_soc_dapm_switch:
2495 case snd_soc_dapm_mixer:
2496 case snd_soc_dapm_mixer_named_ctl:
2497 dynamic_sink = true;
2503 if (dynamic_source && dynamic_sink) {
2505 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2506 source->name, control, sink->name);
2508 } else if (!dynamic_source && !dynamic_sink) {
2510 "Control not supported for path %s -> [%s] -> %s\n",
2511 source->name, control, sink->name);
2518 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2519 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2520 const char *control,
2521 int (*connected)(struct snd_soc_dapm_widget *source,
2522 struct snd_soc_dapm_widget *sink))
2524 struct snd_soc_dapm_path *path;
2527 if (wsink->is_supply && !wsource->is_supply) {
2529 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2530 wsource->name, wsink->name);
2534 if (connected && !wsource->is_supply) {
2536 "connected() callback only supported for supply widgets (%s -> %s)\n",
2537 wsource->name, wsink->name);
2541 if (wsource->is_supply && control) {
2543 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2544 wsource->name, control, wsink->name);
2548 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2552 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2556 path->source = wsource;
2558 path->connected = connected;
2559 INIT_LIST_HEAD(&path->list);
2560 INIT_LIST_HEAD(&path->list_kcontrol);
2561 INIT_LIST_HEAD(&path->list_source);
2562 INIT_LIST_HEAD(&path->list_sink);
2564 if (wsource->is_supply || wsink->is_supply)
2565 path->is_supply = 1;
2567 /* connect static paths */
2568 if (control == NULL) {
2571 switch (wsource->id) {
2572 case snd_soc_dapm_demux:
2573 ret = dapm_connect_mux(dapm, path, control, wsource);
2581 switch (wsink->id) {
2582 case snd_soc_dapm_mux:
2583 ret = dapm_connect_mux(dapm, path, control, wsink);
2587 case snd_soc_dapm_switch:
2588 case snd_soc_dapm_mixer:
2589 case snd_soc_dapm_mixer_named_ctl:
2590 ret = dapm_connect_mixer(dapm, path, control);
2599 list_add(&path->list, &dapm->card->paths);
2600 list_add(&path->list_sink, &wsink->sources);
2601 list_add(&path->list_source, &wsource->sinks);
2603 dapm_update_widget_flags(wsource);
2604 dapm_update_widget_flags(wsink);
2606 dapm_mark_dirty(wsource, "Route added");
2607 dapm_mark_dirty(wsink, "Route added");
2609 if (dapm->card->instantiated && path->connect)
2610 dapm_path_invalidate(path);
2618 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2619 const struct snd_soc_dapm_route *route)
2621 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2622 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2625 char prefixed_sink[80];
2626 char prefixed_source[80];
2630 prefix = soc_dapm_prefix(dapm);
2632 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2633 prefix, route->sink);
2634 sink = prefixed_sink;
2635 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2636 prefix, route->source);
2637 source = prefixed_source;
2640 source = route->source;
2643 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2644 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2646 if (wsink && wsource)
2650 * find src and dest widgets over all widgets but favor a widget from
2651 * current DAPM context
2653 list_for_each_entry(w, &dapm->card->widgets, list) {
2654 if (!wsink && !(strcmp(w->name, sink))) {
2656 if (w->dapm == dapm) {
2663 if (!wsource && !(strcmp(w->name, source))) {
2665 if (w->dapm == dapm) {
2672 /* use widget from another DAPM context if not found from this */
2678 if (wsource == NULL) {
2679 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2683 if (wsink == NULL) {
2684 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2690 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2691 dapm_wcache_update(&dapm->path_source_cache, wsource);
2693 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2700 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2701 source, route->control, sink);
2705 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2706 const struct snd_soc_dapm_route *route)
2708 struct snd_soc_dapm_widget *wsource, *wsink;
2709 struct snd_soc_dapm_path *path, *p;
2712 char prefixed_sink[80];
2713 char prefixed_source[80];
2716 if (route->control) {
2718 "ASoC: Removal of routes with controls not supported\n");
2722 prefix = soc_dapm_prefix(dapm);
2724 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2725 prefix, route->sink);
2726 sink = prefixed_sink;
2727 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2728 prefix, route->source);
2729 source = prefixed_source;
2732 source = route->source;
2736 list_for_each_entry(p, &dapm->card->paths, list) {
2737 if (strcmp(p->source->name, source) != 0)
2739 if (strcmp(p->sink->name, sink) != 0)
2746 wsource = path->source;
2749 dapm_mark_dirty(wsource, "Route removed");
2750 dapm_mark_dirty(wsink, "Route removed");
2752 dapm_path_invalidate(path);
2754 dapm_free_path(path);
2756 /* Update any path related flags */
2757 dapm_update_widget_flags(wsource);
2758 dapm_update_widget_flags(wsink);
2760 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2768 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2769 * @dapm: DAPM context
2770 * @route: audio routes
2771 * @num: number of routes
2773 * Connects 2 dapm widgets together via a named audio path. The sink is
2774 * the widget receiving the audio signal, whilst the source is the sender
2775 * of the audio signal.
2777 * Returns 0 for success else error. On error all resources can be freed
2778 * with a call to snd_soc_card_free().
2780 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2781 const struct snd_soc_dapm_route *route, int num)
2785 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2786 for (i = 0; i < num; i++) {
2787 r = snd_soc_dapm_add_route(dapm, route);
2789 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2791 route->control ? route->control : "direct",
2797 mutex_unlock(&dapm->card->dapm_mutex);
2801 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2804 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2805 * @dapm: DAPM context
2806 * @route: audio routes
2807 * @num: number of routes
2809 * Removes routes from the DAPM context.
2811 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2812 const struct snd_soc_dapm_route *route, int num)
2816 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2817 for (i = 0; i < num; i++) {
2818 snd_soc_dapm_del_route(dapm, route);
2821 mutex_unlock(&dapm->card->dapm_mutex);
2825 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2827 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2828 const struct snd_soc_dapm_route *route)
2830 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2833 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2836 struct snd_soc_dapm_path *path;
2840 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2846 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2851 if (route->control || route->connected)
2852 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2853 route->source, route->sink);
2855 list_for_each_entry(path, &source->sinks, list_source) {
2856 if (path->sink == sink) {
2863 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2864 route->source, route->sink);
2866 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2867 count, route->source, route->sink);
2873 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2874 * @dapm: DAPM context
2875 * @route: audio routes
2876 * @num: number of routes
2878 * Mark existing routes matching those specified in the passed array
2879 * as being weak, meaning that they are ignored for the purpose of
2880 * power decisions. The main intended use case is for sidetone paths
2881 * which couple audio between other independent paths if they are both
2882 * active in order to make the combination work better at the user
2883 * level but which aren't intended to be "used".
2885 * Note that CODEC drivers should not use this as sidetone type paths
2886 * can frequently also be used as bypass paths.
2888 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2889 const struct snd_soc_dapm_route *route, int num)
2894 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2895 for (i = 0; i < num; i++) {
2896 err = snd_soc_dapm_weak_route(dapm, route);
2901 mutex_unlock(&dapm->card->dapm_mutex);
2905 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2908 * snd_soc_dapm_new_widgets - add new dapm widgets
2909 * @dapm: DAPM context
2911 * Checks the codec for any new dapm widgets and creates them if found.
2913 * Returns 0 for success.
2915 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2917 struct snd_soc_dapm_widget *w;
2920 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2922 list_for_each_entry(w, &card->widgets, list)
2927 if (w->num_kcontrols) {
2928 w->kcontrols = kzalloc(w->num_kcontrols *
2929 sizeof(struct snd_kcontrol *),
2931 if (!w->kcontrols) {
2932 mutex_unlock(&card->dapm_mutex);
2938 case snd_soc_dapm_switch:
2939 case snd_soc_dapm_mixer:
2940 case snd_soc_dapm_mixer_named_ctl:
2943 case snd_soc_dapm_mux:
2944 case snd_soc_dapm_demux:
2947 case snd_soc_dapm_pga:
2948 case snd_soc_dapm_out_drv:
2951 case snd_soc_dapm_dai_link:
2952 dapm_new_dai_link(w);
2958 /* Read the initial power state from the device */
2960 soc_dapm_read(w->dapm, w->reg, &val);
2961 val = val >> w->shift;
2963 if (val == w->on_val)
2969 dapm_mark_dirty(w, "new widget");
2970 dapm_debugfs_add_widget(w);
2973 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2974 mutex_unlock(&card->dapm_mutex);
2977 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2980 * snd_soc_dapm_get_volsw - dapm mixer get callback
2981 * @kcontrol: mixer control
2982 * @ucontrol: control element information
2984 * Callback to get the value of a dapm mixer control.
2986 * Returns 0 for success.
2988 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2989 struct snd_ctl_elem_value *ucontrol)
2991 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2992 struct snd_soc_card *card = dapm->card;
2993 struct soc_mixer_control *mc =
2994 (struct soc_mixer_control *)kcontrol->private_value;
2996 unsigned int shift = mc->shift;
2998 unsigned int mask = (1 << fls(max)) - 1;
2999 unsigned int invert = mc->invert;
3003 if (snd_soc_volsw_is_stereo(mc))
3005 "ASoC: Control '%s' is stereo, which is not supported\n",
3008 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3009 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3010 ret = soc_dapm_read(dapm, reg, &val);
3011 val = (val >> shift) & mask;
3013 val = dapm_kcontrol_get_value(kcontrol);
3015 mutex_unlock(&card->dapm_mutex);
3018 ucontrol->value.integer.value[0] = max - val;
3020 ucontrol->value.integer.value[0] = val;
3024 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3027 * snd_soc_dapm_put_volsw - dapm mixer set callback
3028 * @kcontrol: mixer control
3029 * @ucontrol: control element information
3031 * Callback to set the value of a dapm mixer control.
3033 * Returns 0 for success.
3035 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3036 struct snd_ctl_elem_value *ucontrol)
3038 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3039 struct snd_soc_card *card = dapm->card;
3040 struct soc_mixer_control *mc =
3041 (struct soc_mixer_control *)kcontrol->private_value;
3043 unsigned int shift = mc->shift;
3045 unsigned int mask = (1 << fls(max)) - 1;
3046 unsigned int invert = mc->invert;
3048 int connect, change, reg_change = 0;
3049 struct snd_soc_dapm_update update;
3052 if (snd_soc_volsw_is_stereo(mc))
3054 "ASoC: Control '%s' is stereo, which is not supported\n",
3057 val = (ucontrol->value.integer.value[0] & mask);
3063 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3065 change = dapm_kcontrol_set_value(kcontrol, val);
3067 if (reg != SND_SOC_NOPM) {
3068 mask = mask << shift;
3071 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
3074 if (change || reg_change) {
3076 update.kcontrol = kcontrol;
3080 card->update = &update;
3082 change |= reg_change;
3084 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
3086 card->update = NULL;
3089 mutex_unlock(&card->dapm_mutex);
3092 soc_dpcm_runtime_update(card);
3096 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3099 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3100 * @kcontrol: mixer control
3101 * @ucontrol: control element information
3103 * Callback to get the value of a dapm enumerated double mixer control.
3105 * Returns 0 for success.
3107 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3108 struct snd_ctl_elem_value *ucontrol)
3110 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3111 struct snd_soc_card *card = dapm->card;
3112 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3113 unsigned int reg_val, val;
3115 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3116 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3117 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3119 mutex_unlock(&card->dapm_mutex);
3123 reg_val = dapm_kcontrol_get_value(kcontrol);
3125 mutex_unlock(&card->dapm_mutex);
3127 val = (reg_val >> e->shift_l) & e->mask;
3128 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3129 if (e->shift_l != e->shift_r) {
3130 val = (reg_val >> e->shift_r) & e->mask;
3131 val = snd_soc_enum_val_to_item(e, val);
3132 ucontrol->value.enumerated.item[1] = val;
3137 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3140 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3141 * @kcontrol: mixer control
3142 * @ucontrol: control element information
3144 * Callback to set the value of a dapm enumerated double mixer control.
3146 * Returns 0 for success.
3148 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3149 struct snd_ctl_elem_value *ucontrol)
3151 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3152 struct snd_soc_card *card = dapm->card;
3153 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3154 unsigned int *item = ucontrol->value.enumerated.item;
3155 unsigned int val, change, reg_change = 0;
3157 struct snd_soc_dapm_update update;
3160 if (item[0] >= e->items)
3163 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3164 mask = e->mask << e->shift_l;
3165 if (e->shift_l != e->shift_r) {
3166 if (item[1] > e->items)
3168 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
3169 mask |= e->mask << e->shift_r;
3172 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3174 change = dapm_kcontrol_set_value(kcontrol, val);
3176 if (e->reg != SND_SOC_NOPM)
3177 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3179 if (change || reg_change) {
3181 update.kcontrol = kcontrol;
3182 update.reg = e->reg;
3185 card->update = &update;
3187 change |= reg_change;
3189 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3191 card->update = NULL;
3194 mutex_unlock(&card->dapm_mutex);
3197 soc_dpcm_runtime_update(card);
3201 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3204 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3206 * @kcontrol: mixer control
3207 * @uinfo: control element information
3209 * Callback to provide information about a pin switch control.
3211 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3212 struct snd_ctl_elem_info *uinfo)
3214 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3216 uinfo->value.integer.min = 0;
3217 uinfo->value.integer.max = 1;
3221 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3224 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3226 * @kcontrol: mixer control
3229 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3230 struct snd_ctl_elem_value *ucontrol)
3232 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3233 const char *pin = (const char *)kcontrol->private_value;
3235 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3237 ucontrol->value.integer.value[0] =
3238 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3240 mutex_unlock(&card->dapm_mutex);
3244 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3247 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3249 * @kcontrol: mixer control
3252 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3253 struct snd_ctl_elem_value *ucontrol)
3255 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3256 const char *pin = (const char *)kcontrol->private_value;
3258 if (ucontrol->value.integer.value[0])
3259 snd_soc_dapm_enable_pin(&card->dapm, pin);
3261 snd_soc_dapm_disable_pin(&card->dapm, pin);
3263 snd_soc_dapm_sync(&card->dapm);
3266 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3268 struct snd_soc_dapm_widget *
3269 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3270 const struct snd_soc_dapm_widget *widget)
3272 struct snd_soc_dapm_widget *w;
3274 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3275 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3278 "ASoC: Failed to create DAPM control %s\n",
3281 mutex_unlock(&dapm->card->dapm_mutex);
3285 struct snd_soc_dapm_widget *
3286 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3287 const struct snd_soc_dapm_widget *widget)
3289 struct snd_soc_dapm_widget *w;
3293 if ((w = dapm_cnew_widget(widget)) == NULL)
3297 case snd_soc_dapm_regulator_supply:
3298 w->regulator = devm_regulator_get(dapm->dev, w->name);
3299 if (IS_ERR(w->regulator)) {
3300 ret = PTR_ERR(w->regulator);
3301 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3306 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3307 ret = regulator_allow_bypass(w->regulator, true);
3309 dev_warn(w->dapm->dev,
3310 "ASoC: Failed to bypass %s: %d\n",
3314 case snd_soc_dapm_clock_supply:
3315 #ifdef CONFIG_CLKDEV_LOOKUP
3316 w->clk = devm_clk_get(dapm->dev, w->name);
3317 if (IS_ERR(w->clk)) {
3318 ret = PTR_ERR(w->clk);
3319 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3331 prefix = soc_dapm_prefix(dapm);
3333 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3335 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3337 if (w->name == NULL) {
3343 case snd_soc_dapm_mic:
3345 w->power_check = dapm_generic_check_power;
3347 case snd_soc_dapm_input:
3348 if (!dapm->card->fully_routed)
3350 w->power_check = dapm_generic_check_power;
3352 case snd_soc_dapm_spk:
3353 case snd_soc_dapm_hp:
3355 w->power_check = dapm_generic_check_power;
3357 case snd_soc_dapm_output:
3358 if (!dapm->card->fully_routed)
3360 w->power_check = dapm_generic_check_power;
3362 case snd_soc_dapm_vmid:
3363 case snd_soc_dapm_siggen:
3365 w->power_check = dapm_always_on_check_power;
3367 case snd_soc_dapm_mux:
3368 case snd_soc_dapm_demux:
3369 case snd_soc_dapm_switch:
3370 case snd_soc_dapm_mixer:
3371 case snd_soc_dapm_mixer_named_ctl:
3372 case snd_soc_dapm_adc:
3373 case snd_soc_dapm_aif_out:
3374 case snd_soc_dapm_dac:
3375 case snd_soc_dapm_aif_in:
3376 case snd_soc_dapm_pga:
3377 case snd_soc_dapm_out_drv:
3378 case snd_soc_dapm_micbias:
3379 case snd_soc_dapm_line:
3380 case snd_soc_dapm_dai_link:
3381 case snd_soc_dapm_dai_out:
3382 case snd_soc_dapm_dai_in:
3383 w->power_check = dapm_generic_check_power;
3385 case snd_soc_dapm_supply:
3386 case snd_soc_dapm_regulator_supply:
3387 case snd_soc_dapm_clock_supply:
3388 case snd_soc_dapm_kcontrol:
3390 w->power_check = dapm_supply_check_power;
3393 w->power_check = dapm_always_on_check_power;
3398 INIT_LIST_HEAD(&w->sources);
3399 INIT_LIST_HEAD(&w->sinks);
3400 INIT_LIST_HEAD(&w->list);
3401 INIT_LIST_HEAD(&w->dirty);
3402 list_add_tail(&w->list, &dapm->card->widgets);
3407 /* machine layer set ups unconnected pins and insertions */
3413 * snd_soc_dapm_new_controls - create new dapm controls
3414 * @dapm: DAPM context
3415 * @widget: widget array
3416 * @num: number of widgets
3418 * Creates new DAPM controls based upon the templates.
3420 * Returns 0 for success else error.
3422 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3423 const struct snd_soc_dapm_widget *widget,
3426 struct snd_soc_dapm_widget *w;
3430 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3431 for (i = 0; i < num; i++) {
3432 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3435 "ASoC: Failed to create DAPM control %s\n",
3442 mutex_unlock(&dapm->card->dapm_mutex);
3445 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3447 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3448 struct snd_kcontrol *kcontrol, int event)
3450 struct snd_soc_dapm_path *source_p, *sink_p;
3451 struct snd_soc_dai *source, *sink;
3452 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
3453 struct snd_pcm_substream substream;
3454 struct snd_pcm_hw_params *params = NULL;
3458 if (WARN_ON(!config) ||
3459 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3462 /* We only support a single source and sink, pick the first */
3463 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3465 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3468 if (WARN_ON(!source_p || !sink_p) ||
3469 WARN_ON(!sink_p->source || !source_p->sink) ||
3470 WARN_ON(!source_p->source || !sink_p->sink))
3473 source = source_p->source->priv;
3474 sink = sink_p->sink->priv;
3476 /* Be a little careful as we don't want to overflow the mask array */
3477 if (config->formats) {
3478 fmt = ffs(config->formats) - 1;
3480 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3485 /* Currently very limited parameter selection */
3486 params = kzalloc(sizeof(*params), GFP_KERNEL);
3491 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3493 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3495 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3498 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3499 = config->channels_min;
3500 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3501 = config->channels_max;
3503 memset(&substream, 0, sizeof(substream));
3506 case SND_SOC_DAPM_PRE_PMU:
3507 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3508 ret = soc_dai_hw_params(&substream, params, source);
3512 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3513 ret = soc_dai_hw_params(&substream, params, sink);
3518 case SND_SOC_DAPM_POST_PMU:
3519 ret = snd_soc_dai_digital_mute(sink, 0,
3520 SNDRV_PCM_STREAM_PLAYBACK);
3521 if (ret != 0 && ret != -ENOTSUPP)
3522 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3526 case SND_SOC_DAPM_PRE_PMD:
3527 ret = snd_soc_dai_digital_mute(sink, 1,
3528 SNDRV_PCM_STREAM_PLAYBACK);
3529 if (ret != 0 && ret != -ENOTSUPP)
3530 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3535 WARN(1, "Unknown event %d\n", event);
3544 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3545 struct snd_ctl_elem_value *ucontrol)
3547 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3549 ucontrol->value.integer.value[0] = w->params_select;
3554 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3555 struct snd_ctl_elem_value *ucontrol)
3557 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3559 /* Can't change the config when widget is already powered */
3563 if (ucontrol->value.integer.value[0] == w->params_select)
3566 if (ucontrol->value.integer.value[0] >= w->num_params)
3569 w->params_select = ucontrol->value.integer.value[0];
3574 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3575 const struct snd_soc_pcm_stream *params,
3576 unsigned int num_params,
3577 struct snd_soc_dapm_widget *source,
3578 struct snd_soc_dapm_widget *sink)
3580 struct snd_soc_dapm_widget template;
3581 struct snd_soc_dapm_widget *w;
3584 unsigned long private_value;
3585 const char **w_param_text;
3586 struct soc_enum w_param_enum[] = {
3587 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3589 struct snd_kcontrol_new kcontrol_dai_link[] = {
3590 SOC_ENUM_EXT(NULL, w_param_enum[0],
3591 snd_soc_dapm_dai_link_get,
3592 snd_soc_dapm_dai_link_put),
3594 const struct snd_soc_pcm_stream *config = params;
3596 w_param_text = devm_kcalloc(card->dev, num_params,
3597 sizeof(char *), GFP_KERNEL);
3601 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3602 source->name, sink->name);
3605 goto outfree_w_param;
3608 for (count = 0 ; count < num_params; count++) {
3609 if (!config->stream_name) {
3610 dev_warn(card->dapm.dev,
3611 "ASoC: anonymous config %d for dai link %s\n",
3613 w_param_text[count] =
3614 devm_kasprintf(card->dev, GFP_KERNEL,
3615 "Anonymous Configuration %d",
3617 if (!w_param_text[count]) {
3619 goto outfree_link_name;
3622 w_param_text[count] = devm_kmemdup(card->dev,
3623 config->stream_name,
3624 strlen(config->stream_name) + 1,
3626 if (!w_param_text[count]) {
3628 goto outfree_link_name;
3633 w_param_enum[0].items = num_params;
3634 w_param_enum[0].texts = w_param_text;
3636 memset(&template, 0, sizeof(template));
3637 template.reg = SND_SOC_NOPM;
3638 template.id = snd_soc_dapm_dai_link;
3639 template.name = link_name;
3640 template.event = snd_soc_dai_link_event;
3641 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3642 SND_SOC_DAPM_PRE_PMD;
3643 template.num_kcontrols = 1;
3644 /* duplicate w_param_enum on heap so that memory persists */
3646 (unsigned long) devm_kmemdup(card->dev,
3647 (void *)(kcontrol_dai_link[0].private_value),
3648 sizeof(struct soc_enum), GFP_KERNEL);
3649 if (!private_value) {
3650 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3653 goto outfree_link_name;
3655 kcontrol_dai_link[0].private_value = private_value;
3656 /* duplicate kcontrol_dai_link on heap so that memory persists */
3657 template.kcontrol_news =
3658 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3659 sizeof(struct snd_kcontrol_new),
3661 if (!template.kcontrol_news) {
3662 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3665 goto outfree_private_value;
3668 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3670 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
3672 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3675 goto outfree_kcontrol_news;
3679 w->num_params = num_params;
3681 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3684 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3687 devm_kfree(card->dev, w);
3688 outfree_kcontrol_news:
3689 devm_kfree(card->dev, (void *)template.kcontrol_news);
3690 outfree_private_value:
3691 devm_kfree(card->dev, (void *)private_value);
3693 devm_kfree(card->dev, link_name);
3695 for (count = 0 ; count < num_params; count++)
3696 devm_kfree(card->dev, (void *)w_param_text[count]);
3697 devm_kfree(card->dev, w_param_text);
3702 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3703 struct snd_soc_dai *dai)
3705 struct snd_soc_dapm_widget template;
3706 struct snd_soc_dapm_widget *w;
3708 WARN_ON(dapm->dev != dai->dev);
3710 memset(&template, 0, sizeof(template));
3711 template.reg = SND_SOC_NOPM;
3713 if (dai->driver->playback.stream_name) {
3714 template.id = snd_soc_dapm_dai_in;
3715 template.name = dai->driver->playback.stream_name;
3716 template.sname = dai->driver->playback.stream_name;
3718 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3721 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
3723 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3724 dai->driver->playback.stream_name);
3729 dai->playback_widget = w;
3732 if (dai->driver->capture.stream_name) {
3733 template.id = snd_soc_dapm_dai_out;
3734 template.name = dai->driver->capture.stream_name;
3735 template.sname = dai->driver->capture.stream_name;
3737 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3740 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
3742 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3743 dai->driver->capture.stream_name);
3748 dai->capture_widget = w;
3754 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3756 struct snd_soc_dapm_widget *dai_w, *w;
3757 struct snd_soc_dapm_widget *src, *sink;
3758 struct snd_soc_dai *dai;
3760 /* For each DAI widget... */
3761 list_for_each_entry(dai_w, &card->widgets, list) {
3762 switch (dai_w->id) {
3763 case snd_soc_dapm_dai_in:
3764 case snd_soc_dapm_dai_out:
3772 /* ...find all widgets with the same stream and link them */
3773 list_for_each_entry(w, &card->widgets, list) {
3774 if (w->dapm != dai_w->dapm)
3778 case snd_soc_dapm_dai_in:
3779 case snd_soc_dapm_dai_out:
3785 if (!w->sname || !strstr(w->sname, dai_w->name))
3788 if (dai_w->id == snd_soc_dapm_dai_in) {
3795 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3796 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
3803 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3804 struct snd_soc_pcm_runtime *rtd)
3806 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3807 struct snd_soc_dapm_widget *sink, *source;
3810 for (i = 0; i < rtd->num_codecs; i++) {
3811 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
3813 /* there is no point in connecting BE DAI links with dummies */
3814 if (snd_soc_dai_is_dummy(codec_dai) ||
3815 snd_soc_dai_is_dummy(cpu_dai))
3818 /* connect BE DAI playback if widgets are valid */
3819 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3820 source = cpu_dai->playback_widget;
3821 sink = codec_dai->playback_widget;
3822 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3823 cpu_dai->component->name, source->name,
3824 codec_dai->component->name, sink->name);
3826 snd_soc_dapm_add_path(&card->dapm, source, sink,
3830 /* connect BE DAI capture if widgets are valid */
3831 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3832 source = codec_dai->capture_widget;
3833 sink = cpu_dai->capture_widget;
3834 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3835 codec_dai->component->name, source->name,
3836 cpu_dai->component->name, sink->name);
3838 snd_soc_dapm_add_path(&card->dapm, source, sink,
3844 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3847 struct snd_soc_dapm_widget *w;
3849 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3850 w = dai->playback_widget;
3852 w = dai->capture_widget;
3855 dapm_mark_dirty(w, "stream event");
3858 case SND_SOC_DAPM_STREAM_START:
3861 case SND_SOC_DAPM_STREAM_STOP:
3864 case SND_SOC_DAPM_STREAM_SUSPEND:
3865 case SND_SOC_DAPM_STREAM_RESUME:
3866 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3867 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3871 if (w->id == snd_soc_dapm_dai_in) {
3872 w->is_source = w->active;
3873 dapm_widget_invalidate_input_paths(w);
3875 w->is_sink = w->active;
3876 dapm_widget_invalidate_output_paths(w);
3881 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3883 struct snd_soc_pcm_runtime *rtd = card->rtd;
3886 /* for each BE DAI link... */
3887 for (i = 0; i < card->num_rtd; i++) {
3888 rtd = &card->rtd[i];
3891 * dynamic FE links have no fixed DAI mapping.
3892 * CODEC<->CODEC links have no direct connection.
3894 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3897 dapm_connect_dai_link_widgets(card, rtd);
3901 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3906 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3907 for (i = 0; i < rtd->num_codecs; i++)
3908 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
3910 dapm_power_widgets(rtd->card, event);
3914 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3915 * @rtd: PCM runtime data
3916 * @stream: stream name
3917 * @event: stream event
3919 * Sends a stream event to the dapm core. The core then makes any
3920 * necessary widget power changes.
3922 * Returns 0 for success else error.
3924 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3927 struct snd_soc_card *card = rtd->card;
3929 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3930 soc_dapm_stream_event(rtd, stream, event);
3931 mutex_unlock(&card->dapm_mutex);
3935 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3936 * @dapm: DAPM context
3939 * Enables input/output pin and its parents or children widgets iff there is
3940 * a valid audio route and active audio stream.
3942 * Requires external locking.
3944 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3945 * do any widget power switching.
3947 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3950 return snd_soc_dapm_set_pin(dapm, pin, 1);
3952 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3955 * snd_soc_dapm_enable_pin - enable pin.
3956 * @dapm: DAPM context
3959 * Enables input/output pin and its parents or children widgets iff there is
3960 * a valid audio route and active audio stream.
3962 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3963 * do any widget power switching.
3965 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3969 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3971 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3973 mutex_unlock(&dapm->card->dapm_mutex);
3977 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3980 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3981 * @dapm: DAPM context
3984 * Enables input/output pin regardless of any other state. This is
3985 * intended for use with microphone bias supplies used in microphone
3988 * Requires external locking.
3990 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3991 * do any widget power switching.
3993 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3996 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3999 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4003 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4004 if (!w->connected) {
4006 * w->force does not affect the number of input or output paths,
4007 * so we only have to recheck if w->connected is changed
4009 dapm_widget_invalidate_input_paths(w);
4010 dapm_widget_invalidate_output_paths(w);
4014 dapm_mark_dirty(w, "force enable");
4018 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4021 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4022 * @dapm: DAPM context
4025 * Enables input/output pin regardless of any other state. This is
4026 * intended for use with microphone bias supplies used in microphone
4029 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4030 * do any widget power switching.
4032 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4037 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4039 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4041 mutex_unlock(&dapm->card->dapm_mutex);
4045 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4048 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4049 * @dapm: DAPM context
4052 * Disables input/output pin and its parents or children widgets.
4054 * Requires external locking.
4056 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4057 * do any widget power switching.
4059 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4062 return snd_soc_dapm_set_pin(dapm, pin, 0);
4064 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4067 * snd_soc_dapm_disable_pin - disable pin.
4068 * @dapm: DAPM context
4071 * Disables input/output pin and its parents or children widgets.
4073 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4074 * do any widget power switching.
4076 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4081 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4083 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4085 mutex_unlock(&dapm->card->dapm_mutex);
4089 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4092 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4093 * @dapm: DAPM context
4096 * Marks the specified pin as being not connected, disabling it along
4097 * any parent or child widgets. At present this is identical to
4098 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4099 * additional things such as disabling controls which only affect
4100 * paths through the pin.
4102 * Requires external locking.
4104 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4105 * do any widget power switching.
4107 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4110 return snd_soc_dapm_set_pin(dapm, pin, 0);
4112 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4115 * snd_soc_dapm_nc_pin - permanently disable pin.
4116 * @dapm: DAPM context
4119 * Marks the specified pin as being not connected, disabling it along
4120 * any parent or child widgets. At present this is identical to
4121 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4122 * additional things such as disabling controls which only affect
4123 * paths through the pin.
4125 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4126 * do any widget power switching.
4128 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4132 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4134 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4136 mutex_unlock(&dapm->card->dapm_mutex);
4140 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4143 * snd_soc_dapm_get_pin_status - get audio pin status
4144 * @dapm: DAPM context
4145 * @pin: audio signal pin endpoint (or start point)
4147 * Get audio pin status - connected or disconnected.
4149 * Returns 1 for connected otherwise 0.
4151 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4154 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4157 return w->connected;
4161 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4164 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4165 * @dapm: DAPM context
4166 * @pin: audio signal pin endpoint (or start point)
4168 * Mark the given endpoint or pin as ignoring suspend. When the
4169 * system is disabled a path between two endpoints flagged as ignoring
4170 * suspend will not be disabled. The path must already be enabled via
4171 * normal means at suspend time, it will not be turned on if it was not
4174 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4177 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4180 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4184 w->ignore_suspend = 1;
4188 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4191 * snd_soc_dapm_free - free dapm resources
4192 * @dapm: DAPM context
4194 * Free all dapm widgets and resources.
4196 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4198 dapm_debugfs_cleanup(dapm);
4199 dapm_free_widgets(dapm);
4200 list_del(&dapm->list);
4202 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4204 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4206 struct snd_soc_card *card = dapm->card;
4207 struct snd_soc_dapm_widget *w;
4208 LIST_HEAD(down_list);
4211 mutex_lock(&card->dapm_mutex);
4213 list_for_each_entry(w, &dapm->card->widgets, list) {
4214 if (w->dapm != dapm)
4217 dapm_seq_insert(w, &down_list, false);
4223 /* If there were no widgets to power down we're already in
4227 if (dapm->bias_level == SND_SOC_BIAS_ON)
4228 snd_soc_dapm_set_bias_level(dapm,
4229 SND_SOC_BIAS_PREPARE);
4230 dapm_seq_run(card, &down_list, 0, false);
4231 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4232 snd_soc_dapm_set_bias_level(dapm,
4233 SND_SOC_BIAS_STANDBY);
4236 mutex_unlock(&card->dapm_mutex);
4240 * snd_soc_dapm_shutdown - callback for system shutdown
4242 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4244 struct snd_soc_dapm_context *dapm;
4246 list_for_each_entry(dapm, &card->dapm_list, list) {
4247 if (dapm != &card->dapm) {
4248 soc_dapm_shutdown_dapm(dapm);
4249 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4250 snd_soc_dapm_set_bias_level(dapm,
4255 soc_dapm_shutdown_dapm(&card->dapm);
4256 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4257 snd_soc_dapm_set_bias_level(&card->dapm,
4261 /* Module information */
4262 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4263 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4264 MODULE_LICENSE("GPL");