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);
162 void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
164 struct snd_soc_card *card = dapm->card;
165 struct snd_soc_dapm_widget *w;
167 mutex_lock(&card->dapm_mutex);
169 list_for_each_entry(w, &card->widgets, list) {
171 case snd_soc_dapm_input:
172 case snd_soc_dapm_output:
173 dapm_mark_dirty(w, "Rechecking inputs and outputs");
180 mutex_unlock(&card->dapm_mutex);
182 EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
184 /* create a new dapm widget */
185 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
186 const struct snd_soc_dapm_widget *_widget)
188 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
191 struct dapm_kcontrol_data {
193 struct snd_soc_dapm_widget *widget;
194 struct list_head paths;
195 struct snd_soc_dapm_widget_list *wlist;
198 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
199 struct snd_kcontrol *kcontrol)
201 struct dapm_kcontrol_data *data;
202 struct soc_mixer_control *mc;
204 data = kzalloc(sizeof(*data), GFP_KERNEL);
206 dev_err(widget->dapm->dev,
207 "ASoC: can't allocate kcontrol data for %s\n",
212 INIT_LIST_HEAD(&data->paths);
214 switch (widget->id) {
215 case snd_soc_dapm_switch:
216 case snd_soc_dapm_mixer:
217 case snd_soc_dapm_mixer_named_ctl:
218 mc = (struct soc_mixer_control *)kcontrol->private_value;
220 if (mc->autodisable) {
221 struct snd_soc_dapm_widget template;
223 memset(&template, 0, sizeof(template));
224 template.reg = mc->reg;
225 template.mask = (1 << fls(mc->max)) - 1;
226 template.shift = mc->shift;
228 template.off_val = mc->max;
230 template.off_val = 0;
231 template.on_val = template.off_val;
232 template.id = snd_soc_dapm_kcontrol;
233 template.name = kcontrol->id.name;
235 data->value = template.on_val;
237 data->widget = snd_soc_dapm_new_control(widget->dapm,
249 kcontrol->private_data = data;
254 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
256 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
261 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
262 const struct snd_kcontrol *kcontrol)
264 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
269 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
270 struct snd_soc_dapm_widget *widget)
272 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
273 struct snd_soc_dapm_widget_list *new_wlist;
277 n = data->wlist->num_widgets + 1;
281 new_wlist = krealloc(data->wlist,
282 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
286 new_wlist->widgets[n - 1] = widget;
287 new_wlist->num_widgets = n;
289 data->wlist = new_wlist;
294 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
295 struct snd_soc_dapm_path *path)
297 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
299 list_add_tail(&path->list_kcontrol, &data->paths);
302 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
303 path->source, NULL, NULL);
307 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
309 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
314 return data->widget->power;
317 static struct list_head *dapm_kcontrol_get_path_list(
318 const struct snd_kcontrol *kcontrol)
320 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
325 #define dapm_kcontrol_for_each_path(path, kcontrol) \
326 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
329 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
331 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
335 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
337 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
340 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
342 if (data->value == value)
346 data->widget->on_val = value;
354 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
356 * @kcontrol: The kcontrol
358 * Note: This function must only be used on kcontrols that are known to have
359 * been registered for a CODEC. Otherwise the behaviour is undefined.
361 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
362 struct snd_kcontrol *kcontrol)
364 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
366 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
369 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
370 * @kcontrol: The kcontrol
372 struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
374 return snd_soc_dapm_to_codec(snd_soc_dapm_kcontrol_dapm(kcontrol));
376 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
378 static void dapm_reset(struct snd_soc_card *card)
380 struct snd_soc_dapm_widget *w;
382 lockdep_assert_held(&card->dapm_mutex);
384 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
386 list_for_each_entry(w, &card->widgets, list) {
387 w->new_power = w->power;
388 w->power_checked = false;
394 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
396 if (!dapm->component)
398 return dapm->component->name_prefix;
401 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
404 if (!dapm->component)
406 return snd_soc_component_read(dapm->component, reg, value);
409 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
410 int reg, unsigned int mask, unsigned int value)
412 if (!dapm->component)
414 return snd_soc_component_update_bits_async(dapm->component, reg,
418 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
419 int reg, unsigned int mask, unsigned int value)
421 if (!dapm->component)
423 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
426 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
429 snd_soc_component_async_complete(dapm->component);
433 * snd_soc_dapm_set_bias_level - set the bias level for the system
434 * @dapm: DAPM context
435 * @level: level to configure
437 * Configure the bias (power) levels for the SoC audio device.
439 * Returns 0 for success else error.
441 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
442 enum snd_soc_bias_level level)
444 struct snd_soc_card *card = dapm->card;
447 trace_snd_soc_bias_level_start(card, level);
449 if (card && card->set_bias_level)
450 ret = card->set_bias_level(card, dapm, level);
454 if (dapm->set_bias_level)
455 ret = dapm->set_bias_level(dapm, level);
456 else if (!card || dapm != &card->dapm)
457 dapm->bias_level = level;
462 if (card && card->set_bias_level_post)
463 ret = card->set_bias_level_post(card, dapm, level);
465 trace_snd_soc_bias_level_done(card, level);
470 /* connect mux widget to its interconnecting audio paths */
471 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
472 struct snd_soc_dapm_path *path, const char *control_name)
474 const struct snd_kcontrol_new *kcontrol = &path->sink->kcontrol_news[0];
475 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
476 unsigned int val, item;
479 if (e->reg != SND_SOC_NOPM) {
480 soc_dapm_read(dapm, e->reg, &val);
481 val = (val >> e->shift_l) & e->mask;
482 item = snd_soc_enum_val_to_item(e, val);
484 /* since a virtual mux has no backing registers to
485 * decide which path to connect, it will try to match
486 * with the first enumeration. This is to ensure
487 * that the default mux choice (the first) will be
488 * correctly powered up during initialization.
493 for (i = 0; i < e->items; i++) {
494 if (!(strcmp(control_name, e->texts[i]))) {
495 path->name = e->texts[i];
507 /* set up initial codec paths */
508 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i)
510 struct soc_mixer_control *mc = (struct soc_mixer_control *)
511 p->sink->kcontrol_news[i].private_value;
512 unsigned int reg = mc->reg;
513 unsigned int shift = mc->shift;
514 unsigned int max = mc->max;
515 unsigned int mask = (1 << fls(max)) - 1;
516 unsigned int invert = mc->invert;
519 if (reg != SND_SOC_NOPM) {
520 soc_dapm_read(p->sink->dapm, reg, &val);
521 val = (val >> shift) & mask;
530 /* connect mixer widget to its interconnecting audio paths */
531 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
532 struct snd_soc_dapm_path *path, const char *control_name)
536 /* search for mixer kcontrol */
537 for (i = 0; i < path->sink->num_kcontrols; i++) {
538 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
539 path->name = path->sink->kcontrol_news[i].name;
540 dapm_set_mixer_path_status(path, i);
547 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
548 struct snd_soc_dapm_widget *kcontrolw,
549 const struct snd_kcontrol_new *kcontrol_new,
550 struct snd_kcontrol **kcontrol)
552 struct snd_soc_dapm_widget *w;
557 list_for_each_entry(w, &dapm->card->widgets, list) {
558 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
560 for (i = 0; i < w->num_kcontrols; i++) {
561 if (&w->kcontrol_news[i] == kcontrol_new) {
563 *kcontrol = w->kcontrols[i];
573 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
574 * create it. Either way, add the widget into the control's widget list
576 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
579 struct snd_soc_dapm_context *dapm = w->dapm;
580 struct snd_card *card = dapm->card->snd_card;
584 struct snd_kcontrol *kcontrol;
585 bool wname_in_long_name, kcname_in_long_name;
586 char *long_name = NULL;
590 prefix = soc_dapm_prefix(dapm);
592 prefix_len = strlen(prefix) + 1;
596 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
601 wname_in_long_name = false;
602 kcname_in_long_name = true;
605 case snd_soc_dapm_switch:
606 case snd_soc_dapm_mixer:
607 wname_in_long_name = true;
608 kcname_in_long_name = true;
610 case snd_soc_dapm_mixer_named_ctl:
611 wname_in_long_name = false;
612 kcname_in_long_name = true;
614 case snd_soc_dapm_mux:
615 wname_in_long_name = true;
616 kcname_in_long_name = false;
623 if (wname_in_long_name && kcname_in_long_name) {
625 * The control will get a prefix from the control
626 * creation process but we're also using the same
627 * prefix for widgets so cut the prefix off the
628 * front of the widget name.
630 long_name = kasprintf(GFP_KERNEL, "%s %s",
631 w->name + prefix_len,
632 w->kcontrol_news[kci].name);
633 if (long_name == NULL)
637 } else if (wname_in_long_name) {
639 name = w->name + prefix_len;
642 name = w->kcontrol_news[kci].name;
645 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
652 kcontrol->private_free = dapm_kcontrol_free;
654 ret = dapm_kcontrol_data_alloc(w, kcontrol);
656 snd_ctl_free_one(kcontrol);
660 ret = snd_ctl_add(card, kcontrol);
663 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
669 ret = dapm_kcontrol_add_widget(kcontrol, w);
671 w->kcontrols[kci] = kcontrol;
679 /* create new dapm mixer control */
680 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
683 struct snd_soc_dapm_path *path;
686 for (i = 0; i < w->num_kcontrols; i++) {
688 list_for_each_entry(path, &w->sources, list_sink) {
689 /* mixer/mux paths name must match control name */
690 if (path->name != (char *)w->kcontrol_news[i].name)
693 if (w->kcontrols[i]) {
694 dapm_kcontrol_add_path(w->kcontrols[i], path);
698 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
702 dapm_kcontrol_add_path(w->kcontrols[i], path);
709 /* create new dapm mux control */
710 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
712 struct snd_soc_dapm_context *dapm = w->dapm;
713 struct snd_soc_dapm_path *path;
716 if (w->num_kcontrols != 1) {
718 "ASoC: mux %s has incorrect number of controls\n",
723 if (list_empty(&w->sources)) {
724 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
728 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
732 list_for_each_entry(path, &w->sources, list_sink) {
734 dapm_kcontrol_add_path(w->kcontrols[0], path);
740 /* create new dapm volume control */
741 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
743 if (w->num_kcontrols)
744 dev_err(w->dapm->dev,
745 "ASoC: PGA controls not supported: '%s'\n", w->name);
750 /* We implement power down on suspend by checking the power state of
751 * the ALSA card - when we are suspending the ALSA state for the card
754 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
756 int level = snd_power_get_state(widget->dapm->card->snd_card);
759 case SNDRV_CTL_POWER_D3hot:
760 case SNDRV_CTL_POWER_D3cold:
761 if (widget->ignore_suspend)
762 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
764 return widget->ignore_suspend;
770 /* add widget to list if it's not already in the list */
771 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
772 struct snd_soc_dapm_widget *w)
774 struct snd_soc_dapm_widget_list *wlist;
775 int wlistsize, wlistentries, i;
782 /* is this widget already in the list */
783 for (i = 0; i < wlist->num_widgets; i++) {
784 if (wlist->widgets[i] == w)
788 /* allocate some new space */
789 wlistentries = wlist->num_widgets + 1;
790 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
791 wlistentries * sizeof(struct snd_soc_dapm_widget *);
792 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
794 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
800 /* insert the widget */
801 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
802 w->name, wlist->num_widgets);
804 wlist->widgets[wlist->num_widgets] = w;
805 wlist->num_widgets++;
810 * Recursively check for a completed path to an active or physically connected
811 * output widget. Returns number of complete paths.
813 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
814 struct snd_soc_dapm_widget_list **list)
816 struct snd_soc_dapm_path *path;
819 if (widget->outputs >= 0)
820 return widget->outputs;
822 DAPM_UPDATE_STAT(widget, path_checks);
824 if (widget->is_sink && widget->connected) {
825 widget->outputs = snd_soc_dapm_suspend_check(widget);
826 return widget->outputs;
829 list_for_each_entry(path, &widget->sinks, list_source) {
830 DAPM_UPDATE_STAT(widget, neighbour_checks);
832 if (path->weak || path->is_supply)
838 trace_snd_soc_dapm_output_path(widget, path);
843 /* do we need to add this widget to the list ? */
846 err = dapm_list_add_widget(list, path->sink);
848 dev_err(widget->dapm->dev,
849 "ASoC: could not add widget %s\n",
856 con += is_connected_output_ep(path->sink, list);
862 widget->outputs = con;
868 * Recursively check for a completed path to an active or physically connected
869 * input widget. Returns number of complete paths.
871 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
872 struct snd_soc_dapm_widget_list **list)
874 struct snd_soc_dapm_path *path;
877 if (widget->inputs >= 0)
878 return widget->inputs;
880 DAPM_UPDATE_STAT(widget, path_checks);
882 if (widget->is_source && widget->connected) {
883 widget->inputs = snd_soc_dapm_suspend_check(widget);
884 return widget->inputs;
887 list_for_each_entry(path, &widget->sources, list_sink) {
888 DAPM_UPDATE_STAT(widget, neighbour_checks);
890 if (path->weak || path->is_supply)
896 trace_snd_soc_dapm_input_path(widget, path);
901 /* do we need to add this widget to the list ? */
904 err = dapm_list_add_widget(list, path->source);
906 dev_err(widget->dapm->dev,
907 "ASoC: could not add widget %s\n",
914 con += is_connected_input_ep(path->source, list);
920 widget->inputs = con;
926 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
928 * @stream: stream direction.
929 * @list: list of active widgets for this stream.
931 * Queries DAPM graph as to whether an valid audio stream path exists for
932 * the initial stream specified by name. This takes into account
933 * current mixer and mux kcontrol settings. Creates list of valid widgets.
935 * Returns the number of valid paths or negative error.
937 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
938 struct snd_soc_dapm_widget_list **list)
940 struct snd_soc_card *card = dai->card;
943 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
946 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
947 paths = is_connected_output_ep(dai->playback_widget, list);
949 paths = is_connected_input_ep(dai->capture_widget, list);
951 trace_snd_soc_dapm_connected(paths, stream);
952 mutex_unlock(&card->dapm_mutex);
958 * Handler for regulator supply widget.
960 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
961 struct snd_kcontrol *kcontrol, int event)
965 soc_dapm_async_complete(w->dapm);
967 if (SND_SOC_DAPM_EVENT_ON(event)) {
968 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
969 ret = regulator_allow_bypass(w->regulator, false);
971 dev_warn(w->dapm->dev,
972 "ASoC: Failed to unbypass %s: %d\n",
976 return regulator_enable(w->regulator);
978 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
979 ret = regulator_allow_bypass(w->regulator, true);
981 dev_warn(w->dapm->dev,
982 "ASoC: Failed to bypass %s: %d\n",
986 return regulator_disable_deferred(w->regulator, w->shift);
989 EXPORT_SYMBOL_GPL(dapm_regulator_event);
992 * Handler for clock supply widget.
994 int dapm_clock_event(struct snd_soc_dapm_widget *w,
995 struct snd_kcontrol *kcontrol, int event)
1000 soc_dapm_async_complete(w->dapm);
1002 #ifdef CONFIG_HAVE_CLK
1003 if (SND_SOC_DAPM_EVENT_ON(event)) {
1004 return clk_prepare_enable(w->clk);
1006 clk_disable_unprepare(w->clk);
1012 EXPORT_SYMBOL_GPL(dapm_clock_event);
1014 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1016 if (w->power_checked)
1017 return w->new_power;
1022 w->new_power = w->power_check(w);
1024 w->power_checked = true;
1026 return w->new_power;
1029 /* Generic check to see if a widget should be powered.
1031 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1035 DAPM_UPDATE_STAT(w, power_checks);
1037 in = is_connected_input_ep(w, NULL);
1038 out = is_connected_output_ep(w, NULL);
1039 return out != 0 && in != 0;
1042 /* Check to see if a power supply is needed */
1043 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1045 struct snd_soc_dapm_path *path;
1047 DAPM_UPDATE_STAT(w, power_checks);
1049 /* Check if one of our outputs is connected */
1050 list_for_each_entry(path, &w->sinks, list_source) {
1051 DAPM_UPDATE_STAT(w, neighbour_checks);
1056 if (path->connected &&
1057 !path->connected(path->source, path->sink))
1060 if (dapm_widget_power_check(path->sink))
1067 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1072 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1073 struct snd_soc_dapm_widget *b,
1081 sort = dapm_down_seq;
1083 if (sort[a->id] != sort[b->id])
1084 return sort[a->id] - sort[b->id];
1085 if (a->subseq != b->subseq) {
1087 return a->subseq - b->subseq;
1089 return b->subseq - a->subseq;
1091 if (a->reg != b->reg)
1092 return a->reg - b->reg;
1093 if (a->dapm != b->dapm)
1094 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1099 /* Insert a widget in order into a DAPM power sequence. */
1100 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1101 struct list_head *list,
1104 struct snd_soc_dapm_widget *w;
1106 list_for_each_entry(w, list, power_list)
1107 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1108 list_add_tail(&new_widget->power_list, &w->power_list);
1112 list_add_tail(&new_widget->power_list, list);
1115 static void dapm_seq_check_event(struct snd_soc_card *card,
1116 struct snd_soc_dapm_widget *w, int event)
1118 const char *ev_name;
1122 case SND_SOC_DAPM_PRE_PMU:
1123 ev_name = "PRE_PMU";
1126 case SND_SOC_DAPM_POST_PMU:
1127 ev_name = "POST_PMU";
1130 case SND_SOC_DAPM_PRE_PMD:
1131 ev_name = "PRE_PMD";
1134 case SND_SOC_DAPM_POST_PMD:
1135 ev_name = "POST_PMD";
1138 case SND_SOC_DAPM_WILL_PMU:
1139 ev_name = "WILL_PMU";
1142 case SND_SOC_DAPM_WILL_PMD:
1143 ev_name = "WILL_PMD";
1147 WARN(1, "Unknown event %d\n", event);
1151 if (w->new_power != power)
1154 if (w->event && (w->event_flags & event)) {
1155 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1157 soc_dapm_async_complete(w->dapm);
1158 trace_snd_soc_dapm_widget_event_start(w, event);
1159 ret = w->event(w, NULL, event);
1160 trace_snd_soc_dapm_widget_event_done(w, event);
1162 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1163 ev_name, w->name, ret);
1167 /* Apply the coalesced changes from a DAPM sequence */
1168 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1169 struct list_head *pending)
1171 struct snd_soc_dapm_context *dapm;
1172 struct snd_soc_dapm_widget *w;
1174 unsigned int value = 0;
1175 unsigned int mask = 0;
1177 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1181 list_for_each_entry(w, pending, power_list) {
1182 WARN_ON(reg != w->reg || dapm != w->dapm);
1183 w->power = w->new_power;
1185 mask |= w->mask << w->shift;
1187 value |= w->on_val << w->shift;
1189 value |= w->off_val << w->shift;
1191 pop_dbg(dapm->dev, card->pop_time,
1192 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1193 w->name, reg, value, mask);
1195 /* Check for events */
1196 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1197 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1201 /* Any widget will do, they should all be updating the
1205 pop_dbg(dapm->dev, card->pop_time,
1206 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1207 value, mask, reg, card->pop_time);
1208 pop_wait(card->pop_time);
1209 soc_dapm_update_bits(dapm, reg, mask, value);
1212 list_for_each_entry(w, pending, power_list) {
1213 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1214 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1218 /* Apply a DAPM power sequence.
1220 * We walk over a pre-sorted list of widgets to apply power to. In
1221 * order to minimise the number of writes to the device required
1222 * multiple widgets will be updated in a single write where possible.
1223 * Currently anything that requires more than a single write is not
1226 static void dapm_seq_run(struct snd_soc_card *card,
1227 struct list_head *list, int event, bool power_up)
1229 struct snd_soc_dapm_widget *w, *n;
1230 struct snd_soc_dapm_context *d;
1233 int cur_subseq = -1;
1234 int cur_reg = SND_SOC_NOPM;
1235 struct snd_soc_dapm_context *cur_dapm = NULL;
1242 sort = dapm_down_seq;
1244 list_for_each_entry_safe(w, n, list, power_list) {
1247 /* Do we need to apply any queued changes? */
1248 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1249 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1250 if (!list_empty(&pending))
1251 dapm_seq_run_coalesced(card, &pending);
1253 if (cur_dapm && cur_dapm->seq_notifier) {
1254 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1255 if (sort[i] == cur_sort)
1256 cur_dapm->seq_notifier(cur_dapm,
1261 if (cur_dapm && w->dapm != cur_dapm)
1262 soc_dapm_async_complete(cur_dapm);
1264 INIT_LIST_HEAD(&pending);
1266 cur_subseq = INT_MIN;
1267 cur_reg = SND_SOC_NOPM;
1272 case snd_soc_dapm_pre:
1274 list_for_each_entry_safe_continue(w, n, list,
1277 if (event == SND_SOC_DAPM_STREAM_START)
1279 NULL, SND_SOC_DAPM_PRE_PMU);
1280 else if (event == SND_SOC_DAPM_STREAM_STOP)
1282 NULL, SND_SOC_DAPM_PRE_PMD);
1285 case snd_soc_dapm_post:
1287 list_for_each_entry_safe_continue(w, n, list,
1290 if (event == SND_SOC_DAPM_STREAM_START)
1292 NULL, SND_SOC_DAPM_POST_PMU);
1293 else if (event == SND_SOC_DAPM_STREAM_STOP)
1295 NULL, SND_SOC_DAPM_POST_PMD);
1299 /* Queue it up for application */
1300 cur_sort = sort[w->id];
1301 cur_subseq = w->subseq;
1304 list_move(&w->power_list, &pending);
1309 dev_err(w->dapm->dev,
1310 "ASoC: Failed to apply widget power: %d\n", ret);
1313 if (!list_empty(&pending))
1314 dapm_seq_run_coalesced(card, &pending);
1316 if (cur_dapm && cur_dapm->seq_notifier) {
1317 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1318 if (sort[i] == cur_sort)
1319 cur_dapm->seq_notifier(cur_dapm,
1323 list_for_each_entry(d, &card->dapm_list, list) {
1324 soc_dapm_async_complete(d);
1328 static void dapm_widget_update(struct snd_soc_card *card)
1330 struct snd_soc_dapm_update *update = card->update;
1331 struct snd_soc_dapm_widget_list *wlist;
1332 struct snd_soc_dapm_widget *w = NULL;
1336 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1339 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1341 for (wi = 0; wi < wlist->num_widgets; wi++) {
1342 w = wlist->widgets[wi];
1344 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1345 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1347 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1355 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1358 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1361 for (wi = 0; wi < wlist->num_widgets; wi++) {
1362 w = wlist->widgets[wi];
1364 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1365 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1367 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1373 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1374 * they're changing state.
1376 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1378 struct snd_soc_dapm_context *d = data;
1381 /* If we're off and we're not supposed to be go into STANDBY */
1382 if (d->bias_level == SND_SOC_BIAS_OFF &&
1383 d->target_bias_level != SND_SOC_BIAS_OFF) {
1385 pm_runtime_get_sync(d->dev);
1387 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1390 "ASoC: Failed to turn on bias: %d\n", ret);
1393 /* Prepare for a transition to ON or away from ON */
1394 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1395 d->bias_level != SND_SOC_BIAS_ON) ||
1396 (d->target_bias_level != SND_SOC_BIAS_ON &&
1397 d->bias_level == SND_SOC_BIAS_ON)) {
1398 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1401 "ASoC: Failed to prepare bias: %d\n", ret);
1405 /* Async callback run prior to DAPM sequences - brings to their final
1408 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1410 struct snd_soc_dapm_context *d = data;
1413 /* If we just powered the last thing off drop to standby bias */
1414 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1415 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1416 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1417 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1419 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1423 /* If we're in standby and can support bias off then do that */
1424 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1425 d->target_bias_level == SND_SOC_BIAS_OFF) {
1426 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1428 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1432 pm_runtime_put(d->dev);
1435 /* If we just powered up then move to active bias */
1436 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1437 d->target_bias_level == SND_SOC_BIAS_ON) {
1438 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1440 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1445 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1446 bool power, bool connect)
1448 /* If a connection is being made or broken then that update
1449 * will have marked the peer dirty, otherwise the widgets are
1450 * not connected and this update has no impact. */
1454 /* If the peer is already in the state we're moving to then we
1455 * won't have an impact on it. */
1456 if (power != peer->power)
1457 dapm_mark_dirty(peer, "peer state change");
1460 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1461 struct list_head *up_list,
1462 struct list_head *down_list)
1464 struct snd_soc_dapm_path *path;
1466 if (w->power == power)
1469 trace_snd_soc_dapm_widget_power(w, power);
1471 /* If we changed our power state perhaps our neigbours changed
1474 list_for_each_entry(path, &w->sources, list_sink)
1475 dapm_widget_set_peer_power(path->source, power, path->connect);
1477 /* Supplies can't affect their outputs, only their inputs */
1478 if (!w->is_supply) {
1479 list_for_each_entry(path, &w->sinks, list_source)
1480 dapm_widget_set_peer_power(path->sink, power,
1485 dapm_seq_insert(w, up_list, true);
1487 dapm_seq_insert(w, down_list, false);
1490 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1491 struct list_head *up_list,
1492 struct list_head *down_list)
1497 case snd_soc_dapm_pre:
1498 dapm_seq_insert(w, down_list, false);
1500 case snd_soc_dapm_post:
1501 dapm_seq_insert(w, up_list, true);
1505 power = dapm_widget_power_check(w);
1507 dapm_widget_set_power(w, power, up_list, down_list);
1512 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1514 if (dapm->idle_bias_off)
1517 switch (snd_power_get_state(dapm->card->snd_card)) {
1518 case SNDRV_CTL_POWER_D3hot:
1519 case SNDRV_CTL_POWER_D3cold:
1520 return dapm->suspend_bias_off;
1529 * Scan each dapm widget for complete audio path.
1530 * A complete path is a route that has valid endpoints i.e.:-
1532 * o DAC to output pin.
1533 * o Input Pin to ADC.
1534 * o Input pin to Output pin (bypass, sidetone)
1535 * o DAC to ADC (loopback).
1537 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1539 struct snd_soc_dapm_widget *w;
1540 struct snd_soc_dapm_context *d;
1542 LIST_HEAD(down_list);
1543 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1544 enum snd_soc_bias_level bias;
1546 lockdep_assert_held(&card->dapm_mutex);
1548 trace_snd_soc_dapm_start(card);
1550 list_for_each_entry(d, &card->dapm_list, list) {
1551 if (dapm_idle_bias_off(d))
1552 d->target_bias_level = SND_SOC_BIAS_OFF;
1554 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1559 /* Check which widgets we need to power and store them in
1560 * lists indicating if they should be powered up or down. We
1561 * only check widgets that have been flagged as dirty but note
1562 * that new widgets may be added to the dirty list while we
1565 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1566 dapm_power_one_widget(w, &up_list, &down_list);
1569 list_for_each_entry(w, &card->widgets, list) {
1571 case snd_soc_dapm_pre:
1572 case snd_soc_dapm_post:
1573 /* These widgets always need to be powered */
1576 list_del_init(&w->dirty);
1583 /* Supplies and micbiases only bring the
1584 * context up to STANDBY as unless something
1585 * else is active and passing audio they
1586 * generally don't require full power. Signal
1587 * generators are virtual pins and have no
1588 * power impact themselves.
1591 case snd_soc_dapm_siggen:
1592 case snd_soc_dapm_vmid:
1594 case snd_soc_dapm_supply:
1595 case snd_soc_dapm_regulator_supply:
1596 case snd_soc_dapm_clock_supply:
1597 case snd_soc_dapm_micbias:
1598 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1599 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1602 d->target_bias_level = SND_SOC_BIAS_ON;
1609 /* Force all contexts in the card to the same bias state if
1610 * they're not ground referenced.
1612 bias = SND_SOC_BIAS_OFF;
1613 list_for_each_entry(d, &card->dapm_list, list)
1614 if (d->target_bias_level > bias)
1615 bias = d->target_bias_level;
1616 list_for_each_entry(d, &card->dapm_list, list)
1617 if (!dapm_idle_bias_off(d))
1618 d->target_bias_level = bias;
1620 trace_snd_soc_dapm_walk_done(card);
1622 /* Run card bias changes at first */
1623 dapm_pre_sequence_async(&card->dapm, 0);
1624 /* Run other bias changes in parallel */
1625 list_for_each_entry(d, &card->dapm_list, list) {
1626 if (d != &card->dapm)
1627 async_schedule_domain(dapm_pre_sequence_async, d,
1630 async_synchronize_full_domain(&async_domain);
1632 list_for_each_entry(w, &down_list, power_list) {
1633 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1636 list_for_each_entry(w, &up_list, power_list) {
1637 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1640 /* Power down widgets first; try to avoid amplifying pops. */
1641 dapm_seq_run(card, &down_list, event, false);
1643 dapm_widget_update(card);
1646 dapm_seq_run(card, &up_list, event, true);
1648 /* Run all the bias changes in parallel */
1649 list_for_each_entry(d, &card->dapm_list, list) {
1650 if (d != &card->dapm)
1651 async_schedule_domain(dapm_post_sequence_async, d,
1654 async_synchronize_full_domain(&async_domain);
1655 /* Run card bias changes at last */
1656 dapm_post_sequence_async(&card->dapm, 0);
1658 /* do we need to notify any clients that DAPM event is complete */
1659 list_for_each_entry(d, &card->dapm_list, list) {
1660 if (d->stream_event)
1661 d->stream_event(d, event);
1664 pop_dbg(card->dev, card->pop_time,
1665 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1666 pop_wait(card->pop_time);
1668 trace_snd_soc_dapm_done(card);
1673 #ifdef CONFIG_DEBUG_FS
1674 static ssize_t dapm_widget_power_read_file(struct file *file,
1675 char __user *user_buf,
1676 size_t count, loff_t *ppos)
1678 struct snd_soc_dapm_widget *w = file->private_data;
1682 struct snd_soc_dapm_path *p = NULL;
1684 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1688 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
1693 in = is_connected_input_ep(w, NULL);
1694 out = is_connected_output_ep(w, NULL);
1697 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1698 w->name, w->power ? "On" : "Off",
1699 w->force ? " (forced)" : "", in, out);
1702 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1703 " - R%d(0x%x) mask 0x%x",
1704 w->reg, w->reg, w->mask << w->shift);
1706 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1709 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1711 w->active ? "active" : "inactive");
1713 list_for_each_entry(p, &w->sources, list_sink) {
1714 if (p->connected && !p->connected(w, p->source))
1718 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1719 " in \"%s\" \"%s\"\n",
1720 p->name ? p->name : "static",
1723 list_for_each_entry(p, &w->sinks, list_source) {
1724 if (p->connected && !p->connected(w, p->sink))
1728 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1729 " out \"%s\" \"%s\"\n",
1730 p->name ? p->name : "static",
1734 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1740 static const struct file_operations dapm_widget_power_fops = {
1741 .open = simple_open,
1742 .read = dapm_widget_power_read_file,
1743 .llseek = default_llseek,
1746 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1747 size_t count, loff_t *ppos)
1749 struct snd_soc_dapm_context *dapm = file->private_data;
1752 switch (dapm->bias_level) {
1753 case SND_SOC_BIAS_ON:
1756 case SND_SOC_BIAS_PREPARE:
1757 level = "Prepare\n";
1759 case SND_SOC_BIAS_STANDBY:
1760 level = "Standby\n";
1762 case SND_SOC_BIAS_OFF:
1766 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
1767 level = "Unknown\n";
1771 return simple_read_from_buffer(user_buf, count, ppos, level,
1775 static const struct file_operations dapm_bias_fops = {
1776 .open = simple_open,
1777 .read = dapm_bias_read_file,
1778 .llseek = default_llseek,
1781 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1782 struct dentry *parent)
1786 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1788 if (!dapm->debugfs_dapm) {
1790 "ASoC: Failed to create DAPM debugfs directory\n");
1794 d = debugfs_create_file("bias_level", 0444,
1795 dapm->debugfs_dapm, dapm,
1799 "ASoC: Failed to create bias level debugfs file\n");
1802 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1804 struct snd_soc_dapm_context *dapm = w->dapm;
1807 if (!dapm->debugfs_dapm || !w->name)
1810 d = debugfs_create_file(w->name, 0444,
1811 dapm->debugfs_dapm, w,
1812 &dapm_widget_power_fops);
1814 dev_warn(w->dapm->dev,
1815 "ASoC: Failed to create %s debugfs file\n",
1819 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1821 debugfs_remove_recursive(dapm->debugfs_dapm);
1825 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1826 struct dentry *parent)
1830 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1834 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1841 * soc_dapm_connect_path() - Connects or disconnects a path
1842 * @path: The path to update
1843 * @connect: The new connect state of the path. True if the path is connected,
1844 * false if it is disconneted.
1845 * @reason: The reason why the path changed (for debugging only)
1847 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
1848 bool connect, const char *reason)
1850 if (path->connect == connect)
1853 path->connect = connect;
1854 dapm_mark_dirty(path->source, reason);
1855 dapm_mark_dirty(path->sink, reason);
1858 /* test and update the power status of a mux widget */
1859 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
1860 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1862 struct snd_soc_dapm_path *path;
1866 lockdep_assert_held(&card->dapm_mutex);
1868 /* find dapm widget path assoc with kcontrol */
1869 dapm_kcontrol_for_each_path(path, kcontrol) {
1871 /* we now need to match the string in the enum to the path */
1872 if (!(strcmp(path->name, e->texts[mux])))
1877 soc_dapm_connect_path(path, connect, "mux update");
1881 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
1886 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
1887 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
1888 struct snd_soc_dapm_update *update)
1890 struct snd_soc_card *card = dapm->card;
1893 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1894 card->update = update;
1895 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
1896 card->update = NULL;
1897 mutex_unlock(&card->dapm_mutex);
1899 soc_dpcm_runtime_update(card);
1902 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1904 /* test and update the power status of a mixer or switch widget */
1905 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
1906 struct snd_kcontrol *kcontrol, int connect)
1908 struct snd_soc_dapm_path *path;
1911 lockdep_assert_held(&card->dapm_mutex);
1913 /* find dapm widget path assoc with kcontrol */
1914 dapm_kcontrol_for_each_path(path, kcontrol) {
1916 soc_dapm_connect_path(path, connect, "mixer update");
1920 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
1925 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
1926 struct snd_kcontrol *kcontrol, int connect,
1927 struct snd_soc_dapm_update *update)
1929 struct snd_soc_card *card = dapm->card;
1932 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1933 card->update = update;
1934 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
1935 card->update = NULL;
1936 mutex_unlock(&card->dapm_mutex);
1938 soc_dpcm_runtime_update(card);
1941 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
1943 static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
1945 struct snd_soc_dapm_widget *w;
1947 char *state = "not set";
1949 list_for_each_entry(w, &codec->component.card->widgets, list) {
1950 if (w->dapm != &codec->dapm)
1953 /* only display widgets that burnm power */
1955 case snd_soc_dapm_hp:
1956 case snd_soc_dapm_mic:
1957 case snd_soc_dapm_spk:
1958 case snd_soc_dapm_line:
1959 case snd_soc_dapm_micbias:
1960 case snd_soc_dapm_dac:
1961 case snd_soc_dapm_adc:
1962 case snd_soc_dapm_pga:
1963 case snd_soc_dapm_out_drv:
1964 case snd_soc_dapm_mixer:
1965 case snd_soc_dapm_mixer_named_ctl:
1966 case snd_soc_dapm_supply:
1967 case snd_soc_dapm_regulator_supply:
1968 case snd_soc_dapm_clock_supply:
1970 count += sprintf(buf + count, "%s: %s\n",
1971 w->name, w->power ? "On":"Off");
1978 switch (codec->dapm.bias_level) {
1979 case SND_SOC_BIAS_ON:
1982 case SND_SOC_BIAS_PREPARE:
1985 case SND_SOC_BIAS_STANDBY:
1988 case SND_SOC_BIAS_OFF:
1992 count += sprintf(buf + count, "PM State: %s\n", state);
1997 /* show dapm widget status in sys fs */
1998 static ssize_t dapm_widget_show(struct device *dev,
1999 struct device_attribute *attr, char *buf)
2001 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2004 for (i = 0; i < rtd->num_codecs; i++) {
2005 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2006 count += dapm_widget_show_codec(codec, buf + count);
2012 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2014 int snd_soc_dapm_sys_add(struct device *dev)
2016 return device_create_file(dev, &dev_attr_dapm_widget);
2019 static void snd_soc_dapm_sys_remove(struct device *dev)
2021 device_remove_file(dev, &dev_attr_dapm_widget);
2024 static void dapm_free_path(struct snd_soc_dapm_path *path)
2026 list_del(&path->list_sink);
2027 list_del(&path->list_source);
2028 list_del(&path->list_kcontrol);
2029 list_del(&path->list);
2033 /* free all dapm widgets and resources */
2034 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2036 struct snd_soc_dapm_widget *w, *next_w;
2037 struct snd_soc_dapm_path *p, *next_p;
2039 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2040 if (w->dapm != dapm)
2044 * remove source and sink paths associated to this widget.
2045 * While removing the path, remove reference to it from both
2046 * source and sink widgets so that path is removed only once.
2048 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2051 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2054 kfree(w->kcontrols);
2060 static struct snd_soc_dapm_widget *dapm_find_widget(
2061 struct snd_soc_dapm_context *dapm, const char *pin,
2062 bool search_other_contexts)
2064 struct snd_soc_dapm_widget *w;
2065 struct snd_soc_dapm_widget *fallback = NULL;
2067 list_for_each_entry(w, &dapm->card->widgets, list) {
2068 if (!strcmp(w->name, pin)) {
2069 if (w->dapm == dapm)
2076 if (search_other_contexts)
2082 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2083 const char *pin, int status)
2085 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2087 dapm_assert_locked(dapm);
2090 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2094 if (w->connected != status)
2095 dapm_mark_dirty(w, "pin configuration");
2097 w->connected = status;
2105 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2106 * @dapm: DAPM context
2108 * Walks all dapm audio paths and powers widgets according to their
2109 * stream or path usage.
2111 * Requires external locking.
2113 * Returns 0 for success.
2115 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2118 * Suppress early reports (eg, jacks syncing their state) to avoid
2119 * silly DAPM runs during card startup.
2121 if (!dapm->card || !dapm->card->instantiated)
2124 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2126 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2129 * snd_soc_dapm_sync - scan and power dapm paths
2130 * @dapm: DAPM context
2132 * Walks all dapm audio paths and powers widgets according to their
2133 * stream or path usage.
2135 * Returns 0 for success.
2137 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2141 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2142 ret = snd_soc_dapm_sync_unlocked(dapm);
2143 mutex_unlock(&dapm->card->dapm_mutex);
2146 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2149 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2150 * @w: The widget for which to update the flags
2152 * Some widgets have a dynamic category which depends on which neighbors they
2153 * are connected to. This function update the category for these widgets.
2155 * This function must be called whenever a path is added or removed to a widget.
2157 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2159 struct snd_soc_dapm_path *p;
2162 case snd_soc_dapm_input:
2164 list_for_each_entry(p, &w->sources, list_sink) {
2165 if (p->source->id == snd_soc_dapm_micbias ||
2166 p->source->id == snd_soc_dapm_mic ||
2167 p->source->id == snd_soc_dapm_line ||
2168 p->source->id == snd_soc_dapm_output) {
2174 case snd_soc_dapm_output:
2176 list_for_each_entry(p, &w->sinks, list_source) {
2177 if (p->sink->id == snd_soc_dapm_spk ||
2178 p->sink->id == snd_soc_dapm_hp ||
2179 p->sink->id == snd_soc_dapm_line ||
2180 p->sink->id == snd_soc_dapm_input) {
2186 case snd_soc_dapm_line:
2187 w->is_sink = !list_empty(&w->sources);
2188 w->is_source = !list_empty(&w->sinks);
2195 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2196 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2197 const char *control,
2198 int (*connected)(struct snd_soc_dapm_widget *source,
2199 struct snd_soc_dapm_widget *sink))
2201 struct snd_soc_dapm_path *path;
2204 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2208 path->source = wsource;
2210 path->connected = connected;
2211 INIT_LIST_HEAD(&path->list);
2212 INIT_LIST_HEAD(&path->list_kcontrol);
2213 INIT_LIST_HEAD(&path->list_source);
2214 INIT_LIST_HEAD(&path->list_sink);
2216 if (wsource->is_supply || wsink->is_supply)
2217 path->is_supply = 1;
2219 /* connect static paths */
2220 if (control == NULL) {
2223 /* connect dynamic paths */
2224 switch (wsink->id) {
2225 case snd_soc_dapm_mux:
2226 ret = dapm_connect_mux(dapm, path, control);
2230 case snd_soc_dapm_switch:
2231 case snd_soc_dapm_mixer:
2232 case snd_soc_dapm_mixer_named_ctl:
2233 ret = dapm_connect_mixer(dapm, path, control);
2239 "Control not supported for path %s -> [%s] -> %s\n",
2240 wsource->name, control, wsink->name);
2246 list_add(&path->list, &dapm->card->paths);
2247 list_add(&path->list_sink, &wsink->sources);
2248 list_add(&path->list_source, &wsource->sinks);
2250 dapm_update_widget_flags(wsource);
2251 dapm_update_widget_flags(wsink);
2253 dapm_mark_dirty(wsource, "Route added");
2254 dapm_mark_dirty(wsink, "Route added");
2262 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2263 const struct snd_soc_dapm_route *route)
2265 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2266 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2269 char prefixed_sink[80];
2270 char prefixed_source[80];
2274 prefix = soc_dapm_prefix(dapm);
2276 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2277 prefix, route->sink);
2278 sink = prefixed_sink;
2279 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2280 prefix, route->source);
2281 source = prefixed_source;
2284 source = route->source;
2288 * find src and dest widgets over all widgets but favor a widget from
2289 * current DAPM context
2291 list_for_each_entry(w, &dapm->card->widgets, list) {
2292 if (!wsink && !(strcmp(w->name, sink))) {
2294 if (w->dapm == dapm)
2298 if (!wsource && !(strcmp(w->name, source))) {
2300 if (w->dapm == dapm)
2304 /* use widget from another DAPM context if not found from this */
2310 if (wsource == NULL) {
2311 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2315 if (wsink == NULL) {
2316 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2321 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2328 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2329 source, route->control, sink);
2333 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2334 const struct snd_soc_dapm_route *route)
2336 struct snd_soc_dapm_widget *wsource, *wsink;
2337 struct snd_soc_dapm_path *path, *p;
2340 char prefixed_sink[80];
2341 char prefixed_source[80];
2344 if (route->control) {
2346 "ASoC: Removal of routes with controls not supported\n");
2350 prefix = soc_dapm_prefix(dapm);
2352 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2353 prefix, route->sink);
2354 sink = prefixed_sink;
2355 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2356 prefix, route->source);
2357 source = prefixed_source;
2360 source = route->source;
2364 list_for_each_entry(p, &dapm->card->paths, list) {
2365 if (strcmp(p->source->name, source) != 0)
2367 if (strcmp(p->sink->name, sink) != 0)
2374 wsource = path->source;
2377 dapm_mark_dirty(wsource, "Route removed");
2378 dapm_mark_dirty(wsink, "Route removed");
2380 dapm_free_path(path);
2382 /* Update any path related flags */
2383 dapm_update_widget_flags(wsource);
2384 dapm_update_widget_flags(wsink);
2386 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2394 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2395 * @dapm: DAPM context
2396 * @route: audio routes
2397 * @num: number of routes
2399 * Connects 2 dapm widgets together via a named audio path. The sink is
2400 * the widget receiving the audio signal, whilst the source is the sender
2401 * of the audio signal.
2403 * Returns 0 for success else error. On error all resources can be freed
2404 * with a call to snd_soc_card_free().
2406 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2407 const struct snd_soc_dapm_route *route, int num)
2411 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2412 for (i = 0; i < num; i++) {
2413 r = snd_soc_dapm_add_route(dapm, route);
2415 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2417 route->control ? route->control : "direct",
2423 mutex_unlock(&dapm->card->dapm_mutex);
2427 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2430 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2431 * @dapm: DAPM context
2432 * @route: audio routes
2433 * @num: number of routes
2435 * Removes routes from the DAPM context.
2437 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2438 const struct snd_soc_dapm_route *route, int num)
2442 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2443 for (i = 0; i < num; i++) {
2444 snd_soc_dapm_del_route(dapm, route);
2447 mutex_unlock(&dapm->card->dapm_mutex);
2451 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2453 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2454 const struct snd_soc_dapm_route *route)
2456 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2459 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2462 struct snd_soc_dapm_path *path;
2466 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2472 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2477 if (route->control || route->connected)
2478 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2479 route->source, route->sink);
2481 list_for_each_entry(path, &source->sinks, list_source) {
2482 if (path->sink == sink) {
2489 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2490 route->source, route->sink);
2492 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2493 count, route->source, route->sink);
2499 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2500 * @dapm: DAPM context
2501 * @route: audio routes
2502 * @num: number of routes
2504 * Mark existing routes matching those specified in the passed array
2505 * as being weak, meaning that they are ignored for the purpose of
2506 * power decisions. The main intended use case is for sidetone paths
2507 * which couple audio between other independent paths if they are both
2508 * active in order to make the combination work better at the user
2509 * level but which aren't intended to be "used".
2511 * Note that CODEC drivers should not use this as sidetone type paths
2512 * can frequently also be used as bypass paths.
2514 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2515 const struct snd_soc_dapm_route *route, int num)
2520 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2521 for (i = 0; i < num; i++) {
2522 err = snd_soc_dapm_weak_route(dapm, route);
2527 mutex_unlock(&dapm->card->dapm_mutex);
2531 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2534 * snd_soc_dapm_new_widgets - add new dapm widgets
2535 * @dapm: DAPM context
2537 * Checks the codec for any new dapm widgets and creates them if found.
2539 * Returns 0 for success.
2541 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2543 struct snd_soc_dapm_widget *w;
2546 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2548 list_for_each_entry(w, &card->widgets, list)
2553 if (w->num_kcontrols) {
2554 w->kcontrols = kzalloc(w->num_kcontrols *
2555 sizeof(struct snd_kcontrol *),
2557 if (!w->kcontrols) {
2558 mutex_unlock(&card->dapm_mutex);
2564 case snd_soc_dapm_switch:
2565 case snd_soc_dapm_mixer:
2566 case snd_soc_dapm_mixer_named_ctl:
2569 case snd_soc_dapm_mux:
2572 case snd_soc_dapm_pga:
2573 case snd_soc_dapm_out_drv:
2580 /* Read the initial power state from the device */
2582 soc_dapm_read(w->dapm, w->reg, &val);
2583 val = val >> w->shift;
2585 if (val == w->on_val)
2591 dapm_mark_dirty(w, "new widget");
2592 dapm_debugfs_add_widget(w);
2595 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2596 mutex_unlock(&card->dapm_mutex);
2599 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2602 * snd_soc_dapm_get_volsw - dapm mixer get callback
2603 * @kcontrol: mixer control
2604 * @ucontrol: control element information
2606 * Callback to get the value of a dapm mixer control.
2608 * Returns 0 for success.
2610 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_value *ucontrol)
2613 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2614 struct snd_soc_card *card = dapm->card;
2615 struct soc_mixer_control *mc =
2616 (struct soc_mixer_control *)kcontrol->private_value;
2618 unsigned int shift = mc->shift;
2620 unsigned int mask = (1 << fls(max)) - 1;
2621 unsigned int invert = mc->invert;
2625 if (snd_soc_volsw_is_stereo(mc))
2627 "ASoC: Control '%s' is stereo, which is not supported\n",
2630 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2631 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2632 ret = soc_dapm_read(dapm, reg, &val);
2633 val = (val >> shift) & mask;
2635 val = dapm_kcontrol_get_value(kcontrol);
2637 mutex_unlock(&card->dapm_mutex);
2640 ucontrol->value.integer.value[0] = max - val;
2642 ucontrol->value.integer.value[0] = val;
2646 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2649 * snd_soc_dapm_put_volsw - dapm mixer set callback
2650 * @kcontrol: mixer control
2651 * @ucontrol: control element information
2653 * Callback to set the value of a dapm mixer control.
2655 * Returns 0 for success.
2657 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2658 struct snd_ctl_elem_value *ucontrol)
2660 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2661 struct snd_soc_card *card = dapm->card;
2662 struct soc_mixer_control *mc =
2663 (struct soc_mixer_control *)kcontrol->private_value;
2665 unsigned int shift = mc->shift;
2667 unsigned int mask = (1 << fls(max)) - 1;
2668 unsigned int invert = mc->invert;
2670 int connect, change, reg_change = 0;
2671 struct snd_soc_dapm_update update;
2674 if (snd_soc_volsw_is_stereo(mc))
2676 "ASoC: Control '%s' is stereo, which is not supported\n",
2679 val = (ucontrol->value.integer.value[0] & mask);
2685 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2687 change = dapm_kcontrol_set_value(kcontrol, val);
2689 if (reg != SND_SOC_NOPM) {
2690 mask = mask << shift;
2693 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
2696 if (change || reg_change) {
2698 update.kcontrol = kcontrol;
2702 card->update = &update;
2704 change |= reg_change;
2706 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2708 card->update = NULL;
2711 mutex_unlock(&card->dapm_mutex);
2714 soc_dpcm_runtime_update(card);
2718 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2721 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2722 * @kcontrol: mixer control
2723 * @ucontrol: control element information
2725 * Callback to get the value of a dapm enumerated double mixer control.
2727 * Returns 0 for success.
2729 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2730 struct snd_ctl_elem_value *ucontrol)
2732 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2733 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2734 unsigned int reg_val, val;
2736 if (e->reg != SND_SOC_NOPM) {
2737 int ret = soc_dapm_read(dapm, e->reg, ®_val);
2741 reg_val = dapm_kcontrol_get_value(kcontrol);
2744 val = (reg_val >> e->shift_l) & e->mask;
2745 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2746 if (e->shift_l != e->shift_r) {
2747 val = (reg_val >> e->shift_r) & e->mask;
2748 val = snd_soc_enum_val_to_item(e, val);
2749 ucontrol->value.enumerated.item[1] = val;
2754 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2757 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2758 * @kcontrol: mixer control
2759 * @ucontrol: control element information
2761 * Callback to set the value of a dapm enumerated double mixer control.
2763 * Returns 0 for success.
2765 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2766 struct snd_ctl_elem_value *ucontrol)
2768 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2769 struct snd_soc_card *card = dapm->card;
2770 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2771 unsigned int *item = ucontrol->value.enumerated.item;
2772 unsigned int val, change;
2774 struct snd_soc_dapm_update update;
2777 if (item[0] >= e->items)
2780 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2781 mask = e->mask << e->shift_l;
2782 if (e->shift_l != e->shift_r) {
2783 if (item[1] > e->items)
2785 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
2786 mask |= e->mask << e->shift_r;
2789 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2791 if (e->reg != SND_SOC_NOPM)
2792 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
2794 change = dapm_kcontrol_set_value(kcontrol, val);
2797 if (e->reg != SND_SOC_NOPM) {
2798 update.kcontrol = kcontrol;
2799 update.reg = e->reg;
2802 card->update = &update;
2805 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
2807 card->update = NULL;
2810 mutex_unlock(&card->dapm_mutex);
2813 soc_dpcm_runtime_update(card);
2817 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2820 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2822 * @kcontrol: mixer control
2823 * @uinfo: control element information
2825 * Callback to provide information about a pin switch control.
2827 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2828 struct snd_ctl_elem_info *uinfo)
2830 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2832 uinfo->value.integer.min = 0;
2833 uinfo->value.integer.max = 1;
2837 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2840 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2842 * @kcontrol: mixer control
2845 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2846 struct snd_ctl_elem_value *ucontrol)
2848 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2849 const char *pin = (const char *)kcontrol->private_value;
2851 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2853 ucontrol->value.integer.value[0] =
2854 snd_soc_dapm_get_pin_status(&card->dapm, pin);
2856 mutex_unlock(&card->dapm_mutex);
2860 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2863 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2865 * @kcontrol: mixer control
2868 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_value *ucontrol)
2871 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2872 const char *pin = (const char *)kcontrol->private_value;
2874 if (ucontrol->value.integer.value[0])
2875 snd_soc_dapm_enable_pin(&card->dapm, pin);
2877 snd_soc_dapm_disable_pin(&card->dapm, pin);
2879 snd_soc_dapm_sync(&card->dapm);
2882 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2884 static struct snd_soc_dapm_widget *
2885 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2886 const struct snd_soc_dapm_widget *widget)
2888 struct snd_soc_dapm_widget *w;
2892 if ((w = dapm_cnew_widget(widget)) == NULL)
2896 case snd_soc_dapm_regulator_supply:
2897 w->regulator = devm_regulator_get(dapm->dev, w->name);
2898 if (IS_ERR(w->regulator)) {
2899 ret = PTR_ERR(w->regulator);
2900 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
2905 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
2906 ret = regulator_allow_bypass(w->regulator, true);
2908 dev_warn(w->dapm->dev,
2909 "ASoC: Failed to bypass %s: %d\n",
2913 case snd_soc_dapm_clock_supply:
2914 #ifdef CONFIG_CLKDEV_LOOKUP
2915 w->clk = devm_clk_get(dapm->dev, w->name);
2916 if (IS_ERR(w->clk)) {
2917 ret = PTR_ERR(w->clk);
2918 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
2930 prefix = soc_dapm_prefix(dapm);
2932 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
2934 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
2936 if (w->name == NULL) {
2942 case snd_soc_dapm_mic:
2943 case snd_soc_dapm_input:
2945 w->power_check = dapm_generic_check_power;
2947 case snd_soc_dapm_spk:
2948 case snd_soc_dapm_hp:
2949 case snd_soc_dapm_output:
2951 w->power_check = dapm_generic_check_power;
2953 case snd_soc_dapm_vmid:
2954 case snd_soc_dapm_siggen:
2956 w->power_check = dapm_always_on_check_power;
2958 case snd_soc_dapm_mux:
2959 case snd_soc_dapm_switch:
2960 case snd_soc_dapm_mixer:
2961 case snd_soc_dapm_mixer_named_ctl:
2962 case snd_soc_dapm_adc:
2963 case snd_soc_dapm_aif_out:
2964 case snd_soc_dapm_dac:
2965 case snd_soc_dapm_aif_in:
2966 case snd_soc_dapm_pga:
2967 case snd_soc_dapm_out_drv:
2968 case snd_soc_dapm_micbias:
2969 case snd_soc_dapm_line:
2970 case snd_soc_dapm_dai_link:
2971 case snd_soc_dapm_dai_out:
2972 case snd_soc_dapm_dai_in:
2973 w->power_check = dapm_generic_check_power;
2975 case snd_soc_dapm_supply:
2976 case snd_soc_dapm_regulator_supply:
2977 case snd_soc_dapm_clock_supply:
2978 case snd_soc_dapm_kcontrol:
2980 w->power_check = dapm_supply_check_power;
2983 w->power_check = dapm_always_on_check_power;
2988 if (dapm->component)
2989 w->codec = dapm->component->codec;
2990 INIT_LIST_HEAD(&w->sources);
2991 INIT_LIST_HEAD(&w->sinks);
2992 INIT_LIST_HEAD(&w->list);
2993 INIT_LIST_HEAD(&w->dirty);
2994 list_add(&w->list, &dapm->card->widgets);
2996 /* machine layer set ups unconnected pins and insertions */
3002 * snd_soc_dapm_new_controls - create new dapm controls
3003 * @dapm: DAPM context
3004 * @widget: widget array
3005 * @num: number of widgets
3007 * Creates new DAPM controls based upon the templates.
3009 * Returns 0 for success else error.
3011 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3012 const struct snd_soc_dapm_widget *widget,
3015 struct snd_soc_dapm_widget *w;
3019 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3020 for (i = 0; i < num; i++) {
3021 w = snd_soc_dapm_new_control(dapm, widget);
3024 "ASoC: Failed to create DAPM control %s\n",
3031 mutex_unlock(&dapm->card->dapm_mutex);
3034 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3036 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3037 struct snd_kcontrol *kcontrol, int event)
3039 struct snd_soc_dapm_path *source_p, *sink_p;
3040 struct snd_soc_dai *source, *sink;
3041 const struct snd_soc_pcm_stream *config = w->params;
3042 struct snd_pcm_substream substream;
3043 struct snd_pcm_hw_params *params = NULL;
3047 if (WARN_ON(!config) ||
3048 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3051 /* We only support a single source and sink, pick the first */
3052 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3054 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3057 if (WARN_ON(!source_p || !sink_p) ||
3058 WARN_ON(!sink_p->source || !source_p->sink) ||
3059 WARN_ON(!source_p->source || !sink_p->sink))
3062 source = source_p->source->priv;
3063 sink = sink_p->sink->priv;
3065 /* Be a little careful as we don't want to overflow the mask array */
3066 if (config->formats) {
3067 fmt = ffs(config->formats) - 1;
3069 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3074 /* Currently very limited parameter selection */
3075 params = kzalloc(sizeof(*params), GFP_KERNEL);
3080 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3082 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3084 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3087 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3088 = config->channels_min;
3089 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3090 = config->channels_max;
3092 memset(&substream, 0, sizeof(substream));
3095 case SND_SOC_DAPM_PRE_PMU:
3096 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3097 ret = soc_dai_hw_params(&substream, params, source);
3101 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3102 ret = soc_dai_hw_params(&substream, params, sink);
3107 case SND_SOC_DAPM_POST_PMU:
3108 ret = snd_soc_dai_digital_mute(sink, 0,
3109 SNDRV_PCM_STREAM_PLAYBACK);
3110 if (ret != 0 && ret != -ENOTSUPP)
3111 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3115 case SND_SOC_DAPM_PRE_PMD:
3116 ret = snd_soc_dai_digital_mute(sink, 1,
3117 SNDRV_PCM_STREAM_PLAYBACK);
3118 if (ret != 0 && ret != -ENOTSUPP)
3119 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3124 WARN(1, "Unknown event %d\n", event);
3133 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3134 const struct snd_soc_pcm_stream *params,
3135 struct snd_soc_dapm_widget *source,
3136 struct snd_soc_dapm_widget *sink)
3138 struct snd_soc_dapm_widget template;
3139 struct snd_soc_dapm_widget *w;
3144 len = strlen(source->name) + strlen(sink->name) + 2;
3145 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3148 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3150 memset(&template, 0, sizeof(template));
3151 template.reg = SND_SOC_NOPM;
3152 template.id = snd_soc_dapm_dai_link;
3153 template.name = link_name;
3154 template.event = snd_soc_dai_link_event;
3155 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3156 SND_SOC_DAPM_PRE_PMD;
3158 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3160 w = snd_soc_dapm_new_control(&card->dapm, &template);
3162 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3169 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3172 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3175 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3176 struct snd_soc_dai *dai)
3178 struct snd_soc_dapm_widget template;
3179 struct snd_soc_dapm_widget *w;
3181 WARN_ON(dapm->dev != dai->dev);
3183 memset(&template, 0, sizeof(template));
3184 template.reg = SND_SOC_NOPM;
3186 if (dai->driver->playback.stream_name) {
3187 template.id = snd_soc_dapm_dai_in;
3188 template.name = dai->driver->playback.stream_name;
3189 template.sname = dai->driver->playback.stream_name;
3191 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3194 w = snd_soc_dapm_new_control(dapm, &template);
3196 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3197 dai->driver->playback.stream_name);
3202 dai->playback_widget = w;
3205 if (dai->driver->capture.stream_name) {
3206 template.id = snd_soc_dapm_dai_out;
3207 template.name = dai->driver->capture.stream_name;
3208 template.sname = dai->driver->capture.stream_name;
3210 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3213 w = snd_soc_dapm_new_control(dapm, &template);
3215 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3216 dai->driver->capture.stream_name);
3221 dai->capture_widget = w;
3227 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3229 struct snd_soc_dapm_widget *dai_w, *w;
3230 struct snd_soc_dapm_widget *src, *sink;
3231 struct snd_soc_dai *dai;
3233 /* For each DAI widget... */
3234 list_for_each_entry(dai_w, &card->widgets, list) {
3235 switch (dai_w->id) {
3236 case snd_soc_dapm_dai_in:
3237 case snd_soc_dapm_dai_out:
3245 /* ...find all widgets with the same stream and link them */
3246 list_for_each_entry(w, &card->widgets, list) {
3247 if (w->dapm != dai_w->dapm)
3251 case snd_soc_dapm_dai_in:
3252 case snd_soc_dapm_dai_out:
3258 if (!w->sname || !strstr(w->sname, dai_w->name))
3261 if (dai_w->id == snd_soc_dapm_dai_in) {
3268 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3269 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
3276 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3277 struct snd_soc_pcm_runtime *rtd)
3279 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3280 struct snd_soc_dapm_widget *sink, *source;
3283 for (i = 0; i < rtd->num_codecs; i++) {
3284 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
3286 /* there is no point in connecting BE DAI links with dummies */
3287 if (snd_soc_dai_is_dummy(codec_dai) ||
3288 snd_soc_dai_is_dummy(cpu_dai))
3291 /* connect BE DAI playback if widgets are valid */
3292 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3293 source = cpu_dai->playback_widget;
3294 sink = codec_dai->playback_widget;
3295 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3296 cpu_dai->component->name, source->name,
3297 codec_dai->component->name, sink->name);
3299 snd_soc_dapm_add_path(&card->dapm, source, sink,
3303 /* connect BE DAI capture if widgets are valid */
3304 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3305 source = codec_dai->capture_widget;
3306 sink = cpu_dai->capture_widget;
3307 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3308 codec_dai->component->name, source->name,
3309 cpu_dai->component->name, sink->name);
3311 snd_soc_dapm_add_path(&card->dapm, source, sink,
3317 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3320 struct snd_soc_dapm_widget *w;
3322 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3323 w = dai->playback_widget;
3325 w = dai->capture_widget;
3328 dapm_mark_dirty(w, "stream event");
3331 case SND_SOC_DAPM_STREAM_START:
3334 case SND_SOC_DAPM_STREAM_STOP:
3337 case SND_SOC_DAPM_STREAM_SUSPEND:
3338 case SND_SOC_DAPM_STREAM_RESUME:
3339 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3340 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3344 if (w->id == snd_soc_dapm_dai_in)
3345 w->is_source = w->active;
3347 w->is_sink = w->active;
3351 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3353 struct snd_soc_pcm_runtime *rtd = card->rtd;
3356 /* for each BE DAI link... */
3357 for (i = 0; i < card->num_rtd; i++) {
3358 rtd = &card->rtd[i];
3361 * dynamic FE links have no fixed DAI mapping.
3362 * CODEC<->CODEC links have no direct connection.
3364 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3367 dapm_connect_dai_link_widgets(card, rtd);
3371 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3376 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3377 for (i = 0; i < rtd->num_codecs; i++)
3378 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
3380 dapm_power_widgets(rtd->card, event);
3384 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3385 * @rtd: PCM runtime data
3386 * @stream: stream name
3387 * @event: stream event
3389 * Sends a stream event to the dapm core. The core then makes any
3390 * necessary widget power changes.
3392 * Returns 0 for success else error.
3394 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3397 struct snd_soc_card *card = rtd->card;
3399 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3400 soc_dapm_stream_event(rtd, stream, event);
3401 mutex_unlock(&card->dapm_mutex);
3405 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3406 * @dapm: DAPM context
3409 * Enables input/output pin and its parents or children widgets iff there is
3410 * a valid audio route and active audio stream.
3412 * Requires external locking.
3414 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3415 * do any widget power switching.
3417 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3420 return snd_soc_dapm_set_pin(dapm, pin, 1);
3422 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3425 * snd_soc_dapm_enable_pin - enable pin.
3426 * @dapm: DAPM context
3429 * Enables input/output pin and its parents or children widgets iff there is
3430 * a valid audio route and active audio stream.
3432 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3433 * do any widget power switching.
3435 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3439 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3441 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3443 mutex_unlock(&dapm->card->dapm_mutex);
3447 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3450 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3451 * @dapm: DAPM context
3454 * Enables input/output pin regardless of any other state. This is
3455 * intended for use with microphone bias supplies used in microphone
3458 * Requires external locking.
3460 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3461 * do any widget power switching.
3463 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3466 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3469 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3473 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3476 dapm_mark_dirty(w, "force enable");
3480 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3483 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3484 * @dapm: DAPM context
3487 * Enables input/output pin regardless of any other state. This is
3488 * intended for use with microphone bias supplies used in microphone
3491 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3492 * do any widget power switching.
3494 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3499 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3501 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
3503 mutex_unlock(&dapm->card->dapm_mutex);
3507 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3510 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3511 * @dapm: DAPM context
3514 * Disables input/output pin and its parents or children widgets.
3516 * Requires external locking.
3518 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3519 * do any widget power switching.
3521 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3524 return snd_soc_dapm_set_pin(dapm, pin, 0);
3526 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3529 * snd_soc_dapm_disable_pin - disable pin.
3530 * @dapm: DAPM context
3533 * Disables input/output pin and its parents or children widgets.
3535 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3536 * do any widget power switching.
3538 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3543 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3545 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3547 mutex_unlock(&dapm->card->dapm_mutex);
3551 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3554 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3555 * @dapm: DAPM context
3558 * Marks the specified pin as being not connected, disabling it along
3559 * any parent or child widgets. At present this is identical to
3560 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3561 * additional things such as disabling controls which only affect
3562 * paths through the pin.
3564 * Requires external locking.
3566 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3567 * do any widget power switching.
3569 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3572 return snd_soc_dapm_set_pin(dapm, pin, 0);
3574 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3577 * snd_soc_dapm_nc_pin - permanently disable pin.
3578 * @dapm: DAPM context
3581 * Marks the specified pin as being not connected, disabling it along
3582 * any parent or child widgets. At present this is identical to
3583 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3584 * additional things such as disabling controls which only affect
3585 * paths through the pin.
3587 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3588 * do any widget power switching.
3590 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3594 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3596 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3598 mutex_unlock(&dapm->card->dapm_mutex);
3602 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3605 * snd_soc_dapm_get_pin_status - get audio pin status
3606 * @dapm: DAPM context
3607 * @pin: audio signal pin endpoint (or start point)
3609 * Get audio pin status - connected or disconnected.
3611 * Returns 1 for connected otherwise 0.
3613 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3616 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3619 return w->connected;
3623 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3626 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3627 * @dapm: DAPM context
3628 * @pin: audio signal pin endpoint (or start point)
3630 * Mark the given endpoint or pin as ignoring suspend. When the
3631 * system is disabled a path between two endpoints flagged as ignoring
3632 * suspend will not be disabled. The path must already be enabled via
3633 * normal means at suspend time, it will not be turned on if it was not
3636 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3639 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3642 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3646 w->ignore_suspend = 1;
3650 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3653 * dapm_is_external_path() - Checks if a path is a external path
3654 * @card: The card the path belongs to
3655 * @path: The path to check
3657 * Returns true if the path is either between two different DAPM contexts or
3658 * between two external pins of the same DAPM context. Otherwise returns
3661 static bool dapm_is_external_path(struct snd_soc_card *card,
3662 struct snd_soc_dapm_path *path)
3665 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3666 path->source->name, path->source->id, path->source->dapm,
3667 path->sink->name, path->sink->id, path->sink->dapm);
3669 /* Connection between two different DAPM contexts */
3670 if (path->source->dapm != path->sink->dapm)
3673 /* Loopback connection from external pin to external pin */
3674 if (path->sink->id == snd_soc_dapm_input) {
3675 switch (path->source->id) {
3676 case snd_soc_dapm_output:
3677 case snd_soc_dapm_micbias:
3687 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3688 struct snd_soc_dapm_widget *w)
3690 struct snd_soc_dapm_path *p;
3692 list_for_each_entry(p, &w->sources, list_sink) {
3693 if (dapm_is_external_path(card, p))
3697 list_for_each_entry(p, &w->sinks, list_source) {
3698 if (dapm_is_external_path(card, p))
3706 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3707 * @card: The card whose pins should be processed
3709 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3710 * which are unused. Pins are used if they are connected externally to a
3711 * component, whether that be to some other device, or a loop-back connection to
3712 * the component itself.
3714 void snd_soc_dapm_auto_nc_pins(struct snd_soc_card *card)
3716 struct snd_soc_dapm_widget *w;
3718 dev_dbg(card->dev, "ASoC: Auto NC: DAPMs: card:%p\n", &card->dapm);
3720 list_for_each_entry(w, &card->widgets, list) {
3722 case snd_soc_dapm_input:
3723 case snd_soc_dapm_output:
3724 case snd_soc_dapm_micbias:
3725 dev_dbg(card->dev, "ASoC: Auto NC: Checking widget %s\n",
3727 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3729 "... Not in map; disabling\n");
3730 snd_soc_dapm_nc_pin(w->dapm, w->name);
3740 * snd_soc_dapm_free - free dapm resources
3741 * @dapm: DAPM context
3743 * Free all dapm widgets and resources.
3745 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3747 snd_soc_dapm_sys_remove(dapm->dev);
3748 dapm_debugfs_cleanup(dapm);
3749 dapm_free_widgets(dapm);
3750 list_del(&dapm->list);
3752 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3754 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
3756 struct snd_soc_card *card = dapm->card;
3757 struct snd_soc_dapm_widget *w;
3758 LIST_HEAD(down_list);
3761 mutex_lock(&card->dapm_mutex);
3763 list_for_each_entry(w, &dapm->card->widgets, list) {
3764 if (w->dapm != dapm)
3767 dapm_seq_insert(w, &down_list, false);
3773 /* If there were no widgets to power down we're already in
3777 if (dapm->bias_level == SND_SOC_BIAS_ON)
3778 snd_soc_dapm_set_bias_level(dapm,
3779 SND_SOC_BIAS_PREPARE);
3780 dapm_seq_run(card, &down_list, 0, false);
3781 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3782 snd_soc_dapm_set_bias_level(dapm,
3783 SND_SOC_BIAS_STANDBY);
3786 mutex_unlock(&card->dapm_mutex);
3790 * snd_soc_dapm_shutdown - callback for system shutdown
3792 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3794 struct snd_soc_dapm_context *dapm;
3796 list_for_each_entry(dapm, &card->dapm_list, list) {
3797 if (dapm != &card->dapm) {
3798 soc_dapm_shutdown_dapm(dapm);
3799 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3800 snd_soc_dapm_set_bias_level(dapm,
3805 soc_dapm_shutdown_dapm(&card->dapm);
3806 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3807 snd_soc_dapm_set_bias_level(&card->dapm,
3811 /* Module information */
3812 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3813 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3814 MODULE_LICENSE("GPL");