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 static struct snd_soc_dapm_widget *
56 snd_soc_dapm_new_control(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_dac] = 7,
74 [snd_soc_dapm_switch] = 8,
75 [snd_soc_dapm_mixer] = 8,
76 [snd_soc_dapm_mixer_named_ctl] = 8,
77 [snd_soc_dapm_pga] = 9,
78 [snd_soc_dapm_adc] = 10,
79 [snd_soc_dapm_out_drv] = 11,
80 [snd_soc_dapm_hp] = 11,
81 [snd_soc_dapm_spk] = 11,
82 [snd_soc_dapm_line] = 11,
83 [snd_soc_dapm_kcontrol] = 12,
84 [snd_soc_dapm_post] = 13,
87 static int dapm_down_seq[] = {
88 [snd_soc_dapm_pre] = 0,
89 [snd_soc_dapm_kcontrol] = 1,
90 [snd_soc_dapm_adc] = 2,
91 [snd_soc_dapm_hp] = 3,
92 [snd_soc_dapm_spk] = 3,
93 [snd_soc_dapm_line] = 3,
94 [snd_soc_dapm_out_drv] = 3,
95 [snd_soc_dapm_pga] = 4,
96 [snd_soc_dapm_switch] = 5,
97 [snd_soc_dapm_mixer_named_ctl] = 5,
98 [snd_soc_dapm_mixer] = 5,
99 [snd_soc_dapm_dac] = 6,
100 [snd_soc_dapm_mic] = 7,
101 [snd_soc_dapm_micbias] = 8,
102 [snd_soc_dapm_mux] = 9,
103 [snd_soc_dapm_aif_in] = 10,
104 [snd_soc_dapm_aif_out] = 10,
105 [snd_soc_dapm_dai_in] = 10,
106 [snd_soc_dapm_dai_out] = 10,
107 [snd_soc_dapm_dai_link] = 11,
108 [snd_soc_dapm_supply] = 12,
109 [snd_soc_dapm_clock_supply] = 13,
110 [snd_soc_dapm_regulator_supply] = 13,
111 [snd_soc_dapm_post] = 14,
114 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
116 if (dapm->card && dapm->card->instantiated)
117 lockdep_assert_held(&dapm->card->dapm_mutex);
120 static void pop_wait(u32 pop_time)
123 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
126 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
134 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
139 vsnprintf(buf, PAGE_SIZE, fmt, args);
140 dev_info(dev, "%s", buf);
146 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
148 return !list_empty(&w->dirty);
151 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
153 dapm_assert_locked(w->dapm);
155 if (!dapm_dirty_widget(w)) {
156 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
158 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
163 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of input
165 * @w: The widget for which to invalidate the cached number of input paths
167 * The function resets the cached number of inputs for the specified widget and
168 * all widgets that can be reached via outgoing paths from the widget.
170 * This function must be called if the number of input paths for a widget might
171 * have changed. E.g. if the source state of a widget changes or a path is added
172 * or activated with the widget as the sink.
174 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
176 struct snd_soc_dapm_widget *sink;
177 struct snd_soc_dapm_path *p;
180 dapm_assert_locked(w->dapm);
186 list_add_tail(&w->work_list, &list);
188 list_for_each_entry(w, &list, work_list) {
189 list_for_each_entry(p, &w->sinks, list_source) {
190 if (p->is_supply || p->weak || !p->connect)
193 if (sink->inputs != -1) {
195 list_add_tail(&sink->work_list, &list);
202 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
204 * @w: The widget for which to invalidate the cached number of output paths
206 * Resets the cached number of outputs for the specified widget and all widgets
207 * that can be reached via incoming paths from the widget.
209 * This function must be called if the number of output paths for a widget might
210 * have changed. E.g. if the sink state of a widget changes or a path is added
211 * or activated with the widget as the source.
213 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
215 struct snd_soc_dapm_widget *source;
216 struct snd_soc_dapm_path *p;
219 dapm_assert_locked(w->dapm);
221 if (w->outputs == -1)
225 list_add_tail(&w->work_list, &list);
227 list_for_each_entry(w, &list, work_list) {
228 list_for_each_entry(p, &w->sources, list_sink) {
229 if (p->is_supply || p->weak || !p->connect)
232 if (source->outputs != -1) {
233 source->outputs = -1;
234 list_add_tail(&source->work_list, &list);
241 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
242 * for the widgets connected to a path
243 * @p: The path to invalidate
245 * Resets the cached number of inputs for the sink of the path and the cached
246 * number of outputs for the source of the path.
248 * This function must be called when a path is added, removed or the connected
251 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
254 * Weak paths or supply paths do not influence the number of input or
255 * output paths of their neighbors.
257 if (p->weak || p->is_supply)
261 * The number of connected endpoints is the sum of the number of
262 * connected endpoints of all neighbors. If a node with 0 connected
263 * endpoints is either connected or disconnected that sum won't change,
264 * so there is no need to re-check the path.
266 if (p->source->inputs != 0)
267 dapm_widget_invalidate_input_paths(p->sink);
268 if (p->sink->outputs != 0)
269 dapm_widget_invalidate_output_paths(p->source);
272 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
274 struct snd_soc_dapm_widget *w;
276 mutex_lock(&card->dapm_mutex);
278 list_for_each_entry(w, &card->widgets, list) {
279 if (w->is_sink || w->is_source) {
280 dapm_mark_dirty(w, "Rechecking endpoints");
282 dapm_widget_invalidate_output_paths(w);
284 dapm_widget_invalidate_input_paths(w);
288 mutex_unlock(&card->dapm_mutex);
290 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
292 /* create a new dapm widget */
293 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
294 const struct snd_soc_dapm_widget *_widget)
296 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
299 struct dapm_kcontrol_data {
301 struct snd_soc_dapm_widget *widget;
302 struct list_head paths;
303 struct snd_soc_dapm_widget_list *wlist;
306 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
307 struct snd_kcontrol *kcontrol)
309 struct dapm_kcontrol_data *data;
310 struct soc_mixer_control *mc;
312 data = kzalloc(sizeof(*data), GFP_KERNEL);
314 dev_err(widget->dapm->dev,
315 "ASoC: can't allocate kcontrol data for %s\n",
320 INIT_LIST_HEAD(&data->paths);
322 switch (widget->id) {
323 case snd_soc_dapm_switch:
324 case snd_soc_dapm_mixer:
325 case snd_soc_dapm_mixer_named_ctl:
326 mc = (struct soc_mixer_control *)kcontrol->private_value;
328 if (mc->autodisable) {
329 struct snd_soc_dapm_widget template;
331 memset(&template, 0, sizeof(template));
332 template.reg = mc->reg;
333 template.mask = (1 << fls(mc->max)) - 1;
334 template.shift = mc->shift;
336 template.off_val = mc->max;
338 template.off_val = 0;
339 template.on_val = template.off_val;
340 template.id = snd_soc_dapm_kcontrol;
341 template.name = kcontrol->id.name;
343 data->value = template.on_val;
345 data->widget = snd_soc_dapm_new_control(widget->dapm,
357 kcontrol->private_data = data;
362 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
364 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
369 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
370 const struct snd_kcontrol *kcontrol)
372 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
377 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
378 struct snd_soc_dapm_widget *widget)
380 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
381 struct snd_soc_dapm_widget_list *new_wlist;
385 n = data->wlist->num_widgets + 1;
389 new_wlist = krealloc(data->wlist,
390 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
394 new_wlist->widgets[n - 1] = widget;
395 new_wlist->num_widgets = n;
397 data->wlist = new_wlist;
402 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
403 struct snd_soc_dapm_path *path)
405 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
407 list_add_tail(&path->list_kcontrol, &data->paths);
410 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
411 path->source, NULL, NULL);
415 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
417 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
422 return data->widget->power;
425 static struct list_head *dapm_kcontrol_get_path_list(
426 const struct snd_kcontrol *kcontrol)
428 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
433 #define dapm_kcontrol_for_each_path(path, kcontrol) \
434 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
437 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
439 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
443 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
445 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
448 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
450 if (data->value == value)
454 data->widget->on_val = value;
462 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
464 * @kcontrol: The kcontrol
466 * Note: This function must only be used on kcontrols that are known to have
467 * been registered for a CODEC. Otherwise the behaviour is undefined.
469 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
470 struct snd_kcontrol *kcontrol)
472 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
474 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
476 static void dapm_reset(struct snd_soc_card *card)
478 struct snd_soc_dapm_widget *w;
480 lockdep_assert_held(&card->dapm_mutex);
482 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
484 list_for_each_entry(w, &card->widgets, list) {
485 w->new_power = w->power;
486 w->power_checked = false;
490 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
492 if (!dapm->component)
494 return dapm->component->name_prefix;
497 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
500 if (!dapm->component)
502 return snd_soc_component_read(dapm->component, reg, value);
505 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
506 int reg, unsigned int mask, unsigned int value)
508 if (!dapm->component)
510 return snd_soc_component_update_bits(dapm->component, reg,
514 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
515 int reg, unsigned int mask, unsigned int value)
517 if (!dapm->component)
519 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
522 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
525 snd_soc_component_async_complete(dapm->component);
529 * snd_soc_dapm_set_bias_level - set the bias level for the system
530 * @dapm: DAPM context
531 * @level: level to configure
533 * Configure the bias (power) levels for the SoC audio device.
535 * Returns 0 for success else error.
537 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
538 enum snd_soc_bias_level level)
540 struct snd_soc_card *card = dapm->card;
543 trace_snd_soc_bias_level_start(card, level);
545 if (card && card->set_bias_level)
546 ret = card->set_bias_level(card, dapm, level);
550 if (dapm->set_bias_level)
551 ret = dapm->set_bias_level(dapm, level);
552 else if (!card || dapm != &card->dapm)
553 dapm->bias_level = level;
558 if (card && card->set_bias_level_post)
559 ret = card->set_bias_level_post(card, dapm, level);
561 trace_snd_soc_bias_level_done(card, level);
566 /* connect mux widget to its interconnecting audio paths */
567 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
568 struct snd_soc_dapm_path *path, const char *control_name)
570 const struct snd_kcontrol_new *kcontrol = &path->sink->kcontrol_news[0];
571 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
572 unsigned int val, item;
575 if (e->reg != SND_SOC_NOPM) {
576 soc_dapm_read(dapm, e->reg, &val);
577 val = (val >> e->shift_l) & e->mask;
578 item = snd_soc_enum_val_to_item(e, val);
580 /* since a virtual mux has no backing registers to
581 * decide which path to connect, it will try to match
582 * with the first enumeration. This is to ensure
583 * that the default mux choice (the first) will be
584 * correctly powered up during initialization.
589 for (i = 0; i < e->items; i++) {
590 if (!(strcmp(control_name, e->texts[i]))) {
591 path->name = e->texts[i];
603 /* set up initial codec paths */
604 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
606 struct soc_mixer_control *mc = (struct soc_mixer_control *)
607 p->sink->kcontrol_news[i].private_value;
608 unsigned int reg = mc->reg;
609 unsigned int shift = mc->shift;
610 unsigned int max = mc->max;
611 unsigned int mask = (1 << fls(max)) - 1;
612 unsigned int invert = mc->invert;
615 if (reg != SND_SOC_NOPM) {
616 soc_dapm_read(p->sink->dapm, reg, &val);
617 val = (val >> shift) & mask;
626 /* connect mixer widget to its interconnecting audio paths */
627 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
628 struct snd_soc_dapm_path *path, const char *control_name)
632 /* search for mixer kcontrol */
633 for (i = 0; i < path->sink->num_kcontrols; i++) {
634 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
635 path->name = path->sink->kcontrol_news[i].name;
636 dapm_set_mixer_path_status(path, i);
643 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
644 struct snd_soc_dapm_widget *kcontrolw,
645 const struct snd_kcontrol_new *kcontrol_new,
646 struct snd_kcontrol **kcontrol)
648 struct snd_soc_dapm_widget *w;
653 list_for_each_entry(w, &dapm->card->widgets, list) {
654 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
656 for (i = 0; i < w->num_kcontrols; i++) {
657 if (&w->kcontrol_news[i] == kcontrol_new) {
659 *kcontrol = w->kcontrols[i];
669 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
670 * create it. Either way, add the widget into the control's widget list
672 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
675 struct snd_soc_dapm_context *dapm = w->dapm;
676 struct snd_card *card = dapm->card->snd_card;
680 struct snd_kcontrol *kcontrol;
681 bool wname_in_long_name, kcname_in_long_name;
682 char *long_name = NULL;
686 prefix = soc_dapm_prefix(dapm);
688 prefix_len = strlen(prefix) + 1;
692 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
697 wname_in_long_name = false;
698 kcname_in_long_name = true;
701 case snd_soc_dapm_switch:
702 case snd_soc_dapm_mixer:
703 wname_in_long_name = true;
704 kcname_in_long_name = true;
706 case snd_soc_dapm_mixer_named_ctl:
707 wname_in_long_name = false;
708 kcname_in_long_name = true;
710 case snd_soc_dapm_mux:
711 wname_in_long_name = true;
712 kcname_in_long_name = false;
719 if (wname_in_long_name && kcname_in_long_name) {
721 * The control will get a prefix from the control
722 * creation process but we're also using the same
723 * prefix for widgets so cut the prefix off the
724 * front of the widget name.
726 long_name = kasprintf(GFP_KERNEL, "%s %s",
727 w->name + prefix_len,
728 w->kcontrol_news[kci].name);
729 if (long_name == NULL)
733 } else if (wname_in_long_name) {
735 name = w->name + prefix_len;
738 name = w->kcontrol_news[kci].name;
741 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
748 kcontrol->private_free = dapm_kcontrol_free;
750 ret = dapm_kcontrol_data_alloc(w, kcontrol);
752 snd_ctl_free_one(kcontrol);
756 ret = snd_ctl_add(card, kcontrol);
759 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
765 ret = dapm_kcontrol_add_widget(kcontrol, w);
767 w->kcontrols[kci] = kcontrol;
775 /* create new dapm mixer control */
776 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
779 struct snd_soc_dapm_path *path;
782 for (i = 0; i < w->num_kcontrols; i++) {
784 list_for_each_entry(path, &w->sources, list_sink) {
785 /* mixer/mux paths name must match control name */
786 if (path->name != (char *)w->kcontrol_news[i].name)
789 if (w->kcontrols[i]) {
790 dapm_kcontrol_add_path(w->kcontrols[i], path);
794 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
798 dapm_kcontrol_add_path(w->kcontrols[i], path);
805 /* create new dapm mux control */
806 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
808 struct snd_soc_dapm_context *dapm = w->dapm;
809 struct snd_soc_dapm_path *path;
812 if (w->num_kcontrols != 1) {
814 "ASoC: mux %s has incorrect number of controls\n",
819 if (list_empty(&w->sources)) {
820 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
824 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
828 list_for_each_entry(path, &w->sources, list_sink) {
830 dapm_kcontrol_add_path(w->kcontrols[0], path);
836 /* create new dapm volume control */
837 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
839 if (w->num_kcontrols)
840 dev_err(w->dapm->dev,
841 "ASoC: PGA controls not supported: '%s'\n", w->name);
846 /* create new dapm dai link control */
847 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
850 struct snd_kcontrol *kcontrol;
851 struct snd_soc_dapm_context *dapm = w->dapm;
852 struct snd_card *card = dapm->card->snd_card;
854 /* create control for links with > 1 config */
855 if (w->num_params <= 1)
859 for (i = 0; i < w->num_kcontrols; i++) {
860 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
862 ret = snd_ctl_add(card, kcontrol);
865 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
866 w->name, w->kcontrol_news[i].name, ret);
869 kcontrol->private_data = w;
870 w->kcontrols[i] = kcontrol;
876 /* We implement power down on suspend by checking the power state of
877 * the ALSA card - when we are suspending the ALSA state for the card
880 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
882 int level = snd_power_get_state(widget->dapm->card->snd_card);
885 case SNDRV_CTL_POWER_D3hot:
886 case SNDRV_CTL_POWER_D3cold:
887 if (widget->ignore_suspend)
888 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
890 return widget->ignore_suspend;
896 /* add widget to list if it's not already in the list */
897 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
898 struct snd_soc_dapm_widget *w)
900 struct snd_soc_dapm_widget_list *wlist;
901 int wlistsize, wlistentries, i;
908 /* is this widget already in the list */
909 for (i = 0; i < wlist->num_widgets; i++) {
910 if (wlist->widgets[i] == w)
914 /* allocate some new space */
915 wlistentries = wlist->num_widgets + 1;
916 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
917 wlistentries * sizeof(struct snd_soc_dapm_widget *);
918 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
920 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
926 /* insert the widget */
927 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
928 w->name, wlist->num_widgets);
930 wlist->widgets[wlist->num_widgets] = w;
931 wlist->num_widgets++;
936 * Recursively check for a completed path to an active or physically connected
937 * output widget. Returns number of complete paths.
939 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
940 struct snd_soc_dapm_widget_list **list)
942 struct snd_soc_dapm_path *path;
945 if (widget->outputs >= 0)
946 return widget->outputs;
948 DAPM_UPDATE_STAT(widget, path_checks);
950 if (widget->is_sink && widget->connected) {
951 widget->outputs = snd_soc_dapm_suspend_check(widget);
952 return widget->outputs;
955 list_for_each_entry(path, &widget->sinks, list_source) {
956 DAPM_UPDATE_STAT(widget, neighbour_checks);
958 if (path->weak || path->is_supply)
964 trace_snd_soc_dapm_output_path(widget, path);
969 /* do we need to add this widget to the list ? */
972 err = dapm_list_add_widget(list, path->sink);
974 dev_err(widget->dapm->dev,
975 "ASoC: could not add widget %s\n",
982 con += is_connected_output_ep(path->sink, list);
988 widget->outputs = con;
994 * Recursively check for a completed path to an active or physically connected
995 * input widget. Returns number of complete paths.
997 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
998 struct snd_soc_dapm_widget_list **list)
1000 struct snd_soc_dapm_path *path;
1003 if (widget->inputs >= 0)
1004 return widget->inputs;
1006 DAPM_UPDATE_STAT(widget, path_checks);
1008 if (widget->is_source && widget->connected) {
1009 widget->inputs = snd_soc_dapm_suspend_check(widget);
1010 return widget->inputs;
1013 list_for_each_entry(path, &widget->sources, list_sink) {
1014 DAPM_UPDATE_STAT(widget, neighbour_checks);
1016 if (path->weak || path->is_supply)
1022 trace_snd_soc_dapm_input_path(widget, path);
1024 if (path->connect) {
1027 /* do we need to add this widget to the list ? */
1030 err = dapm_list_add_widget(list, path->source);
1032 dev_err(widget->dapm->dev,
1033 "ASoC: could not add widget %s\n",
1040 con += is_connected_input_ep(path->source, list);
1046 widget->inputs = con;
1052 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1053 * @dai: the soc DAI.
1054 * @stream: stream direction.
1055 * @list: list of active widgets for this stream.
1057 * Queries DAPM graph as to whether an valid audio stream path exists for
1058 * the initial stream specified by name. This takes into account
1059 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1061 * Returns the number of valid paths or negative error.
1063 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1064 struct snd_soc_dapm_widget_list **list)
1066 struct snd_soc_card *card = dai->component->card;
1067 struct snd_soc_dapm_widget *w;
1070 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1073 * For is_connected_{output,input}_ep fully discover the graph we need
1074 * to reset the cached number of inputs and outputs.
1076 list_for_each_entry(w, &card->widgets, list) {
1081 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1082 paths = is_connected_output_ep(dai->playback_widget, list);
1084 paths = is_connected_input_ep(dai->capture_widget, list);
1086 trace_snd_soc_dapm_connected(paths, stream);
1087 mutex_unlock(&card->dapm_mutex);
1093 * Handler for regulator supply widget.
1095 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1096 struct snd_kcontrol *kcontrol, int event)
1100 soc_dapm_async_complete(w->dapm);
1102 if (SND_SOC_DAPM_EVENT_ON(event)) {
1103 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1104 ret = regulator_allow_bypass(w->regulator, false);
1106 dev_warn(w->dapm->dev,
1107 "ASoC: Failed to unbypass %s: %d\n",
1111 return regulator_enable(w->regulator);
1113 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1114 ret = regulator_allow_bypass(w->regulator, true);
1116 dev_warn(w->dapm->dev,
1117 "ASoC: Failed to bypass %s: %d\n",
1121 return regulator_disable_deferred(w->regulator, w->shift);
1124 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1127 * Handler for clock supply widget.
1129 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1130 struct snd_kcontrol *kcontrol, int event)
1135 soc_dapm_async_complete(w->dapm);
1137 #ifdef CONFIG_HAVE_CLK
1138 if (SND_SOC_DAPM_EVENT_ON(event)) {
1139 return clk_prepare_enable(w->clk);
1141 clk_disable_unprepare(w->clk);
1147 EXPORT_SYMBOL_GPL(dapm_clock_event);
1149 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1151 if (w->power_checked)
1152 return w->new_power;
1157 w->new_power = w->power_check(w);
1159 w->power_checked = true;
1161 return w->new_power;
1164 /* Generic check to see if a widget should be powered.
1166 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1170 DAPM_UPDATE_STAT(w, power_checks);
1172 in = is_connected_input_ep(w, NULL);
1173 out = is_connected_output_ep(w, NULL);
1174 return out != 0 && in != 0;
1177 /* Check to see if a power supply is needed */
1178 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1180 struct snd_soc_dapm_path *path;
1182 DAPM_UPDATE_STAT(w, power_checks);
1184 /* Check if one of our outputs is connected */
1185 list_for_each_entry(path, &w->sinks, list_source) {
1186 DAPM_UPDATE_STAT(w, neighbour_checks);
1191 if (path->connected &&
1192 !path->connected(path->source, path->sink))
1195 if (dapm_widget_power_check(path->sink))
1202 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1207 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1208 struct snd_soc_dapm_widget *b,
1216 sort = dapm_down_seq;
1218 if (sort[a->id] != sort[b->id])
1219 return sort[a->id] - sort[b->id];
1220 if (a->subseq != b->subseq) {
1222 return a->subseq - b->subseq;
1224 return b->subseq - a->subseq;
1226 if (a->reg != b->reg)
1227 return a->reg - b->reg;
1228 if (a->dapm != b->dapm)
1229 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1234 /* Insert a widget in order into a DAPM power sequence. */
1235 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1236 struct list_head *list,
1239 struct snd_soc_dapm_widget *w;
1241 list_for_each_entry(w, list, power_list)
1242 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1243 list_add_tail(&new_widget->power_list, &w->power_list);
1247 list_add_tail(&new_widget->power_list, list);
1250 static void dapm_seq_check_event(struct snd_soc_card *card,
1251 struct snd_soc_dapm_widget *w, int event)
1253 const char *ev_name;
1257 case SND_SOC_DAPM_PRE_PMU:
1258 ev_name = "PRE_PMU";
1261 case SND_SOC_DAPM_POST_PMU:
1262 ev_name = "POST_PMU";
1265 case SND_SOC_DAPM_PRE_PMD:
1266 ev_name = "PRE_PMD";
1269 case SND_SOC_DAPM_POST_PMD:
1270 ev_name = "POST_PMD";
1273 case SND_SOC_DAPM_WILL_PMU:
1274 ev_name = "WILL_PMU";
1277 case SND_SOC_DAPM_WILL_PMD:
1278 ev_name = "WILL_PMD";
1282 WARN(1, "Unknown event %d\n", event);
1286 if (w->new_power != power)
1289 if (w->event && (w->event_flags & event)) {
1290 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1292 soc_dapm_async_complete(w->dapm);
1293 trace_snd_soc_dapm_widget_event_start(w, event);
1294 ret = w->event(w, NULL, event);
1295 trace_snd_soc_dapm_widget_event_done(w, event);
1297 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1298 ev_name, w->name, ret);
1302 /* Apply the coalesced changes from a DAPM sequence */
1303 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1304 struct list_head *pending)
1306 struct snd_soc_dapm_context *dapm;
1307 struct snd_soc_dapm_widget *w;
1309 unsigned int value = 0;
1310 unsigned int mask = 0;
1312 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1316 list_for_each_entry(w, pending, power_list) {
1317 WARN_ON(reg != w->reg || dapm != w->dapm);
1318 w->power = w->new_power;
1320 mask |= w->mask << w->shift;
1322 value |= w->on_val << w->shift;
1324 value |= w->off_val << w->shift;
1326 pop_dbg(dapm->dev, card->pop_time,
1327 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1328 w->name, reg, value, mask);
1330 /* Check for events */
1331 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1332 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1336 /* Any widget will do, they should all be updating the
1340 pop_dbg(dapm->dev, card->pop_time,
1341 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1342 value, mask, reg, card->pop_time);
1343 pop_wait(card->pop_time);
1344 soc_dapm_update_bits(dapm, reg, mask, value);
1347 list_for_each_entry(w, pending, power_list) {
1348 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1349 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1353 /* Apply a DAPM power sequence.
1355 * We walk over a pre-sorted list of widgets to apply power to. In
1356 * order to minimise the number of writes to the device required
1357 * multiple widgets will be updated in a single write where possible.
1358 * Currently anything that requires more than a single write is not
1361 static void dapm_seq_run(struct snd_soc_card *card,
1362 struct list_head *list, int event, bool power_up)
1364 struct snd_soc_dapm_widget *w, *n;
1365 struct snd_soc_dapm_context *d;
1368 int cur_subseq = -1;
1369 int cur_reg = SND_SOC_NOPM;
1370 struct snd_soc_dapm_context *cur_dapm = NULL;
1377 sort = dapm_down_seq;
1379 list_for_each_entry_safe(w, n, list, power_list) {
1382 /* Do we need to apply any queued changes? */
1383 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1384 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1385 if (!list_empty(&pending))
1386 dapm_seq_run_coalesced(card, &pending);
1388 if (cur_dapm && cur_dapm->seq_notifier) {
1389 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1390 if (sort[i] == cur_sort)
1391 cur_dapm->seq_notifier(cur_dapm,
1396 if (cur_dapm && w->dapm != cur_dapm)
1397 soc_dapm_async_complete(cur_dapm);
1399 INIT_LIST_HEAD(&pending);
1401 cur_subseq = INT_MIN;
1402 cur_reg = SND_SOC_NOPM;
1407 case snd_soc_dapm_pre:
1409 list_for_each_entry_safe_continue(w, n, list,
1412 if (event == SND_SOC_DAPM_STREAM_START)
1414 NULL, SND_SOC_DAPM_PRE_PMU);
1415 else if (event == SND_SOC_DAPM_STREAM_STOP)
1417 NULL, SND_SOC_DAPM_PRE_PMD);
1420 case snd_soc_dapm_post:
1422 list_for_each_entry_safe_continue(w, n, list,
1425 if (event == SND_SOC_DAPM_STREAM_START)
1427 NULL, SND_SOC_DAPM_POST_PMU);
1428 else if (event == SND_SOC_DAPM_STREAM_STOP)
1430 NULL, SND_SOC_DAPM_POST_PMD);
1434 /* Queue it up for application */
1435 cur_sort = sort[w->id];
1436 cur_subseq = w->subseq;
1439 list_move(&w->power_list, &pending);
1444 dev_err(w->dapm->dev,
1445 "ASoC: Failed to apply widget power: %d\n", ret);
1448 if (!list_empty(&pending))
1449 dapm_seq_run_coalesced(card, &pending);
1451 if (cur_dapm && cur_dapm->seq_notifier) {
1452 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1453 if (sort[i] == cur_sort)
1454 cur_dapm->seq_notifier(cur_dapm,
1458 list_for_each_entry(d, &card->dapm_list, list) {
1459 soc_dapm_async_complete(d);
1463 static void dapm_widget_update(struct snd_soc_card *card)
1465 struct snd_soc_dapm_update *update = card->update;
1466 struct snd_soc_dapm_widget_list *wlist;
1467 struct snd_soc_dapm_widget *w = NULL;
1471 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1474 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1476 for (wi = 0; wi < wlist->num_widgets; wi++) {
1477 w = wlist->widgets[wi];
1479 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1480 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1482 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1490 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1493 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1496 for (wi = 0; wi < wlist->num_widgets; wi++) {
1497 w = wlist->widgets[wi];
1499 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1500 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1502 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1508 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1509 * they're changing state.
1511 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1513 struct snd_soc_dapm_context *d = data;
1516 /* If we're off and we're not supposed to be go into STANDBY */
1517 if (d->bias_level == SND_SOC_BIAS_OFF &&
1518 d->target_bias_level != SND_SOC_BIAS_OFF) {
1520 pm_runtime_get_sync(d->dev);
1522 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1525 "ASoC: Failed to turn on bias: %d\n", ret);
1528 /* Prepare for a transition to ON or away from ON */
1529 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1530 d->bias_level != SND_SOC_BIAS_ON) ||
1531 (d->target_bias_level != SND_SOC_BIAS_ON &&
1532 d->bias_level == SND_SOC_BIAS_ON)) {
1533 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1536 "ASoC: Failed to prepare bias: %d\n", ret);
1540 /* Async callback run prior to DAPM sequences - brings to their final
1543 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1545 struct snd_soc_dapm_context *d = data;
1548 /* If we just powered the last thing off drop to standby bias */
1549 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1550 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1551 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1552 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1554 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1558 /* If we're in standby and can support bias off then do that */
1559 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1560 d->target_bias_level == SND_SOC_BIAS_OFF) {
1561 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1563 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1567 pm_runtime_put(d->dev);
1570 /* If we just powered up then move to active bias */
1571 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1572 d->target_bias_level == SND_SOC_BIAS_ON) {
1573 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1575 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1580 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1581 bool power, bool connect)
1583 /* If a connection is being made or broken then that update
1584 * will have marked the peer dirty, otherwise the widgets are
1585 * not connected and this update has no impact. */
1589 /* If the peer is already in the state we're moving to then we
1590 * won't have an impact on it. */
1591 if (power != peer->power)
1592 dapm_mark_dirty(peer, "peer state change");
1595 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1596 struct list_head *up_list,
1597 struct list_head *down_list)
1599 struct snd_soc_dapm_path *path;
1601 if (w->power == power)
1604 trace_snd_soc_dapm_widget_power(w, power);
1606 /* If we changed our power state perhaps our neigbours changed
1609 list_for_each_entry(path, &w->sources, list_sink)
1610 dapm_widget_set_peer_power(path->source, power, path->connect);
1612 /* Supplies can't affect their outputs, only their inputs */
1613 if (!w->is_supply) {
1614 list_for_each_entry(path, &w->sinks, list_source)
1615 dapm_widget_set_peer_power(path->sink, power,
1620 dapm_seq_insert(w, up_list, true);
1622 dapm_seq_insert(w, down_list, false);
1625 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1626 struct list_head *up_list,
1627 struct list_head *down_list)
1632 case snd_soc_dapm_pre:
1633 dapm_seq_insert(w, down_list, false);
1635 case snd_soc_dapm_post:
1636 dapm_seq_insert(w, up_list, true);
1640 power = dapm_widget_power_check(w);
1642 dapm_widget_set_power(w, power, up_list, down_list);
1647 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1649 if (dapm->idle_bias_off)
1652 switch (snd_power_get_state(dapm->card->snd_card)) {
1653 case SNDRV_CTL_POWER_D3hot:
1654 case SNDRV_CTL_POWER_D3cold:
1655 return dapm->suspend_bias_off;
1664 * Scan each dapm widget for complete audio path.
1665 * A complete path is a route that has valid endpoints i.e.:-
1667 * o DAC to output pin.
1668 * o Input Pin to ADC.
1669 * o Input pin to Output pin (bypass, sidetone)
1670 * o DAC to ADC (loopback).
1672 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1674 struct snd_soc_dapm_widget *w;
1675 struct snd_soc_dapm_context *d;
1677 LIST_HEAD(down_list);
1678 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1679 enum snd_soc_bias_level bias;
1681 lockdep_assert_held(&card->dapm_mutex);
1683 trace_snd_soc_dapm_start(card);
1685 list_for_each_entry(d, &card->dapm_list, list) {
1686 if (dapm_idle_bias_off(d))
1687 d->target_bias_level = SND_SOC_BIAS_OFF;
1689 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1694 /* Check which widgets we need to power and store them in
1695 * lists indicating if they should be powered up or down. We
1696 * only check widgets that have been flagged as dirty but note
1697 * that new widgets may be added to the dirty list while we
1700 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1701 dapm_power_one_widget(w, &up_list, &down_list);
1704 list_for_each_entry(w, &card->widgets, list) {
1706 case snd_soc_dapm_pre:
1707 case snd_soc_dapm_post:
1708 /* These widgets always need to be powered */
1711 list_del_init(&w->dirty);
1718 /* Supplies and micbiases only bring the
1719 * context up to STANDBY as unless something
1720 * else is active and passing audio they
1721 * generally don't require full power. Signal
1722 * generators are virtual pins and have no
1723 * power impact themselves.
1726 case snd_soc_dapm_siggen:
1727 case snd_soc_dapm_vmid:
1729 case snd_soc_dapm_supply:
1730 case snd_soc_dapm_regulator_supply:
1731 case snd_soc_dapm_clock_supply:
1732 case snd_soc_dapm_micbias:
1733 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1734 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1737 d->target_bias_level = SND_SOC_BIAS_ON;
1744 /* Force all contexts in the card to the same bias state if
1745 * they're not ground referenced.
1747 bias = SND_SOC_BIAS_OFF;
1748 list_for_each_entry(d, &card->dapm_list, list)
1749 if (d->target_bias_level > bias)
1750 bias = d->target_bias_level;
1751 list_for_each_entry(d, &card->dapm_list, list)
1752 if (!dapm_idle_bias_off(d))
1753 d->target_bias_level = bias;
1755 trace_snd_soc_dapm_walk_done(card);
1757 /* Run card bias changes at first */
1758 dapm_pre_sequence_async(&card->dapm, 0);
1759 /* Run other bias changes in parallel */
1760 list_for_each_entry(d, &card->dapm_list, list) {
1761 if (d != &card->dapm)
1762 async_schedule_domain(dapm_pre_sequence_async, d,
1765 async_synchronize_full_domain(&async_domain);
1767 list_for_each_entry(w, &down_list, power_list) {
1768 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1771 list_for_each_entry(w, &up_list, power_list) {
1772 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1775 /* Power down widgets first; try to avoid amplifying pops. */
1776 dapm_seq_run(card, &down_list, event, false);
1778 dapm_widget_update(card);
1781 dapm_seq_run(card, &up_list, event, true);
1783 /* Run all the bias changes in parallel */
1784 list_for_each_entry(d, &card->dapm_list, list) {
1785 if (d != &card->dapm)
1786 async_schedule_domain(dapm_post_sequence_async, d,
1789 async_synchronize_full_domain(&async_domain);
1790 /* Run card bias changes at last */
1791 dapm_post_sequence_async(&card->dapm, 0);
1793 /* do we need to notify any clients that DAPM event is complete */
1794 list_for_each_entry(d, &card->dapm_list, list) {
1795 if (d->stream_event)
1796 d->stream_event(d, event);
1799 pop_dbg(card->dev, card->pop_time,
1800 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1801 pop_wait(card->pop_time);
1803 trace_snd_soc_dapm_done(card);
1808 #ifdef CONFIG_DEBUG_FS
1809 static ssize_t dapm_widget_power_read_file(struct file *file,
1810 char __user *user_buf,
1811 size_t count, loff_t *ppos)
1813 struct snd_soc_dapm_widget *w = file->private_data;
1817 struct snd_soc_dapm_path *p = NULL;
1819 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1823 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1828 in = is_connected_input_ep(w, NULL);
1829 out = is_connected_output_ep(w, NULL);
1832 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1833 w->name, w->power ? "On" : "Off",
1834 w->force ? " (forced)" : "", in, out);
1837 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1838 " - R%d(0x%x) mask 0x%x",
1839 w->reg, w->reg, w->mask << w->shift);
1841 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1844 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1846 w->active ? "active" : "inactive");
1848 list_for_each_entry(p, &w->sources, list_sink) {
1849 if (p->connected && !p->connected(w, p->source))
1853 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1854 " in \"%s\" \"%s\"\n",
1855 p->name ? p->name : "static",
1858 list_for_each_entry(p, &w->sinks, list_source) {
1859 if (p->connected && !p->connected(w, p->sink))
1863 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1864 " out \"%s\" \"%s\"\n",
1865 p->name ? p->name : "static",
1869 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1875 static const struct file_operations dapm_widget_power_fops = {
1876 .open = simple_open,
1877 .read = dapm_widget_power_read_file,
1878 .llseek = default_llseek,
1881 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1882 size_t count, loff_t *ppos)
1884 struct snd_soc_dapm_context *dapm = file->private_data;
1887 switch (dapm->bias_level) {
1888 case SND_SOC_BIAS_ON:
1891 case SND_SOC_BIAS_PREPARE:
1892 level = "Prepare\n";
1894 case SND_SOC_BIAS_STANDBY:
1895 level = "Standby\n";
1897 case SND_SOC_BIAS_OFF:
1901 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
1902 level = "Unknown\n";
1906 return simple_read_from_buffer(user_buf, count, ppos, level,
1910 static const struct file_operations dapm_bias_fops = {
1911 .open = simple_open,
1912 .read = dapm_bias_read_file,
1913 .llseek = default_llseek,
1916 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1917 struct dentry *parent)
1921 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1923 if (!dapm->debugfs_dapm) {
1925 "ASoC: Failed to create DAPM debugfs directory\n");
1929 d = debugfs_create_file("bias_level", 0444,
1930 dapm->debugfs_dapm, dapm,
1934 "ASoC: Failed to create bias level debugfs file\n");
1937 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1939 struct snd_soc_dapm_context *dapm = w->dapm;
1942 if (!dapm->debugfs_dapm || !w->name)
1945 d = debugfs_create_file(w->name, 0444,
1946 dapm->debugfs_dapm, w,
1947 &dapm_widget_power_fops);
1949 dev_warn(w->dapm->dev,
1950 "ASoC: Failed to create %s debugfs file\n",
1954 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1956 debugfs_remove_recursive(dapm->debugfs_dapm);
1960 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1961 struct dentry *parent)
1965 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1969 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1976 * soc_dapm_connect_path() - Connects or disconnects a path
1977 * @path: The path to update
1978 * @connect: The new connect state of the path. True if the path is connected,
1979 * false if it is disconneted.
1980 * @reason: The reason why the path changed (for debugging only)
1982 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
1983 bool connect, const char *reason)
1985 if (path->connect == connect)
1988 path->connect = connect;
1989 dapm_mark_dirty(path->source, reason);
1990 dapm_mark_dirty(path->sink, reason);
1991 dapm_path_invalidate(path);
1994 /* test and update the power status of a mux widget */
1995 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
1996 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1998 struct snd_soc_dapm_path *path;
2002 lockdep_assert_held(&card->dapm_mutex);
2004 /* find dapm widget path assoc with kcontrol */
2005 dapm_kcontrol_for_each_path(path, kcontrol) {
2007 /* we now need to match the string in the enum to the path */
2008 if (!(strcmp(path->name, e->texts[mux])))
2013 soc_dapm_connect_path(path, connect, "mux update");
2017 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2022 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2023 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2024 struct snd_soc_dapm_update *update)
2026 struct snd_soc_card *card = dapm->card;
2029 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2030 card->update = update;
2031 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2032 card->update = NULL;
2033 mutex_unlock(&card->dapm_mutex);
2035 soc_dpcm_runtime_update(card);
2038 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2040 /* test and update the power status of a mixer or switch widget */
2041 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2042 struct snd_kcontrol *kcontrol, int connect)
2044 struct snd_soc_dapm_path *path;
2047 lockdep_assert_held(&card->dapm_mutex);
2049 /* find dapm widget path assoc with kcontrol */
2050 dapm_kcontrol_for_each_path(path, kcontrol) {
2052 soc_dapm_connect_path(path, connect, "mixer update");
2056 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2061 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2062 struct snd_kcontrol *kcontrol, int connect,
2063 struct snd_soc_dapm_update *update)
2065 struct snd_soc_card *card = dapm->card;
2068 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2069 card->update = update;
2070 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2071 card->update = NULL;
2072 mutex_unlock(&card->dapm_mutex);
2074 soc_dpcm_runtime_update(card);
2077 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2079 static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
2081 struct snd_soc_dapm_widget *w;
2083 char *state = "not set";
2085 list_for_each_entry(w, &codec->component.card->widgets, list) {
2086 if (w->dapm != &codec->dapm)
2089 /* only display widgets that burnm power */
2091 case snd_soc_dapm_hp:
2092 case snd_soc_dapm_mic:
2093 case snd_soc_dapm_spk:
2094 case snd_soc_dapm_line:
2095 case snd_soc_dapm_micbias:
2096 case snd_soc_dapm_dac:
2097 case snd_soc_dapm_adc:
2098 case snd_soc_dapm_pga:
2099 case snd_soc_dapm_out_drv:
2100 case snd_soc_dapm_mixer:
2101 case snd_soc_dapm_mixer_named_ctl:
2102 case snd_soc_dapm_supply:
2103 case snd_soc_dapm_regulator_supply:
2104 case snd_soc_dapm_clock_supply:
2106 count += sprintf(buf + count, "%s: %s\n",
2107 w->name, w->power ? "On":"Off");
2114 switch (codec->dapm.bias_level) {
2115 case SND_SOC_BIAS_ON:
2118 case SND_SOC_BIAS_PREPARE:
2121 case SND_SOC_BIAS_STANDBY:
2124 case SND_SOC_BIAS_OFF:
2128 count += sprintf(buf + count, "PM State: %s\n", state);
2133 /* show dapm widget status in sys fs */
2134 static ssize_t dapm_widget_show(struct device *dev,
2135 struct device_attribute *attr, char *buf)
2137 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2140 for (i = 0; i < rtd->num_codecs; i++) {
2141 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2142 count += dapm_widget_show_codec(codec, buf + count);
2148 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2150 struct attribute *soc_dapm_dev_attrs[] = {
2151 &dev_attr_dapm_widget.attr,
2155 static void dapm_free_path(struct snd_soc_dapm_path *path)
2157 list_del(&path->list_sink);
2158 list_del(&path->list_source);
2159 list_del(&path->list_kcontrol);
2160 list_del(&path->list);
2164 /* free all dapm widgets and resources */
2165 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2167 struct snd_soc_dapm_widget *w, *next_w;
2168 struct snd_soc_dapm_path *p, *next_p;
2170 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2171 if (w->dapm != dapm)
2175 * remove source and sink paths associated to this widget.
2176 * While removing the path, remove reference to it from both
2177 * source and sink widgets so that path is removed only once.
2179 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2182 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2185 kfree(w->kcontrols);
2191 static struct snd_soc_dapm_widget *dapm_find_widget(
2192 struct snd_soc_dapm_context *dapm, const char *pin,
2193 bool search_other_contexts)
2195 struct snd_soc_dapm_widget *w;
2196 struct snd_soc_dapm_widget *fallback = NULL;
2198 list_for_each_entry(w, &dapm->card->widgets, list) {
2199 if (!strcmp(w->name, pin)) {
2200 if (w->dapm == dapm)
2207 if (search_other_contexts)
2213 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2214 const char *pin, int status)
2216 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2218 dapm_assert_locked(dapm);
2221 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2225 if (w->connected != status) {
2226 dapm_mark_dirty(w, "pin configuration");
2227 dapm_widget_invalidate_input_paths(w);
2228 dapm_widget_invalidate_output_paths(w);
2231 w->connected = status;
2239 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2240 * @dapm: DAPM context
2242 * Walks all dapm audio paths and powers widgets according to their
2243 * stream or path usage.
2245 * Requires external locking.
2247 * Returns 0 for success.
2249 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2252 * Suppress early reports (eg, jacks syncing their state) to avoid
2253 * silly DAPM runs during card startup.
2255 if (!dapm->card || !dapm->card->instantiated)
2258 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2260 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2263 * snd_soc_dapm_sync - scan and power dapm paths
2264 * @dapm: DAPM context
2266 * Walks all dapm audio paths and powers widgets according to their
2267 * stream or path usage.
2269 * Returns 0 for success.
2271 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2275 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2276 ret = snd_soc_dapm_sync_unlocked(dapm);
2277 mutex_unlock(&dapm->card->dapm_mutex);
2280 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2283 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2284 * @w: The widget for which to update the flags
2286 * Some widgets have a dynamic category which depends on which neighbors they
2287 * are connected to. This function update the category for these widgets.
2289 * This function must be called whenever a path is added or removed to a widget.
2291 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2293 struct snd_soc_dapm_path *p;
2296 case snd_soc_dapm_input:
2297 /* On a fully routed card a input is never a source */
2298 if (w->dapm->card->fully_routed)
2301 list_for_each_entry(p, &w->sources, list_sink) {
2302 if (p->source->id == snd_soc_dapm_micbias ||
2303 p->source->id == snd_soc_dapm_mic ||
2304 p->source->id == snd_soc_dapm_line ||
2305 p->source->id == snd_soc_dapm_output) {
2311 case snd_soc_dapm_output:
2312 /* On a fully routed card a output is never a sink */
2313 if (w->dapm->card->fully_routed)
2316 list_for_each_entry(p, &w->sinks, list_source) {
2317 if (p->sink->id == snd_soc_dapm_spk ||
2318 p->sink->id == snd_soc_dapm_hp ||
2319 p->sink->id == snd_soc_dapm_line ||
2320 p->sink->id == snd_soc_dapm_input) {
2326 case snd_soc_dapm_line:
2327 w->is_sink = !list_empty(&w->sources);
2328 w->is_source = !list_empty(&w->sinks);
2335 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2336 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2337 const char *control,
2338 int (*connected)(struct snd_soc_dapm_widget *source,
2339 struct snd_soc_dapm_widget *sink))
2341 struct snd_soc_dapm_path *path;
2344 if (wsink->is_supply && !wsource->is_supply) {
2346 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2347 wsource->name, wsink->name);
2351 if (connected && !wsource->is_supply) {
2353 "connected() callback only supported for supply widgets (%s -> %s)\n",
2354 wsource->name, wsink->name);
2358 if (wsource->is_supply && control) {
2360 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2361 wsource->name, control, wsink->name);
2365 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2369 path->source = wsource;
2371 path->connected = connected;
2372 INIT_LIST_HEAD(&path->list);
2373 INIT_LIST_HEAD(&path->list_kcontrol);
2374 INIT_LIST_HEAD(&path->list_source);
2375 INIT_LIST_HEAD(&path->list_sink);
2377 if (wsource->is_supply || wsink->is_supply)
2378 path->is_supply = 1;
2380 /* connect static paths */
2381 if (control == NULL) {
2384 /* connect dynamic paths */
2385 switch (wsink->id) {
2386 case snd_soc_dapm_mux:
2387 ret = dapm_connect_mux(dapm, path, control);
2391 case snd_soc_dapm_switch:
2392 case snd_soc_dapm_mixer:
2393 case snd_soc_dapm_mixer_named_ctl:
2394 ret = dapm_connect_mixer(dapm, path, control);
2400 "Control not supported for path %s -> [%s] -> %s\n",
2401 wsource->name, control, wsink->name);
2407 list_add(&path->list, &dapm->card->paths);
2408 list_add(&path->list_sink, &wsink->sources);
2409 list_add(&path->list_source, &wsource->sinks);
2411 dapm_update_widget_flags(wsource);
2412 dapm_update_widget_flags(wsink);
2414 dapm_mark_dirty(wsource, "Route added");
2415 dapm_mark_dirty(wsink, "Route added");
2417 if (dapm->card->instantiated && path->connect)
2418 dapm_path_invalidate(path);
2426 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2427 const struct snd_soc_dapm_route *route)
2429 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2430 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2433 char prefixed_sink[80];
2434 char prefixed_source[80];
2438 prefix = soc_dapm_prefix(dapm);
2440 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2441 prefix, route->sink);
2442 sink = prefixed_sink;
2443 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2444 prefix, route->source);
2445 source = prefixed_source;
2448 source = route->source;
2452 * find src and dest widgets over all widgets but favor a widget from
2453 * current DAPM context
2455 list_for_each_entry(w, &dapm->card->widgets, list) {
2456 if (!wsink && !(strcmp(w->name, sink))) {
2458 if (w->dapm == dapm)
2462 if (!wsource && !(strcmp(w->name, source))) {
2464 if (w->dapm == dapm)
2468 /* use widget from another DAPM context if not found from this */
2474 if (wsource == NULL) {
2475 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2479 if (wsink == NULL) {
2480 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2485 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2492 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2493 source, route->control, sink);
2497 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2498 const struct snd_soc_dapm_route *route)
2500 struct snd_soc_dapm_widget *wsource, *wsink;
2501 struct snd_soc_dapm_path *path, *p;
2504 char prefixed_sink[80];
2505 char prefixed_source[80];
2508 if (route->control) {
2510 "ASoC: Removal of routes with controls not supported\n");
2514 prefix = soc_dapm_prefix(dapm);
2516 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2517 prefix, route->sink);
2518 sink = prefixed_sink;
2519 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2520 prefix, route->source);
2521 source = prefixed_source;
2524 source = route->source;
2528 list_for_each_entry(p, &dapm->card->paths, list) {
2529 if (strcmp(p->source->name, source) != 0)
2531 if (strcmp(p->sink->name, sink) != 0)
2538 wsource = path->source;
2541 dapm_mark_dirty(wsource, "Route removed");
2542 dapm_mark_dirty(wsink, "Route removed");
2544 dapm_path_invalidate(path);
2546 dapm_free_path(path);
2548 /* Update any path related flags */
2549 dapm_update_widget_flags(wsource);
2550 dapm_update_widget_flags(wsink);
2552 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2560 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2561 * @dapm: DAPM context
2562 * @route: audio routes
2563 * @num: number of routes
2565 * Connects 2 dapm widgets together via a named audio path. The sink is
2566 * the widget receiving the audio signal, whilst the source is the sender
2567 * of the audio signal.
2569 * Returns 0 for success else error. On error all resources can be freed
2570 * with a call to snd_soc_card_free().
2572 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2573 const struct snd_soc_dapm_route *route, int num)
2577 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2578 for (i = 0; i < num; i++) {
2579 r = snd_soc_dapm_add_route(dapm, route);
2581 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2583 route->control ? route->control : "direct",
2589 mutex_unlock(&dapm->card->dapm_mutex);
2593 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2596 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2597 * @dapm: DAPM context
2598 * @route: audio routes
2599 * @num: number of routes
2601 * Removes routes from the DAPM context.
2603 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2604 const struct snd_soc_dapm_route *route, int num)
2608 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2609 for (i = 0; i < num; i++) {
2610 snd_soc_dapm_del_route(dapm, route);
2613 mutex_unlock(&dapm->card->dapm_mutex);
2617 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2619 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2620 const struct snd_soc_dapm_route *route)
2622 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2625 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2628 struct snd_soc_dapm_path *path;
2632 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2638 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2643 if (route->control || route->connected)
2644 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2645 route->source, route->sink);
2647 list_for_each_entry(path, &source->sinks, list_source) {
2648 if (path->sink == sink) {
2655 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2656 route->source, route->sink);
2658 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2659 count, route->source, route->sink);
2665 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2666 * @dapm: DAPM context
2667 * @route: audio routes
2668 * @num: number of routes
2670 * Mark existing routes matching those specified in the passed array
2671 * as being weak, meaning that they are ignored for the purpose of
2672 * power decisions. The main intended use case is for sidetone paths
2673 * which couple audio between other independent paths if they are both
2674 * active in order to make the combination work better at the user
2675 * level but which aren't intended to be "used".
2677 * Note that CODEC drivers should not use this as sidetone type paths
2678 * can frequently also be used as bypass paths.
2680 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2681 const struct snd_soc_dapm_route *route, int num)
2686 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2687 for (i = 0; i < num; i++) {
2688 err = snd_soc_dapm_weak_route(dapm, route);
2693 mutex_unlock(&dapm->card->dapm_mutex);
2697 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2700 * snd_soc_dapm_new_widgets - add new dapm widgets
2701 * @dapm: DAPM context
2703 * Checks the codec for any new dapm widgets and creates them if found.
2705 * Returns 0 for success.
2707 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2709 struct snd_soc_dapm_widget *w;
2712 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2714 list_for_each_entry(w, &card->widgets, list)
2719 if (w->num_kcontrols) {
2720 w->kcontrols = kzalloc(w->num_kcontrols *
2721 sizeof(struct snd_kcontrol *),
2723 if (!w->kcontrols) {
2724 mutex_unlock(&card->dapm_mutex);
2730 case snd_soc_dapm_switch:
2731 case snd_soc_dapm_mixer:
2732 case snd_soc_dapm_mixer_named_ctl:
2735 case snd_soc_dapm_mux:
2738 case snd_soc_dapm_pga:
2739 case snd_soc_dapm_out_drv:
2742 case snd_soc_dapm_dai_link:
2743 dapm_new_dai_link(w);
2749 /* Read the initial power state from the device */
2751 soc_dapm_read(w->dapm, w->reg, &val);
2752 val = val >> w->shift;
2754 if (val == w->on_val)
2760 dapm_mark_dirty(w, "new widget");
2761 dapm_debugfs_add_widget(w);
2764 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2765 mutex_unlock(&card->dapm_mutex);
2768 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2771 * snd_soc_dapm_get_volsw - dapm mixer get callback
2772 * @kcontrol: mixer control
2773 * @ucontrol: control element information
2775 * Callback to get the value of a dapm mixer control.
2777 * Returns 0 for success.
2779 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2780 struct snd_ctl_elem_value *ucontrol)
2782 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2783 struct snd_soc_card *card = dapm->card;
2784 struct soc_mixer_control *mc =
2785 (struct soc_mixer_control *)kcontrol->private_value;
2787 unsigned int shift = mc->shift;
2789 unsigned int mask = (1 << fls(max)) - 1;
2790 unsigned int invert = mc->invert;
2794 if (snd_soc_volsw_is_stereo(mc))
2796 "ASoC: Control '%s' is stereo, which is not supported\n",
2799 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2800 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2801 ret = soc_dapm_read(dapm, reg, &val);
2802 val = (val >> shift) & mask;
2804 val = dapm_kcontrol_get_value(kcontrol);
2806 mutex_unlock(&card->dapm_mutex);
2809 ucontrol->value.integer.value[0] = max - val;
2811 ucontrol->value.integer.value[0] = val;
2815 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2818 * snd_soc_dapm_put_volsw - dapm mixer set callback
2819 * @kcontrol: mixer control
2820 * @ucontrol: control element information
2822 * Callback to set the value of a dapm mixer control.
2824 * Returns 0 for success.
2826 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_value *ucontrol)
2829 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2830 struct snd_soc_card *card = dapm->card;
2831 struct soc_mixer_control *mc =
2832 (struct soc_mixer_control *)kcontrol->private_value;
2834 unsigned int shift = mc->shift;
2836 unsigned int mask = (1 << fls(max)) - 1;
2837 unsigned int invert = mc->invert;
2839 int connect, change, reg_change = 0;
2840 struct snd_soc_dapm_update update;
2843 if (snd_soc_volsw_is_stereo(mc))
2845 "ASoC: Control '%s' is stereo, which is not supported\n",
2848 val = (ucontrol->value.integer.value[0] & mask);
2854 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2856 change = dapm_kcontrol_set_value(kcontrol, val);
2858 if (reg != SND_SOC_NOPM) {
2859 mask = mask << shift;
2862 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
2865 if (change || reg_change) {
2867 update.kcontrol = kcontrol;
2871 card->update = &update;
2873 change |= reg_change;
2875 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2877 card->update = NULL;
2880 mutex_unlock(&card->dapm_mutex);
2883 soc_dpcm_runtime_update(card);
2887 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2890 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2891 * @kcontrol: mixer control
2892 * @ucontrol: control element information
2894 * Callback to get the value of a dapm enumerated double mixer control.
2896 * Returns 0 for success.
2898 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
2901 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2902 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2903 unsigned int reg_val, val;
2905 if (e->reg != SND_SOC_NOPM) {
2906 int ret = soc_dapm_read(dapm, e->reg, ®_val);
2910 reg_val = dapm_kcontrol_get_value(kcontrol);
2913 val = (reg_val >> e->shift_l) & e->mask;
2914 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2915 if (e->shift_l != e->shift_r) {
2916 val = (reg_val >> e->shift_r) & e->mask;
2917 val = snd_soc_enum_val_to_item(e, val);
2918 ucontrol->value.enumerated.item[1] = val;
2923 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2926 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2927 * @kcontrol: mixer control
2928 * @ucontrol: control element information
2930 * Callback to set the value of a dapm enumerated double mixer control.
2932 * Returns 0 for success.
2934 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2935 struct snd_ctl_elem_value *ucontrol)
2937 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2938 struct snd_soc_card *card = dapm->card;
2939 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2940 unsigned int *item = ucontrol->value.enumerated.item;
2941 unsigned int val, change;
2943 struct snd_soc_dapm_update update;
2946 if (item[0] >= e->items)
2949 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2950 mask = e->mask << e->shift_l;
2951 if (e->shift_l != e->shift_r) {
2952 if (item[1] > e->items)
2954 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
2955 mask |= e->mask << e->shift_r;
2958 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2960 if (e->reg != SND_SOC_NOPM)
2961 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
2963 change = dapm_kcontrol_set_value(kcontrol, val);
2966 if (e->reg != SND_SOC_NOPM) {
2967 update.kcontrol = kcontrol;
2968 update.reg = e->reg;
2971 card->update = &update;
2974 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
2976 card->update = NULL;
2979 mutex_unlock(&card->dapm_mutex);
2982 soc_dpcm_runtime_update(card);
2986 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2989 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2991 * @kcontrol: mixer control
2992 * @uinfo: control element information
2994 * Callback to provide information about a pin switch control.
2996 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2997 struct snd_ctl_elem_info *uinfo)
2999 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3001 uinfo->value.integer.min = 0;
3002 uinfo->value.integer.max = 1;
3006 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3009 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3011 * @kcontrol: mixer control
3014 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3015 struct snd_ctl_elem_value *ucontrol)
3017 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3018 const char *pin = (const char *)kcontrol->private_value;
3020 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3022 ucontrol->value.integer.value[0] =
3023 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3025 mutex_unlock(&card->dapm_mutex);
3029 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3032 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3034 * @kcontrol: mixer control
3037 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3038 struct snd_ctl_elem_value *ucontrol)
3040 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3041 const char *pin = (const char *)kcontrol->private_value;
3043 if (ucontrol->value.integer.value[0])
3044 snd_soc_dapm_enable_pin(&card->dapm, pin);
3046 snd_soc_dapm_disable_pin(&card->dapm, pin);
3048 snd_soc_dapm_sync(&card->dapm);
3051 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3053 static struct snd_soc_dapm_widget *
3054 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3055 const struct snd_soc_dapm_widget *widget)
3057 struct snd_soc_dapm_widget *w;
3061 if ((w = dapm_cnew_widget(widget)) == NULL)
3065 case snd_soc_dapm_regulator_supply:
3066 w->regulator = devm_regulator_get(dapm->dev, w->name);
3067 if (IS_ERR(w->regulator)) {
3068 ret = PTR_ERR(w->regulator);
3069 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3074 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3075 ret = regulator_allow_bypass(w->regulator, true);
3077 dev_warn(w->dapm->dev,
3078 "ASoC: Failed to bypass %s: %d\n",
3082 case snd_soc_dapm_clock_supply:
3083 #ifdef CONFIG_CLKDEV_LOOKUP
3084 w->clk = devm_clk_get(dapm->dev, w->name);
3085 if (IS_ERR(w->clk)) {
3086 ret = PTR_ERR(w->clk);
3087 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3099 prefix = soc_dapm_prefix(dapm);
3101 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3103 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3105 if (w->name == NULL) {
3111 case snd_soc_dapm_mic:
3113 w->power_check = dapm_generic_check_power;
3115 case snd_soc_dapm_input:
3116 if (!dapm->card->fully_routed)
3118 w->power_check = dapm_generic_check_power;
3120 case snd_soc_dapm_spk:
3121 case snd_soc_dapm_hp:
3123 w->power_check = dapm_generic_check_power;
3125 case snd_soc_dapm_output:
3126 if (!dapm->card->fully_routed)
3128 w->power_check = dapm_generic_check_power;
3130 case snd_soc_dapm_vmid:
3131 case snd_soc_dapm_siggen:
3133 w->power_check = dapm_always_on_check_power;
3135 case snd_soc_dapm_mux:
3136 case snd_soc_dapm_switch:
3137 case snd_soc_dapm_mixer:
3138 case snd_soc_dapm_mixer_named_ctl:
3139 case snd_soc_dapm_adc:
3140 case snd_soc_dapm_aif_out:
3141 case snd_soc_dapm_dac:
3142 case snd_soc_dapm_aif_in:
3143 case snd_soc_dapm_pga:
3144 case snd_soc_dapm_out_drv:
3145 case snd_soc_dapm_micbias:
3146 case snd_soc_dapm_line:
3147 case snd_soc_dapm_dai_link:
3148 case snd_soc_dapm_dai_out:
3149 case snd_soc_dapm_dai_in:
3150 w->power_check = dapm_generic_check_power;
3152 case snd_soc_dapm_supply:
3153 case snd_soc_dapm_regulator_supply:
3154 case snd_soc_dapm_clock_supply:
3155 case snd_soc_dapm_kcontrol:
3157 w->power_check = dapm_supply_check_power;
3160 w->power_check = dapm_always_on_check_power;
3165 INIT_LIST_HEAD(&w->sources);
3166 INIT_LIST_HEAD(&w->sinks);
3167 INIT_LIST_HEAD(&w->list);
3168 INIT_LIST_HEAD(&w->dirty);
3169 list_add(&w->list, &dapm->card->widgets);
3174 /* machine layer set ups unconnected pins and insertions */
3180 * snd_soc_dapm_new_controls - create new dapm controls
3181 * @dapm: DAPM context
3182 * @widget: widget array
3183 * @num: number of widgets
3185 * Creates new DAPM controls based upon the templates.
3187 * Returns 0 for success else error.
3189 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3190 const struct snd_soc_dapm_widget *widget,
3193 struct snd_soc_dapm_widget *w;
3197 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3198 for (i = 0; i < num; i++) {
3199 w = snd_soc_dapm_new_control(dapm, widget);
3202 "ASoC: Failed to create DAPM control %s\n",
3209 mutex_unlock(&dapm->card->dapm_mutex);
3212 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3214 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3215 struct snd_kcontrol *kcontrol, int event)
3217 struct snd_soc_dapm_path *source_p, *sink_p;
3218 struct snd_soc_dai *source, *sink;
3219 const struct snd_soc_pcm_stream *config = w->params + w->params_select;
3220 struct snd_pcm_substream substream;
3221 struct snd_pcm_hw_params *params = NULL;
3225 if (WARN_ON(!config) ||
3226 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3229 /* We only support a single source and sink, pick the first */
3230 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3232 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3235 if (WARN_ON(!source_p || !sink_p) ||
3236 WARN_ON(!sink_p->source || !source_p->sink) ||
3237 WARN_ON(!source_p->source || !sink_p->sink))
3240 source = source_p->source->priv;
3241 sink = sink_p->sink->priv;
3243 /* Be a little careful as we don't want to overflow the mask array */
3244 if (config->formats) {
3245 fmt = ffs(config->formats) - 1;
3247 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3252 /* Currently very limited parameter selection */
3253 params = kzalloc(sizeof(*params), GFP_KERNEL);
3258 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3260 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3262 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3265 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3266 = config->channels_min;
3267 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3268 = config->channels_max;
3270 memset(&substream, 0, sizeof(substream));
3273 case SND_SOC_DAPM_PRE_PMU:
3274 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3275 ret = soc_dai_hw_params(&substream, params, source);
3279 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3280 ret = soc_dai_hw_params(&substream, params, sink);
3285 case SND_SOC_DAPM_POST_PMU:
3286 ret = snd_soc_dai_digital_mute(sink, 0,
3287 SNDRV_PCM_STREAM_PLAYBACK);
3288 if (ret != 0 && ret != -ENOTSUPP)
3289 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3293 case SND_SOC_DAPM_PRE_PMD:
3294 ret = snd_soc_dai_digital_mute(sink, 1,
3295 SNDRV_PCM_STREAM_PLAYBACK);
3296 if (ret != 0 && ret != -ENOTSUPP)
3297 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3302 WARN(1, "Unknown event %d\n", event);
3311 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3312 struct snd_ctl_elem_value *ucontrol)
3314 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3316 ucontrol->value.integer.value[0] = w->params_select;
3321 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
3322 struct snd_ctl_elem_value *ucontrol)
3324 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3326 /* Can't change the config when widget is already powered */
3330 if (ucontrol->value.integer.value[0] == w->params_select)
3333 if (ucontrol->value.integer.value[0] >= w->num_params)
3336 w->params_select = ucontrol->value.integer.value[0];
3341 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3342 const struct snd_soc_pcm_stream *params,
3343 unsigned int num_params,
3344 struct snd_soc_dapm_widget *source,
3345 struct snd_soc_dapm_widget *sink)
3347 struct snd_soc_dapm_widget template;
3348 struct snd_soc_dapm_widget *w;
3351 unsigned long private_value;
3352 const char **w_param_text;
3353 struct soc_enum w_param_enum[] = {
3354 SOC_ENUM_SINGLE(0, 0, 0, NULL),
3356 struct snd_kcontrol_new kcontrol_dai_link[] = {
3357 SOC_ENUM_EXT(NULL, w_param_enum[0],
3358 snd_soc_dapm_dai_link_get,
3359 snd_soc_dapm_dai_link_put),
3361 const struct snd_soc_pcm_stream *config = params;
3363 w_param_text = devm_kcalloc(card->dev, num_params,
3364 sizeof(char *), GFP_KERNEL);
3368 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
3369 source->name, sink->name);
3372 goto outfree_w_param;
3375 for (count = 0 ; count < num_params; count++) {
3376 if (!config->stream_name) {
3377 dev_warn(card->dapm.dev,
3378 "ASoC: anonymous config %d for dai link %s\n",
3380 w_param_text[count] =
3381 devm_kasprintf(card->dev, GFP_KERNEL,
3382 "Anonymous Configuration %d",
3384 if (!w_param_text[count]) {
3386 goto outfree_link_name;
3389 w_param_text[count] = devm_kmemdup(card->dev,
3390 config->stream_name,
3391 strlen(config->stream_name) + 1,
3393 if (!w_param_text[count]) {
3395 goto outfree_link_name;
3400 w_param_enum[0].items = num_params;
3401 w_param_enum[0].texts = w_param_text;
3403 memset(&template, 0, sizeof(template));
3404 template.reg = SND_SOC_NOPM;
3405 template.id = snd_soc_dapm_dai_link;
3406 template.name = link_name;
3407 template.event = snd_soc_dai_link_event;
3408 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3409 SND_SOC_DAPM_PRE_PMD;
3410 template.num_kcontrols = 1;
3411 /* duplicate w_param_enum on heap so that memory persists */
3413 (unsigned long) devm_kmemdup(card->dev,
3414 (void *)(kcontrol_dai_link[0].private_value),
3415 sizeof(struct soc_enum), GFP_KERNEL);
3416 if (!private_value) {
3417 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3420 goto outfree_link_name;
3422 kcontrol_dai_link[0].private_value = private_value;
3423 /* duplicate kcontrol_dai_link on heap so that memory persists */
3424 template.kcontrol_news =
3425 devm_kmemdup(card->dev, &kcontrol_dai_link[0],
3426 sizeof(struct snd_kcontrol_new),
3428 if (!template.kcontrol_news) {
3429 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
3432 goto outfree_private_value;
3435 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3437 w = snd_soc_dapm_new_control(&card->dapm, &template);
3439 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3442 goto outfree_kcontrol_news;
3446 w->num_params = num_params;
3448 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3451 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3454 devm_kfree(card->dev, w);
3455 outfree_kcontrol_news:
3456 devm_kfree(card->dev, (void *)template.kcontrol_news);
3457 outfree_private_value:
3458 devm_kfree(card->dev, (void *)private_value);
3460 devm_kfree(card->dev, link_name);
3462 for (count = 0 ; count < num_params; count++)
3463 devm_kfree(card->dev, (void *)w_param_text[count]);
3464 devm_kfree(card->dev, w_param_text);
3469 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3470 struct snd_soc_dai *dai)
3472 struct snd_soc_dapm_widget template;
3473 struct snd_soc_dapm_widget *w;
3475 WARN_ON(dapm->dev != dai->dev);
3477 memset(&template, 0, sizeof(template));
3478 template.reg = SND_SOC_NOPM;
3480 if (dai->driver->playback.stream_name) {
3481 template.id = snd_soc_dapm_dai_in;
3482 template.name = dai->driver->playback.stream_name;
3483 template.sname = dai->driver->playback.stream_name;
3485 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3488 w = snd_soc_dapm_new_control(dapm, &template);
3490 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3491 dai->driver->playback.stream_name);
3496 dai->playback_widget = w;
3499 if (dai->driver->capture.stream_name) {
3500 template.id = snd_soc_dapm_dai_out;
3501 template.name = dai->driver->capture.stream_name;
3502 template.sname = dai->driver->capture.stream_name;
3504 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3507 w = snd_soc_dapm_new_control(dapm, &template);
3509 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3510 dai->driver->capture.stream_name);
3515 dai->capture_widget = w;
3521 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3523 struct snd_soc_dapm_widget *dai_w, *w;
3524 struct snd_soc_dapm_widget *src, *sink;
3525 struct snd_soc_dai *dai;
3527 /* For each DAI widget... */
3528 list_for_each_entry(dai_w, &card->widgets, list) {
3529 switch (dai_w->id) {
3530 case snd_soc_dapm_dai_in:
3531 case snd_soc_dapm_dai_out:
3539 /* ...find all widgets with the same stream and link them */
3540 list_for_each_entry(w, &card->widgets, list) {
3541 if (w->dapm != dai_w->dapm)
3545 case snd_soc_dapm_dai_in:
3546 case snd_soc_dapm_dai_out:
3552 if (!w->sname || !strstr(w->sname, dai_w->name))
3555 if (dai_w->id == snd_soc_dapm_dai_in) {
3562 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3563 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
3570 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3571 struct snd_soc_pcm_runtime *rtd)
3573 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3574 struct snd_soc_dapm_widget *sink, *source;
3577 for (i = 0; i < rtd->num_codecs; i++) {
3578 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
3580 /* there is no point in connecting BE DAI links with dummies */
3581 if (snd_soc_dai_is_dummy(codec_dai) ||
3582 snd_soc_dai_is_dummy(cpu_dai))
3585 /* connect BE DAI playback if widgets are valid */
3586 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3587 source = cpu_dai->playback_widget;
3588 sink = codec_dai->playback_widget;
3589 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3590 cpu_dai->component->name, source->name,
3591 codec_dai->component->name, sink->name);
3593 snd_soc_dapm_add_path(&card->dapm, source, sink,
3597 /* connect BE DAI capture if widgets are valid */
3598 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3599 source = codec_dai->capture_widget;
3600 sink = cpu_dai->capture_widget;
3601 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3602 codec_dai->component->name, source->name,
3603 cpu_dai->component->name, sink->name);
3605 snd_soc_dapm_add_path(&card->dapm, source, sink,
3611 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3614 struct snd_soc_dapm_widget *w;
3616 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3617 w = dai->playback_widget;
3619 w = dai->capture_widget;
3622 dapm_mark_dirty(w, "stream event");
3625 case SND_SOC_DAPM_STREAM_START:
3628 case SND_SOC_DAPM_STREAM_STOP:
3631 case SND_SOC_DAPM_STREAM_SUSPEND:
3632 case SND_SOC_DAPM_STREAM_RESUME:
3633 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3634 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3638 if (w->id == snd_soc_dapm_dai_in) {
3639 w->is_source = w->active;
3640 dapm_widget_invalidate_input_paths(w);
3642 w->is_sink = w->active;
3643 dapm_widget_invalidate_output_paths(w);
3648 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3650 struct snd_soc_pcm_runtime *rtd = card->rtd;
3653 /* for each BE DAI link... */
3654 for (i = 0; i < card->num_rtd; i++) {
3655 rtd = &card->rtd[i];
3658 * dynamic FE links have no fixed DAI mapping.
3659 * CODEC<->CODEC links have no direct connection.
3661 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3664 dapm_connect_dai_link_widgets(card, rtd);
3668 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3673 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3674 for (i = 0; i < rtd->num_codecs; i++)
3675 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
3677 dapm_power_widgets(rtd->card, event);
3681 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3682 * @rtd: PCM runtime data
3683 * @stream: stream name
3684 * @event: stream event
3686 * Sends a stream event to the dapm core. The core then makes any
3687 * necessary widget power changes.
3689 * Returns 0 for success else error.
3691 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3694 struct snd_soc_card *card = rtd->card;
3696 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3697 soc_dapm_stream_event(rtd, stream, event);
3698 mutex_unlock(&card->dapm_mutex);
3702 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3703 * @dapm: DAPM context
3706 * Enables input/output pin and its parents or children widgets iff there is
3707 * a valid audio route and active audio stream.
3709 * Requires external locking.
3711 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3712 * do any widget power switching.
3714 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3717 return snd_soc_dapm_set_pin(dapm, pin, 1);
3719 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3722 * snd_soc_dapm_enable_pin - enable pin.
3723 * @dapm: DAPM context
3726 * Enables input/output pin and its parents or children widgets iff there is
3727 * a valid audio route and active audio stream.
3729 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3730 * do any widget power switching.
3732 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3736 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3738 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3740 mutex_unlock(&dapm->card->dapm_mutex);
3744 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3747 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3748 * @dapm: DAPM context
3751 * Enables input/output pin regardless of any other state. This is
3752 * intended for use with microphone bias supplies used in microphone
3755 * Requires external locking.
3757 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3758 * do any widget power switching.
3760 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3763 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3766 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3770 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3771 if (!w->connected) {
3773 * w->force does not affect the number of input or output paths,
3774 * so we only have to recheck if w->connected is changed
3776 dapm_widget_invalidate_input_paths(w);
3777 dapm_widget_invalidate_output_paths(w);
3781 dapm_mark_dirty(w, "force enable");
3785 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3788 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3789 * @dapm: DAPM context
3792 * Enables input/output pin regardless of any other state. This is
3793 * intended for use with microphone bias supplies used in microphone
3796 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3797 * do any widget power switching.
3799 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3804 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3806 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
3808 mutex_unlock(&dapm->card->dapm_mutex);
3812 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3815 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3816 * @dapm: DAPM context
3819 * Disables input/output pin and its parents or children widgets.
3821 * Requires external locking.
3823 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3824 * do any widget power switching.
3826 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3829 return snd_soc_dapm_set_pin(dapm, pin, 0);
3831 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3834 * snd_soc_dapm_disable_pin - disable pin.
3835 * @dapm: DAPM context
3838 * Disables input/output pin and its parents or children widgets.
3840 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3841 * do any widget power switching.
3843 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3848 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3850 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3852 mutex_unlock(&dapm->card->dapm_mutex);
3856 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3859 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3860 * @dapm: DAPM context
3863 * Marks the specified pin as being not connected, disabling it along
3864 * any parent or child widgets. At present this is identical to
3865 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3866 * additional things such as disabling controls which only affect
3867 * paths through the pin.
3869 * Requires external locking.
3871 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3872 * do any widget power switching.
3874 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3877 return snd_soc_dapm_set_pin(dapm, pin, 0);
3879 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3882 * snd_soc_dapm_nc_pin - permanently disable pin.
3883 * @dapm: DAPM context
3886 * Marks the specified pin as being not connected, disabling it along
3887 * any parent or child widgets. At present this is identical to
3888 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3889 * additional things such as disabling controls which only affect
3890 * paths through the pin.
3892 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3893 * do any widget power switching.
3895 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3899 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3901 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3903 mutex_unlock(&dapm->card->dapm_mutex);
3907 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3910 * snd_soc_dapm_get_pin_status - get audio pin status
3911 * @dapm: DAPM context
3912 * @pin: audio signal pin endpoint (or start point)
3914 * Get audio pin status - connected or disconnected.
3916 * Returns 1 for connected otherwise 0.
3918 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3921 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3924 return w->connected;
3928 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3931 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3932 * @dapm: DAPM context
3933 * @pin: audio signal pin endpoint (or start point)
3935 * Mark the given endpoint or pin as ignoring suspend. When the
3936 * system is disabled a path between two endpoints flagged as ignoring
3937 * suspend will not be disabled. The path must already be enabled via
3938 * normal means at suspend time, it will not be turned on if it was not
3941 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3944 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3947 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3951 w->ignore_suspend = 1;
3955 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3958 * snd_soc_dapm_free - free dapm resources
3959 * @dapm: DAPM context
3961 * Free all dapm widgets and resources.
3963 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3965 dapm_debugfs_cleanup(dapm);
3966 dapm_free_widgets(dapm);
3967 list_del(&dapm->list);
3969 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3971 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
3973 struct snd_soc_card *card = dapm->card;
3974 struct snd_soc_dapm_widget *w;
3975 LIST_HEAD(down_list);
3978 mutex_lock(&card->dapm_mutex);
3980 list_for_each_entry(w, &dapm->card->widgets, list) {
3981 if (w->dapm != dapm)
3984 dapm_seq_insert(w, &down_list, false);
3990 /* If there were no widgets to power down we're already in
3994 if (dapm->bias_level == SND_SOC_BIAS_ON)
3995 snd_soc_dapm_set_bias_level(dapm,
3996 SND_SOC_BIAS_PREPARE);
3997 dapm_seq_run(card, &down_list, 0, false);
3998 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3999 snd_soc_dapm_set_bias_level(dapm,
4000 SND_SOC_BIAS_STANDBY);
4003 mutex_unlock(&card->dapm_mutex);
4007 * snd_soc_dapm_shutdown - callback for system shutdown
4009 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4011 struct snd_soc_dapm_context *dapm;
4013 list_for_each_entry(dapm, &card->dapm_list, list) {
4014 if (dapm != &card->dapm) {
4015 soc_dapm_shutdown_dapm(dapm);
4016 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4017 snd_soc_dapm_set_bias_level(dapm,
4022 soc_dapm_shutdown_dapm(&card->dapm);
4023 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4024 snd_soc_dapm_set_bias_level(&card->dapm,
4028 /* Module information */
4029 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4030 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4031 MODULE_LICENSE("GPL");