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 /* We implement power down on suspend by checking the power state of
847 * the ALSA card - when we are suspending the ALSA state for the card
850 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
852 int level = snd_power_get_state(widget->dapm->card->snd_card);
855 case SNDRV_CTL_POWER_D3hot:
856 case SNDRV_CTL_POWER_D3cold:
857 if (widget->ignore_suspend)
858 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
860 return widget->ignore_suspend;
866 /* add widget to list if it's not already in the list */
867 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
868 struct snd_soc_dapm_widget *w)
870 struct snd_soc_dapm_widget_list *wlist;
871 int wlistsize, wlistentries, i;
878 /* is this widget already in the list */
879 for (i = 0; i < wlist->num_widgets; i++) {
880 if (wlist->widgets[i] == w)
884 /* allocate some new space */
885 wlistentries = wlist->num_widgets + 1;
886 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
887 wlistentries * sizeof(struct snd_soc_dapm_widget *);
888 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
890 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
896 /* insert the widget */
897 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
898 w->name, wlist->num_widgets);
900 wlist->widgets[wlist->num_widgets] = w;
901 wlist->num_widgets++;
906 * Recursively check for a completed path to an active or physically connected
907 * output widget. Returns number of complete paths.
909 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
910 struct snd_soc_dapm_widget_list **list)
912 struct snd_soc_dapm_path *path;
915 if (widget->outputs >= 0)
916 return widget->outputs;
918 DAPM_UPDATE_STAT(widget, path_checks);
920 if (widget->is_sink && widget->connected) {
921 widget->outputs = snd_soc_dapm_suspend_check(widget);
922 return widget->outputs;
925 list_for_each_entry(path, &widget->sinks, list_source) {
926 DAPM_UPDATE_STAT(widget, neighbour_checks);
928 if (path->weak || path->is_supply)
934 trace_snd_soc_dapm_output_path(widget, path);
939 /* do we need to add this widget to the list ? */
942 err = dapm_list_add_widget(list, path->sink);
944 dev_err(widget->dapm->dev,
945 "ASoC: could not add widget %s\n",
952 con += is_connected_output_ep(path->sink, list);
958 widget->outputs = con;
964 * Recursively check for a completed path to an active or physically connected
965 * input widget. Returns number of complete paths.
967 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
968 struct snd_soc_dapm_widget_list **list)
970 struct snd_soc_dapm_path *path;
973 if (widget->inputs >= 0)
974 return widget->inputs;
976 DAPM_UPDATE_STAT(widget, path_checks);
978 if (widget->is_source && widget->connected) {
979 widget->inputs = snd_soc_dapm_suspend_check(widget);
980 return widget->inputs;
983 list_for_each_entry(path, &widget->sources, list_sink) {
984 DAPM_UPDATE_STAT(widget, neighbour_checks);
986 if (path->weak || path->is_supply)
992 trace_snd_soc_dapm_input_path(widget, path);
997 /* do we need to add this widget to the list ? */
1000 err = dapm_list_add_widget(list, path->source);
1002 dev_err(widget->dapm->dev,
1003 "ASoC: could not add widget %s\n",
1010 con += is_connected_input_ep(path->source, list);
1016 widget->inputs = con;
1022 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1023 * @dai: the soc DAI.
1024 * @stream: stream direction.
1025 * @list: list of active widgets for this stream.
1027 * Queries DAPM graph as to whether an valid audio stream path exists for
1028 * the initial stream specified by name. This takes into account
1029 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1031 * Returns the number of valid paths or negative error.
1033 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1034 struct snd_soc_dapm_widget_list **list)
1036 struct snd_soc_card *card = dai->component->card;
1037 struct snd_soc_dapm_widget *w;
1040 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1043 * For is_connected_{output,input}_ep fully discover the graph we need
1044 * to reset the cached number of inputs and outputs.
1046 list_for_each_entry(w, &card->widgets, list) {
1051 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1052 paths = is_connected_output_ep(dai->playback_widget, list);
1054 paths = is_connected_input_ep(dai->capture_widget, list);
1056 trace_snd_soc_dapm_connected(paths, stream);
1057 mutex_unlock(&card->dapm_mutex);
1063 * Handler for regulator supply widget.
1065 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1066 struct snd_kcontrol *kcontrol, int event)
1070 soc_dapm_async_complete(w->dapm);
1072 if (SND_SOC_DAPM_EVENT_ON(event)) {
1073 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1074 ret = regulator_allow_bypass(w->regulator, false);
1076 dev_warn(w->dapm->dev,
1077 "ASoC: Failed to unbypass %s: %d\n",
1081 return regulator_enable(w->regulator);
1083 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1084 ret = regulator_allow_bypass(w->regulator, true);
1086 dev_warn(w->dapm->dev,
1087 "ASoC: Failed to bypass %s: %d\n",
1091 return regulator_disable_deferred(w->regulator, w->shift);
1094 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1097 * Handler for clock supply widget.
1099 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1100 struct snd_kcontrol *kcontrol, int event)
1105 soc_dapm_async_complete(w->dapm);
1107 #ifdef CONFIG_HAVE_CLK
1108 if (SND_SOC_DAPM_EVENT_ON(event)) {
1109 return clk_prepare_enable(w->clk);
1111 clk_disable_unprepare(w->clk);
1117 EXPORT_SYMBOL_GPL(dapm_clock_event);
1119 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1121 if (w->power_checked)
1122 return w->new_power;
1127 w->new_power = w->power_check(w);
1129 w->power_checked = true;
1131 return w->new_power;
1134 /* Generic check to see if a widget should be powered.
1136 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1140 DAPM_UPDATE_STAT(w, power_checks);
1142 in = is_connected_input_ep(w, NULL);
1143 out = is_connected_output_ep(w, NULL);
1144 return out != 0 && in != 0;
1147 /* Check to see if a power supply is needed */
1148 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1150 struct snd_soc_dapm_path *path;
1152 DAPM_UPDATE_STAT(w, power_checks);
1154 /* Check if one of our outputs is connected */
1155 list_for_each_entry(path, &w->sinks, list_source) {
1156 DAPM_UPDATE_STAT(w, neighbour_checks);
1161 if (path->connected &&
1162 !path->connected(path->source, path->sink))
1165 if (dapm_widget_power_check(path->sink))
1172 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1177 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1178 struct snd_soc_dapm_widget *b,
1186 sort = dapm_down_seq;
1188 if (sort[a->id] != sort[b->id])
1189 return sort[a->id] - sort[b->id];
1190 if (a->subseq != b->subseq) {
1192 return a->subseq - b->subseq;
1194 return b->subseq - a->subseq;
1196 if (a->reg != b->reg)
1197 return a->reg - b->reg;
1198 if (a->dapm != b->dapm)
1199 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1204 /* Insert a widget in order into a DAPM power sequence. */
1205 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1206 struct list_head *list,
1209 struct snd_soc_dapm_widget *w;
1211 list_for_each_entry(w, list, power_list)
1212 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1213 list_add_tail(&new_widget->power_list, &w->power_list);
1217 list_add_tail(&new_widget->power_list, list);
1220 static void dapm_seq_check_event(struct snd_soc_card *card,
1221 struct snd_soc_dapm_widget *w, int event)
1223 const char *ev_name;
1227 case SND_SOC_DAPM_PRE_PMU:
1228 ev_name = "PRE_PMU";
1231 case SND_SOC_DAPM_POST_PMU:
1232 ev_name = "POST_PMU";
1235 case SND_SOC_DAPM_PRE_PMD:
1236 ev_name = "PRE_PMD";
1239 case SND_SOC_DAPM_POST_PMD:
1240 ev_name = "POST_PMD";
1243 case SND_SOC_DAPM_WILL_PMU:
1244 ev_name = "WILL_PMU";
1247 case SND_SOC_DAPM_WILL_PMD:
1248 ev_name = "WILL_PMD";
1252 WARN(1, "Unknown event %d\n", event);
1256 if (w->new_power != power)
1259 if (w->event && (w->event_flags & event)) {
1260 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1262 soc_dapm_async_complete(w->dapm);
1263 trace_snd_soc_dapm_widget_event_start(w, event);
1264 ret = w->event(w, NULL, event);
1265 trace_snd_soc_dapm_widget_event_done(w, event);
1267 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1268 ev_name, w->name, ret);
1272 /* Apply the coalesced changes from a DAPM sequence */
1273 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1274 struct list_head *pending)
1276 struct snd_soc_dapm_context *dapm;
1277 struct snd_soc_dapm_widget *w;
1279 unsigned int value = 0;
1280 unsigned int mask = 0;
1282 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1286 list_for_each_entry(w, pending, power_list) {
1287 WARN_ON(reg != w->reg || dapm != w->dapm);
1288 w->power = w->new_power;
1290 mask |= w->mask << w->shift;
1292 value |= w->on_val << w->shift;
1294 value |= w->off_val << w->shift;
1296 pop_dbg(dapm->dev, card->pop_time,
1297 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1298 w->name, reg, value, mask);
1300 /* Check for events */
1301 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1302 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1306 /* Any widget will do, they should all be updating the
1310 pop_dbg(dapm->dev, card->pop_time,
1311 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1312 value, mask, reg, card->pop_time);
1313 pop_wait(card->pop_time);
1314 soc_dapm_update_bits(dapm, reg, mask, value);
1317 list_for_each_entry(w, pending, power_list) {
1318 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1319 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1323 /* Apply a DAPM power sequence.
1325 * We walk over a pre-sorted list of widgets to apply power to. In
1326 * order to minimise the number of writes to the device required
1327 * multiple widgets will be updated in a single write where possible.
1328 * Currently anything that requires more than a single write is not
1331 static void dapm_seq_run(struct snd_soc_card *card,
1332 struct list_head *list, int event, bool power_up)
1334 struct snd_soc_dapm_widget *w, *n;
1335 struct snd_soc_dapm_context *d;
1338 int cur_subseq = -1;
1339 int cur_reg = SND_SOC_NOPM;
1340 struct snd_soc_dapm_context *cur_dapm = NULL;
1347 sort = dapm_down_seq;
1349 list_for_each_entry_safe(w, n, list, power_list) {
1352 /* Do we need to apply any queued changes? */
1353 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1354 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1355 if (!list_empty(&pending))
1356 dapm_seq_run_coalesced(card, &pending);
1358 if (cur_dapm && cur_dapm->seq_notifier) {
1359 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1360 if (sort[i] == cur_sort)
1361 cur_dapm->seq_notifier(cur_dapm,
1366 if (cur_dapm && w->dapm != cur_dapm)
1367 soc_dapm_async_complete(cur_dapm);
1369 INIT_LIST_HEAD(&pending);
1371 cur_subseq = INT_MIN;
1372 cur_reg = SND_SOC_NOPM;
1377 case snd_soc_dapm_pre:
1379 list_for_each_entry_safe_continue(w, n, list,
1382 if (event == SND_SOC_DAPM_STREAM_START)
1384 NULL, SND_SOC_DAPM_PRE_PMU);
1385 else if (event == SND_SOC_DAPM_STREAM_STOP)
1387 NULL, SND_SOC_DAPM_PRE_PMD);
1390 case snd_soc_dapm_post:
1392 list_for_each_entry_safe_continue(w, n, list,
1395 if (event == SND_SOC_DAPM_STREAM_START)
1397 NULL, SND_SOC_DAPM_POST_PMU);
1398 else if (event == SND_SOC_DAPM_STREAM_STOP)
1400 NULL, SND_SOC_DAPM_POST_PMD);
1404 /* Queue it up for application */
1405 cur_sort = sort[w->id];
1406 cur_subseq = w->subseq;
1409 list_move(&w->power_list, &pending);
1414 dev_err(w->dapm->dev,
1415 "ASoC: Failed to apply widget power: %d\n", ret);
1418 if (!list_empty(&pending))
1419 dapm_seq_run_coalesced(card, &pending);
1421 if (cur_dapm && cur_dapm->seq_notifier) {
1422 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1423 if (sort[i] == cur_sort)
1424 cur_dapm->seq_notifier(cur_dapm,
1428 list_for_each_entry(d, &card->dapm_list, list) {
1429 soc_dapm_async_complete(d);
1433 static void dapm_widget_update(struct snd_soc_card *card)
1435 struct snd_soc_dapm_update *update = card->update;
1436 struct snd_soc_dapm_widget_list *wlist;
1437 struct snd_soc_dapm_widget *w = NULL;
1441 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1444 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1446 for (wi = 0; wi < wlist->num_widgets; wi++) {
1447 w = wlist->widgets[wi];
1449 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1450 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1452 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1460 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1463 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1466 for (wi = 0; wi < wlist->num_widgets; wi++) {
1467 w = wlist->widgets[wi];
1469 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1470 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1472 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1478 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1479 * they're changing state.
1481 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1483 struct snd_soc_dapm_context *d = data;
1486 /* If we're off and we're not supposed to be go into STANDBY */
1487 if (d->bias_level == SND_SOC_BIAS_OFF &&
1488 d->target_bias_level != SND_SOC_BIAS_OFF) {
1490 pm_runtime_get_sync(d->dev);
1492 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1495 "ASoC: Failed to turn on bias: %d\n", ret);
1498 /* Prepare for a transition to ON or away from ON */
1499 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1500 d->bias_level != SND_SOC_BIAS_ON) ||
1501 (d->target_bias_level != SND_SOC_BIAS_ON &&
1502 d->bias_level == SND_SOC_BIAS_ON)) {
1503 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1506 "ASoC: Failed to prepare bias: %d\n", ret);
1510 /* Async callback run prior to DAPM sequences - brings to their final
1513 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1515 struct snd_soc_dapm_context *d = data;
1518 /* If we just powered the last thing off drop to standby bias */
1519 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1520 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1521 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1522 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1524 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1528 /* If we're in standby and can support bias off then do that */
1529 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1530 d->target_bias_level == SND_SOC_BIAS_OFF) {
1531 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1533 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1537 pm_runtime_put(d->dev);
1540 /* If we just powered up then move to active bias */
1541 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1542 d->target_bias_level == SND_SOC_BIAS_ON) {
1543 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1545 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1550 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1551 bool power, bool connect)
1553 /* If a connection is being made or broken then that update
1554 * will have marked the peer dirty, otherwise the widgets are
1555 * not connected and this update has no impact. */
1559 /* If the peer is already in the state we're moving to then we
1560 * won't have an impact on it. */
1561 if (power != peer->power)
1562 dapm_mark_dirty(peer, "peer state change");
1565 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1566 struct list_head *up_list,
1567 struct list_head *down_list)
1569 struct snd_soc_dapm_path *path;
1571 if (w->power == power)
1574 trace_snd_soc_dapm_widget_power(w, power);
1576 /* If we changed our power state perhaps our neigbours changed
1579 list_for_each_entry(path, &w->sources, list_sink)
1580 dapm_widget_set_peer_power(path->source, power, path->connect);
1582 /* Supplies can't affect their outputs, only their inputs */
1583 if (!w->is_supply) {
1584 list_for_each_entry(path, &w->sinks, list_source)
1585 dapm_widget_set_peer_power(path->sink, power,
1590 dapm_seq_insert(w, up_list, true);
1592 dapm_seq_insert(w, down_list, false);
1595 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1596 struct list_head *up_list,
1597 struct list_head *down_list)
1602 case snd_soc_dapm_pre:
1603 dapm_seq_insert(w, down_list, false);
1605 case snd_soc_dapm_post:
1606 dapm_seq_insert(w, up_list, true);
1610 power = dapm_widget_power_check(w);
1612 dapm_widget_set_power(w, power, up_list, down_list);
1617 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1619 if (dapm->idle_bias_off)
1622 switch (snd_power_get_state(dapm->card->snd_card)) {
1623 case SNDRV_CTL_POWER_D3hot:
1624 case SNDRV_CTL_POWER_D3cold:
1625 return dapm->suspend_bias_off;
1634 * Scan each dapm widget for complete audio path.
1635 * A complete path is a route that has valid endpoints i.e.:-
1637 * o DAC to output pin.
1638 * o Input Pin to ADC.
1639 * o Input pin to Output pin (bypass, sidetone)
1640 * o DAC to ADC (loopback).
1642 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1644 struct snd_soc_dapm_widget *w;
1645 struct snd_soc_dapm_context *d;
1647 LIST_HEAD(down_list);
1648 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1649 enum snd_soc_bias_level bias;
1651 lockdep_assert_held(&card->dapm_mutex);
1653 trace_snd_soc_dapm_start(card);
1655 list_for_each_entry(d, &card->dapm_list, list) {
1656 if (dapm_idle_bias_off(d))
1657 d->target_bias_level = SND_SOC_BIAS_OFF;
1659 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1664 /* Check which widgets we need to power and store them in
1665 * lists indicating if they should be powered up or down. We
1666 * only check widgets that have been flagged as dirty but note
1667 * that new widgets may be added to the dirty list while we
1670 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1671 dapm_power_one_widget(w, &up_list, &down_list);
1674 list_for_each_entry(w, &card->widgets, list) {
1676 case snd_soc_dapm_pre:
1677 case snd_soc_dapm_post:
1678 /* These widgets always need to be powered */
1681 list_del_init(&w->dirty);
1688 /* Supplies and micbiases only bring the
1689 * context up to STANDBY as unless something
1690 * else is active and passing audio they
1691 * generally don't require full power. Signal
1692 * generators are virtual pins and have no
1693 * power impact themselves.
1696 case snd_soc_dapm_siggen:
1697 case snd_soc_dapm_vmid:
1699 case snd_soc_dapm_supply:
1700 case snd_soc_dapm_regulator_supply:
1701 case snd_soc_dapm_clock_supply:
1702 case snd_soc_dapm_micbias:
1703 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1704 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1707 d->target_bias_level = SND_SOC_BIAS_ON;
1714 /* Force all contexts in the card to the same bias state if
1715 * they're not ground referenced.
1717 bias = SND_SOC_BIAS_OFF;
1718 list_for_each_entry(d, &card->dapm_list, list)
1719 if (d->target_bias_level > bias)
1720 bias = d->target_bias_level;
1721 list_for_each_entry(d, &card->dapm_list, list)
1722 if (!dapm_idle_bias_off(d))
1723 d->target_bias_level = bias;
1725 trace_snd_soc_dapm_walk_done(card);
1727 /* Run card bias changes at first */
1728 dapm_pre_sequence_async(&card->dapm, 0);
1729 /* Run other bias changes in parallel */
1730 list_for_each_entry(d, &card->dapm_list, list) {
1731 if (d != &card->dapm)
1732 async_schedule_domain(dapm_pre_sequence_async, d,
1735 async_synchronize_full_domain(&async_domain);
1737 list_for_each_entry(w, &down_list, power_list) {
1738 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1741 list_for_each_entry(w, &up_list, power_list) {
1742 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1745 /* Power down widgets first; try to avoid amplifying pops. */
1746 dapm_seq_run(card, &down_list, event, false);
1748 dapm_widget_update(card);
1751 dapm_seq_run(card, &up_list, event, true);
1753 /* Run all the bias changes in parallel */
1754 list_for_each_entry(d, &card->dapm_list, list) {
1755 if (d != &card->dapm)
1756 async_schedule_domain(dapm_post_sequence_async, d,
1759 async_synchronize_full_domain(&async_domain);
1760 /* Run card bias changes at last */
1761 dapm_post_sequence_async(&card->dapm, 0);
1763 /* do we need to notify any clients that DAPM event is complete */
1764 list_for_each_entry(d, &card->dapm_list, list) {
1765 if (d->stream_event)
1766 d->stream_event(d, event);
1769 pop_dbg(card->dev, card->pop_time,
1770 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1771 pop_wait(card->pop_time);
1773 trace_snd_soc_dapm_done(card);
1778 #ifdef CONFIG_DEBUG_FS
1779 static ssize_t dapm_widget_power_read_file(struct file *file,
1780 char __user *user_buf,
1781 size_t count, loff_t *ppos)
1783 struct snd_soc_dapm_widget *w = file->private_data;
1787 struct snd_soc_dapm_path *p = NULL;
1789 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1793 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1798 in = is_connected_input_ep(w, NULL);
1799 out = is_connected_output_ep(w, NULL);
1802 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1803 w->name, w->power ? "On" : "Off",
1804 w->force ? " (forced)" : "", in, out);
1807 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1808 " - R%d(0x%x) mask 0x%x",
1809 w->reg, w->reg, w->mask << w->shift);
1811 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1814 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1816 w->active ? "active" : "inactive");
1818 list_for_each_entry(p, &w->sources, list_sink) {
1819 if (p->connected && !p->connected(w, p->source))
1823 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1824 " in \"%s\" \"%s\"\n",
1825 p->name ? p->name : "static",
1828 list_for_each_entry(p, &w->sinks, list_source) {
1829 if (p->connected && !p->connected(w, p->sink))
1833 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1834 " out \"%s\" \"%s\"\n",
1835 p->name ? p->name : "static",
1839 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1845 static const struct file_operations dapm_widget_power_fops = {
1846 .open = simple_open,
1847 .read = dapm_widget_power_read_file,
1848 .llseek = default_llseek,
1851 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1852 size_t count, loff_t *ppos)
1854 struct snd_soc_dapm_context *dapm = file->private_data;
1857 switch (dapm->bias_level) {
1858 case SND_SOC_BIAS_ON:
1861 case SND_SOC_BIAS_PREPARE:
1862 level = "Prepare\n";
1864 case SND_SOC_BIAS_STANDBY:
1865 level = "Standby\n";
1867 case SND_SOC_BIAS_OFF:
1871 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
1872 level = "Unknown\n";
1876 return simple_read_from_buffer(user_buf, count, ppos, level,
1880 static const struct file_operations dapm_bias_fops = {
1881 .open = simple_open,
1882 .read = dapm_bias_read_file,
1883 .llseek = default_llseek,
1886 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1887 struct dentry *parent)
1894 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1896 if (!dapm->debugfs_dapm) {
1898 "ASoC: Failed to create DAPM debugfs directory\n");
1902 d = debugfs_create_file("bias_level", 0444,
1903 dapm->debugfs_dapm, dapm,
1907 "ASoC: Failed to create bias level debugfs file\n");
1910 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1912 struct snd_soc_dapm_context *dapm = w->dapm;
1915 if (!dapm->debugfs_dapm || !w->name)
1918 d = debugfs_create_file(w->name, 0444,
1919 dapm->debugfs_dapm, w,
1920 &dapm_widget_power_fops);
1922 dev_warn(w->dapm->dev,
1923 "ASoC: Failed to create %s debugfs file\n",
1927 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1929 debugfs_remove_recursive(dapm->debugfs_dapm);
1933 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1934 struct dentry *parent)
1938 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1942 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1949 * soc_dapm_connect_path() - Connects or disconnects a path
1950 * @path: The path to update
1951 * @connect: The new connect state of the path. True if the path is connected,
1952 * false if it is disconneted.
1953 * @reason: The reason why the path changed (for debugging only)
1955 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
1956 bool connect, const char *reason)
1958 if (path->connect == connect)
1961 path->connect = connect;
1962 dapm_mark_dirty(path->source, reason);
1963 dapm_mark_dirty(path->sink, reason);
1964 dapm_path_invalidate(path);
1967 /* test and update the power status of a mux widget */
1968 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
1969 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1971 struct snd_soc_dapm_path *path;
1975 lockdep_assert_held(&card->dapm_mutex);
1977 /* find dapm widget path assoc with kcontrol */
1978 dapm_kcontrol_for_each_path(path, kcontrol) {
1980 /* we now need to match the string in the enum to the path */
1981 if (!(strcmp(path->name, e->texts[mux])))
1986 soc_dapm_connect_path(path, connect, "mux update");
1990 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
1995 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
1996 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
1997 struct snd_soc_dapm_update *update)
1999 struct snd_soc_card *card = dapm->card;
2002 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2003 card->update = update;
2004 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2005 card->update = NULL;
2006 mutex_unlock(&card->dapm_mutex);
2008 soc_dpcm_runtime_update(card);
2011 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2013 /* test and update the power status of a mixer or switch widget */
2014 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2015 struct snd_kcontrol *kcontrol, int connect)
2017 struct snd_soc_dapm_path *path;
2020 lockdep_assert_held(&card->dapm_mutex);
2022 /* find dapm widget path assoc with kcontrol */
2023 dapm_kcontrol_for_each_path(path, kcontrol) {
2025 soc_dapm_connect_path(path, connect, "mixer update");
2029 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2034 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2035 struct snd_kcontrol *kcontrol, int connect,
2036 struct snd_soc_dapm_update *update)
2038 struct snd_soc_card *card = dapm->card;
2041 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2042 card->update = update;
2043 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2044 card->update = NULL;
2045 mutex_unlock(&card->dapm_mutex);
2047 soc_dpcm_runtime_update(card);
2050 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2052 static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
2054 struct snd_soc_dapm_widget *w;
2056 char *state = "not set";
2058 list_for_each_entry(w, &codec->component.card->widgets, list) {
2059 if (w->dapm != &codec->dapm)
2062 /* only display widgets that burnm power */
2064 case snd_soc_dapm_hp:
2065 case snd_soc_dapm_mic:
2066 case snd_soc_dapm_spk:
2067 case snd_soc_dapm_line:
2068 case snd_soc_dapm_micbias:
2069 case snd_soc_dapm_dac:
2070 case snd_soc_dapm_adc:
2071 case snd_soc_dapm_pga:
2072 case snd_soc_dapm_out_drv:
2073 case snd_soc_dapm_mixer:
2074 case snd_soc_dapm_mixer_named_ctl:
2075 case snd_soc_dapm_supply:
2076 case snd_soc_dapm_regulator_supply:
2077 case snd_soc_dapm_clock_supply:
2079 count += sprintf(buf + count, "%s: %s\n",
2080 w->name, w->power ? "On":"Off");
2087 switch (codec->dapm.bias_level) {
2088 case SND_SOC_BIAS_ON:
2091 case SND_SOC_BIAS_PREPARE:
2094 case SND_SOC_BIAS_STANDBY:
2097 case SND_SOC_BIAS_OFF:
2101 count += sprintf(buf + count, "PM State: %s\n", state);
2106 /* show dapm widget status in sys fs */
2107 static ssize_t dapm_widget_show(struct device *dev,
2108 struct device_attribute *attr, char *buf)
2110 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2113 for (i = 0; i < rtd->num_codecs; i++) {
2114 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2115 count += dapm_widget_show_codec(codec, buf + count);
2121 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2123 struct attribute *soc_dapm_dev_attrs[] = {
2124 &dev_attr_dapm_widget.attr,
2128 static void dapm_free_path(struct snd_soc_dapm_path *path)
2130 list_del(&path->list_sink);
2131 list_del(&path->list_source);
2132 list_del(&path->list_kcontrol);
2133 list_del(&path->list);
2137 /* free all dapm widgets and resources */
2138 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2140 struct snd_soc_dapm_widget *w, *next_w;
2141 struct snd_soc_dapm_path *p, *next_p;
2143 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2144 if (w->dapm != dapm)
2148 * remove source and sink paths associated to this widget.
2149 * While removing the path, remove reference to it from both
2150 * source and sink widgets so that path is removed only once.
2152 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2155 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2158 kfree(w->kcontrols);
2164 static struct snd_soc_dapm_widget *dapm_find_widget(
2165 struct snd_soc_dapm_context *dapm, const char *pin,
2166 bool search_other_contexts)
2168 struct snd_soc_dapm_widget *w;
2169 struct snd_soc_dapm_widget *fallback = NULL;
2171 list_for_each_entry(w, &dapm->card->widgets, list) {
2172 if (!strcmp(w->name, pin)) {
2173 if (w->dapm == dapm)
2180 if (search_other_contexts)
2186 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2187 const char *pin, int status)
2189 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2191 dapm_assert_locked(dapm);
2194 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2198 if (w->connected != status) {
2199 dapm_mark_dirty(w, "pin configuration");
2200 dapm_widget_invalidate_input_paths(w);
2201 dapm_widget_invalidate_output_paths(w);
2204 w->connected = status;
2212 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2213 * @dapm: DAPM context
2215 * Walks all dapm audio paths and powers widgets according to their
2216 * stream or path usage.
2218 * Requires external locking.
2220 * Returns 0 for success.
2222 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2225 * Suppress early reports (eg, jacks syncing their state) to avoid
2226 * silly DAPM runs during card startup.
2228 if (!dapm->card || !dapm->card->instantiated)
2231 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2233 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2236 * snd_soc_dapm_sync - scan and power dapm paths
2237 * @dapm: DAPM context
2239 * Walks all dapm audio paths and powers widgets according to their
2240 * stream or path usage.
2242 * Returns 0 for success.
2244 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2248 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2249 ret = snd_soc_dapm_sync_unlocked(dapm);
2250 mutex_unlock(&dapm->card->dapm_mutex);
2253 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2256 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2257 * @w: The widget for which to update the flags
2259 * Some widgets have a dynamic category which depends on which neighbors they
2260 * are connected to. This function update the category for these widgets.
2262 * This function must be called whenever a path is added or removed to a widget.
2264 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2266 struct snd_soc_dapm_path *p;
2269 case snd_soc_dapm_input:
2270 /* On a fully routed card a input is never a source */
2271 if (w->dapm->card->fully_routed)
2274 list_for_each_entry(p, &w->sources, list_sink) {
2275 if (p->source->id == snd_soc_dapm_micbias ||
2276 p->source->id == snd_soc_dapm_mic ||
2277 p->source->id == snd_soc_dapm_line ||
2278 p->source->id == snd_soc_dapm_output) {
2284 case snd_soc_dapm_output:
2285 /* On a fully routed card a output is never a sink */
2286 if (w->dapm->card->fully_routed)
2289 list_for_each_entry(p, &w->sinks, list_source) {
2290 if (p->sink->id == snd_soc_dapm_spk ||
2291 p->sink->id == snd_soc_dapm_hp ||
2292 p->sink->id == snd_soc_dapm_line ||
2293 p->sink->id == snd_soc_dapm_input) {
2299 case snd_soc_dapm_line:
2300 w->is_sink = !list_empty(&w->sources);
2301 w->is_source = !list_empty(&w->sinks);
2308 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2309 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2310 const char *control,
2311 int (*connected)(struct snd_soc_dapm_widget *source,
2312 struct snd_soc_dapm_widget *sink))
2314 struct snd_soc_dapm_path *path;
2317 if (wsink->is_supply && !wsource->is_supply) {
2319 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2320 wsource->name, wsink->name);
2324 if (connected && !wsource->is_supply) {
2326 "connected() callback only supported for supply widgets (%s -> %s)\n",
2327 wsource->name, wsink->name);
2331 if (wsource->is_supply && control) {
2333 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2334 wsource->name, control, wsink->name);
2338 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2342 path->source = wsource;
2344 path->connected = connected;
2345 INIT_LIST_HEAD(&path->list);
2346 INIT_LIST_HEAD(&path->list_kcontrol);
2347 INIT_LIST_HEAD(&path->list_source);
2348 INIT_LIST_HEAD(&path->list_sink);
2350 if (wsource->is_supply || wsink->is_supply)
2351 path->is_supply = 1;
2353 /* connect static paths */
2354 if (control == NULL) {
2357 /* connect dynamic paths */
2358 switch (wsink->id) {
2359 case snd_soc_dapm_mux:
2360 ret = dapm_connect_mux(dapm, path, control);
2364 case snd_soc_dapm_switch:
2365 case snd_soc_dapm_mixer:
2366 case snd_soc_dapm_mixer_named_ctl:
2367 ret = dapm_connect_mixer(dapm, path, control);
2373 "Control not supported for path %s -> [%s] -> %s\n",
2374 wsource->name, control, wsink->name);
2380 list_add(&path->list, &dapm->card->paths);
2381 list_add(&path->list_sink, &wsink->sources);
2382 list_add(&path->list_source, &wsource->sinks);
2384 dapm_update_widget_flags(wsource);
2385 dapm_update_widget_flags(wsink);
2387 dapm_mark_dirty(wsource, "Route added");
2388 dapm_mark_dirty(wsink, "Route added");
2390 if (dapm->card->instantiated && path->connect)
2391 dapm_path_invalidate(path);
2399 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2400 const struct snd_soc_dapm_route *route)
2402 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2403 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2406 char prefixed_sink[80];
2407 char prefixed_source[80];
2411 prefix = soc_dapm_prefix(dapm);
2413 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2414 prefix, route->sink);
2415 sink = prefixed_sink;
2416 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2417 prefix, route->source);
2418 source = prefixed_source;
2421 source = route->source;
2425 * find src and dest widgets over all widgets but favor a widget from
2426 * current DAPM context
2428 list_for_each_entry(w, &dapm->card->widgets, list) {
2429 if (!wsink && !(strcmp(w->name, sink))) {
2431 if (w->dapm == dapm)
2435 if (!wsource && !(strcmp(w->name, source))) {
2437 if (w->dapm == dapm)
2441 /* use widget from another DAPM context if not found from this */
2447 if (wsource == NULL) {
2448 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2452 if (wsink == NULL) {
2453 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2458 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2465 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2466 source, route->control, sink);
2470 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2471 const struct snd_soc_dapm_route *route)
2473 struct snd_soc_dapm_widget *wsource, *wsink;
2474 struct snd_soc_dapm_path *path, *p;
2477 char prefixed_sink[80];
2478 char prefixed_source[80];
2481 if (route->control) {
2483 "ASoC: Removal of routes with controls not supported\n");
2487 prefix = soc_dapm_prefix(dapm);
2489 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2490 prefix, route->sink);
2491 sink = prefixed_sink;
2492 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2493 prefix, route->source);
2494 source = prefixed_source;
2497 source = route->source;
2501 list_for_each_entry(p, &dapm->card->paths, list) {
2502 if (strcmp(p->source->name, source) != 0)
2504 if (strcmp(p->sink->name, sink) != 0)
2511 wsource = path->source;
2514 dapm_mark_dirty(wsource, "Route removed");
2515 dapm_mark_dirty(wsink, "Route removed");
2517 dapm_path_invalidate(path);
2519 dapm_free_path(path);
2521 /* Update any path related flags */
2522 dapm_update_widget_flags(wsource);
2523 dapm_update_widget_flags(wsink);
2525 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2533 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2534 * @dapm: DAPM context
2535 * @route: audio routes
2536 * @num: number of routes
2538 * Connects 2 dapm widgets together via a named audio path. The sink is
2539 * the widget receiving the audio signal, whilst the source is the sender
2540 * of the audio signal.
2542 * Returns 0 for success else error. On error all resources can be freed
2543 * with a call to snd_soc_card_free().
2545 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2546 const struct snd_soc_dapm_route *route, int num)
2550 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2551 for (i = 0; i < num; i++) {
2552 r = snd_soc_dapm_add_route(dapm, route);
2554 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2556 route->control ? route->control : "direct",
2562 mutex_unlock(&dapm->card->dapm_mutex);
2566 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2569 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2570 * @dapm: DAPM context
2571 * @route: audio routes
2572 * @num: number of routes
2574 * Removes routes from the DAPM context.
2576 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2577 const struct snd_soc_dapm_route *route, int num)
2581 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2582 for (i = 0; i < num; i++) {
2583 snd_soc_dapm_del_route(dapm, route);
2586 mutex_unlock(&dapm->card->dapm_mutex);
2590 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2592 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2593 const struct snd_soc_dapm_route *route)
2595 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2598 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2601 struct snd_soc_dapm_path *path;
2605 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2611 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2616 if (route->control || route->connected)
2617 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2618 route->source, route->sink);
2620 list_for_each_entry(path, &source->sinks, list_source) {
2621 if (path->sink == sink) {
2628 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2629 route->source, route->sink);
2631 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2632 count, route->source, route->sink);
2638 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2639 * @dapm: DAPM context
2640 * @route: audio routes
2641 * @num: number of routes
2643 * Mark existing routes matching those specified in the passed array
2644 * as being weak, meaning that they are ignored for the purpose of
2645 * power decisions. The main intended use case is for sidetone paths
2646 * which couple audio between other independent paths if they are both
2647 * active in order to make the combination work better at the user
2648 * level but which aren't intended to be "used".
2650 * Note that CODEC drivers should not use this as sidetone type paths
2651 * can frequently also be used as bypass paths.
2653 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2654 const struct snd_soc_dapm_route *route, int num)
2659 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2660 for (i = 0; i < num; i++) {
2661 err = snd_soc_dapm_weak_route(dapm, route);
2666 mutex_unlock(&dapm->card->dapm_mutex);
2670 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2673 * snd_soc_dapm_new_widgets - add new dapm widgets
2674 * @dapm: DAPM context
2676 * Checks the codec for any new dapm widgets and creates them if found.
2678 * Returns 0 for success.
2680 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2682 struct snd_soc_dapm_widget *w;
2685 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2687 list_for_each_entry(w, &card->widgets, list)
2692 if (w->num_kcontrols) {
2693 w->kcontrols = kzalloc(w->num_kcontrols *
2694 sizeof(struct snd_kcontrol *),
2696 if (!w->kcontrols) {
2697 mutex_unlock(&card->dapm_mutex);
2703 case snd_soc_dapm_switch:
2704 case snd_soc_dapm_mixer:
2705 case snd_soc_dapm_mixer_named_ctl:
2708 case snd_soc_dapm_mux:
2711 case snd_soc_dapm_pga:
2712 case snd_soc_dapm_out_drv:
2719 /* Read the initial power state from the device */
2721 soc_dapm_read(w->dapm, w->reg, &val);
2722 val = val >> w->shift;
2724 if (val == w->on_val)
2730 dapm_mark_dirty(w, "new widget");
2731 dapm_debugfs_add_widget(w);
2734 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2735 mutex_unlock(&card->dapm_mutex);
2738 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2741 * snd_soc_dapm_get_volsw - dapm mixer get callback
2742 * @kcontrol: mixer control
2743 * @ucontrol: control element information
2745 * Callback to get the value of a dapm mixer control.
2747 * Returns 0 for success.
2749 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2750 struct snd_ctl_elem_value *ucontrol)
2752 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2753 struct snd_soc_card *card = dapm->card;
2754 struct soc_mixer_control *mc =
2755 (struct soc_mixer_control *)kcontrol->private_value;
2757 unsigned int shift = mc->shift;
2759 unsigned int mask = (1 << fls(max)) - 1;
2760 unsigned int invert = mc->invert;
2764 if (snd_soc_volsw_is_stereo(mc))
2766 "ASoC: Control '%s' is stereo, which is not supported\n",
2769 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2770 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2771 ret = soc_dapm_read(dapm, reg, &val);
2772 val = (val >> shift) & mask;
2774 val = dapm_kcontrol_get_value(kcontrol);
2776 mutex_unlock(&card->dapm_mutex);
2779 ucontrol->value.integer.value[0] = max - val;
2781 ucontrol->value.integer.value[0] = val;
2785 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2788 * snd_soc_dapm_put_volsw - dapm mixer set callback
2789 * @kcontrol: mixer control
2790 * @ucontrol: control element information
2792 * Callback to set the value of a dapm mixer control.
2794 * Returns 0 for success.
2796 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2797 struct snd_ctl_elem_value *ucontrol)
2799 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2800 struct snd_soc_card *card = dapm->card;
2801 struct soc_mixer_control *mc =
2802 (struct soc_mixer_control *)kcontrol->private_value;
2804 unsigned int shift = mc->shift;
2806 unsigned int mask = (1 << fls(max)) - 1;
2807 unsigned int invert = mc->invert;
2809 int connect, change, reg_change = 0;
2810 struct snd_soc_dapm_update update;
2813 if (snd_soc_volsw_is_stereo(mc))
2815 "ASoC: Control '%s' is stereo, which is not supported\n",
2818 val = (ucontrol->value.integer.value[0] & mask);
2824 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2826 change = dapm_kcontrol_set_value(kcontrol, val);
2828 if (reg != SND_SOC_NOPM) {
2829 mask = mask << shift;
2832 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
2835 if (change || reg_change) {
2837 update.kcontrol = kcontrol;
2841 card->update = &update;
2843 change |= reg_change;
2845 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2847 card->update = NULL;
2850 mutex_unlock(&card->dapm_mutex);
2853 soc_dpcm_runtime_update(card);
2857 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2860 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2861 * @kcontrol: mixer control
2862 * @ucontrol: control element information
2864 * Callback to get the value of a dapm enumerated double mixer control.
2866 * Returns 0 for success.
2868 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_value *ucontrol)
2871 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2872 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2873 unsigned int reg_val, val;
2875 if (e->reg != SND_SOC_NOPM) {
2876 int ret = soc_dapm_read(dapm, e->reg, ®_val);
2880 reg_val = dapm_kcontrol_get_value(kcontrol);
2883 val = (reg_val >> e->shift_l) & e->mask;
2884 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2885 if (e->shift_l != e->shift_r) {
2886 val = (reg_val >> e->shift_r) & e->mask;
2887 val = snd_soc_enum_val_to_item(e, val);
2888 ucontrol->value.enumerated.item[1] = val;
2893 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2896 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2897 * @kcontrol: mixer control
2898 * @ucontrol: control element information
2900 * Callback to set the value of a dapm enumerated double mixer control.
2902 * Returns 0 for success.
2904 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
2907 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2908 struct snd_soc_card *card = dapm->card;
2909 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2910 unsigned int *item = ucontrol->value.enumerated.item;
2911 unsigned int val, change;
2913 struct snd_soc_dapm_update update;
2916 if (item[0] >= e->items)
2919 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2920 mask = e->mask << e->shift_l;
2921 if (e->shift_l != e->shift_r) {
2922 if (item[1] > e->items)
2924 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
2925 mask |= e->mask << e->shift_r;
2928 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2930 if (e->reg != SND_SOC_NOPM)
2931 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
2933 change = dapm_kcontrol_set_value(kcontrol, val);
2936 if (e->reg != SND_SOC_NOPM) {
2937 update.kcontrol = kcontrol;
2938 update.reg = e->reg;
2941 card->update = &update;
2944 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
2946 card->update = NULL;
2949 mutex_unlock(&card->dapm_mutex);
2952 soc_dpcm_runtime_update(card);
2956 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2959 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2961 * @kcontrol: mixer control
2962 * @uinfo: control element information
2964 * Callback to provide information about a pin switch control.
2966 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2967 struct snd_ctl_elem_info *uinfo)
2969 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2971 uinfo->value.integer.min = 0;
2972 uinfo->value.integer.max = 1;
2976 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2979 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2981 * @kcontrol: mixer control
2984 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2985 struct snd_ctl_elem_value *ucontrol)
2987 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2988 const char *pin = (const char *)kcontrol->private_value;
2990 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2992 ucontrol->value.integer.value[0] =
2993 snd_soc_dapm_get_pin_status(&card->dapm, pin);
2995 mutex_unlock(&card->dapm_mutex);
2999 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3002 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3004 * @kcontrol: mixer control
3007 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3008 struct snd_ctl_elem_value *ucontrol)
3010 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3011 const char *pin = (const char *)kcontrol->private_value;
3013 if (ucontrol->value.integer.value[0])
3014 snd_soc_dapm_enable_pin(&card->dapm, pin);
3016 snd_soc_dapm_disable_pin(&card->dapm, pin);
3018 snd_soc_dapm_sync(&card->dapm);
3021 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3023 static struct snd_soc_dapm_widget *
3024 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3025 const struct snd_soc_dapm_widget *widget)
3027 struct snd_soc_dapm_widget *w;
3031 if ((w = dapm_cnew_widget(widget)) == NULL)
3035 case snd_soc_dapm_regulator_supply:
3036 w->regulator = devm_regulator_get(dapm->dev, w->name);
3037 if (IS_ERR(w->regulator)) {
3038 ret = PTR_ERR(w->regulator);
3039 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3044 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3045 ret = regulator_allow_bypass(w->regulator, true);
3047 dev_warn(w->dapm->dev,
3048 "ASoC: Failed to bypass %s: %d\n",
3052 case snd_soc_dapm_clock_supply:
3053 #ifdef CONFIG_CLKDEV_LOOKUP
3054 w->clk = devm_clk_get(dapm->dev, w->name);
3055 if (IS_ERR(w->clk)) {
3056 ret = PTR_ERR(w->clk);
3057 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3069 prefix = soc_dapm_prefix(dapm);
3071 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3073 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3075 if (w->name == NULL) {
3081 case snd_soc_dapm_mic:
3083 w->power_check = dapm_generic_check_power;
3085 case snd_soc_dapm_input:
3086 if (!dapm->card->fully_routed)
3088 w->power_check = dapm_generic_check_power;
3090 case snd_soc_dapm_spk:
3091 case snd_soc_dapm_hp:
3093 w->power_check = dapm_generic_check_power;
3095 case snd_soc_dapm_output:
3096 if (!dapm->card->fully_routed)
3098 w->power_check = dapm_generic_check_power;
3100 case snd_soc_dapm_vmid:
3101 case snd_soc_dapm_siggen:
3103 w->power_check = dapm_always_on_check_power;
3105 case snd_soc_dapm_mux:
3106 case snd_soc_dapm_switch:
3107 case snd_soc_dapm_mixer:
3108 case snd_soc_dapm_mixer_named_ctl:
3109 case snd_soc_dapm_adc:
3110 case snd_soc_dapm_aif_out:
3111 case snd_soc_dapm_dac:
3112 case snd_soc_dapm_aif_in:
3113 case snd_soc_dapm_pga:
3114 case snd_soc_dapm_out_drv:
3115 case snd_soc_dapm_micbias:
3116 case snd_soc_dapm_line:
3117 case snd_soc_dapm_dai_link:
3118 case snd_soc_dapm_dai_out:
3119 case snd_soc_dapm_dai_in:
3120 w->power_check = dapm_generic_check_power;
3122 case snd_soc_dapm_supply:
3123 case snd_soc_dapm_regulator_supply:
3124 case snd_soc_dapm_clock_supply:
3125 case snd_soc_dapm_kcontrol:
3127 w->power_check = dapm_supply_check_power;
3130 w->power_check = dapm_always_on_check_power;
3135 INIT_LIST_HEAD(&w->sources);
3136 INIT_LIST_HEAD(&w->sinks);
3137 INIT_LIST_HEAD(&w->list);
3138 INIT_LIST_HEAD(&w->dirty);
3139 list_add(&w->list, &dapm->card->widgets);
3144 /* machine layer set ups unconnected pins and insertions */
3150 * snd_soc_dapm_new_controls - create new dapm controls
3151 * @dapm: DAPM context
3152 * @widget: widget array
3153 * @num: number of widgets
3155 * Creates new DAPM controls based upon the templates.
3157 * Returns 0 for success else error.
3159 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3160 const struct snd_soc_dapm_widget *widget,
3163 struct snd_soc_dapm_widget *w;
3167 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3168 for (i = 0; i < num; i++) {
3169 w = snd_soc_dapm_new_control(dapm, widget);
3172 "ASoC: Failed to create DAPM control %s\n",
3179 mutex_unlock(&dapm->card->dapm_mutex);
3182 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3184 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3185 struct snd_kcontrol *kcontrol, int event)
3187 struct snd_soc_dapm_path *source_p, *sink_p;
3188 struct snd_soc_dai *source, *sink;
3189 const struct snd_soc_pcm_stream *config = w->params;
3190 struct snd_pcm_substream substream;
3191 struct snd_pcm_hw_params *params = NULL;
3195 if (WARN_ON(!config) ||
3196 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3199 /* We only support a single source and sink, pick the first */
3200 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3202 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3205 if (WARN_ON(!source_p || !sink_p) ||
3206 WARN_ON(!sink_p->source || !source_p->sink) ||
3207 WARN_ON(!source_p->source || !sink_p->sink))
3210 source = source_p->source->priv;
3211 sink = sink_p->sink->priv;
3213 /* Be a little careful as we don't want to overflow the mask array */
3214 if (config->formats) {
3215 fmt = ffs(config->formats) - 1;
3217 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3222 /* Currently very limited parameter selection */
3223 params = kzalloc(sizeof(*params), GFP_KERNEL);
3228 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3230 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3232 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3235 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3236 = config->channels_min;
3237 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3238 = config->channels_max;
3240 memset(&substream, 0, sizeof(substream));
3243 case SND_SOC_DAPM_PRE_PMU:
3244 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3245 ret = soc_dai_hw_params(&substream, params, source);
3249 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3250 ret = soc_dai_hw_params(&substream, params, sink);
3255 case SND_SOC_DAPM_POST_PMU:
3256 ret = snd_soc_dai_digital_mute(sink, 0,
3257 SNDRV_PCM_STREAM_PLAYBACK);
3258 if (ret != 0 && ret != -ENOTSUPP)
3259 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3263 case SND_SOC_DAPM_PRE_PMD:
3264 ret = snd_soc_dai_digital_mute(sink, 1,
3265 SNDRV_PCM_STREAM_PLAYBACK);
3266 if (ret != 0 && ret != -ENOTSUPP)
3267 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3272 WARN(1, "Unknown event %d\n", event);
3281 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3282 const struct snd_soc_pcm_stream *params,
3283 struct snd_soc_dapm_widget *source,
3284 struct snd_soc_dapm_widget *sink)
3286 struct snd_soc_dapm_widget template;
3287 struct snd_soc_dapm_widget *w;
3292 len = strlen(source->name) + strlen(sink->name) + 2;
3293 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3296 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3298 memset(&template, 0, sizeof(template));
3299 template.reg = SND_SOC_NOPM;
3300 template.id = snd_soc_dapm_dai_link;
3301 template.name = link_name;
3302 template.event = snd_soc_dai_link_event;
3303 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3304 SND_SOC_DAPM_PRE_PMD;
3306 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3308 w = snd_soc_dapm_new_control(&card->dapm, &template);
3310 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3317 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3320 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3323 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3324 struct snd_soc_dai *dai)
3326 struct snd_soc_dapm_widget template;
3327 struct snd_soc_dapm_widget *w;
3329 WARN_ON(dapm->dev != dai->dev);
3331 memset(&template, 0, sizeof(template));
3332 template.reg = SND_SOC_NOPM;
3334 if (dai->driver->playback.stream_name) {
3335 template.id = snd_soc_dapm_dai_in;
3336 template.name = dai->driver->playback.stream_name;
3337 template.sname = dai->driver->playback.stream_name;
3339 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3342 w = snd_soc_dapm_new_control(dapm, &template);
3344 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3345 dai->driver->playback.stream_name);
3350 dai->playback_widget = w;
3353 if (dai->driver->capture.stream_name) {
3354 template.id = snd_soc_dapm_dai_out;
3355 template.name = dai->driver->capture.stream_name;
3356 template.sname = dai->driver->capture.stream_name;
3358 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3361 w = snd_soc_dapm_new_control(dapm, &template);
3363 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3364 dai->driver->capture.stream_name);
3369 dai->capture_widget = w;
3375 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3377 struct snd_soc_dapm_widget *dai_w, *w;
3378 struct snd_soc_dapm_widget *src, *sink;
3379 struct snd_soc_dai *dai;
3381 /* For each DAI widget... */
3382 list_for_each_entry(dai_w, &card->widgets, list) {
3383 switch (dai_w->id) {
3384 case snd_soc_dapm_dai_in:
3385 case snd_soc_dapm_dai_out:
3393 /* ...find all widgets with the same stream and link them */
3394 list_for_each_entry(w, &card->widgets, list) {
3395 if (w->dapm != dai_w->dapm)
3399 case snd_soc_dapm_dai_in:
3400 case snd_soc_dapm_dai_out:
3406 if (!w->sname || !strstr(w->sname, dai_w->name))
3409 if (dai_w->id == snd_soc_dapm_dai_in) {
3416 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3417 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
3424 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3425 struct snd_soc_pcm_runtime *rtd)
3427 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3428 struct snd_soc_dapm_widget *sink, *source;
3431 for (i = 0; i < rtd->num_codecs; i++) {
3432 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
3434 /* there is no point in connecting BE DAI links with dummies */
3435 if (snd_soc_dai_is_dummy(codec_dai) ||
3436 snd_soc_dai_is_dummy(cpu_dai))
3439 /* connect BE DAI playback if widgets are valid */
3440 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3441 source = cpu_dai->playback_widget;
3442 sink = codec_dai->playback_widget;
3443 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3444 cpu_dai->component->name, source->name,
3445 codec_dai->component->name, sink->name);
3447 snd_soc_dapm_add_path(&card->dapm, source, sink,
3451 /* connect BE DAI capture if widgets are valid */
3452 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3453 source = codec_dai->capture_widget;
3454 sink = cpu_dai->capture_widget;
3455 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3456 codec_dai->component->name, source->name,
3457 cpu_dai->component->name, sink->name);
3459 snd_soc_dapm_add_path(&card->dapm, source, sink,
3465 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3468 struct snd_soc_dapm_widget *w;
3470 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3471 w = dai->playback_widget;
3473 w = dai->capture_widget;
3476 dapm_mark_dirty(w, "stream event");
3479 case SND_SOC_DAPM_STREAM_START:
3482 case SND_SOC_DAPM_STREAM_STOP:
3485 case SND_SOC_DAPM_STREAM_SUSPEND:
3486 case SND_SOC_DAPM_STREAM_RESUME:
3487 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3488 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3492 if (w->id == snd_soc_dapm_dai_in) {
3493 w->is_source = w->active;
3494 dapm_widget_invalidate_input_paths(w);
3496 w->is_sink = w->active;
3497 dapm_widget_invalidate_output_paths(w);
3502 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3504 struct snd_soc_pcm_runtime *rtd = card->rtd;
3507 /* for each BE DAI link... */
3508 for (i = 0; i < card->num_rtd; i++) {
3509 rtd = &card->rtd[i];
3512 * dynamic FE links have no fixed DAI mapping.
3513 * CODEC<->CODEC links have no direct connection.
3515 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3518 dapm_connect_dai_link_widgets(card, rtd);
3522 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3527 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3528 for (i = 0; i < rtd->num_codecs; i++)
3529 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
3531 dapm_power_widgets(rtd->card, event);
3535 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3536 * @rtd: PCM runtime data
3537 * @stream: stream name
3538 * @event: stream event
3540 * Sends a stream event to the dapm core. The core then makes any
3541 * necessary widget power changes.
3543 * Returns 0 for success else error.
3545 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3548 struct snd_soc_card *card = rtd->card;
3550 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3551 soc_dapm_stream_event(rtd, stream, event);
3552 mutex_unlock(&card->dapm_mutex);
3556 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3557 * @dapm: DAPM context
3560 * Enables input/output pin and its parents or children widgets iff there is
3561 * a valid audio route and active audio stream.
3563 * Requires external locking.
3565 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3566 * do any widget power switching.
3568 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3571 return snd_soc_dapm_set_pin(dapm, pin, 1);
3573 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3576 * snd_soc_dapm_enable_pin - enable pin.
3577 * @dapm: DAPM context
3580 * Enables input/output pin and its parents or children widgets iff there is
3581 * a valid audio route and active audio stream.
3583 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3584 * do any widget power switching.
3586 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3590 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3592 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3594 mutex_unlock(&dapm->card->dapm_mutex);
3598 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3601 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3602 * @dapm: DAPM context
3605 * Enables input/output pin regardless of any other state. This is
3606 * intended for use with microphone bias supplies used in microphone
3609 * Requires external locking.
3611 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3612 * do any widget power switching.
3614 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3617 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3620 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3624 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3625 if (!w->connected) {
3627 * w->force does not affect the number of input or output paths,
3628 * so we only have to recheck if w->connected is changed
3630 dapm_widget_invalidate_input_paths(w);
3631 dapm_widget_invalidate_output_paths(w);
3635 dapm_mark_dirty(w, "force enable");
3639 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3642 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3643 * @dapm: DAPM context
3646 * Enables input/output pin regardless of any other state. This is
3647 * intended for use with microphone bias supplies used in microphone
3650 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3651 * do any widget power switching.
3653 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3658 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3660 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
3662 mutex_unlock(&dapm->card->dapm_mutex);
3666 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3669 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3670 * @dapm: DAPM context
3673 * Disables input/output pin and its parents or children widgets.
3675 * Requires external locking.
3677 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3678 * do any widget power switching.
3680 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3683 return snd_soc_dapm_set_pin(dapm, pin, 0);
3685 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3688 * snd_soc_dapm_disable_pin - disable pin.
3689 * @dapm: DAPM context
3692 * Disables input/output pin and its parents or children widgets.
3694 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3695 * do any widget power switching.
3697 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3702 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3704 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3706 mutex_unlock(&dapm->card->dapm_mutex);
3710 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3713 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3714 * @dapm: DAPM context
3717 * Marks the specified pin as being not connected, disabling it along
3718 * any parent or child widgets. At present this is identical to
3719 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3720 * additional things such as disabling controls which only affect
3721 * paths through the pin.
3723 * Requires external locking.
3725 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3726 * do any widget power switching.
3728 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3731 return snd_soc_dapm_set_pin(dapm, pin, 0);
3733 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3736 * snd_soc_dapm_nc_pin - permanently disable pin.
3737 * @dapm: DAPM context
3740 * Marks the specified pin as being not connected, disabling it along
3741 * any parent or child widgets. At present this is identical to
3742 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3743 * additional things such as disabling controls which only affect
3744 * paths through the pin.
3746 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3747 * do any widget power switching.
3749 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3753 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3755 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3757 mutex_unlock(&dapm->card->dapm_mutex);
3761 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3764 * snd_soc_dapm_get_pin_status - get audio pin status
3765 * @dapm: DAPM context
3766 * @pin: audio signal pin endpoint (or start point)
3768 * Get audio pin status - connected or disconnected.
3770 * Returns 1 for connected otherwise 0.
3772 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3775 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3778 return w->connected;
3782 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3785 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3786 * @dapm: DAPM context
3787 * @pin: audio signal pin endpoint (or start point)
3789 * Mark the given endpoint or pin as ignoring suspend. When the
3790 * system is disabled a path between two endpoints flagged as ignoring
3791 * suspend will not be disabled. The path must already be enabled via
3792 * normal means at suspend time, it will not be turned on if it was not
3795 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3798 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3801 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3805 w->ignore_suspend = 1;
3809 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3812 * snd_soc_dapm_free - free dapm resources
3813 * @dapm: DAPM context
3815 * Free all dapm widgets and resources.
3817 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3819 dapm_debugfs_cleanup(dapm);
3820 dapm_free_widgets(dapm);
3821 list_del(&dapm->list);
3823 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3825 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
3827 struct snd_soc_card *card = dapm->card;
3828 struct snd_soc_dapm_widget *w;
3829 LIST_HEAD(down_list);
3832 mutex_lock(&card->dapm_mutex);
3834 list_for_each_entry(w, &dapm->card->widgets, list) {
3835 if (w->dapm != dapm)
3838 dapm_seq_insert(w, &down_list, false);
3844 /* If there were no widgets to power down we're already in
3848 if (dapm->bias_level == SND_SOC_BIAS_ON)
3849 snd_soc_dapm_set_bias_level(dapm,
3850 SND_SOC_BIAS_PREPARE);
3851 dapm_seq_run(card, &down_list, 0, false);
3852 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3853 snd_soc_dapm_set_bias_level(dapm,
3854 SND_SOC_BIAS_STANDBY);
3857 mutex_unlock(&card->dapm_mutex);
3861 * snd_soc_dapm_shutdown - callback for system shutdown
3863 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3865 struct snd_soc_dapm_context *dapm;
3867 list_for_each_entry(dapm, &card->dapm_list, list) {
3868 if (dapm != &card->dapm) {
3869 soc_dapm_shutdown_dapm(dapm);
3870 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3871 snd_soc_dapm_set_bias_level(dapm,
3876 soc_dapm_shutdown_dapm(&card->dapm);
3877 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3878 snd_soc_dapm_set_bias_level(&card->dapm,
3882 /* Module information */
3883 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3884 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3885 MODULE_LICENSE("GPL");