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_virt_mux] = 6,
74 [snd_soc_dapm_value_mux] = 6,
75 [snd_soc_dapm_dac] = 7,
76 [snd_soc_dapm_switch] = 8,
77 [snd_soc_dapm_mixer] = 8,
78 [snd_soc_dapm_mixer_named_ctl] = 8,
79 [snd_soc_dapm_pga] = 9,
80 [snd_soc_dapm_adc] = 10,
81 [snd_soc_dapm_out_drv] = 11,
82 [snd_soc_dapm_hp] = 11,
83 [snd_soc_dapm_spk] = 11,
84 [snd_soc_dapm_line] = 11,
85 [snd_soc_dapm_kcontrol] = 12,
86 [snd_soc_dapm_post] = 13,
89 static int dapm_down_seq[] = {
90 [snd_soc_dapm_pre] = 0,
91 [snd_soc_dapm_kcontrol] = 1,
92 [snd_soc_dapm_adc] = 2,
93 [snd_soc_dapm_hp] = 3,
94 [snd_soc_dapm_spk] = 3,
95 [snd_soc_dapm_line] = 3,
96 [snd_soc_dapm_out_drv] = 3,
97 [snd_soc_dapm_pga] = 4,
98 [snd_soc_dapm_switch] = 5,
99 [snd_soc_dapm_mixer_named_ctl] = 5,
100 [snd_soc_dapm_mixer] = 5,
101 [snd_soc_dapm_dac] = 6,
102 [snd_soc_dapm_mic] = 7,
103 [snd_soc_dapm_micbias] = 8,
104 [snd_soc_dapm_mux] = 9,
105 [snd_soc_dapm_virt_mux] = 9,
106 [snd_soc_dapm_value_mux] = 9,
107 [snd_soc_dapm_aif_in] = 10,
108 [snd_soc_dapm_aif_out] = 10,
109 [snd_soc_dapm_dai_in] = 10,
110 [snd_soc_dapm_dai_out] = 10,
111 [snd_soc_dapm_dai_link] = 11,
112 [snd_soc_dapm_supply] = 12,
113 [snd_soc_dapm_clock_supply] = 13,
114 [snd_soc_dapm_regulator_supply] = 13,
115 [snd_soc_dapm_post] = 14,
118 static void pop_wait(u32 pop_time)
121 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
124 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
132 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
137 vsnprintf(buf, PAGE_SIZE, fmt, args);
138 dev_info(dev, "%s", buf);
144 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
146 return !list_empty(&w->dirty);
149 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
151 if (!dapm_dirty_widget(w)) {
152 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
154 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
157 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
159 void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
161 struct snd_soc_card *card = dapm->card;
162 struct snd_soc_dapm_widget *w;
164 mutex_lock(&card->dapm_mutex);
166 list_for_each_entry(w, &card->widgets, list) {
168 case snd_soc_dapm_input:
169 case snd_soc_dapm_output:
170 dapm_mark_dirty(w, "Rechecking inputs and outputs");
177 mutex_unlock(&card->dapm_mutex);
179 EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
181 /* create a new dapm widget */
182 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
183 const struct snd_soc_dapm_widget *_widget)
185 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
188 struct dapm_kcontrol_data {
190 struct snd_soc_dapm_widget *widget;
191 struct list_head paths;
192 struct snd_soc_dapm_widget_list *wlist;
195 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
196 struct snd_kcontrol *kcontrol)
198 struct dapm_kcontrol_data *data;
199 struct soc_mixer_control *mc;
201 data = kzalloc(sizeof(*data), GFP_KERNEL);
203 dev_err(widget->dapm->dev,
204 "ASoC: can't allocate kcontrol data for %s\n",
209 INIT_LIST_HEAD(&data->paths);
211 switch (widget->id) {
212 case snd_soc_dapm_switch:
213 case snd_soc_dapm_mixer:
214 case snd_soc_dapm_mixer_named_ctl:
215 mc = (struct soc_mixer_control *)kcontrol->private_value;
217 if (mc->autodisable) {
218 struct snd_soc_dapm_widget template;
220 memset(&template, 0, sizeof(template));
221 template.reg = mc->reg;
222 template.mask = (1 << fls(mc->max)) - 1;
223 template.shift = mc->shift;
225 template.off_val = mc->max;
227 template.off_val = 0;
228 template.on_val = template.off_val;
229 template.id = snd_soc_dapm_kcontrol;
230 template.name = kcontrol->id.name;
232 data->value = template.on_val;
234 data->widget = snd_soc_dapm_new_control(widget->dapm,
246 kcontrol->private_data = data;
251 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
253 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
259 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
260 const struct snd_kcontrol *kcontrol)
262 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
267 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
268 struct snd_soc_dapm_widget *widget)
270 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
271 struct snd_soc_dapm_widget_list *new_wlist;
275 n = data->wlist->num_widgets + 1;
279 new_wlist = krealloc(data->wlist,
280 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
284 new_wlist->widgets[n - 1] = widget;
285 new_wlist->num_widgets = n;
287 data->wlist = new_wlist;
292 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
293 struct snd_soc_dapm_path *path)
295 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
297 list_add_tail(&path->list_kcontrol, &data->paths);
300 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
301 path->source, NULL, NULL);
305 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
307 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
312 return data->widget->power;
315 static struct list_head *dapm_kcontrol_get_path_list(
316 const struct snd_kcontrol *kcontrol)
318 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
323 #define dapm_kcontrol_for_each_path(path, kcontrol) \
324 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
327 static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
329 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
334 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
337 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
339 if (data->value == value)
343 data->widget->on_val = value;
351 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
352 * @kcontrol: The kcontrol
354 struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
356 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
358 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
360 static void dapm_reset(struct snd_soc_card *card)
362 struct snd_soc_dapm_widget *w;
364 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
366 list_for_each_entry(w, &card->widgets, list) {
367 w->new_power = w->power;
368 w->power_checked = false;
374 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
377 return snd_soc_read(w->codec, reg);
378 else if (w->platform)
379 return snd_soc_platform_read(w->platform, reg);
381 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
385 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
388 return snd_soc_write(w->codec, reg, val);
389 else if (w->platform)
390 return snd_soc_platform_write(w->platform, reg, val);
392 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
396 static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
398 if (w->codec && !w->codec->using_regmap)
399 mutex_lock(&w->codec->mutex);
400 else if (w->platform)
401 mutex_lock(&w->platform->mutex);
404 static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
406 if (w->codec && !w->codec->using_regmap)
407 mutex_unlock(&w->codec->mutex);
408 else if (w->platform)
409 mutex_unlock(&w->platform->mutex);
412 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
414 if (dapm->codec && dapm->codec->using_regmap)
415 regmap_async_complete(dapm->codec->control_data);
418 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
419 unsigned short reg, unsigned int mask, unsigned int value)
422 unsigned int old, new;
425 if (w->codec && w->codec->using_regmap) {
426 ret = regmap_update_bits_check_async(w->codec->control_data,
433 ret = soc_widget_read(w, reg);
435 soc_widget_unlock(w);
440 new = (old & ~mask) | (value & mask);
443 ret = soc_widget_write(w, reg, new);
445 soc_widget_unlock(w);
449 soc_widget_unlock(w);
456 * snd_soc_dapm_set_bias_level - set the bias level for the system
457 * @dapm: DAPM context
458 * @level: level to configure
460 * Configure the bias (power) levels for the SoC audio device.
462 * Returns 0 for success else error.
464 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
465 enum snd_soc_bias_level level)
467 struct snd_soc_card *card = dapm->card;
470 trace_snd_soc_bias_level_start(card, level);
472 if (card && card->set_bias_level)
473 ret = card->set_bias_level(card, dapm, level);
478 if (dapm->codec->driver->set_bias_level)
479 ret = dapm->codec->driver->set_bias_level(dapm->codec,
482 dapm->bias_level = level;
483 } else if (!card || dapm != &card->dapm) {
484 dapm->bias_level = level;
490 if (card && card->set_bias_level_post)
491 ret = card->set_bias_level_post(card, dapm, level);
493 trace_snd_soc_bias_level_done(card, level);
498 /* set up initial codec paths */
499 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
500 struct snd_soc_dapm_path *p, int i)
503 case snd_soc_dapm_switch:
504 case snd_soc_dapm_mixer:
505 case snd_soc_dapm_mixer_named_ctl: {
507 struct soc_mixer_control *mc = (struct soc_mixer_control *)
508 w->kcontrol_news[i].private_value;
510 unsigned int shift = mc->shift;
512 unsigned int mask = (1 << fls(max)) - 1;
513 unsigned int invert = mc->invert;
515 if (reg != SND_SOC_NOPM) {
516 val = soc_widget_read(w, reg);
517 val = (val >> shift) & mask;
527 case snd_soc_dapm_mux: {
528 struct soc_enum *e = (struct soc_enum *)
529 w->kcontrol_news[i].private_value;
532 val = soc_widget_read(w, e->reg);
533 item = (val >> e->shift_l) & e->mask;
535 if (item < e->max && !strcmp(p->name, e->texts[item]))
541 case snd_soc_dapm_virt_mux: {
542 struct soc_enum *e = (struct soc_enum *)
543 w->kcontrol_news[i].private_value;
546 /* since a virtual mux has no backing registers to
547 * decide which path to connect, it will try to match
548 * with the first enumeration. This is to ensure
549 * that the default mux choice (the first) will be
550 * correctly powered up during initialization.
552 if (!strcmp(p->name, e->texts[0]))
556 case snd_soc_dapm_value_mux: {
557 struct soc_enum *e = (struct soc_enum *)
558 w->kcontrol_news[i].private_value;
561 val = soc_widget_read(w, e->reg);
562 val = (val >> e->shift_l) & e->mask;
563 for (item = 0; item < e->max; item++) {
564 if (val == e->values[item])
568 if (item < e->max && !strcmp(p->name, e->texts[item]))
574 /* does not affect routing - always connected */
575 case snd_soc_dapm_pga:
576 case snd_soc_dapm_out_drv:
577 case snd_soc_dapm_output:
578 case snd_soc_dapm_adc:
579 case snd_soc_dapm_input:
580 case snd_soc_dapm_siggen:
581 case snd_soc_dapm_dac:
582 case snd_soc_dapm_micbias:
583 case snd_soc_dapm_vmid:
584 case snd_soc_dapm_supply:
585 case snd_soc_dapm_regulator_supply:
586 case snd_soc_dapm_clock_supply:
587 case snd_soc_dapm_aif_in:
588 case snd_soc_dapm_aif_out:
589 case snd_soc_dapm_dai_in:
590 case snd_soc_dapm_dai_out:
591 case snd_soc_dapm_hp:
592 case snd_soc_dapm_mic:
593 case snd_soc_dapm_spk:
594 case snd_soc_dapm_line:
595 case snd_soc_dapm_dai_link:
596 case snd_soc_dapm_kcontrol:
599 /* does affect routing - dynamically connected */
600 case snd_soc_dapm_pre:
601 case snd_soc_dapm_post:
607 /* connect mux widget to its interconnecting audio paths */
608 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
609 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
610 struct snd_soc_dapm_path *path, const char *control_name,
611 const struct snd_kcontrol_new *kcontrol)
613 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
616 for (i = 0; i < e->max; i++) {
617 if (!(strcmp(control_name, e->texts[i]))) {
618 list_add(&path->list, &dapm->card->paths);
619 list_add(&path->list_sink, &dest->sources);
620 list_add(&path->list_source, &src->sinks);
621 path->name = (char*)e->texts[i];
622 dapm_set_path_status(dest, path, 0);
630 /* connect mixer widget to its interconnecting audio paths */
631 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
632 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
633 struct snd_soc_dapm_path *path, const char *control_name)
637 /* search for mixer kcontrol */
638 for (i = 0; i < dest->num_kcontrols; i++) {
639 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
640 list_add(&path->list, &dapm->card->paths);
641 list_add(&path->list_sink, &dest->sources);
642 list_add(&path->list_source, &src->sinks);
643 path->name = dest->kcontrol_news[i].name;
644 dapm_set_path_status(dest, path, i);
651 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
652 struct snd_soc_dapm_widget *kcontrolw,
653 const struct snd_kcontrol_new *kcontrol_new,
654 struct snd_kcontrol **kcontrol)
656 struct snd_soc_dapm_widget *w;
661 list_for_each_entry(w, &dapm->card->widgets, list) {
662 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
664 for (i = 0; i < w->num_kcontrols; i++) {
665 if (&w->kcontrol_news[i] == kcontrol_new) {
667 *kcontrol = w->kcontrols[i];
677 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
678 * create it. Either way, add the widget into the control's widget list
680 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
683 struct snd_soc_dapm_context *dapm = w->dapm;
684 struct snd_card *card = dapm->card->snd_card;
688 struct snd_kcontrol *kcontrol;
689 bool wname_in_long_name, kcname_in_long_name;
695 prefix = dapm->codec->name_prefix;
700 prefix_len = strlen(prefix) + 1;
704 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
709 wname_in_long_name = false;
710 kcname_in_long_name = true;
713 case snd_soc_dapm_switch:
714 case snd_soc_dapm_mixer:
715 wname_in_long_name = true;
716 kcname_in_long_name = true;
718 case snd_soc_dapm_mixer_named_ctl:
719 wname_in_long_name = false;
720 kcname_in_long_name = true;
722 case snd_soc_dapm_mux:
723 case snd_soc_dapm_virt_mux:
724 case snd_soc_dapm_value_mux:
725 wname_in_long_name = true;
726 kcname_in_long_name = false;
733 if (wname_in_long_name && kcname_in_long_name) {
735 * The control will get a prefix from the control
736 * creation process but we're also using the same
737 * prefix for widgets so cut the prefix off the
738 * front of the widget name.
740 long_name = kasprintf(GFP_KERNEL, "%s %s",
741 w->name + prefix_len,
742 w->kcontrol_news[kci].name);
743 if (long_name == NULL)
747 } else if (wname_in_long_name) {
749 name = w->name + prefix_len;
752 name = w->kcontrol_news[kci].name;
755 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
760 kcontrol->private_free = dapm_kcontrol_free;
762 ret = dapm_kcontrol_data_alloc(w, kcontrol);
764 snd_ctl_free_one(kcontrol);
768 ret = snd_ctl_add(card, kcontrol);
771 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
777 ret = dapm_kcontrol_add_widget(kcontrol, w);
781 w->kcontrols[kci] = kcontrol;
786 /* create new dapm mixer control */
787 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
790 struct snd_soc_dapm_path *path;
793 for (i = 0; i < w->num_kcontrols; i++) {
795 list_for_each_entry(path, &w->sources, list_sink) {
796 /* mixer/mux paths name must match control name */
797 if (path->name != (char *)w->kcontrol_news[i].name)
800 if (w->kcontrols[i]) {
801 dapm_kcontrol_add_path(w->kcontrols[i], path);
805 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
809 dapm_kcontrol_add_path(w->kcontrols[i], path);
816 /* create new dapm mux control */
817 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
819 struct snd_soc_dapm_context *dapm = w->dapm;
820 struct snd_soc_dapm_path *path;
823 if (w->num_kcontrols != 1) {
825 "ASoC: mux %s has incorrect number of controls\n",
830 if (list_empty(&w->sources)) {
831 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
835 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
839 list_for_each_entry(path, &w->sources, list_sink)
840 dapm_kcontrol_add_path(w->kcontrols[0], path);
845 /* create new dapm volume control */
846 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
848 if (w->num_kcontrols)
849 dev_err(w->dapm->dev,
850 "ASoC: PGA controls not supported: '%s'\n", w->name);
855 /* reset 'walked' bit for each dapm path */
856 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
857 struct list_head *sink)
859 struct snd_soc_dapm_path *p;
861 list_for_each_entry(p, sink, list_source) {
864 dapm_clear_walk_output(dapm, &p->sink->sinks);
869 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
870 struct list_head *source)
872 struct snd_soc_dapm_path *p;
874 list_for_each_entry(p, source, list_sink) {
877 dapm_clear_walk_input(dapm, &p->source->sources);
883 /* We implement power down on suspend by checking the power state of
884 * the ALSA card - when we are suspending the ALSA state for the card
887 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
889 int level = snd_power_get_state(widget->dapm->card->snd_card);
892 case SNDRV_CTL_POWER_D3hot:
893 case SNDRV_CTL_POWER_D3cold:
894 if (widget->ignore_suspend)
895 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
897 return widget->ignore_suspend;
903 /* add widget to list if it's not already in the list */
904 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
905 struct snd_soc_dapm_widget *w)
907 struct snd_soc_dapm_widget_list *wlist;
908 int wlistsize, wlistentries, i;
915 /* is this widget already in the list */
916 for (i = 0; i < wlist->num_widgets; i++) {
917 if (wlist->widgets[i] == w)
921 /* allocate some new space */
922 wlistentries = wlist->num_widgets + 1;
923 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
924 wlistentries * sizeof(struct snd_soc_dapm_widget *);
925 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
927 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
933 /* insert the widget */
934 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
935 w->name, wlist->num_widgets);
937 wlist->widgets[wlist->num_widgets] = w;
938 wlist->num_widgets++;
943 * Recursively check for a completed path to an active or physically connected
944 * output widget. Returns number of complete paths.
946 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
947 struct snd_soc_dapm_widget_list **list)
949 struct snd_soc_dapm_path *path;
952 if (widget->outputs >= 0)
953 return widget->outputs;
955 DAPM_UPDATE_STAT(widget, path_checks);
957 switch (widget->id) {
958 case snd_soc_dapm_supply:
959 case snd_soc_dapm_regulator_supply:
960 case snd_soc_dapm_clock_supply:
961 case snd_soc_dapm_kcontrol:
967 switch (widget->id) {
968 case snd_soc_dapm_adc:
969 case snd_soc_dapm_aif_out:
970 case snd_soc_dapm_dai_out:
971 if (widget->active) {
972 widget->outputs = snd_soc_dapm_suspend_check(widget);
973 return widget->outputs;
979 if (widget->connected) {
980 /* connected pin ? */
981 if (widget->id == snd_soc_dapm_output && !widget->ext) {
982 widget->outputs = snd_soc_dapm_suspend_check(widget);
983 return widget->outputs;
986 /* connected jack or spk ? */
987 if (widget->id == snd_soc_dapm_hp ||
988 widget->id == snd_soc_dapm_spk ||
989 (widget->id == snd_soc_dapm_line &&
990 !list_empty(&widget->sources))) {
991 widget->outputs = snd_soc_dapm_suspend_check(widget);
992 return widget->outputs;
996 list_for_each_entry(path, &widget->sinks, list_source) {
997 DAPM_UPDATE_STAT(widget, neighbour_checks);
1008 trace_snd_soc_dapm_output_path(widget, path);
1010 if (path->sink && path->connect) {
1014 /* do we need to add this widget to the list ? */
1017 err = dapm_list_add_widget(list, path->sink);
1019 dev_err(widget->dapm->dev,
1020 "ASoC: could not add widget %s\n",
1027 con += is_connected_output_ep(path->sink, list);
1033 widget->outputs = con;
1039 * Recursively check for a completed path to an active or physically connected
1040 * input widget. Returns number of complete paths.
1042 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1043 struct snd_soc_dapm_widget_list **list)
1045 struct snd_soc_dapm_path *path;
1048 if (widget->inputs >= 0)
1049 return widget->inputs;
1051 DAPM_UPDATE_STAT(widget, path_checks);
1053 switch (widget->id) {
1054 case snd_soc_dapm_supply:
1055 case snd_soc_dapm_regulator_supply:
1056 case snd_soc_dapm_clock_supply:
1057 case snd_soc_dapm_kcontrol:
1063 /* active stream ? */
1064 switch (widget->id) {
1065 case snd_soc_dapm_dac:
1066 case snd_soc_dapm_aif_in:
1067 case snd_soc_dapm_dai_in:
1068 if (widget->active) {
1069 widget->inputs = snd_soc_dapm_suspend_check(widget);
1070 return widget->inputs;
1076 if (widget->connected) {
1077 /* connected pin ? */
1078 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1079 widget->inputs = snd_soc_dapm_suspend_check(widget);
1080 return widget->inputs;
1083 /* connected VMID/Bias for lower pops */
1084 if (widget->id == snd_soc_dapm_vmid) {
1085 widget->inputs = snd_soc_dapm_suspend_check(widget);
1086 return widget->inputs;
1089 /* connected jack ? */
1090 if (widget->id == snd_soc_dapm_mic ||
1091 (widget->id == snd_soc_dapm_line &&
1092 !list_empty(&widget->sinks))) {
1093 widget->inputs = snd_soc_dapm_suspend_check(widget);
1094 return widget->inputs;
1097 /* signal generator */
1098 if (widget->id == snd_soc_dapm_siggen) {
1099 widget->inputs = snd_soc_dapm_suspend_check(widget);
1100 return widget->inputs;
1104 list_for_each_entry(path, &widget->sources, list_sink) {
1105 DAPM_UPDATE_STAT(widget, neighbour_checks);
1116 trace_snd_soc_dapm_input_path(widget, path);
1118 if (path->source && path->connect) {
1122 /* do we need to add this widget to the list ? */
1125 err = dapm_list_add_widget(list, path->source);
1127 dev_err(widget->dapm->dev,
1128 "ASoC: could not add widget %s\n",
1135 con += is_connected_input_ep(path->source, list);
1141 widget->inputs = con;
1147 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1148 * @dai: the soc DAI.
1149 * @stream: stream direction.
1150 * @list: list of active widgets for this stream.
1152 * Queries DAPM graph as to whether an valid audio stream path exists for
1153 * the initial stream specified by name. This takes into account
1154 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1156 * Returns the number of valid paths or negative error.
1158 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1159 struct snd_soc_dapm_widget_list **list)
1161 struct snd_soc_card *card = dai->card;
1164 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1167 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1168 paths = is_connected_output_ep(dai->playback_widget, list);
1169 dapm_clear_walk_output(&card->dapm,
1170 &dai->playback_widget->sinks);
1172 paths = is_connected_input_ep(dai->capture_widget, list);
1173 dapm_clear_walk_input(&card->dapm,
1174 &dai->capture_widget->sources);
1177 trace_snd_soc_dapm_connected(paths, stream);
1178 mutex_unlock(&card->dapm_mutex);
1184 * Handler for generic register modifier widget.
1186 int dapm_reg_event(struct snd_soc_dapm_widget *w,
1187 struct snd_kcontrol *kcontrol, int event)
1191 if (SND_SOC_DAPM_EVENT_ON(event))
1196 soc_widget_update_bits_locked(w, -(w->reg + 1),
1197 w->mask << w->shift, val << w->shift);
1201 EXPORT_SYMBOL_GPL(dapm_reg_event);
1204 * Handler for regulator supply widget.
1206 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1207 struct snd_kcontrol *kcontrol, int event)
1211 soc_dapm_async_complete(w->dapm);
1213 if (SND_SOC_DAPM_EVENT_ON(event)) {
1214 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1215 ret = regulator_allow_bypass(w->regulator, false);
1217 dev_warn(w->dapm->dev,
1218 "ASoC: Failed to bypass %s: %d\n",
1222 return regulator_enable(w->regulator);
1224 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1225 ret = regulator_allow_bypass(w->regulator, true);
1227 dev_warn(w->dapm->dev,
1228 "ASoC: Failed to unbypass %s: %d\n",
1232 return regulator_disable_deferred(w->regulator, w->shift);
1235 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1238 * Handler for clock supply widget.
1240 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1241 struct snd_kcontrol *kcontrol, int event)
1246 soc_dapm_async_complete(w->dapm);
1248 #ifdef CONFIG_HAVE_CLK
1249 if (SND_SOC_DAPM_EVENT_ON(event)) {
1250 return clk_prepare_enable(w->clk);
1252 clk_disable_unprepare(w->clk);
1258 EXPORT_SYMBOL_GPL(dapm_clock_event);
1260 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1262 if (w->power_checked)
1263 return w->new_power;
1268 w->new_power = w->power_check(w);
1270 w->power_checked = true;
1272 return w->new_power;
1275 /* Generic check to see if a widget should be powered.
1277 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1281 DAPM_UPDATE_STAT(w, power_checks);
1283 in = is_connected_input_ep(w, NULL);
1284 dapm_clear_walk_input(w->dapm, &w->sources);
1285 out = is_connected_output_ep(w, NULL);
1286 dapm_clear_walk_output(w->dapm, &w->sinks);
1287 return out != 0 && in != 0;
1290 /* Check to see if an ADC has power */
1291 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1295 DAPM_UPDATE_STAT(w, power_checks);
1298 in = is_connected_input_ep(w, NULL);
1299 dapm_clear_walk_input(w->dapm, &w->sources);
1302 return dapm_generic_check_power(w);
1306 /* Check to see if a DAC has power */
1307 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1311 DAPM_UPDATE_STAT(w, power_checks);
1314 out = is_connected_output_ep(w, NULL);
1315 dapm_clear_walk_output(w->dapm, &w->sinks);
1318 return dapm_generic_check_power(w);
1322 /* Check to see if a power supply is needed */
1323 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1325 struct snd_soc_dapm_path *path;
1327 DAPM_UPDATE_STAT(w, power_checks);
1329 /* Check if one of our outputs is connected */
1330 list_for_each_entry(path, &w->sinks, list_source) {
1331 DAPM_UPDATE_STAT(w, neighbour_checks);
1336 if (path->connected &&
1337 !path->connected(path->source, path->sink))
1343 if (dapm_widget_power_check(path->sink))
1350 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1355 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1356 struct snd_soc_dapm_widget *b,
1364 sort = dapm_down_seq;
1366 if (sort[a->id] != sort[b->id])
1367 return sort[a->id] - sort[b->id];
1368 if (a->subseq != b->subseq) {
1370 return a->subseq - b->subseq;
1372 return b->subseq - a->subseq;
1374 if (a->reg != b->reg)
1375 return a->reg - b->reg;
1376 if (a->dapm != b->dapm)
1377 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1382 /* Insert a widget in order into a DAPM power sequence. */
1383 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1384 struct list_head *list,
1387 struct snd_soc_dapm_widget *w;
1389 list_for_each_entry(w, list, power_list)
1390 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1391 list_add_tail(&new_widget->power_list, &w->power_list);
1395 list_add_tail(&new_widget->power_list, list);
1398 static void dapm_seq_check_event(struct snd_soc_card *card,
1399 struct snd_soc_dapm_widget *w, int event)
1401 const char *ev_name;
1405 case SND_SOC_DAPM_PRE_PMU:
1406 ev_name = "PRE_PMU";
1409 case SND_SOC_DAPM_POST_PMU:
1410 ev_name = "POST_PMU";
1413 case SND_SOC_DAPM_PRE_PMD:
1414 ev_name = "PRE_PMD";
1417 case SND_SOC_DAPM_POST_PMD:
1418 ev_name = "POST_PMD";
1421 case SND_SOC_DAPM_WILL_PMU:
1422 ev_name = "WILL_PMU";
1425 case SND_SOC_DAPM_WILL_PMD:
1426 ev_name = "WILL_PMD";
1430 WARN(1, "Unknown event %d\n", event);
1434 if (w->new_power != power)
1437 if (w->event && (w->event_flags & event)) {
1438 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1440 soc_dapm_async_complete(w->dapm);
1441 trace_snd_soc_dapm_widget_event_start(w, event);
1442 ret = w->event(w, NULL, event);
1443 trace_snd_soc_dapm_widget_event_done(w, event);
1445 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1446 ev_name, w->name, ret);
1450 /* Apply the coalesced changes from a DAPM sequence */
1451 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1452 struct list_head *pending)
1454 struct snd_soc_dapm_widget *w;
1456 unsigned int value = 0;
1457 unsigned int mask = 0;
1459 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1462 list_for_each_entry(w, pending, power_list) {
1463 WARN_ON(reg != w->reg);
1464 w->power = w->new_power;
1466 mask |= w->mask << w->shift;
1468 value |= w->on_val << w->shift;
1470 value |= w->off_val << w->shift;
1472 pop_dbg(w->dapm->dev, card->pop_time,
1473 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1474 w->name, reg, value, mask);
1476 /* Check for events */
1477 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1478 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1482 /* Any widget will do, they should all be updating the
1485 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1488 pop_dbg(w->dapm->dev, card->pop_time,
1489 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1490 value, mask, reg, card->pop_time);
1491 pop_wait(card->pop_time);
1492 soc_widget_update_bits_locked(w, reg, mask, value);
1495 list_for_each_entry(w, pending, power_list) {
1496 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1497 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1501 /* Apply a DAPM power sequence.
1503 * We walk over a pre-sorted list of widgets to apply power to. In
1504 * order to minimise the number of writes to the device required
1505 * multiple widgets will be updated in a single write where possible.
1506 * Currently anything that requires more than a single write is not
1509 static void dapm_seq_run(struct snd_soc_card *card,
1510 struct list_head *list, int event, bool power_up)
1512 struct snd_soc_dapm_widget *w, *n;
1513 struct snd_soc_dapm_context *d;
1516 int cur_subseq = -1;
1517 int cur_reg = SND_SOC_NOPM;
1518 struct snd_soc_dapm_context *cur_dapm = NULL;
1525 sort = dapm_down_seq;
1527 list_for_each_entry_safe(w, n, list, power_list) {
1530 /* Do we need to apply any queued changes? */
1531 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1532 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1533 if (!list_empty(&pending))
1534 dapm_seq_run_coalesced(card, &pending);
1536 if (cur_dapm && cur_dapm->seq_notifier) {
1537 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1538 if (sort[i] == cur_sort)
1539 cur_dapm->seq_notifier(cur_dapm,
1544 if (cur_dapm && w->dapm != cur_dapm)
1545 soc_dapm_async_complete(cur_dapm);
1547 INIT_LIST_HEAD(&pending);
1549 cur_subseq = INT_MIN;
1550 cur_reg = SND_SOC_NOPM;
1555 case snd_soc_dapm_pre:
1557 list_for_each_entry_safe_continue(w, n, list,
1560 if (event == SND_SOC_DAPM_STREAM_START)
1562 NULL, SND_SOC_DAPM_PRE_PMU);
1563 else if (event == SND_SOC_DAPM_STREAM_STOP)
1565 NULL, SND_SOC_DAPM_PRE_PMD);
1568 case snd_soc_dapm_post:
1570 list_for_each_entry_safe_continue(w, n, list,
1573 if (event == SND_SOC_DAPM_STREAM_START)
1575 NULL, SND_SOC_DAPM_POST_PMU);
1576 else if (event == SND_SOC_DAPM_STREAM_STOP)
1578 NULL, SND_SOC_DAPM_POST_PMD);
1582 /* Queue it up for application */
1583 cur_sort = sort[w->id];
1584 cur_subseq = w->subseq;
1587 list_move(&w->power_list, &pending);
1592 dev_err(w->dapm->dev,
1593 "ASoC: Failed to apply widget power: %d\n", ret);
1596 if (!list_empty(&pending))
1597 dapm_seq_run_coalesced(card, &pending);
1599 if (cur_dapm && cur_dapm->seq_notifier) {
1600 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1601 if (sort[i] == cur_sort)
1602 cur_dapm->seq_notifier(cur_dapm,
1606 list_for_each_entry(d, &card->dapm_list, list) {
1607 soc_dapm_async_complete(d);
1611 static void dapm_widget_update(struct snd_soc_card *card)
1613 struct snd_soc_dapm_update *update = card->update;
1614 struct snd_soc_dapm_widget_list *wlist;
1615 struct snd_soc_dapm_widget *w = NULL;
1619 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1622 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1624 for (wi = 0; wi < wlist->num_widgets; wi++) {
1625 w = wlist->widgets[wi];
1627 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1628 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1630 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1638 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1641 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1644 for (wi = 0; wi < wlist->num_widgets; wi++) {
1645 w = wlist->widgets[wi];
1647 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1648 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1650 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1656 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1657 * they're changing state.
1659 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1661 struct snd_soc_dapm_context *d = data;
1664 /* If we're off and we're not supposed to be go into STANDBY */
1665 if (d->bias_level == SND_SOC_BIAS_OFF &&
1666 d->target_bias_level != SND_SOC_BIAS_OFF) {
1668 pm_runtime_get_sync(d->dev);
1670 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1673 "ASoC: Failed to turn on bias: %d\n", ret);
1676 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1677 if (d->bias_level != d->target_bias_level) {
1678 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1681 "ASoC: Failed to prepare bias: %d\n", ret);
1685 /* Async callback run prior to DAPM sequences - brings to their final
1688 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1690 struct snd_soc_dapm_context *d = data;
1693 /* If we just powered the last thing off drop to standby bias */
1694 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1695 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1696 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1697 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1699 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1703 /* If we're in standby and can support bias off then do that */
1704 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1705 d->target_bias_level == SND_SOC_BIAS_OFF) {
1706 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1708 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1712 pm_runtime_put(d->dev);
1715 /* If we just powered up then move to active bias */
1716 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1717 d->target_bias_level == SND_SOC_BIAS_ON) {
1718 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1720 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1725 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1726 bool power, bool connect)
1728 /* If a connection is being made or broken then that update
1729 * will have marked the peer dirty, otherwise the widgets are
1730 * not connected and this update has no impact. */
1734 /* If the peer is already in the state we're moving to then we
1735 * won't have an impact on it. */
1736 if (power != peer->power)
1737 dapm_mark_dirty(peer, "peer state change");
1740 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1741 struct list_head *up_list,
1742 struct list_head *down_list)
1744 struct snd_soc_dapm_path *path;
1746 if (w->power == power)
1749 trace_snd_soc_dapm_widget_power(w, power);
1751 /* If we changed our power state perhaps our neigbours changed
1754 list_for_each_entry(path, &w->sources, list_sink) {
1756 dapm_widget_set_peer_power(path->source, power,
1761 case snd_soc_dapm_supply:
1762 case snd_soc_dapm_regulator_supply:
1763 case snd_soc_dapm_clock_supply:
1764 case snd_soc_dapm_kcontrol:
1765 /* Supplies can't affect their outputs, only their inputs */
1768 list_for_each_entry(path, &w->sinks, list_source) {
1770 dapm_widget_set_peer_power(path->sink, power,
1778 dapm_seq_insert(w, up_list, true);
1780 dapm_seq_insert(w, down_list, false);
1783 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1784 struct list_head *up_list,
1785 struct list_head *down_list)
1790 case snd_soc_dapm_pre:
1791 dapm_seq_insert(w, down_list, false);
1793 case snd_soc_dapm_post:
1794 dapm_seq_insert(w, up_list, true);
1798 power = dapm_widget_power_check(w);
1800 dapm_widget_set_power(w, power, up_list, down_list);
1806 * Scan each dapm widget for complete audio path.
1807 * A complete path is a route that has valid endpoints i.e.:-
1809 * o DAC to output pin.
1810 * o Input Pin to ADC.
1811 * o Input pin to Output pin (bypass, sidetone)
1812 * o DAC to ADC (loopback).
1814 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1816 struct snd_soc_dapm_widget *w;
1817 struct snd_soc_dapm_context *d;
1819 LIST_HEAD(down_list);
1820 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1821 enum snd_soc_bias_level bias;
1823 trace_snd_soc_dapm_start(card);
1825 list_for_each_entry(d, &card->dapm_list, list) {
1826 if (d->idle_bias_off)
1827 d->target_bias_level = SND_SOC_BIAS_OFF;
1829 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1834 /* Check which widgets we need to power and store them in
1835 * lists indicating if they should be powered up or down. We
1836 * only check widgets that have been flagged as dirty but note
1837 * that new widgets may be added to the dirty list while we
1840 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1841 dapm_power_one_widget(w, &up_list, &down_list);
1844 list_for_each_entry(w, &card->widgets, list) {
1846 case snd_soc_dapm_pre:
1847 case snd_soc_dapm_post:
1848 /* These widgets always need to be powered */
1851 list_del_init(&w->dirty);
1858 /* Supplies and micbiases only bring the
1859 * context up to STANDBY as unless something
1860 * else is active and passing audio they
1861 * generally don't require full power. Signal
1862 * generators are virtual pins and have no
1863 * power impact themselves.
1866 case snd_soc_dapm_siggen:
1867 case snd_soc_dapm_vmid:
1869 case snd_soc_dapm_supply:
1870 case snd_soc_dapm_regulator_supply:
1871 case snd_soc_dapm_clock_supply:
1872 case snd_soc_dapm_micbias:
1873 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1874 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1877 d->target_bias_level = SND_SOC_BIAS_ON;
1884 /* Force all contexts in the card to the same bias state if
1885 * they're not ground referenced.
1887 bias = SND_SOC_BIAS_OFF;
1888 list_for_each_entry(d, &card->dapm_list, list)
1889 if (d->target_bias_level > bias)
1890 bias = d->target_bias_level;
1891 list_for_each_entry(d, &card->dapm_list, list)
1892 if (!d->idle_bias_off)
1893 d->target_bias_level = bias;
1895 trace_snd_soc_dapm_walk_done(card);
1897 /* Run all the bias changes in parallel */
1898 list_for_each_entry(d, &card->dapm_list, list)
1899 async_schedule_domain(dapm_pre_sequence_async, d,
1901 async_synchronize_full_domain(&async_domain);
1903 list_for_each_entry(w, &down_list, power_list) {
1904 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1907 list_for_each_entry(w, &up_list, power_list) {
1908 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1911 /* Power down widgets first; try to avoid amplifying pops. */
1912 dapm_seq_run(card, &down_list, event, false);
1914 dapm_widget_update(card);
1917 dapm_seq_run(card, &up_list, event, true);
1919 /* Run all the bias changes in parallel */
1920 list_for_each_entry(d, &card->dapm_list, list)
1921 async_schedule_domain(dapm_post_sequence_async, d,
1923 async_synchronize_full_domain(&async_domain);
1925 /* do we need to notify any clients that DAPM event is complete */
1926 list_for_each_entry(d, &card->dapm_list, list) {
1927 if (d->stream_event)
1928 d->stream_event(d, event);
1931 pop_dbg(card->dev, card->pop_time,
1932 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1933 pop_wait(card->pop_time);
1935 trace_snd_soc_dapm_done(card);
1940 #ifdef CONFIG_DEBUG_FS
1941 static ssize_t dapm_widget_power_read_file(struct file *file,
1942 char __user *user_buf,
1943 size_t count, loff_t *ppos)
1945 struct snd_soc_dapm_widget *w = file->private_data;
1949 struct snd_soc_dapm_path *p = NULL;
1951 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1955 in = is_connected_input_ep(w, NULL);
1956 dapm_clear_walk_input(w->dapm, &w->sources);
1957 out = is_connected_output_ep(w, NULL);
1958 dapm_clear_walk_output(w->dapm, &w->sinks);
1960 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1961 w->name, w->power ? "On" : "Off",
1962 w->force ? " (forced)" : "", in, out);
1965 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1966 " - R%d(0x%x) mask 0x%x",
1967 w->reg, w->reg, w->mask << w->shift);
1969 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1972 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1974 w->active ? "active" : "inactive");
1976 list_for_each_entry(p, &w->sources, list_sink) {
1977 if (p->connected && !p->connected(w, p->source))
1981 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1982 " in \"%s\" \"%s\"\n",
1983 p->name ? p->name : "static",
1986 list_for_each_entry(p, &w->sinks, list_source) {
1987 if (p->connected && !p->connected(w, p->sink))
1991 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1992 " out \"%s\" \"%s\"\n",
1993 p->name ? p->name : "static",
1997 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2003 static const struct file_operations dapm_widget_power_fops = {
2004 .open = simple_open,
2005 .read = dapm_widget_power_read_file,
2006 .llseek = default_llseek,
2009 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2010 size_t count, loff_t *ppos)
2012 struct snd_soc_dapm_context *dapm = file->private_data;
2015 switch (dapm->bias_level) {
2016 case SND_SOC_BIAS_ON:
2019 case SND_SOC_BIAS_PREPARE:
2020 level = "Prepare\n";
2022 case SND_SOC_BIAS_STANDBY:
2023 level = "Standby\n";
2025 case SND_SOC_BIAS_OFF:
2029 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2030 level = "Unknown\n";
2034 return simple_read_from_buffer(user_buf, count, ppos, level,
2038 static const struct file_operations dapm_bias_fops = {
2039 .open = simple_open,
2040 .read = dapm_bias_read_file,
2041 .llseek = default_llseek,
2044 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2045 struct dentry *parent)
2049 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2051 if (!dapm->debugfs_dapm) {
2053 "ASoC: Failed to create DAPM debugfs directory\n");
2057 d = debugfs_create_file("bias_level", 0444,
2058 dapm->debugfs_dapm, dapm,
2062 "ASoC: Failed to create bias level debugfs file\n");
2065 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2067 struct snd_soc_dapm_context *dapm = w->dapm;
2070 if (!dapm->debugfs_dapm || !w->name)
2073 d = debugfs_create_file(w->name, 0444,
2074 dapm->debugfs_dapm, w,
2075 &dapm_widget_power_fops);
2077 dev_warn(w->dapm->dev,
2078 "ASoC: Failed to create %s debugfs file\n",
2082 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2084 debugfs_remove_recursive(dapm->debugfs_dapm);
2088 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2089 struct dentry *parent)
2093 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2097 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2103 /* test and update the power status of a mux widget */
2104 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2105 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2107 struct snd_soc_dapm_path *path;
2110 /* find dapm widget path assoc with kcontrol */
2111 dapm_kcontrol_for_each_path(path, kcontrol) {
2112 if (!path->name || !e->texts[mux])
2116 /* we now need to match the string in the enum to the path */
2117 if (!(strcmp(path->name, e->texts[mux]))) {
2118 path->connect = 1; /* new connection */
2119 dapm_mark_dirty(path->source, "mux connection");
2122 dapm_mark_dirty(path->source,
2123 "mux disconnection");
2124 path->connect = 0; /* old connection must be powered down */
2126 dapm_mark_dirty(path->sink, "mux change");
2130 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2135 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2136 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2137 struct snd_soc_dapm_update *update)
2139 struct snd_soc_card *card = dapm->card;
2142 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2143 card->update = update;
2144 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2145 card->update = NULL;
2146 mutex_unlock(&card->dapm_mutex);
2148 soc_dpcm_runtime_update(card);
2151 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2153 /* test and update the power status of a mixer or switch widget */
2154 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2155 struct snd_kcontrol *kcontrol, int connect)
2157 struct snd_soc_dapm_path *path;
2160 /* find dapm widget path assoc with kcontrol */
2161 dapm_kcontrol_for_each_path(path, kcontrol) {
2163 path->connect = connect;
2164 dapm_mark_dirty(path->source, "mixer connection");
2165 dapm_mark_dirty(path->sink, "mixer update");
2169 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2174 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2175 struct snd_kcontrol *kcontrol, int connect,
2176 struct snd_soc_dapm_update *update)
2178 struct snd_soc_card *card = dapm->card;
2181 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2182 card->update = update;
2183 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2184 card->update = NULL;
2185 mutex_unlock(&card->dapm_mutex);
2187 soc_dpcm_runtime_update(card);
2190 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2192 /* show dapm widget status in sys fs */
2193 static ssize_t dapm_widget_show(struct device *dev,
2194 struct device_attribute *attr, char *buf)
2196 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2197 struct snd_soc_codec *codec =rtd->codec;
2198 struct snd_soc_dapm_widget *w;
2200 char *state = "not set";
2202 list_for_each_entry(w, &codec->card->widgets, list) {
2203 if (w->dapm != &codec->dapm)
2206 /* only display widgets that burnm power */
2208 case snd_soc_dapm_hp:
2209 case snd_soc_dapm_mic:
2210 case snd_soc_dapm_spk:
2211 case snd_soc_dapm_line:
2212 case snd_soc_dapm_micbias:
2213 case snd_soc_dapm_dac:
2214 case snd_soc_dapm_adc:
2215 case snd_soc_dapm_pga:
2216 case snd_soc_dapm_out_drv:
2217 case snd_soc_dapm_mixer:
2218 case snd_soc_dapm_mixer_named_ctl:
2219 case snd_soc_dapm_supply:
2220 case snd_soc_dapm_regulator_supply:
2221 case snd_soc_dapm_clock_supply:
2223 count += sprintf(buf + count, "%s: %s\n",
2224 w->name, w->power ? "On":"Off");
2231 switch (codec->dapm.bias_level) {
2232 case SND_SOC_BIAS_ON:
2235 case SND_SOC_BIAS_PREPARE:
2238 case SND_SOC_BIAS_STANDBY:
2241 case SND_SOC_BIAS_OFF:
2245 count += sprintf(buf + count, "PM State: %s\n", state);
2250 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2252 int snd_soc_dapm_sys_add(struct device *dev)
2254 return device_create_file(dev, &dev_attr_dapm_widget);
2257 static void snd_soc_dapm_sys_remove(struct device *dev)
2259 device_remove_file(dev, &dev_attr_dapm_widget);
2262 static void dapm_free_path(struct snd_soc_dapm_path *path)
2264 list_del(&path->list_sink);
2265 list_del(&path->list_source);
2266 list_del(&path->list_kcontrol);
2267 list_del(&path->list);
2271 /* free all dapm widgets and resources */
2272 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2274 struct snd_soc_dapm_widget *w, *next_w;
2275 struct snd_soc_dapm_path *p, *next_p;
2277 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2278 if (w->dapm != dapm)
2282 * remove source and sink paths associated to this widget.
2283 * While removing the path, remove reference to it from both
2284 * source and sink widgets so that path is removed only once.
2286 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2289 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2292 kfree(w->kcontrols);
2298 static struct snd_soc_dapm_widget *dapm_find_widget(
2299 struct snd_soc_dapm_context *dapm, const char *pin,
2300 bool search_other_contexts)
2302 struct snd_soc_dapm_widget *w;
2303 struct snd_soc_dapm_widget *fallback = NULL;
2305 list_for_each_entry(w, &dapm->card->widgets, list) {
2306 if (!strcmp(w->name, pin)) {
2307 if (w->dapm == dapm)
2314 if (search_other_contexts)
2320 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2321 const char *pin, int status)
2323 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2326 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2330 if (w->connected != status)
2331 dapm_mark_dirty(w, "pin configuration");
2333 w->connected = status;
2341 * snd_soc_dapm_sync - scan and power dapm paths
2342 * @dapm: DAPM context
2344 * Walks all dapm audio paths and powers widgets according to their
2345 * stream or path usage.
2347 * Returns 0 for success.
2349 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2354 * Suppress early reports (eg, jacks syncing their state) to avoid
2355 * silly DAPM runs during card startup.
2357 if (!dapm->card || !dapm->card->instantiated)
2360 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2361 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2362 mutex_unlock(&dapm->card->dapm_mutex);
2365 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2367 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2368 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2369 const char *control,
2370 int (*connected)(struct snd_soc_dapm_widget *source,
2371 struct snd_soc_dapm_widget *sink))
2373 struct snd_soc_dapm_path *path;
2376 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2380 path->source = wsource;
2382 path->connected = connected;
2383 INIT_LIST_HEAD(&path->list);
2384 INIT_LIST_HEAD(&path->list_kcontrol);
2385 INIT_LIST_HEAD(&path->list_source);
2386 INIT_LIST_HEAD(&path->list_sink);
2388 /* check for external widgets */
2389 if (wsink->id == snd_soc_dapm_input) {
2390 if (wsource->id == snd_soc_dapm_micbias ||
2391 wsource->id == snd_soc_dapm_mic ||
2392 wsource->id == snd_soc_dapm_line ||
2393 wsource->id == snd_soc_dapm_output)
2396 if (wsource->id == snd_soc_dapm_output) {
2397 if (wsink->id == snd_soc_dapm_spk ||
2398 wsink->id == snd_soc_dapm_hp ||
2399 wsink->id == snd_soc_dapm_line ||
2400 wsink->id == snd_soc_dapm_input)
2404 dapm_mark_dirty(wsource, "Route added");
2405 dapm_mark_dirty(wsink, "Route added");
2407 /* connect static paths */
2408 if (control == NULL) {
2409 list_add(&path->list, &dapm->card->paths);
2410 list_add(&path->list_sink, &wsink->sources);
2411 list_add(&path->list_source, &wsource->sinks);
2416 /* connect dynamic paths */
2417 switch (wsink->id) {
2418 case snd_soc_dapm_adc:
2419 case snd_soc_dapm_dac:
2420 case snd_soc_dapm_pga:
2421 case snd_soc_dapm_out_drv:
2422 case snd_soc_dapm_input:
2423 case snd_soc_dapm_output:
2424 case snd_soc_dapm_siggen:
2425 case snd_soc_dapm_micbias:
2426 case snd_soc_dapm_vmid:
2427 case snd_soc_dapm_pre:
2428 case snd_soc_dapm_post:
2429 case snd_soc_dapm_supply:
2430 case snd_soc_dapm_regulator_supply:
2431 case snd_soc_dapm_clock_supply:
2432 case snd_soc_dapm_aif_in:
2433 case snd_soc_dapm_aif_out:
2434 case snd_soc_dapm_dai_in:
2435 case snd_soc_dapm_dai_out:
2436 case snd_soc_dapm_dai_link:
2437 case snd_soc_dapm_kcontrol:
2438 list_add(&path->list, &dapm->card->paths);
2439 list_add(&path->list_sink, &wsink->sources);
2440 list_add(&path->list_source, &wsource->sinks);
2443 case snd_soc_dapm_mux:
2444 case snd_soc_dapm_virt_mux:
2445 case snd_soc_dapm_value_mux:
2446 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2447 &wsink->kcontrol_news[0]);
2451 case snd_soc_dapm_switch:
2452 case snd_soc_dapm_mixer:
2453 case snd_soc_dapm_mixer_named_ctl:
2454 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2458 case snd_soc_dapm_hp:
2459 case snd_soc_dapm_mic:
2460 case snd_soc_dapm_line:
2461 case snd_soc_dapm_spk:
2462 list_add(&path->list, &dapm->card->paths);
2463 list_add(&path->list_sink, &wsink->sources);
2464 list_add(&path->list_source, &wsource->sinks);
2475 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2476 const struct snd_soc_dapm_route *route)
2478 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2479 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2482 char prefixed_sink[80];
2483 char prefixed_source[80];
2486 if (dapm->codec && dapm->codec->name_prefix) {
2487 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2488 dapm->codec->name_prefix, route->sink);
2489 sink = prefixed_sink;
2490 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2491 dapm->codec->name_prefix, route->source);
2492 source = prefixed_source;
2495 source = route->source;
2499 * find src and dest widgets over all widgets but favor a widget from
2500 * current DAPM context
2502 list_for_each_entry(w, &dapm->card->widgets, list) {
2503 if (!wsink && !(strcmp(w->name, sink))) {
2505 if (w->dapm == dapm)
2509 if (!wsource && !(strcmp(w->name, source))) {
2511 if (w->dapm == dapm)
2515 /* use widget from another DAPM context if not found from this */
2521 if (wsource == NULL) {
2522 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2526 if (wsink == NULL) {
2527 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2532 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2539 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2540 source, route->control, sink);
2544 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2545 const struct snd_soc_dapm_route *route)
2547 struct snd_soc_dapm_path *path, *p;
2550 char prefixed_sink[80];
2551 char prefixed_source[80];
2553 if (route->control) {
2555 "ASoC: Removal of routes with controls not supported\n");
2559 if (dapm->codec && dapm->codec->name_prefix) {
2560 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2561 dapm->codec->name_prefix, route->sink);
2562 sink = prefixed_sink;
2563 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2564 dapm->codec->name_prefix, route->source);
2565 source = prefixed_source;
2568 source = route->source;
2572 list_for_each_entry(p, &dapm->card->paths, list) {
2573 if (strcmp(p->source->name, source) != 0)
2575 if (strcmp(p->sink->name, sink) != 0)
2582 dapm_mark_dirty(path->source, "Route removed");
2583 dapm_mark_dirty(path->sink, "Route removed");
2585 dapm_free_path(path);
2587 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2595 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2596 * @dapm: DAPM context
2597 * @route: audio routes
2598 * @num: number of routes
2600 * Connects 2 dapm widgets together via a named audio path. The sink is
2601 * the widget receiving the audio signal, whilst the source is the sender
2602 * of the audio signal.
2604 * Returns 0 for success else error. On error all resources can be freed
2605 * with a call to snd_soc_card_free().
2607 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2608 const struct snd_soc_dapm_route *route, int num)
2612 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2613 for (i = 0; i < num; i++) {
2614 r = snd_soc_dapm_add_route(dapm, route);
2616 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2618 route->control ? route->control : "direct",
2624 mutex_unlock(&dapm->card->dapm_mutex);
2628 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2631 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2632 * @dapm: DAPM context
2633 * @route: audio routes
2634 * @num: number of routes
2636 * Removes routes from the DAPM context.
2638 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2639 const struct snd_soc_dapm_route *route, int num)
2643 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2644 for (i = 0; i < num; i++) {
2645 snd_soc_dapm_del_route(dapm, route);
2648 mutex_unlock(&dapm->card->dapm_mutex);
2652 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2654 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2655 const struct snd_soc_dapm_route *route)
2657 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2660 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2663 struct snd_soc_dapm_path *path;
2667 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2673 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2678 if (route->control || route->connected)
2679 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2680 route->source, route->sink);
2682 list_for_each_entry(path, &source->sinks, list_source) {
2683 if (path->sink == sink) {
2690 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2691 route->source, route->sink);
2693 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2694 count, route->source, route->sink);
2700 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2701 * @dapm: DAPM context
2702 * @route: audio routes
2703 * @num: number of routes
2705 * Mark existing routes matching those specified in the passed array
2706 * as being weak, meaning that they are ignored for the purpose of
2707 * power decisions. The main intended use case is for sidetone paths
2708 * which couple audio between other independent paths if they are both
2709 * active in order to make the combination work better at the user
2710 * level but which aren't intended to be "used".
2712 * Note that CODEC drivers should not use this as sidetone type paths
2713 * can frequently also be used as bypass paths.
2715 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2716 const struct snd_soc_dapm_route *route, int num)
2721 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2722 for (i = 0; i < num; i++) {
2723 err = snd_soc_dapm_weak_route(dapm, route);
2728 mutex_unlock(&dapm->card->dapm_mutex);
2732 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2735 * snd_soc_dapm_new_widgets - add new dapm widgets
2736 * @dapm: DAPM context
2738 * Checks the codec for any new dapm widgets and creates them if found.
2740 * Returns 0 for success.
2742 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2744 struct snd_soc_dapm_widget *w;
2747 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2749 list_for_each_entry(w, &card->widgets, list)
2754 if (w->num_kcontrols) {
2755 w->kcontrols = kzalloc(w->num_kcontrols *
2756 sizeof(struct snd_kcontrol *),
2758 if (!w->kcontrols) {
2759 mutex_unlock(&card->dapm_mutex);
2765 case snd_soc_dapm_switch:
2766 case snd_soc_dapm_mixer:
2767 case snd_soc_dapm_mixer_named_ctl:
2770 case snd_soc_dapm_mux:
2771 case snd_soc_dapm_virt_mux:
2772 case snd_soc_dapm_value_mux:
2775 case snd_soc_dapm_pga:
2776 case snd_soc_dapm_out_drv:
2783 /* Read the initial power state from the device */
2785 val = soc_widget_read(w, w->reg) >> w->shift;
2787 if (val == w->on_val)
2793 dapm_mark_dirty(w, "new widget");
2794 dapm_debugfs_add_widget(w);
2797 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2798 mutex_unlock(&card->dapm_mutex);
2801 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2804 * snd_soc_dapm_get_volsw - dapm mixer get callback
2805 * @kcontrol: mixer control
2806 * @ucontrol: control element information
2808 * Callback to get the value of a dapm mixer control.
2810 * Returns 0 for success.
2812 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2813 struct snd_ctl_elem_value *ucontrol)
2815 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2816 struct snd_soc_card *card = codec->card;
2817 struct soc_mixer_control *mc =
2818 (struct soc_mixer_control *)kcontrol->private_value;
2820 unsigned int shift = mc->shift;
2822 unsigned int mask = (1 << fls(max)) - 1;
2823 unsigned int invert = mc->invert;
2826 if (snd_soc_volsw_is_stereo(mc))
2827 dev_warn(codec->dapm.dev,
2828 "ASoC: Control '%s' is stereo, which is not supported\n",
2831 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2832 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
2833 val = (snd_soc_read(codec, reg) >> shift) & mask;
2835 val = dapm_kcontrol_get_value(kcontrol);
2836 mutex_unlock(&card->dapm_mutex);
2839 ucontrol->value.integer.value[0] = max - val;
2841 ucontrol->value.integer.value[0] = val;
2845 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2848 * snd_soc_dapm_put_volsw - dapm mixer set callback
2849 * @kcontrol: mixer control
2850 * @ucontrol: control element information
2852 * Callback to set the value of a dapm mixer control.
2854 * Returns 0 for success.
2856 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_value *ucontrol)
2859 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2860 struct snd_soc_card *card = codec->card;
2861 struct soc_mixer_control *mc =
2862 (struct soc_mixer_control *)kcontrol->private_value;
2864 unsigned int shift = mc->shift;
2866 unsigned int mask = (1 << fls(max)) - 1;
2867 unsigned int invert = mc->invert;
2869 int connect, change;
2870 struct snd_soc_dapm_update update;
2873 if (snd_soc_volsw_is_stereo(mc))
2874 dev_warn(codec->dapm.dev,
2875 "ASoC: Control '%s' is stereo, which is not supported\n",
2878 val = (ucontrol->value.integer.value[0] & mask);
2884 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2886 change = dapm_kcontrol_set_value(kcontrol, val);
2888 if (reg != SND_SOC_NOPM) {
2889 mask = mask << shift;
2892 change = snd_soc_test_bits(codec, reg, mask, val);
2896 if (reg != SND_SOC_NOPM) {
2897 update.kcontrol = kcontrol;
2902 card->update = &update;
2905 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2907 card->update = NULL;
2910 mutex_unlock(&card->dapm_mutex);
2913 soc_dpcm_runtime_update(card);
2917 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2920 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2921 * @kcontrol: mixer control
2922 * @ucontrol: control element information
2924 * Callback to get the value of a dapm enumerated double mixer control.
2926 * Returns 0 for success.
2928 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2929 struct snd_ctl_elem_value *ucontrol)
2931 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2932 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2935 val = snd_soc_read(codec, e->reg);
2936 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2937 if (e->shift_l != e->shift_r)
2938 ucontrol->value.enumerated.item[1] =
2939 (val >> e->shift_r) & e->mask;
2943 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2946 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2947 * @kcontrol: mixer control
2948 * @ucontrol: control element information
2950 * Callback to set the value of a dapm enumerated double mixer control.
2952 * Returns 0 for success.
2954 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2955 struct snd_ctl_elem_value *ucontrol)
2957 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2958 struct snd_soc_card *card = codec->card;
2959 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2960 unsigned int val, mux, change;
2962 struct snd_soc_dapm_update update;
2965 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2967 mux = ucontrol->value.enumerated.item[0];
2968 val = mux << e->shift_l;
2969 mask = e->mask << e->shift_l;
2970 if (e->shift_l != e->shift_r) {
2971 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2973 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2974 mask |= e->mask << e->shift_r;
2977 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2979 change = snd_soc_test_bits(codec, e->reg, mask, val);
2981 update.kcontrol = kcontrol;
2982 update.reg = e->reg;
2985 card->update = &update;
2987 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2989 card->update = NULL;
2992 mutex_unlock(&card->dapm_mutex);
2995 soc_dpcm_runtime_update(card);
2999 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3002 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
3003 * @kcontrol: mixer control
3004 * @ucontrol: control element information
3006 * Returns 0 for success.
3008 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
3009 struct snd_ctl_elem_value *ucontrol)
3011 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
3014 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
3017 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
3018 * @kcontrol: mixer control
3019 * @ucontrol: control element information
3021 * Returns 0 for success.
3023 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
3024 struct snd_ctl_elem_value *ucontrol)
3026 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3027 struct snd_soc_card *card = codec->card;
3029 struct soc_enum *e =
3030 (struct soc_enum *)kcontrol->private_value;
3034 if (ucontrol->value.enumerated.item[0] >= e->max)
3037 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3039 value = ucontrol->value.enumerated.item[0];
3040 change = dapm_kcontrol_set_value(kcontrol, value);
3042 ret = soc_dapm_mux_update_power(card, kcontrol, value, e);
3044 mutex_unlock(&card->dapm_mutex);
3047 soc_dpcm_runtime_update(card);
3051 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3054 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3056 * @kcontrol: mixer control
3057 * @ucontrol: control element information
3059 * Callback to get the value of a dapm semi enumerated double mixer control.
3061 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3062 * used for handling bitfield coded enumeration for example.
3064 * Returns 0 for success.
3066 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3067 struct snd_ctl_elem_value *ucontrol)
3069 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3070 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3071 unsigned int reg_val, val, mux;
3073 reg_val = snd_soc_read(codec, e->reg);
3074 val = (reg_val >> e->shift_l) & e->mask;
3075 for (mux = 0; mux < e->max; mux++) {
3076 if (val == e->values[mux])
3079 ucontrol->value.enumerated.item[0] = mux;
3080 if (e->shift_l != e->shift_r) {
3081 val = (reg_val >> e->shift_r) & e->mask;
3082 for (mux = 0; mux < e->max; mux++) {
3083 if (val == e->values[mux])
3086 ucontrol->value.enumerated.item[1] = mux;
3091 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3094 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3096 * @kcontrol: mixer control
3097 * @ucontrol: control element information
3099 * Callback to set the value of a dapm semi enumerated double mixer control.
3101 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3102 * used for handling bitfield coded enumeration for example.
3104 * Returns 0 for success.
3106 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3107 struct snd_ctl_elem_value *ucontrol)
3109 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3110 struct snd_soc_card *card = codec->card;
3111 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3112 unsigned int val, mux, change;
3114 struct snd_soc_dapm_update update;
3117 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3119 mux = ucontrol->value.enumerated.item[0];
3120 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3121 mask = e->mask << e->shift_l;
3122 if (e->shift_l != e->shift_r) {
3123 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3125 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3126 mask |= e->mask << e->shift_r;
3129 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3131 change = snd_soc_test_bits(codec, e->reg, mask, val);
3133 update.kcontrol = kcontrol;
3134 update.reg = e->reg;
3137 card->update = &update;
3139 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
3141 card->update = NULL;
3144 mutex_unlock(&card->dapm_mutex);
3147 soc_dpcm_runtime_update(card);
3151 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3154 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3156 * @kcontrol: mixer control
3157 * @uinfo: control element information
3159 * Callback to provide information about a pin switch control.
3161 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3162 struct snd_ctl_elem_info *uinfo)
3164 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3166 uinfo->value.integer.min = 0;
3167 uinfo->value.integer.max = 1;
3171 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3174 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3176 * @kcontrol: mixer control
3179 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3180 struct snd_ctl_elem_value *ucontrol)
3182 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3183 const char *pin = (const char *)kcontrol->private_value;
3185 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3187 ucontrol->value.integer.value[0] =
3188 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3190 mutex_unlock(&card->dapm_mutex);
3194 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3197 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3199 * @kcontrol: mixer control
3202 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3203 struct snd_ctl_elem_value *ucontrol)
3205 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3206 const char *pin = (const char *)kcontrol->private_value;
3208 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3210 if (ucontrol->value.integer.value[0])
3211 snd_soc_dapm_enable_pin(&card->dapm, pin);
3213 snd_soc_dapm_disable_pin(&card->dapm, pin);
3215 mutex_unlock(&card->dapm_mutex);
3217 snd_soc_dapm_sync(&card->dapm);
3220 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3222 static struct snd_soc_dapm_widget *
3223 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3224 const struct snd_soc_dapm_widget *widget)
3226 struct snd_soc_dapm_widget *w;
3229 if ((w = dapm_cnew_widget(widget)) == NULL)
3233 case snd_soc_dapm_regulator_supply:
3234 w->regulator = devm_regulator_get(dapm->dev, w->name);
3235 if (IS_ERR(w->regulator)) {
3236 ret = PTR_ERR(w->regulator);
3237 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3242 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3243 ret = regulator_allow_bypass(w->regulator, true);
3245 dev_warn(w->dapm->dev,
3246 "ASoC: Failed to unbypass %s: %d\n",
3250 case snd_soc_dapm_clock_supply:
3251 #ifdef CONFIG_CLKDEV_LOOKUP
3252 w->clk = devm_clk_get(dapm->dev, w->name);
3253 if (IS_ERR(w->clk)) {
3254 ret = PTR_ERR(w->clk);
3255 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3267 if (dapm->codec && dapm->codec->name_prefix)
3268 w->name = kasprintf(GFP_KERNEL, "%s %s",
3269 dapm->codec->name_prefix, widget->name);
3271 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3273 if (w->name == NULL) {
3279 case snd_soc_dapm_switch:
3280 case snd_soc_dapm_mixer:
3281 case snd_soc_dapm_mixer_named_ctl:
3282 w->power_check = dapm_generic_check_power;
3284 case snd_soc_dapm_mux:
3285 case snd_soc_dapm_virt_mux:
3286 case snd_soc_dapm_value_mux:
3287 w->power_check = dapm_generic_check_power;
3289 case snd_soc_dapm_dai_out:
3290 w->power_check = dapm_adc_check_power;
3292 case snd_soc_dapm_dai_in:
3293 w->power_check = dapm_dac_check_power;
3295 case snd_soc_dapm_adc:
3296 case snd_soc_dapm_aif_out:
3297 case snd_soc_dapm_dac:
3298 case snd_soc_dapm_aif_in:
3299 case snd_soc_dapm_pga:
3300 case snd_soc_dapm_out_drv:
3301 case snd_soc_dapm_input:
3302 case snd_soc_dapm_output:
3303 case snd_soc_dapm_micbias:
3304 case snd_soc_dapm_spk:
3305 case snd_soc_dapm_hp:
3306 case snd_soc_dapm_mic:
3307 case snd_soc_dapm_line:
3308 case snd_soc_dapm_dai_link:
3309 w->power_check = dapm_generic_check_power;
3311 case snd_soc_dapm_supply:
3312 case snd_soc_dapm_regulator_supply:
3313 case snd_soc_dapm_clock_supply:
3314 case snd_soc_dapm_kcontrol:
3315 w->power_check = dapm_supply_check_power;
3318 w->power_check = dapm_always_on_check_power;
3323 w->codec = dapm->codec;
3324 w->platform = dapm->platform;
3325 INIT_LIST_HEAD(&w->sources);
3326 INIT_LIST_HEAD(&w->sinks);
3327 INIT_LIST_HEAD(&w->list);
3328 INIT_LIST_HEAD(&w->dirty);
3329 list_add(&w->list, &dapm->card->widgets);
3331 /* machine layer set ups unconnected pins and insertions */
3337 * snd_soc_dapm_new_controls - create new dapm controls
3338 * @dapm: DAPM context
3339 * @widget: widget array
3340 * @num: number of widgets
3342 * Creates new DAPM controls based upon the templates.
3344 * Returns 0 for success else error.
3346 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3347 const struct snd_soc_dapm_widget *widget,
3350 struct snd_soc_dapm_widget *w;
3354 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3355 for (i = 0; i < num; i++) {
3356 w = snd_soc_dapm_new_control(dapm, widget);
3359 "ASoC: Failed to create DAPM control %s\n",
3366 mutex_unlock(&dapm->card->dapm_mutex);
3369 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3371 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3372 struct snd_kcontrol *kcontrol, int event)
3374 struct snd_soc_dapm_path *source_p, *sink_p;
3375 struct snd_soc_dai *source, *sink;
3376 const struct snd_soc_pcm_stream *config = w->params;
3377 struct snd_pcm_substream substream;
3378 struct snd_pcm_hw_params *params = NULL;
3382 if (WARN_ON(!config) ||
3383 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3386 /* We only support a single source and sink, pick the first */
3387 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3389 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3392 if (WARN_ON(!source_p || !sink_p) ||
3393 WARN_ON(!sink_p->source || !source_p->sink) ||
3394 WARN_ON(!source_p->source || !sink_p->sink))
3397 source = source_p->source->priv;
3398 sink = sink_p->sink->priv;
3400 /* Be a little careful as we don't want to overflow the mask array */
3401 if (config->formats) {
3402 fmt = ffs(config->formats) - 1;
3404 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3409 /* Currently very limited parameter selection */
3410 params = kzalloc(sizeof(*params), GFP_KERNEL);
3415 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3417 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3419 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3422 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3423 = config->channels_min;
3424 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3425 = config->channels_max;
3427 memset(&substream, 0, sizeof(substream));
3430 case SND_SOC_DAPM_PRE_PMU:
3431 if (source->driver->ops && source->driver->ops->hw_params) {
3432 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3433 ret = source->driver->ops->hw_params(&substream,
3436 dev_err(source->dev,
3437 "ASoC: hw_params() failed: %d\n", ret);
3442 if (sink->driver->ops && sink->driver->ops->hw_params) {
3443 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3444 ret = sink->driver->ops->hw_params(&substream, params,
3448 "ASoC: hw_params() failed: %d\n", ret);
3454 case SND_SOC_DAPM_POST_PMU:
3455 ret = snd_soc_dai_digital_mute(sink, 0,
3456 SNDRV_PCM_STREAM_PLAYBACK);
3457 if (ret != 0 && ret != -ENOTSUPP)
3458 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3462 case SND_SOC_DAPM_PRE_PMD:
3463 ret = snd_soc_dai_digital_mute(sink, 1,
3464 SNDRV_PCM_STREAM_PLAYBACK);
3465 if (ret != 0 && ret != -ENOTSUPP)
3466 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3471 WARN(1, "Unknown event %d\n", event);
3480 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3481 const struct snd_soc_pcm_stream *params,
3482 struct snd_soc_dapm_widget *source,
3483 struct snd_soc_dapm_widget *sink)
3485 struct snd_soc_dapm_route routes[2];
3486 struct snd_soc_dapm_widget template;
3487 struct snd_soc_dapm_widget *w;
3491 len = strlen(source->name) + strlen(sink->name) + 2;
3492 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3495 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3497 memset(&template, 0, sizeof(template));
3498 template.reg = SND_SOC_NOPM;
3499 template.id = snd_soc_dapm_dai_link;
3500 template.name = link_name;
3501 template.event = snd_soc_dai_link_event;
3502 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3503 SND_SOC_DAPM_PRE_PMD;
3505 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3507 w = snd_soc_dapm_new_control(&card->dapm, &template);
3509 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3516 memset(&routes, 0, sizeof(routes));
3518 routes[0].source = source->name;
3519 routes[0].sink = link_name;
3520 routes[1].source = link_name;
3521 routes[1].sink = sink->name;
3523 return snd_soc_dapm_add_routes(&card->dapm, routes,
3524 ARRAY_SIZE(routes));
3527 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3528 struct snd_soc_dai *dai)
3530 struct snd_soc_dapm_widget template;
3531 struct snd_soc_dapm_widget *w;
3533 WARN_ON(dapm->dev != dai->dev);
3535 memset(&template, 0, sizeof(template));
3536 template.reg = SND_SOC_NOPM;
3538 if (dai->driver->playback.stream_name) {
3539 template.id = snd_soc_dapm_dai_in;
3540 template.name = dai->driver->playback.stream_name;
3541 template.sname = dai->driver->playback.stream_name;
3543 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3546 w = snd_soc_dapm_new_control(dapm, &template);
3548 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3549 dai->driver->playback.stream_name);
3554 dai->playback_widget = w;
3557 if (dai->driver->capture.stream_name) {
3558 template.id = snd_soc_dapm_dai_out;
3559 template.name = dai->driver->capture.stream_name;
3560 template.sname = dai->driver->capture.stream_name;
3562 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3565 w = snd_soc_dapm_new_control(dapm, &template);
3567 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3568 dai->driver->capture.stream_name);
3573 dai->capture_widget = w;
3579 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3581 struct snd_soc_dapm_widget *dai_w, *w;
3582 struct snd_soc_dai *dai;
3584 /* For each DAI widget... */
3585 list_for_each_entry(dai_w, &card->widgets, list) {
3586 switch (dai_w->id) {
3587 case snd_soc_dapm_dai_in:
3588 case snd_soc_dapm_dai_out:
3596 /* ...find all widgets with the same stream and link them */
3597 list_for_each_entry(w, &card->widgets, list) {
3598 if (w->dapm != dai_w->dapm)
3602 case snd_soc_dapm_dai_in:
3603 case snd_soc_dapm_dai_out:
3609 if (!w->sname || !strstr(w->sname, dai_w->name))
3612 if (dai->driver->playback.stream_name &&
3614 dai->driver->playback.stream_name)) {
3615 dev_dbg(dai->dev, "%s -> %s\n",
3616 dai->playback_widget->name, w->name);
3618 snd_soc_dapm_add_path(w->dapm,
3619 dai->playback_widget, w, NULL, NULL);
3622 if (dai->driver->capture.stream_name &&
3624 dai->driver->capture.stream_name)) {
3625 dev_dbg(dai->dev, "%s -> %s\n",
3626 w->name, dai->capture_widget->name);
3628 snd_soc_dapm_add_path(w->dapm, w,
3629 dai->capture_widget, NULL, NULL);
3637 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3639 struct snd_soc_pcm_runtime *rtd = card->rtd;
3640 struct snd_soc_dai *cpu_dai, *codec_dai;
3641 struct snd_soc_dapm_route r;
3644 memset(&r, 0, sizeof(r));
3646 /* for each BE DAI link... */
3647 for (i = 0; i < card->num_rtd; i++) {
3648 rtd = &card->rtd[i];
3649 cpu_dai = rtd->cpu_dai;
3650 codec_dai = rtd->codec_dai;
3652 /* dynamic FE links have no fixed DAI mapping */
3653 if (rtd->dai_link->dynamic)
3656 /* there is no point in connecting BE DAI links with dummies */
3657 if (snd_soc_dai_is_dummy(codec_dai) ||
3658 snd_soc_dai_is_dummy(cpu_dai))
3661 /* connect BE DAI playback if widgets are valid */
3662 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3663 r.source = cpu_dai->playback_widget->name;
3664 r.sink = codec_dai->playback_widget->name;
3665 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3666 cpu_dai->codec->name, r.source,
3667 codec_dai->platform->name, r.sink);
3669 snd_soc_dapm_add_route(&card->dapm, &r);
3672 /* connect BE DAI capture if widgets are valid */
3673 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3674 r.source = codec_dai->capture_widget->name;
3675 r.sink = cpu_dai->capture_widget->name;
3676 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3677 codec_dai->codec->name, r.source,
3678 cpu_dai->platform->name, r.sink);
3680 snd_soc_dapm_add_route(&card->dapm, &r);
3686 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3690 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3691 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3692 struct snd_soc_dai *codec_dai = rtd->codec_dai;
3694 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3695 w_cpu = cpu_dai->playback_widget;
3696 w_codec = codec_dai->playback_widget;
3698 w_cpu = cpu_dai->capture_widget;
3699 w_codec = codec_dai->capture_widget;
3704 dapm_mark_dirty(w_cpu, "stream event");
3707 case SND_SOC_DAPM_STREAM_START:
3710 case SND_SOC_DAPM_STREAM_STOP:
3713 case SND_SOC_DAPM_STREAM_SUSPEND:
3714 case SND_SOC_DAPM_STREAM_RESUME:
3715 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3716 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3723 dapm_mark_dirty(w_codec, "stream event");
3726 case SND_SOC_DAPM_STREAM_START:
3727 w_codec->active = 1;
3729 case SND_SOC_DAPM_STREAM_STOP:
3730 w_codec->active = 0;
3732 case SND_SOC_DAPM_STREAM_SUSPEND:
3733 case SND_SOC_DAPM_STREAM_RESUME:
3734 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3735 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3740 dapm_power_widgets(rtd->card, event);
3744 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3745 * @rtd: PCM runtime data
3746 * @stream: stream name
3747 * @event: stream event
3749 * Sends a stream event to the dapm core. The core then makes any
3750 * necessary widget power changes.
3752 * Returns 0 for success else error.
3754 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3757 struct snd_soc_card *card = rtd->card;
3759 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3760 soc_dapm_stream_event(rtd, stream, event);
3761 mutex_unlock(&card->dapm_mutex);
3765 * snd_soc_dapm_enable_pin - enable pin.
3766 * @dapm: DAPM context
3769 * Enables input/output pin and its parents or children widgets iff there is
3770 * a valid audio route and active audio stream.
3771 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3772 * do any widget power switching.
3774 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3776 return snd_soc_dapm_set_pin(dapm, pin, 1);
3778 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3781 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3782 * @dapm: DAPM context
3785 * Enables input/output pin regardless of any other state. This is
3786 * intended for use with microphone bias supplies used in microphone
3789 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3790 * do any widget power switching.
3792 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3795 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3798 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3802 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3805 dapm_mark_dirty(w, "force enable");
3809 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3812 * snd_soc_dapm_disable_pin - disable pin.
3813 * @dapm: DAPM context
3816 * Disables input/output pin and its parents or children widgets.
3817 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3818 * do any widget power switching.
3820 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3823 return snd_soc_dapm_set_pin(dapm, pin, 0);
3825 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3828 * snd_soc_dapm_nc_pin - permanently disable pin.
3829 * @dapm: DAPM context
3832 * Marks the specified pin as being not connected, disabling it along
3833 * any parent or child widgets. At present this is identical to
3834 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3835 * additional things such as disabling controls which only affect
3836 * paths through the pin.
3838 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3839 * do any widget power switching.
3841 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3843 return snd_soc_dapm_set_pin(dapm, pin, 0);
3845 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3848 * snd_soc_dapm_get_pin_status - get audio pin status
3849 * @dapm: DAPM context
3850 * @pin: audio signal pin endpoint (or start point)
3852 * Get audio pin status - connected or disconnected.
3854 * Returns 1 for connected otherwise 0.
3856 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3859 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3862 return w->connected;
3866 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3869 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3870 * @dapm: DAPM context
3871 * @pin: audio signal pin endpoint (or start point)
3873 * Mark the given endpoint or pin as ignoring suspend. When the
3874 * system is disabled a path between two endpoints flagged as ignoring
3875 * suspend will not be disabled. The path must already be enabled via
3876 * normal means at suspend time, it will not be turned on if it was not
3879 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3882 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3885 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3889 w->ignore_suspend = 1;
3893 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3895 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3896 struct snd_soc_dapm_widget *w)
3898 struct snd_soc_dapm_path *p;
3900 list_for_each_entry(p, &card->paths, list) {
3901 if ((p->source == w) || (p->sink == w)) {
3903 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3904 p->source->name, p->source->id, p->source->dapm,
3905 p->sink->name, p->sink->id, p->sink->dapm);
3907 /* Connected to something other than the codec */
3908 if (p->source->dapm != p->sink->dapm)
3911 * Loopback connection from codec external pin to
3912 * codec external pin
3914 if (p->sink->id == snd_soc_dapm_input) {
3915 switch (p->source->id) {
3916 case snd_soc_dapm_output:
3917 case snd_soc_dapm_micbias:
3930 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3931 * @codec: The codec whose pins should be processed
3933 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3934 * which are unused. Pins are used if they are connected externally to the
3935 * codec, whether that be to some other device, or a loop-back connection to
3938 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3940 struct snd_soc_card *card = codec->card;
3941 struct snd_soc_dapm_context *dapm = &codec->dapm;
3942 struct snd_soc_dapm_widget *w;
3944 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
3945 &card->dapm, &codec->dapm);
3947 list_for_each_entry(w, &card->widgets, list) {
3948 if (w->dapm != dapm)
3951 case snd_soc_dapm_input:
3952 case snd_soc_dapm_output:
3953 case snd_soc_dapm_micbias:
3954 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
3956 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3958 "... Not in map; disabling\n");
3959 snd_soc_dapm_nc_pin(dapm, w->name);
3969 * snd_soc_dapm_free - free dapm resources
3970 * @dapm: DAPM context
3972 * Free all dapm widgets and resources.
3974 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3976 snd_soc_dapm_sys_remove(dapm->dev);
3977 dapm_debugfs_cleanup(dapm);
3978 dapm_free_widgets(dapm);
3979 list_del(&dapm->list);
3981 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3983 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3985 struct snd_soc_card *card = dapm->card;
3986 struct snd_soc_dapm_widget *w;
3987 LIST_HEAD(down_list);
3990 mutex_lock(&card->dapm_mutex);
3992 list_for_each_entry(w, &dapm->card->widgets, list) {
3993 if (w->dapm != dapm)
3996 dapm_seq_insert(w, &down_list, false);
4002 /* If there were no widgets to power down we're already in
4006 if (dapm->bias_level == SND_SOC_BIAS_ON)
4007 snd_soc_dapm_set_bias_level(dapm,
4008 SND_SOC_BIAS_PREPARE);
4009 dapm_seq_run(card, &down_list, 0, false);
4010 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4011 snd_soc_dapm_set_bias_level(dapm,
4012 SND_SOC_BIAS_STANDBY);
4015 mutex_unlock(&card->dapm_mutex);
4019 * snd_soc_dapm_shutdown - callback for system shutdown
4021 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4023 struct snd_soc_codec *codec;
4025 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
4026 soc_dapm_shutdown_codec(&codec->dapm);
4027 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4028 snd_soc_dapm_set_bias_level(&codec->dapm,
4033 /* Module information */
4034 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4035 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4036 MODULE_LICENSE("GPL");