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);
258 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
259 const struct snd_kcontrol *kcontrol)
261 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
266 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
267 struct snd_soc_dapm_widget *widget)
269 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
270 struct snd_soc_dapm_widget_list *new_wlist;
274 n = data->wlist->num_widgets + 1;
278 new_wlist = krealloc(data->wlist,
279 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
283 new_wlist->widgets[n - 1] = widget;
284 new_wlist->num_widgets = n;
286 data->wlist = new_wlist;
291 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
292 struct snd_soc_dapm_path *path)
294 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
296 list_add_tail(&path->list_kcontrol, &data->paths);
299 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
300 path->source, NULL, NULL);
304 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
306 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
311 return data->widget->power;
314 static struct list_head *dapm_kcontrol_get_path_list(
315 const struct snd_kcontrol *kcontrol)
317 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
322 #define dapm_kcontrol_for_each_path(path, kcontrol) \
323 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
326 static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
328 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
333 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
336 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
338 if (data->value == value)
342 data->widget->on_val = value;
350 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
351 * @kcontrol: The kcontrol
353 struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
355 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
357 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
359 static void dapm_reset(struct snd_soc_card *card)
361 struct snd_soc_dapm_widget *w;
363 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
365 list_for_each_entry(w, &card->widgets, list) {
366 w->new_power = w->power;
367 w->power_checked = false;
373 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg,
377 *value = snd_soc_read(w->codec, reg);
379 } else if (w->platform) {
380 *value = snd_soc_platform_read(w->platform, reg);
384 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
388 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
391 return snd_soc_write(w->codec, reg, val);
392 else if (w->platform)
393 return snd_soc_platform_write(w->platform, reg, val);
395 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
399 static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
401 if (w->codec && !w->codec->using_regmap)
402 mutex_lock(&w->codec->mutex);
403 else if (w->platform)
404 mutex_lock(&w->platform->mutex);
407 static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
409 if (w->codec && !w->codec->using_regmap)
410 mutex_unlock(&w->codec->mutex);
411 else if (w->platform)
412 mutex_unlock(&w->platform->mutex);
415 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
417 if (dapm->codec && dapm->codec->using_regmap)
418 regmap_async_complete(dapm->codec->control_data);
421 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
422 unsigned short reg, unsigned int mask, unsigned int value)
425 unsigned int old, new;
428 if (w->codec && w->codec->using_regmap) {
429 ret = regmap_update_bits_check_async(w->codec->control_data,
436 ret = soc_widget_read(w, reg, &old);
438 soc_widget_unlock(w);
442 new = (old & ~mask) | (value & mask);
445 ret = soc_widget_write(w, reg, new);
447 soc_widget_unlock(w);
451 soc_widget_unlock(w);
458 * snd_soc_dapm_set_bias_level - set the bias level for the system
459 * @dapm: DAPM context
460 * @level: level to configure
462 * Configure the bias (power) levels for the SoC audio device.
464 * Returns 0 for success else error.
466 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
467 enum snd_soc_bias_level level)
469 struct snd_soc_card *card = dapm->card;
472 trace_snd_soc_bias_level_start(card, level);
474 if (card && card->set_bias_level)
475 ret = card->set_bias_level(card, dapm, level);
480 if (dapm->codec->driver->set_bias_level)
481 ret = dapm->codec->driver->set_bias_level(dapm->codec,
484 dapm->bias_level = level;
485 } else if (!card || dapm != &card->dapm) {
486 dapm->bias_level = level;
492 if (card && card->set_bias_level_post)
493 ret = card->set_bias_level_post(card, dapm, level);
495 trace_snd_soc_bias_level_done(card, level);
500 /* set up initial codec paths */
501 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
502 struct snd_soc_dapm_path *p, int i)
505 case snd_soc_dapm_switch:
506 case snd_soc_dapm_mixer:
507 case snd_soc_dapm_mixer_named_ctl: {
509 struct soc_mixer_control *mc = (struct soc_mixer_control *)
510 w->kcontrol_news[i].private_value;
512 unsigned int shift = mc->shift;
514 unsigned int mask = (1 << fls(max)) - 1;
515 unsigned int invert = mc->invert;
517 if (reg != SND_SOC_NOPM) {
518 soc_widget_read(w, reg, &val);
519 val = (val >> shift) & mask;
529 case snd_soc_dapm_mux: {
530 struct soc_enum *e = (struct soc_enum *)
531 w->kcontrol_news[i].private_value;
534 soc_widget_read(w, e->reg, &val);
535 item = (val >> e->shift_l) & e->mask;
537 if (item < e->max && !strcmp(p->name, e->texts[item]))
543 case snd_soc_dapm_virt_mux: {
544 struct soc_enum *e = (struct soc_enum *)
545 w->kcontrol_news[i].private_value;
548 /* since a virtual mux has no backing registers to
549 * decide which path to connect, it will try to match
550 * with the first enumeration. This is to ensure
551 * that the default mux choice (the first) will be
552 * correctly powered up during initialization.
554 if (!strcmp(p->name, e->texts[0]))
558 case snd_soc_dapm_value_mux: {
559 struct soc_enum *e = (struct soc_enum *)
560 w->kcontrol_news[i].private_value;
563 soc_widget_read(w, e->reg, &val);
564 val = (val >> e->shift_l) & e->mask;
565 for (item = 0; item < e->max; item++) {
566 if (val == e->values[item])
570 if (item < e->max && !strcmp(p->name, e->texts[item]))
576 /* does not affect routing - always connected */
577 case snd_soc_dapm_pga:
578 case snd_soc_dapm_out_drv:
579 case snd_soc_dapm_output:
580 case snd_soc_dapm_adc:
581 case snd_soc_dapm_input:
582 case snd_soc_dapm_siggen:
583 case snd_soc_dapm_dac:
584 case snd_soc_dapm_micbias:
585 case snd_soc_dapm_vmid:
586 case snd_soc_dapm_supply:
587 case snd_soc_dapm_regulator_supply:
588 case snd_soc_dapm_clock_supply:
589 case snd_soc_dapm_aif_in:
590 case snd_soc_dapm_aif_out:
591 case snd_soc_dapm_dai_in:
592 case snd_soc_dapm_dai_out:
593 case snd_soc_dapm_hp:
594 case snd_soc_dapm_mic:
595 case snd_soc_dapm_spk:
596 case snd_soc_dapm_line:
597 case snd_soc_dapm_dai_link:
598 case snd_soc_dapm_kcontrol:
601 /* does affect routing - dynamically connected */
602 case snd_soc_dapm_pre:
603 case snd_soc_dapm_post:
609 /* connect mux widget to its interconnecting audio paths */
610 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
611 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
612 struct snd_soc_dapm_path *path, const char *control_name,
613 const struct snd_kcontrol_new *kcontrol)
615 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
618 for (i = 0; i < e->max; i++) {
619 if (!(strcmp(control_name, e->texts[i]))) {
620 list_add(&path->list, &dapm->card->paths);
621 list_add(&path->list_sink, &dest->sources);
622 list_add(&path->list_source, &src->sinks);
623 path->name = (char*)e->texts[i];
624 dapm_set_path_status(dest, path, 0);
632 /* connect mixer widget to its interconnecting audio paths */
633 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
634 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
635 struct snd_soc_dapm_path *path, const char *control_name)
639 /* search for mixer kcontrol */
640 for (i = 0; i < dest->num_kcontrols; i++) {
641 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
642 list_add(&path->list, &dapm->card->paths);
643 list_add(&path->list_sink, &dest->sources);
644 list_add(&path->list_source, &src->sinks);
645 path->name = dest->kcontrol_news[i].name;
646 dapm_set_path_status(dest, path, i);
653 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
654 struct snd_soc_dapm_widget *kcontrolw,
655 const struct snd_kcontrol_new *kcontrol_new,
656 struct snd_kcontrol **kcontrol)
658 struct snd_soc_dapm_widget *w;
663 list_for_each_entry(w, &dapm->card->widgets, list) {
664 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
666 for (i = 0; i < w->num_kcontrols; i++) {
667 if (&w->kcontrol_news[i] == kcontrol_new) {
669 *kcontrol = w->kcontrols[i];
679 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
680 * create it. Either way, add the widget into the control's widget list
682 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
685 struct snd_soc_dapm_context *dapm = w->dapm;
686 struct snd_card *card = dapm->card->snd_card;
690 struct snd_kcontrol *kcontrol;
691 bool wname_in_long_name, kcname_in_long_name;
697 prefix = dapm->codec->name_prefix;
702 prefix_len = strlen(prefix) + 1;
706 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
711 wname_in_long_name = false;
712 kcname_in_long_name = true;
715 case snd_soc_dapm_switch:
716 case snd_soc_dapm_mixer:
717 wname_in_long_name = true;
718 kcname_in_long_name = true;
720 case snd_soc_dapm_mixer_named_ctl:
721 wname_in_long_name = false;
722 kcname_in_long_name = true;
724 case snd_soc_dapm_mux:
725 case snd_soc_dapm_virt_mux:
726 case snd_soc_dapm_value_mux:
727 wname_in_long_name = true;
728 kcname_in_long_name = false;
735 if (wname_in_long_name && kcname_in_long_name) {
737 * The control will get a prefix from the control
738 * creation process but we're also using the same
739 * prefix for widgets so cut the prefix off the
740 * front of the widget name.
742 long_name = kasprintf(GFP_KERNEL, "%s %s",
743 w->name + prefix_len,
744 w->kcontrol_news[kci].name);
745 if (long_name == NULL)
749 } else if (wname_in_long_name) {
751 name = w->name + prefix_len;
754 name = w->kcontrol_news[kci].name;
757 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
762 kcontrol->private_free = dapm_kcontrol_free;
764 ret = dapm_kcontrol_data_alloc(w, kcontrol);
766 snd_ctl_free_one(kcontrol);
770 ret = snd_ctl_add(card, kcontrol);
773 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
779 ret = dapm_kcontrol_add_widget(kcontrol, w);
783 w->kcontrols[kci] = kcontrol;
788 /* create new dapm mixer control */
789 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
792 struct snd_soc_dapm_path *path;
795 for (i = 0; i < w->num_kcontrols; i++) {
797 list_for_each_entry(path, &w->sources, list_sink) {
798 /* mixer/mux paths name must match control name */
799 if (path->name != (char *)w->kcontrol_news[i].name)
802 if (w->kcontrols[i]) {
803 dapm_kcontrol_add_path(w->kcontrols[i], path);
807 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
811 dapm_kcontrol_add_path(w->kcontrols[i], path);
818 /* create new dapm mux control */
819 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
821 struct snd_soc_dapm_context *dapm = w->dapm;
822 struct snd_soc_dapm_path *path;
825 if (w->num_kcontrols != 1) {
827 "ASoC: mux %s has incorrect number of controls\n",
832 if (list_empty(&w->sources)) {
833 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
837 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
841 list_for_each_entry(path, &w->sources, list_sink)
842 dapm_kcontrol_add_path(w->kcontrols[0], path);
847 /* create new dapm volume control */
848 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
850 if (w->num_kcontrols)
851 dev_err(w->dapm->dev,
852 "ASoC: PGA controls not supported: '%s'\n", w->name);
857 /* reset 'walked' bit for each dapm path */
858 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
859 struct list_head *sink)
861 struct snd_soc_dapm_path *p;
863 list_for_each_entry(p, sink, list_source) {
866 dapm_clear_walk_output(dapm, &p->sink->sinks);
871 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
872 struct list_head *source)
874 struct snd_soc_dapm_path *p;
876 list_for_each_entry(p, source, list_sink) {
879 dapm_clear_walk_input(dapm, &p->source->sources);
885 /* We implement power down on suspend by checking the power state of
886 * the ALSA card - when we are suspending the ALSA state for the card
889 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
891 int level = snd_power_get_state(widget->dapm->card->snd_card);
894 case SNDRV_CTL_POWER_D3hot:
895 case SNDRV_CTL_POWER_D3cold:
896 if (widget->ignore_suspend)
897 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
899 return widget->ignore_suspend;
905 /* add widget to list if it's not already in the list */
906 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
907 struct snd_soc_dapm_widget *w)
909 struct snd_soc_dapm_widget_list *wlist;
910 int wlistsize, wlistentries, i;
917 /* is this widget already in the list */
918 for (i = 0; i < wlist->num_widgets; i++) {
919 if (wlist->widgets[i] == w)
923 /* allocate some new space */
924 wlistentries = wlist->num_widgets + 1;
925 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
926 wlistentries * sizeof(struct snd_soc_dapm_widget *);
927 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
929 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
935 /* insert the widget */
936 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
937 w->name, wlist->num_widgets);
939 wlist->widgets[wlist->num_widgets] = w;
940 wlist->num_widgets++;
945 * Recursively check for a completed path to an active or physically connected
946 * output widget. Returns number of complete paths.
948 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
949 struct snd_soc_dapm_widget_list **list)
951 struct snd_soc_dapm_path *path;
954 if (widget->outputs >= 0)
955 return widget->outputs;
957 DAPM_UPDATE_STAT(widget, path_checks);
959 switch (widget->id) {
960 case snd_soc_dapm_supply:
961 case snd_soc_dapm_regulator_supply:
962 case snd_soc_dapm_clock_supply:
963 case snd_soc_dapm_kcontrol:
969 switch (widget->id) {
970 case snd_soc_dapm_adc:
971 case snd_soc_dapm_aif_out:
972 case snd_soc_dapm_dai_out:
973 if (widget->active) {
974 widget->outputs = snd_soc_dapm_suspend_check(widget);
975 return widget->outputs;
981 if (widget->connected) {
982 /* connected pin ? */
983 if (widget->id == snd_soc_dapm_output && !widget->ext) {
984 widget->outputs = snd_soc_dapm_suspend_check(widget);
985 return widget->outputs;
988 /* connected jack or spk ? */
989 if (widget->id == snd_soc_dapm_hp ||
990 widget->id == snd_soc_dapm_spk ||
991 (widget->id == snd_soc_dapm_line &&
992 !list_empty(&widget->sources))) {
993 widget->outputs = snd_soc_dapm_suspend_check(widget);
994 return widget->outputs;
998 list_for_each_entry(path, &widget->sinks, list_source) {
999 DAPM_UPDATE_STAT(widget, neighbour_checks);
1010 trace_snd_soc_dapm_output_path(widget, path);
1012 if (path->sink && path->connect) {
1016 /* do we need to add this widget to the list ? */
1019 err = dapm_list_add_widget(list, path->sink);
1021 dev_err(widget->dapm->dev,
1022 "ASoC: could not add widget %s\n",
1029 con += is_connected_output_ep(path->sink, list);
1035 widget->outputs = con;
1041 * Recursively check for a completed path to an active or physically connected
1042 * input widget. Returns number of complete paths.
1044 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1045 struct snd_soc_dapm_widget_list **list)
1047 struct snd_soc_dapm_path *path;
1050 if (widget->inputs >= 0)
1051 return widget->inputs;
1053 DAPM_UPDATE_STAT(widget, path_checks);
1055 switch (widget->id) {
1056 case snd_soc_dapm_supply:
1057 case snd_soc_dapm_regulator_supply:
1058 case snd_soc_dapm_clock_supply:
1059 case snd_soc_dapm_kcontrol:
1065 /* active stream ? */
1066 switch (widget->id) {
1067 case snd_soc_dapm_dac:
1068 case snd_soc_dapm_aif_in:
1069 case snd_soc_dapm_dai_in:
1070 if (widget->active) {
1071 widget->inputs = snd_soc_dapm_suspend_check(widget);
1072 return widget->inputs;
1078 if (widget->connected) {
1079 /* connected pin ? */
1080 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1081 widget->inputs = snd_soc_dapm_suspend_check(widget);
1082 return widget->inputs;
1085 /* connected VMID/Bias for lower pops */
1086 if (widget->id == snd_soc_dapm_vmid) {
1087 widget->inputs = snd_soc_dapm_suspend_check(widget);
1088 return widget->inputs;
1091 /* connected jack ? */
1092 if (widget->id == snd_soc_dapm_mic ||
1093 (widget->id == snd_soc_dapm_line &&
1094 !list_empty(&widget->sinks))) {
1095 widget->inputs = snd_soc_dapm_suspend_check(widget);
1096 return widget->inputs;
1099 /* signal generator */
1100 if (widget->id == snd_soc_dapm_siggen) {
1101 widget->inputs = snd_soc_dapm_suspend_check(widget);
1102 return widget->inputs;
1106 list_for_each_entry(path, &widget->sources, list_sink) {
1107 DAPM_UPDATE_STAT(widget, neighbour_checks);
1118 trace_snd_soc_dapm_input_path(widget, path);
1120 if (path->source && path->connect) {
1124 /* do we need to add this widget to the list ? */
1127 err = dapm_list_add_widget(list, path->source);
1129 dev_err(widget->dapm->dev,
1130 "ASoC: could not add widget %s\n",
1137 con += is_connected_input_ep(path->source, list);
1143 widget->inputs = con;
1149 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1150 * @dai: the soc DAI.
1151 * @stream: stream direction.
1152 * @list: list of active widgets for this stream.
1154 * Queries DAPM graph as to whether an valid audio stream path exists for
1155 * the initial stream specified by name. This takes into account
1156 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1158 * Returns the number of valid paths or negative error.
1160 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1161 struct snd_soc_dapm_widget_list **list)
1163 struct snd_soc_card *card = dai->card;
1166 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1169 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1170 paths = is_connected_output_ep(dai->playback_widget, list);
1171 dapm_clear_walk_output(&card->dapm,
1172 &dai->playback_widget->sinks);
1174 paths = is_connected_input_ep(dai->capture_widget, list);
1175 dapm_clear_walk_input(&card->dapm,
1176 &dai->capture_widget->sources);
1179 trace_snd_soc_dapm_connected(paths, stream);
1180 mutex_unlock(&card->dapm_mutex);
1186 * Handler for generic register modifier widget.
1188 int dapm_reg_event(struct snd_soc_dapm_widget *w,
1189 struct snd_kcontrol *kcontrol, int event)
1193 if (SND_SOC_DAPM_EVENT_ON(event))
1198 soc_widget_update_bits_locked(w, -(w->reg + 1),
1199 w->mask << w->shift, val << w->shift);
1203 EXPORT_SYMBOL_GPL(dapm_reg_event);
1206 * Handler for regulator supply widget.
1208 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1209 struct snd_kcontrol *kcontrol, int event)
1213 soc_dapm_async_complete(w->dapm);
1215 if (SND_SOC_DAPM_EVENT_ON(event)) {
1216 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1217 ret = regulator_allow_bypass(w->regulator, false);
1219 dev_warn(w->dapm->dev,
1220 "ASoC: Failed to unbypass %s: %d\n",
1224 return regulator_enable(w->regulator);
1226 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1227 ret = regulator_allow_bypass(w->regulator, true);
1229 dev_warn(w->dapm->dev,
1230 "ASoC: Failed to bypass %s: %d\n",
1234 return regulator_disable_deferred(w->regulator, w->shift);
1237 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1240 * Handler for clock supply widget.
1242 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1243 struct snd_kcontrol *kcontrol, int event)
1248 soc_dapm_async_complete(w->dapm);
1250 #ifdef CONFIG_HAVE_CLK
1251 if (SND_SOC_DAPM_EVENT_ON(event)) {
1252 return clk_prepare_enable(w->clk);
1254 clk_disable_unprepare(w->clk);
1260 EXPORT_SYMBOL_GPL(dapm_clock_event);
1262 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1264 if (w->power_checked)
1265 return w->new_power;
1270 w->new_power = w->power_check(w);
1272 w->power_checked = true;
1274 return w->new_power;
1277 /* Generic check to see if a widget should be powered.
1279 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1283 DAPM_UPDATE_STAT(w, power_checks);
1285 in = is_connected_input_ep(w, NULL);
1286 dapm_clear_walk_input(w->dapm, &w->sources);
1287 out = is_connected_output_ep(w, NULL);
1288 dapm_clear_walk_output(w->dapm, &w->sinks);
1289 return out != 0 && in != 0;
1292 /* Check to see if an ADC has power */
1293 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1297 DAPM_UPDATE_STAT(w, power_checks);
1300 in = is_connected_input_ep(w, NULL);
1301 dapm_clear_walk_input(w->dapm, &w->sources);
1304 return dapm_generic_check_power(w);
1308 /* Check to see if a DAC has power */
1309 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1313 DAPM_UPDATE_STAT(w, power_checks);
1316 out = is_connected_output_ep(w, NULL);
1317 dapm_clear_walk_output(w->dapm, &w->sinks);
1320 return dapm_generic_check_power(w);
1324 /* Check to see if a power supply is needed */
1325 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1327 struct snd_soc_dapm_path *path;
1329 DAPM_UPDATE_STAT(w, power_checks);
1331 /* Check if one of our outputs is connected */
1332 list_for_each_entry(path, &w->sinks, list_source) {
1333 DAPM_UPDATE_STAT(w, neighbour_checks);
1338 if (path->connected &&
1339 !path->connected(path->source, path->sink))
1345 if (dapm_widget_power_check(path->sink))
1352 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1357 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1358 struct snd_soc_dapm_widget *b,
1366 sort = dapm_down_seq;
1368 if (sort[a->id] != sort[b->id])
1369 return sort[a->id] - sort[b->id];
1370 if (a->subseq != b->subseq) {
1372 return a->subseq - b->subseq;
1374 return b->subseq - a->subseq;
1376 if (a->reg != b->reg)
1377 return a->reg - b->reg;
1378 if (a->dapm != b->dapm)
1379 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1384 /* Insert a widget in order into a DAPM power sequence. */
1385 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1386 struct list_head *list,
1389 struct snd_soc_dapm_widget *w;
1391 list_for_each_entry(w, list, power_list)
1392 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1393 list_add_tail(&new_widget->power_list, &w->power_list);
1397 list_add_tail(&new_widget->power_list, list);
1400 static void dapm_seq_check_event(struct snd_soc_card *card,
1401 struct snd_soc_dapm_widget *w, int event)
1403 const char *ev_name;
1407 case SND_SOC_DAPM_PRE_PMU:
1408 ev_name = "PRE_PMU";
1411 case SND_SOC_DAPM_POST_PMU:
1412 ev_name = "POST_PMU";
1415 case SND_SOC_DAPM_PRE_PMD:
1416 ev_name = "PRE_PMD";
1419 case SND_SOC_DAPM_POST_PMD:
1420 ev_name = "POST_PMD";
1423 case SND_SOC_DAPM_WILL_PMU:
1424 ev_name = "WILL_PMU";
1427 case SND_SOC_DAPM_WILL_PMD:
1428 ev_name = "WILL_PMD";
1432 WARN(1, "Unknown event %d\n", event);
1436 if (w->new_power != power)
1439 if (w->event && (w->event_flags & event)) {
1440 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1442 soc_dapm_async_complete(w->dapm);
1443 trace_snd_soc_dapm_widget_event_start(w, event);
1444 ret = w->event(w, NULL, event);
1445 trace_snd_soc_dapm_widget_event_done(w, event);
1447 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1448 ev_name, w->name, ret);
1452 /* Apply the coalesced changes from a DAPM sequence */
1453 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1454 struct list_head *pending)
1456 struct snd_soc_dapm_widget *w;
1458 unsigned int value = 0;
1459 unsigned int mask = 0;
1461 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1464 list_for_each_entry(w, pending, power_list) {
1465 WARN_ON(reg != w->reg);
1466 w->power = w->new_power;
1468 mask |= w->mask << w->shift;
1470 value |= w->on_val << w->shift;
1472 value |= w->off_val << w->shift;
1474 pop_dbg(w->dapm->dev, card->pop_time,
1475 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1476 w->name, reg, value, mask);
1478 /* Check for events */
1479 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1480 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1484 /* Any widget will do, they should all be updating the
1487 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1490 pop_dbg(w->dapm->dev, card->pop_time,
1491 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1492 value, mask, reg, card->pop_time);
1493 pop_wait(card->pop_time);
1494 soc_widget_update_bits_locked(w, reg, mask, value);
1497 list_for_each_entry(w, pending, power_list) {
1498 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1499 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1503 /* Apply a DAPM power sequence.
1505 * We walk over a pre-sorted list of widgets to apply power to. In
1506 * order to minimise the number of writes to the device required
1507 * multiple widgets will be updated in a single write where possible.
1508 * Currently anything that requires more than a single write is not
1511 static void dapm_seq_run(struct snd_soc_card *card,
1512 struct list_head *list, int event, bool power_up)
1514 struct snd_soc_dapm_widget *w, *n;
1515 struct snd_soc_dapm_context *d;
1518 int cur_subseq = -1;
1519 int cur_reg = SND_SOC_NOPM;
1520 struct snd_soc_dapm_context *cur_dapm = NULL;
1527 sort = dapm_down_seq;
1529 list_for_each_entry_safe(w, n, list, power_list) {
1532 /* Do we need to apply any queued changes? */
1533 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1534 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1535 if (!list_empty(&pending))
1536 dapm_seq_run_coalesced(card, &pending);
1538 if (cur_dapm && cur_dapm->seq_notifier) {
1539 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1540 if (sort[i] == cur_sort)
1541 cur_dapm->seq_notifier(cur_dapm,
1546 if (cur_dapm && w->dapm != cur_dapm)
1547 soc_dapm_async_complete(cur_dapm);
1549 INIT_LIST_HEAD(&pending);
1551 cur_subseq = INT_MIN;
1552 cur_reg = SND_SOC_NOPM;
1557 case snd_soc_dapm_pre:
1559 list_for_each_entry_safe_continue(w, n, list,
1562 if (event == SND_SOC_DAPM_STREAM_START)
1564 NULL, SND_SOC_DAPM_PRE_PMU);
1565 else if (event == SND_SOC_DAPM_STREAM_STOP)
1567 NULL, SND_SOC_DAPM_PRE_PMD);
1570 case snd_soc_dapm_post:
1572 list_for_each_entry_safe_continue(w, n, list,
1575 if (event == SND_SOC_DAPM_STREAM_START)
1577 NULL, SND_SOC_DAPM_POST_PMU);
1578 else if (event == SND_SOC_DAPM_STREAM_STOP)
1580 NULL, SND_SOC_DAPM_POST_PMD);
1584 /* Queue it up for application */
1585 cur_sort = sort[w->id];
1586 cur_subseq = w->subseq;
1589 list_move(&w->power_list, &pending);
1594 dev_err(w->dapm->dev,
1595 "ASoC: Failed to apply widget power: %d\n", ret);
1598 if (!list_empty(&pending))
1599 dapm_seq_run_coalesced(card, &pending);
1601 if (cur_dapm && cur_dapm->seq_notifier) {
1602 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1603 if (sort[i] == cur_sort)
1604 cur_dapm->seq_notifier(cur_dapm,
1608 list_for_each_entry(d, &card->dapm_list, list) {
1609 soc_dapm_async_complete(d);
1613 static void dapm_widget_update(struct snd_soc_card *card)
1615 struct snd_soc_dapm_update *update = card->update;
1616 struct snd_soc_dapm_widget_list *wlist;
1617 struct snd_soc_dapm_widget *w = NULL;
1621 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1624 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1626 for (wi = 0; wi < wlist->num_widgets; wi++) {
1627 w = wlist->widgets[wi];
1629 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1630 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1632 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1640 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1643 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1646 for (wi = 0; wi < wlist->num_widgets; wi++) {
1647 w = wlist->widgets[wi];
1649 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1650 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1652 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1658 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1659 * they're changing state.
1661 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1663 struct snd_soc_dapm_context *d = data;
1666 /* If we're off and we're not supposed to be go into STANDBY */
1667 if (d->bias_level == SND_SOC_BIAS_OFF &&
1668 d->target_bias_level != SND_SOC_BIAS_OFF) {
1670 pm_runtime_get_sync(d->dev);
1672 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1675 "ASoC: Failed to turn on bias: %d\n", ret);
1678 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1679 if (d->bias_level != d->target_bias_level) {
1680 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1683 "ASoC: Failed to prepare bias: %d\n", ret);
1687 /* Async callback run prior to DAPM sequences - brings to their final
1690 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1692 struct snd_soc_dapm_context *d = data;
1695 /* If we just powered the last thing off drop to standby bias */
1696 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1697 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1698 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1699 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1701 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1705 /* If we're in standby and can support bias off then do that */
1706 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1707 d->target_bias_level == SND_SOC_BIAS_OFF) {
1708 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1710 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1714 pm_runtime_put(d->dev);
1717 /* If we just powered up then move to active bias */
1718 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1719 d->target_bias_level == SND_SOC_BIAS_ON) {
1720 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1722 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1727 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1728 bool power, bool connect)
1730 /* If a connection is being made or broken then that update
1731 * will have marked the peer dirty, otherwise the widgets are
1732 * not connected and this update has no impact. */
1736 /* If the peer is already in the state we're moving to then we
1737 * won't have an impact on it. */
1738 if (power != peer->power)
1739 dapm_mark_dirty(peer, "peer state change");
1742 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1743 struct list_head *up_list,
1744 struct list_head *down_list)
1746 struct snd_soc_dapm_path *path;
1748 if (w->power == power)
1751 trace_snd_soc_dapm_widget_power(w, power);
1753 /* If we changed our power state perhaps our neigbours changed
1756 list_for_each_entry(path, &w->sources, list_sink) {
1758 dapm_widget_set_peer_power(path->source, power,
1763 case snd_soc_dapm_supply:
1764 case snd_soc_dapm_regulator_supply:
1765 case snd_soc_dapm_clock_supply:
1766 case snd_soc_dapm_kcontrol:
1767 /* Supplies can't affect their outputs, only their inputs */
1770 list_for_each_entry(path, &w->sinks, list_source) {
1772 dapm_widget_set_peer_power(path->sink, power,
1780 dapm_seq_insert(w, up_list, true);
1782 dapm_seq_insert(w, down_list, false);
1785 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1786 struct list_head *up_list,
1787 struct list_head *down_list)
1792 case snd_soc_dapm_pre:
1793 dapm_seq_insert(w, down_list, false);
1795 case snd_soc_dapm_post:
1796 dapm_seq_insert(w, up_list, true);
1800 power = dapm_widget_power_check(w);
1802 dapm_widget_set_power(w, power, up_list, down_list);
1808 * Scan each dapm widget for complete audio path.
1809 * A complete path is a route that has valid endpoints i.e.:-
1811 * o DAC to output pin.
1812 * o Input Pin to ADC.
1813 * o Input pin to Output pin (bypass, sidetone)
1814 * o DAC to ADC (loopback).
1816 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1818 struct snd_soc_dapm_widget *w;
1819 struct snd_soc_dapm_context *d;
1821 LIST_HEAD(down_list);
1822 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1823 enum snd_soc_bias_level bias;
1825 trace_snd_soc_dapm_start(card);
1827 list_for_each_entry(d, &card->dapm_list, list) {
1828 if (d->idle_bias_off)
1829 d->target_bias_level = SND_SOC_BIAS_OFF;
1831 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1836 /* Check which widgets we need to power and store them in
1837 * lists indicating if they should be powered up or down. We
1838 * only check widgets that have been flagged as dirty but note
1839 * that new widgets may be added to the dirty list while we
1842 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1843 dapm_power_one_widget(w, &up_list, &down_list);
1846 list_for_each_entry(w, &card->widgets, list) {
1848 case snd_soc_dapm_pre:
1849 case snd_soc_dapm_post:
1850 /* These widgets always need to be powered */
1853 list_del_init(&w->dirty);
1860 /* Supplies and micbiases only bring the
1861 * context up to STANDBY as unless something
1862 * else is active and passing audio they
1863 * generally don't require full power. Signal
1864 * generators are virtual pins and have no
1865 * power impact themselves.
1868 case snd_soc_dapm_siggen:
1869 case snd_soc_dapm_vmid:
1871 case snd_soc_dapm_supply:
1872 case snd_soc_dapm_regulator_supply:
1873 case snd_soc_dapm_clock_supply:
1874 case snd_soc_dapm_micbias:
1875 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1876 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1879 d->target_bias_level = SND_SOC_BIAS_ON;
1886 /* Force all contexts in the card to the same bias state if
1887 * they're not ground referenced.
1889 bias = SND_SOC_BIAS_OFF;
1890 list_for_each_entry(d, &card->dapm_list, list)
1891 if (d->target_bias_level > bias)
1892 bias = d->target_bias_level;
1893 list_for_each_entry(d, &card->dapm_list, list)
1894 if (!d->idle_bias_off)
1895 d->target_bias_level = bias;
1897 trace_snd_soc_dapm_walk_done(card);
1899 /* Run all the bias changes in parallel */
1900 list_for_each_entry(d, &card->dapm_list, list)
1901 async_schedule_domain(dapm_pre_sequence_async, d,
1903 async_synchronize_full_domain(&async_domain);
1905 list_for_each_entry(w, &down_list, power_list) {
1906 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1909 list_for_each_entry(w, &up_list, power_list) {
1910 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1913 /* Power down widgets first; try to avoid amplifying pops. */
1914 dapm_seq_run(card, &down_list, event, false);
1916 dapm_widget_update(card);
1919 dapm_seq_run(card, &up_list, event, true);
1921 /* Run all the bias changes in parallel */
1922 list_for_each_entry(d, &card->dapm_list, list)
1923 async_schedule_domain(dapm_post_sequence_async, d,
1925 async_synchronize_full_domain(&async_domain);
1927 /* do we need to notify any clients that DAPM event is complete */
1928 list_for_each_entry(d, &card->dapm_list, list) {
1929 if (d->stream_event)
1930 d->stream_event(d, event);
1933 pop_dbg(card->dev, card->pop_time,
1934 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1935 pop_wait(card->pop_time);
1937 trace_snd_soc_dapm_done(card);
1942 #ifdef CONFIG_DEBUG_FS
1943 static ssize_t dapm_widget_power_read_file(struct file *file,
1944 char __user *user_buf,
1945 size_t count, loff_t *ppos)
1947 struct snd_soc_dapm_widget *w = file->private_data;
1951 struct snd_soc_dapm_path *p = NULL;
1953 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1957 in = is_connected_input_ep(w, NULL);
1958 dapm_clear_walk_input(w->dapm, &w->sources);
1959 out = is_connected_output_ep(w, NULL);
1960 dapm_clear_walk_output(w->dapm, &w->sinks);
1962 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1963 w->name, w->power ? "On" : "Off",
1964 w->force ? " (forced)" : "", in, out);
1967 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1968 " - R%d(0x%x) mask 0x%x",
1969 w->reg, w->reg, w->mask << w->shift);
1971 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1974 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1976 w->active ? "active" : "inactive");
1978 list_for_each_entry(p, &w->sources, list_sink) {
1979 if (p->connected && !p->connected(w, p->source))
1983 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1984 " in \"%s\" \"%s\"\n",
1985 p->name ? p->name : "static",
1988 list_for_each_entry(p, &w->sinks, list_source) {
1989 if (p->connected && !p->connected(w, p->sink))
1993 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1994 " out \"%s\" \"%s\"\n",
1995 p->name ? p->name : "static",
1999 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2005 static const struct file_operations dapm_widget_power_fops = {
2006 .open = simple_open,
2007 .read = dapm_widget_power_read_file,
2008 .llseek = default_llseek,
2011 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2012 size_t count, loff_t *ppos)
2014 struct snd_soc_dapm_context *dapm = file->private_data;
2017 switch (dapm->bias_level) {
2018 case SND_SOC_BIAS_ON:
2021 case SND_SOC_BIAS_PREPARE:
2022 level = "Prepare\n";
2024 case SND_SOC_BIAS_STANDBY:
2025 level = "Standby\n";
2027 case SND_SOC_BIAS_OFF:
2031 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2032 level = "Unknown\n";
2036 return simple_read_from_buffer(user_buf, count, ppos, level,
2040 static const struct file_operations dapm_bias_fops = {
2041 .open = simple_open,
2042 .read = dapm_bias_read_file,
2043 .llseek = default_llseek,
2046 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2047 struct dentry *parent)
2051 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2053 if (!dapm->debugfs_dapm) {
2055 "ASoC: Failed to create DAPM debugfs directory\n");
2059 d = debugfs_create_file("bias_level", 0444,
2060 dapm->debugfs_dapm, dapm,
2064 "ASoC: Failed to create bias level debugfs file\n");
2067 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2069 struct snd_soc_dapm_context *dapm = w->dapm;
2072 if (!dapm->debugfs_dapm || !w->name)
2075 d = debugfs_create_file(w->name, 0444,
2076 dapm->debugfs_dapm, w,
2077 &dapm_widget_power_fops);
2079 dev_warn(w->dapm->dev,
2080 "ASoC: Failed to create %s debugfs file\n",
2084 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2086 debugfs_remove_recursive(dapm->debugfs_dapm);
2090 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2091 struct dentry *parent)
2095 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2099 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2105 /* test and update the power status of a mux widget */
2106 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2107 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2109 struct snd_soc_dapm_path *path;
2112 /* find dapm widget path assoc with kcontrol */
2113 dapm_kcontrol_for_each_path(path, kcontrol) {
2114 if (!path->name || !e->texts[mux])
2118 /* we now need to match the string in the enum to the path */
2119 if (!(strcmp(path->name, e->texts[mux]))) {
2120 path->connect = 1; /* new connection */
2121 dapm_mark_dirty(path->source, "mux connection");
2124 dapm_mark_dirty(path->source,
2125 "mux disconnection");
2126 path->connect = 0; /* old connection must be powered down */
2128 dapm_mark_dirty(path->sink, "mux change");
2132 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2137 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2138 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2139 struct snd_soc_dapm_update *update)
2141 struct snd_soc_card *card = dapm->card;
2144 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2145 card->update = update;
2146 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2147 card->update = NULL;
2148 mutex_unlock(&card->dapm_mutex);
2150 soc_dpcm_runtime_update(card);
2153 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2155 /* test and update the power status of a mixer or switch widget */
2156 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2157 struct snd_kcontrol *kcontrol, int connect)
2159 struct snd_soc_dapm_path *path;
2162 /* find dapm widget path assoc with kcontrol */
2163 dapm_kcontrol_for_each_path(path, kcontrol) {
2165 path->connect = connect;
2166 dapm_mark_dirty(path->source, "mixer connection");
2167 dapm_mark_dirty(path->sink, "mixer update");
2171 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2176 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2177 struct snd_kcontrol *kcontrol, int connect,
2178 struct snd_soc_dapm_update *update)
2180 struct snd_soc_card *card = dapm->card;
2183 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2184 card->update = update;
2185 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2186 card->update = NULL;
2187 mutex_unlock(&card->dapm_mutex);
2189 soc_dpcm_runtime_update(card);
2192 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2194 /* show dapm widget status in sys fs */
2195 static ssize_t dapm_widget_show(struct device *dev,
2196 struct device_attribute *attr, char *buf)
2198 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2199 struct snd_soc_codec *codec =rtd->codec;
2200 struct snd_soc_dapm_widget *w;
2202 char *state = "not set";
2204 list_for_each_entry(w, &codec->card->widgets, list) {
2205 if (w->dapm != &codec->dapm)
2208 /* only display widgets that burnm power */
2210 case snd_soc_dapm_hp:
2211 case snd_soc_dapm_mic:
2212 case snd_soc_dapm_spk:
2213 case snd_soc_dapm_line:
2214 case snd_soc_dapm_micbias:
2215 case snd_soc_dapm_dac:
2216 case snd_soc_dapm_adc:
2217 case snd_soc_dapm_pga:
2218 case snd_soc_dapm_out_drv:
2219 case snd_soc_dapm_mixer:
2220 case snd_soc_dapm_mixer_named_ctl:
2221 case snd_soc_dapm_supply:
2222 case snd_soc_dapm_regulator_supply:
2223 case snd_soc_dapm_clock_supply:
2225 count += sprintf(buf + count, "%s: %s\n",
2226 w->name, w->power ? "On":"Off");
2233 switch (codec->dapm.bias_level) {
2234 case SND_SOC_BIAS_ON:
2237 case SND_SOC_BIAS_PREPARE:
2240 case SND_SOC_BIAS_STANDBY:
2243 case SND_SOC_BIAS_OFF:
2247 count += sprintf(buf + count, "PM State: %s\n", state);
2252 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2254 int snd_soc_dapm_sys_add(struct device *dev)
2256 return device_create_file(dev, &dev_attr_dapm_widget);
2259 static void snd_soc_dapm_sys_remove(struct device *dev)
2261 device_remove_file(dev, &dev_attr_dapm_widget);
2264 static void dapm_free_path(struct snd_soc_dapm_path *path)
2266 list_del(&path->list_sink);
2267 list_del(&path->list_source);
2268 list_del(&path->list_kcontrol);
2269 list_del(&path->list);
2273 /* free all dapm widgets and resources */
2274 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2276 struct snd_soc_dapm_widget *w, *next_w;
2277 struct snd_soc_dapm_path *p, *next_p;
2279 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2280 if (w->dapm != dapm)
2284 * remove source and sink paths associated to this widget.
2285 * While removing the path, remove reference to it from both
2286 * source and sink widgets so that path is removed only once.
2288 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2291 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2294 kfree(w->kcontrols);
2300 static struct snd_soc_dapm_widget *dapm_find_widget(
2301 struct snd_soc_dapm_context *dapm, const char *pin,
2302 bool search_other_contexts)
2304 struct snd_soc_dapm_widget *w;
2305 struct snd_soc_dapm_widget *fallback = NULL;
2307 list_for_each_entry(w, &dapm->card->widgets, list) {
2308 if (!strcmp(w->name, pin)) {
2309 if (w->dapm == dapm)
2316 if (search_other_contexts)
2322 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2323 const char *pin, int status)
2325 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2328 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2332 if (w->connected != status)
2333 dapm_mark_dirty(w, "pin configuration");
2335 w->connected = status;
2343 * snd_soc_dapm_sync - scan and power dapm paths
2344 * @dapm: DAPM context
2346 * Walks all dapm audio paths and powers widgets according to their
2347 * stream or path usage.
2349 * Returns 0 for success.
2351 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2356 * Suppress early reports (eg, jacks syncing their state) to avoid
2357 * silly DAPM runs during card startup.
2359 if (!dapm->card || !dapm->card->instantiated)
2362 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2363 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2364 mutex_unlock(&dapm->card->dapm_mutex);
2367 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2369 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2370 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2371 const char *control,
2372 int (*connected)(struct snd_soc_dapm_widget *source,
2373 struct snd_soc_dapm_widget *sink))
2375 struct snd_soc_dapm_path *path;
2378 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2382 path->source = wsource;
2384 path->connected = connected;
2385 INIT_LIST_HEAD(&path->list);
2386 INIT_LIST_HEAD(&path->list_kcontrol);
2387 INIT_LIST_HEAD(&path->list_source);
2388 INIT_LIST_HEAD(&path->list_sink);
2390 /* check for external widgets */
2391 if (wsink->id == snd_soc_dapm_input) {
2392 if (wsource->id == snd_soc_dapm_micbias ||
2393 wsource->id == snd_soc_dapm_mic ||
2394 wsource->id == snd_soc_dapm_line ||
2395 wsource->id == snd_soc_dapm_output)
2398 if (wsource->id == snd_soc_dapm_output) {
2399 if (wsink->id == snd_soc_dapm_spk ||
2400 wsink->id == snd_soc_dapm_hp ||
2401 wsink->id == snd_soc_dapm_line ||
2402 wsink->id == snd_soc_dapm_input)
2406 dapm_mark_dirty(wsource, "Route added");
2407 dapm_mark_dirty(wsink, "Route added");
2409 /* connect static paths */
2410 if (control == NULL) {
2411 list_add(&path->list, &dapm->card->paths);
2412 list_add(&path->list_sink, &wsink->sources);
2413 list_add(&path->list_source, &wsource->sinks);
2418 /* connect dynamic paths */
2419 switch (wsink->id) {
2420 case snd_soc_dapm_adc:
2421 case snd_soc_dapm_dac:
2422 case snd_soc_dapm_pga:
2423 case snd_soc_dapm_out_drv:
2424 case snd_soc_dapm_input:
2425 case snd_soc_dapm_output:
2426 case snd_soc_dapm_siggen:
2427 case snd_soc_dapm_micbias:
2428 case snd_soc_dapm_vmid:
2429 case snd_soc_dapm_pre:
2430 case snd_soc_dapm_post:
2431 case snd_soc_dapm_supply:
2432 case snd_soc_dapm_regulator_supply:
2433 case snd_soc_dapm_clock_supply:
2434 case snd_soc_dapm_aif_in:
2435 case snd_soc_dapm_aif_out:
2436 case snd_soc_dapm_dai_in:
2437 case snd_soc_dapm_dai_out:
2438 case snd_soc_dapm_dai_link:
2439 case snd_soc_dapm_kcontrol:
2440 list_add(&path->list, &dapm->card->paths);
2441 list_add(&path->list_sink, &wsink->sources);
2442 list_add(&path->list_source, &wsource->sinks);
2445 case snd_soc_dapm_mux:
2446 case snd_soc_dapm_virt_mux:
2447 case snd_soc_dapm_value_mux:
2448 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2449 &wsink->kcontrol_news[0]);
2453 case snd_soc_dapm_switch:
2454 case snd_soc_dapm_mixer:
2455 case snd_soc_dapm_mixer_named_ctl:
2456 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2460 case snd_soc_dapm_hp:
2461 case snd_soc_dapm_mic:
2462 case snd_soc_dapm_line:
2463 case snd_soc_dapm_spk:
2464 list_add(&path->list, &dapm->card->paths);
2465 list_add(&path->list_sink, &wsink->sources);
2466 list_add(&path->list_source, &wsource->sinks);
2477 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2478 const struct snd_soc_dapm_route *route,
2479 unsigned int is_prefixed)
2481 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2482 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2485 char prefixed_sink[80];
2486 char prefixed_source[80];
2489 if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
2490 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2491 dapm->codec->name_prefix, route->sink);
2492 sink = prefixed_sink;
2493 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2494 dapm->codec->name_prefix, route->source);
2495 source = prefixed_source;
2498 source = route->source;
2502 * find src and dest widgets over all widgets but favor a widget from
2503 * current DAPM context
2505 list_for_each_entry(w, &dapm->card->widgets, list) {
2506 if (!wsink && !(strcmp(w->name, sink))) {
2508 if (w->dapm == dapm)
2512 if (!wsource && !(strcmp(w->name, source))) {
2514 if (w->dapm == dapm)
2518 /* use widget from another DAPM context if not found from this */
2524 if (wsource == NULL) {
2525 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2529 if (wsink == NULL) {
2530 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2535 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2542 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2543 source, route->control, sink);
2547 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2548 const struct snd_soc_dapm_route *route)
2550 struct snd_soc_dapm_path *path, *p;
2553 char prefixed_sink[80];
2554 char prefixed_source[80];
2556 if (route->control) {
2558 "ASoC: Removal of routes with controls not supported\n");
2562 if (dapm->codec && dapm->codec->name_prefix) {
2563 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2564 dapm->codec->name_prefix, route->sink);
2565 sink = prefixed_sink;
2566 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2567 dapm->codec->name_prefix, route->source);
2568 source = prefixed_source;
2571 source = route->source;
2575 list_for_each_entry(p, &dapm->card->paths, list) {
2576 if (strcmp(p->source->name, source) != 0)
2578 if (strcmp(p->sink->name, sink) != 0)
2585 dapm_mark_dirty(path->source, "Route removed");
2586 dapm_mark_dirty(path->sink, "Route removed");
2588 dapm_free_path(path);
2590 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2598 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2599 * @dapm: DAPM context
2600 * @route: audio routes
2601 * @num: number of routes
2603 * Connects 2 dapm widgets together via a named audio path. The sink is
2604 * the widget receiving the audio signal, whilst the source is the sender
2605 * of the audio signal.
2607 * Returns 0 for success else error. On error all resources can be freed
2608 * with a call to snd_soc_card_free().
2610 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2611 const struct snd_soc_dapm_route *route, int num)
2615 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2616 for (i = 0; i < num; i++) {
2617 r = snd_soc_dapm_add_route(dapm, route, false);
2619 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2621 route->control ? route->control : "direct",
2627 mutex_unlock(&dapm->card->dapm_mutex);
2631 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2634 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2635 * @dapm: DAPM context
2636 * @route: audio routes
2637 * @num: number of routes
2639 * Removes routes from the DAPM context.
2641 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2642 const struct snd_soc_dapm_route *route, int num)
2646 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2647 for (i = 0; i < num; i++) {
2648 snd_soc_dapm_del_route(dapm, route);
2651 mutex_unlock(&dapm->card->dapm_mutex);
2655 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2657 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2658 const struct snd_soc_dapm_route *route)
2660 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2663 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2666 struct snd_soc_dapm_path *path;
2670 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2676 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2681 if (route->control || route->connected)
2682 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2683 route->source, route->sink);
2685 list_for_each_entry(path, &source->sinks, list_source) {
2686 if (path->sink == sink) {
2693 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2694 route->source, route->sink);
2696 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2697 count, route->source, route->sink);
2703 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2704 * @dapm: DAPM context
2705 * @route: audio routes
2706 * @num: number of routes
2708 * Mark existing routes matching those specified in the passed array
2709 * as being weak, meaning that they are ignored for the purpose of
2710 * power decisions. The main intended use case is for sidetone paths
2711 * which couple audio between other independent paths if they are both
2712 * active in order to make the combination work better at the user
2713 * level but which aren't intended to be "used".
2715 * Note that CODEC drivers should not use this as sidetone type paths
2716 * can frequently also be used as bypass paths.
2718 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2719 const struct snd_soc_dapm_route *route, int num)
2724 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2725 for (i = 0; i < num; i++) {
2726 err = snd_soc_dapm_weak_route(dapm, route);
2731 mutex_unlock(&dapm->card->dapm_mutex);
2735 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2738 * snd_soc_dapm_new_widgets - add new dapm widgets
2739 * @dapm: DAPM context
2741 * Checks the codec for any new dapm widgets and creates them if found.
2743 * Returns 0 for success.
2745 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2747 struct snd_soc_dapm_widget *w;
2750 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2752 list_for_each_entry(w, &card->widgets, list)
2757 if (w->num_kcontrols) {
2758 w->kcontrols = kzalloc(w->num_kcontrols *
2759 sizeof(struct snd_kcontrol *),
2761 if (!w->kcontrols) {
2762 mutex_unlock(&card->dapm_mutex);
2768 case snd_soc_dapm_switch:
2769 case snd_soc_dapm_mixer:
2770 case snd_soc_dapm_mixer_named_ctl:
2773 case snd_soc_dapm_mux:
2774 case snd_soc_dapm_virt_mux:
2775 case snd_soc_dapm_value_mux:
2778 case snd_soc_dapm_pga:
2779 case snd_soc_dapm_out_drv:
2786 /* Read the initial power state from the device */
2788 soc_widget_read(w, w->reg, &val);
2789 val = val >> w->shift;
2791 if (val == w->on_val)
2797 dapm_mark_dirty(w, "new widget");
2798 dapm_debugfs_add_widget(w);
2801 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2802 mutex_unlock(&card->dapm_mutex);
2805 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2808 * snd_soc_dapm_get_volsw - dapm mixer get callback
2809 * @kcontrol: mixer control
2810 * @ucontrol: control element information
2812 * Callback to get the value of a dapm mixer control.
2814 * Returns 0 for success.
2816 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_value *ucontrol)
2819 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2820 struct snd_soc_card *card = codec->card;
2821 struct soc_mixer_control *mc =
2822 (struct soc_mixer_control *)kcontrol->private_value;
2824 unsigned int shift = mc->shift;
2826 unsigned int mask = (1 << fls(max)) - 1;
2827 unsigned int invert = mc->invert;
2830 if (snd_soc_volsw_is_stereo(mc))
2831 dev_warn(codec->dapm.dev,
2832 "ASoC: Control '%s' is stereo, which is not supported\n",
2835 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2836 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
2837 val = (snd_soc_read(codec, reg) >> shift) & mask;
2839 val = dapm_kcontrol_get_value(kcontrol);
2840 mutex_unlock(&card->dapm_mutex);
2843 ucontrol->value.integer.value[0] = max - val;
2845 ucontrol->value.integer.value[0] = val;
2849 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2852 * snd_soc_dapm_put_volsw - dapm mixer set callback
2853 * @kcontrol: mixer control
2854 * @ucontrol: control element information
2856 * Callback to set the value of a dapm mixer control.
2858 * Returns 0 for success.
2860 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2861 struct snd_ctl_elem_value *ucontrol)
2863 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2864 struct snd_soc_card *card = codec->card;
2865 struct soc_mixer_control *mc =
2866 (struct soc_mixer_control *)kcontrol->private_value;
2868 unsigned int shift = mc->shift;
2870 unsigned int mask = (1 << fls(max)) - 1;
2871 unsigned int invert = mc->invert;
2873 int connect, change;
2874 struct snd_soc_dapm_update update;
2877 if (snd_soc_volsw_is_stereo(mc))
2878 dev_warn(codec->dapm.dev,
2879 "ASoC: Control '%s' is stereo, which is not supported\n",
2882 val = (ucontrol->value.integer.value[0] & mask);
2888 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2890 change = dapm_kcontrol_set_value(kcontrol, val);
2892 if (reg != SND_SOC_NOPM) {
2893 mask = mask << shift;
2896 if (snd_soc_test_bits(codec, reg, mask, val)) {
2897 update.kcontrol = kcontrol;
2901 card->update = &update;
2906 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2908 card->update = NULL;
2911 mutex_unlock(&card->dapm_mutex);
2914 soc_dpcm_runtime_update(card);
2918 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2921 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2922 * @kcontrol: mixer control
2923 * @ucontrol: control element information
2925 * Callback to get the value of a dapm enumerated double mixer control.
2927 * Returns 0 for success.
2929 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2930 struct snd_ctl_elem_value *ucontrol)
2932 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2933 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2936 val = snd_soc_read(codec, e->reg);
2937 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2938 if (e->shift_l != e->shift_r)
2939 ucontrol->value.enumerated.item[1] =
2940 (val >> e->shift_r) & e->mask;
2944 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2947 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2948 * @kcontrol: mixer control
2949 * @ucontrol: control element information
2951 * Callback to set the value of a dapm enumerated double mixer control.
2953 * Returns 0 for success.
2955 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2956 struct snd_ctl_elem_value *ucontrol)
2958 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2959 struct snd_soc_card *card = codec->card;
2960 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2961 unsigned int val, mux, change;
2963 struct snd_soc_dapm_update update;
2966 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2968 mux = ucontrol->value.enumerated.item[0];
2969 val = mux << e->shift_l;
2970 mask = e->mask << e->shift_l;
2971 if (e->shift_l != e->shift_r) {
2972 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2974 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2975 mask |= e->mask << e->shift_r;
2978 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2980 change = snd_soc_test_bits(codec, e->reg, mask, val);
2982 update.kcontrol = kcontrol;
2983 update.reg = e->reg;
2986 card->update = &update;
2988 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2990 card->update = NULL;
2993 mutex_unlock(&card->dapm_mutex);
2996 soc_dpcm_runtime_update(card);
3000 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3003 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
3004 * @kcontrol: mixer control
3005 * @ucontrol: control element information
3007 * Returns 0 for success.
3009 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
3010 struct snd_ctl_elem_value *ucontrol)
3012 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
3015 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
3018 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
3019 * @kcontrol: mixer control
3020 * @ucontrol: control element information
3022 * Returns 0 for success.
3024 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
3025 struct snd_ctl_elem_value *ucontrol)
3027 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3028 struct snd_soc_card *card = codec->card;
3030 struct soc_enum *e =
3031 (struct soc_enum *)kcontrol->private_value;
3035 if (ucontrol->value.enumerated.item[0] >= e->max)
3038 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3040 value = ucontrol->value.enumerated.item[0];
3041 change = dapm_kcontrol_set_value(kcontrol, value);
3043 ret = soc_dapm_mux_update_power(card, kcontrol, value, e);
3045 mutex_unlock(&card->dapm_mutex);
3048 soc_dpcm_runtime_update(card);
3052 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3055 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3057 * @kcontrol: mixer control
3058 * @ucontrol: control element information
3060 * Callback to get the value of a dapm semi enumerated double mixer control.
3062 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3063 * used for handling bitfield coded enumeration for example.
3065 * Returns 0 for success.
3067 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3068 struct snd_ctl_elem_value *ucontrol)
3070 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3071 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3072 unsigned int reg_val, val, mux;
3074 reg_val = snd_soc_read(codec, e->reg);
3075 val = (reg_val >> e->shift_l) & e->mask;
3076 for (mux = 0; mux < e->max; mux++) {
3077 if (val == e->values[mux])
3080 ucontrol->value.enumerated.item[0] = mux;
3081 if (e->shift_l != e->shift_r) {
3082 val = (reg_val >> e->shift_r) & e->mask;
3083 for (mux = 0; mux < e->max; mux++) {
3084 if (val == e->values[mux])
3087 ucontrol->value.enumerated.item[1] = mux;
3092 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3095 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3097 * @kcontrol: mixer control
3098 * @ucontrol: control element information
3100 * Callback to set the value of a dapm semi enumerated double mixer control.
3102 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3103 * used for handling bitfield coded enumeration for example.
3105 * Returns 0 for success.
3107 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3108 struct snd_ctl_elem_value *ucontrol)
3110 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3111 struct snd_soc_card *card = codec->card;
3112 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3113 unsigned int val, mux, change;
3115 struct snd_soc_dapm_update update;
3118 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3120 mux = ucontrol->value.enumerated.item[0];
3121 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3122 mask = e->mask << e->shift_l;
3123 if (e->shift_l != e->shift_r) {
3124 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3126 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3127 mask |= e->mask << e->shift_r;
3130 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3132 change = snd_soc_test_bits(codec, e->reg, mask, val);
3134 update.kcontrol = kcontrol;
3135 update.reg = e->reg;
3138 card->update = &update;
3140 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
3142 card->update = NULL;
3145 mutex_unlock(&card->dapm_mutex);
3148 soc_dpcm_runtime_update(card);
3152 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3155 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3157 * @kcontrol: mixer control
3158 * @uinfo: control element information
3160 * Callback to provide information about a pin switch control.
3162 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3163 struct snd_ctl_elem_info *uinfo)
3165 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3167 uinfo->value.integer.min = 0;
3168 uinfo->value.integer.max = 1;
3172 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3175 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3177 * @kcontrol: mixer control
3180 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3181 struct snd_ctl_elem_value *ucontrol)
3183 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3184 const char *pin = (const char *)kcontrol->private_value;
3186 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3188 ucontrol->value.integer.value[0] =
3189 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3191 mutex_unlock(&card->dapm_mutex);
3195 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3198 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3200 * @kcontrol: mixer control
3203 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3204 struct snd_ctl_elem_value *ucontrol)
3206 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3207 const char *pin = (const char *)kcontrol->private_value;
3209 if (ucontrol->value.integer.value[0])
3210 snd_soc_dapm_enable_pin(&card->dapm, pin);
3212 snd_soc_dapm_disable_pin(&card->dapm, pin);
3214 snd_soc_dapm_sync(&card->dapm);
3217 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3219 static struct snd_soc_dapm_widget *
3220 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3221 const struct snd_soc_dapm_widget *widget)
3223 struct snd_soc_dapm_widget *w;
3226 if ((w = dapm_cnew_widget(widget)) == NULL)
3230 case snd_soc_dapm_regulator_supply:
3231 w->regulator = devm_regulator_get(dapm->dev, w->name);
3232 if (IS_ERR(w->regulator)) {
3233 ret = PTR_ERR(w->regulator);
3234 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3239 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3240 ret = regulator_allow_bypass(w->regulator, true);
3242 dev_warn(w->dapm->dev,
3243 "ASoC: Failed to bypass %s: %d\n",
3247 case snd_soc_dapm_clock_supply:
3248 #ifdef CONFIG_CLKDEV_LOOKUP
3249 w->clk = devm_clk_get(dapm->dev, w->name);
3250 if (IS_ERR(w->clk)) {
3251 ret = PTR_ERR(w->clk);
3252 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3264 if (dapm->codec && dapm->codec->name_prefix)
3265 w->name = kasprintf(GFP_KERNEL, "%s %s",
3266 dapm->codec->name_prefix, widget->name);
3268 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3270 if (w->name == NULL) {
3276 case snd_soc_dapm_switch:
3277 case snd_soc_dapm_mixer:
3278 case snd_soc_dapm_mixer_named_ctl:
3279 w->power_check = dapm_generic_check_power;
3281 case snd_soc_dapm_mux:
3282 case snd_soc_dapm_virt_mux:
3283 case snd_soc_dapm_value_mux:
3284 w->power_check = dapm_generic_check_power;
3286 case snd_soc_dapm_dai_out:
3287 w->power_check = dapm_adc_check_power;
3289 case snd_soc_dapm_dai_in:
3290 w->power_check = dapm_dac_check_power;
3292 case snd_soc_dapm_adc:
3293 case snd_soc_dapm_aif_out:
3294 case snd_soc_dapm_dac:
3295 case snd_soc_dapm_aif_in:
3296 case snd_soc_dapm_pga:
3297 case snd_soc_dapm_out_drv:
3298 case snd_soc_dapm_input:
3299 case snd_soc_dapm_output:
3300 case snd_soc_dapm_micbias:
3301 case snd_soc_dapm_spk:
3302 case snd_soc_dapm_hp:
3303 case snd_soc_dapm_mic:
3304 case snd_soc_dapm_line:
3305 case snd_soc_dapm_dai_link:
3306 w->power_check = dapm_generic_check_power;
3308 case snd_soc_dapm_supply:
3309 case snd_soc_dapm_regulator_supply:
3310 case snd_soc_dapm_clock_supply:
3311 case snd_soc_dapm_kcontrol:
3312 w->power_check = dapm_supply_check_power;
3315 w->power_check = dapm_always_on_check_power;
3320 w->codec = dapm->codec;
3321 w->platform = dapm->platform;
3322 INIT_LIST_HEAD(&w->sources);
3323 INIT_LIST_HEAD(&w->sinks);
3324 INIT_LIST_HEAD(&w->list);
3325 INIT_LIST_HEAD(&w->dirty);
3326 list_add(&w->list, &dapm->card->widgets);
3328 /* machine layer set ups unconnected pins and insertions */
3334 * snd_soc_dapm_new_controls - create new dapm controls
3335 * @dapm: DAPM context
3336 * @widget: widget array
3337 * @num: number of widgets
3339 * Creates new DAPM controls based upon the templates.
3341 * Returns 0 for success else error.
3343 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3344 const struct snd_soc_dapm_widget *widget,
3347 struct snd_soc_dapm_widget *w;
3351 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3352 for (i = 0; i < num; i++) {
3353 w = snd_soc_dapm_new_control(dapm, widget);
3356 "ASoC: Failed to create DAPM control %s\n",
3363 mutex_unlock(&dapm->card->dapm_mutex);
3366 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3368 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3369 struct snd_kcontrol *kcontrol, int event)
3371 struct snd_soc_dapm_path *source_p, *sink_p;
3372 struct snd_soc_dai *source, *sink;
3373 const struct snd_soc_pcm_stream *config = w->params;
3374 struct snd_pcm_substream substream;
3375 struct snd_pcm_hw_params *params = NULL;
3379 if (WARN_ON(!config) ||
3380 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3383 /* We only support a single source and sink, pick the first */
3384 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3386 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3389 if (WARN_ON(!source_p || !sink_p) ||
3390 WARN_ON(!sink_p->source || !source_p->sink) ||
3391 WARN_ON(!source_p->source || !sink_p->sink))
3394 source = source_p->source->priv;
3395 sink = sink_p->sink->priv;
3397 /* Be a little careful as we don't want to overflow the mask array */
3398 if (config->formats) {
3399 fmt = ffs(config->formats) - 1;
3401 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3406 /* Currently very limited parameter selection */
3407 params = kzalloc(sizeof(*params), GFP_KERNEL);
3412 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3414 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3416 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3419 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3420 = config->channels_min;
3421 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3422 = config->channels_max;
3424 memset(&substream, 0, sizeof(substream));
3427 case SND_SOC_DAPM_PRE_PMU:
3428 if (source->driver->ops && source->driver->ops->hw_params) {
3429 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3430 ret = source->driver->ops->hw_params(&substream,
3433 dev_err(source->dev,
3434 "ASoC: hw_params() failed: %d\n", ret);
3439 if (sink->driver->ops && sink->driver->ops->hw_params) {
3440 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3441 ret = sink->driver->ops->hw_params(&substream, params,
3445 "ASoC: hw_params() failed: %d\n", ret);
3451 case SND_SOC_DAPM_POST_PMU:
3452 ret = snd_soc_dai_digital_mute(sink, 0,
3453 SNDRV_PCM_STREAM_PLAYBACK);
3454 if (ret != 0 && ret != -ENOTSUPP)
3455 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3459 case SND_SOC_DAPM_PRE_PMD:
3460 ret = snd_soc_dai_digital_mute(sink, 1,
3461 SNDRV_PCM_STREAM_PLAYBACK);
3462 if (ret != 0 && ret != -ENOTSUPP)
3463 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3468 WARN(1, "Unknown event %d\n", event);
3477 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3478 const struct snd_soc_pcm_stream *params,
3479 struct snd_soc_dapm_widget *source,
3480 struct snd_soc_dapm_widget *sink)
3482 struct snd_soc_dapm_route routes[2];
3483 struct snd_soc_dapm_widget template;
3484 struct snd_soc_dapm_widget *w;
3488 len = strlen(source->name) + strlen(sink->name) + 2;
3489 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3492 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3494 memset(&template, 0, sizeof(template));
3495 template.reg = SND_SOC_NOPM;
3496 template.id = snd_soc_dapm_dai_link;
3497 template.name = link_name;
3498 template.event = snd_soc_dai_link_event;
3499 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3500 SND_SOC_DAPM_PRE_PMD;
3502 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3504 w = snd_soc_dapm_new_control(&card->dapm, &template);
3506 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3513 memset(&routes, 0, sizeof(routes));
3515 routes[0].source = source->name;
3516 routes[0].sink = link_name;
3517 routes[1].source = link_name;
3518 routes[1].sink = sink->name;
3520 return snd_soc_dapm_add_routes(&card->dapm, routes,
3521 ARRAY_SIZE(routes));
3524 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3525 struct snd_soc_dai *dai)
3527 struct snd_soc_dapm_widget template;
3528 struct snd_soc_dapm_widget *w;
3530 WARN_ON(dapm->dev != dai->dev);
3532 memset(&template, 0, sizeof(template));
3533 template.reg = SND_SOC_NOPM;
3535 if (dai->driver->playback.stream_name) {
3536 template.id = snd_soc_dapm_dai_in;
3537 template.name = dai->driver->playback.stream_name;
3538 template.sname = dai->driver->playback.stream_name;
3540 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3543 w = snd_soc_dapm_new_control(dapm, &template);
3545 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3546 dai->driver->playback.stream_name);
3551 dai->playback_widget = w;
3554 if (dai->driver->capture.stream_name) {
3555 template.id = snd_soc_dapm_dai_out;
3556 template.name = dai->driver->capture.stream_name;
3557 template.sname = dai->driver->capture.stream_name;
3559 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3562 w = snd_soc_dapm_new_control(dapm, &template);
3564 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3565 dai->driver->capture.stream_name);
3570 dai->capture_widget = w;
3576 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3578 struct snd_soc_dapm_widget *dai_w, *w;
3579 struct snd_soc_dai *dai;
3581 /* For each DAI widget... */
3582 list_for_each_entry(dai_w, &card->widgets, list) {
3583 switch (dai_w->id) {
3584 case snd_soc_dapm_dai_in:
3585 case snd_soc_dapm_dai_out:
3593 /* ...find all widgets with the same stream and link them */
3594 list_for_each_entry(w, &card->widgets, list) {
3595 if (w->dapm != dai_w->dapm)
3599 case snd_soc_dapm_dai_in:
3600 case snd_soc_dapm_dai_out:
3606 if (!w->sname || !strstr(w->sname, dai_w->name))
3609 if (dai->driver->playback.stream_name &&
3611 dai->driver->playback.stream_name)) {
3612 dev_dbg(dai->dev, "%s -> %s\n",
3613 dai->playback_widget->name, w->name);
3615 snd_soc_dapm_add_path(w->dapm,
3616 dai->playback_widget, w, NULL, NULL);
3619 if (dai->driver->capture.stream_name &&
3621 dai->driver->capture.stream_name)) {
3622 dev_dbg(dai->dev, "%s -> %s\n",
3623 w->name, dai->capture_widget->name);
3625 snd_soc_dapm_add_path(w->dapm, w,
3626 dai->capture_widget, NULL, NULL);
3634 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3636 struct snd_soc_pcm_runtime *rtd = card->rtd;
3637 struct snd_soc_dai *cpu_dai, *codec_dai;
3638 struct snd_soc_dapm_route r;
3641 memset(&r, 0, sizeof(r));
3643 /* for each BE DAI link... */
3644 for (i = 0; i < card->num_rtd; i++) {
3645 rtd = &card->rtd[i];
3646 cpu_dai = rtd->cpu_dai;
3647 codec_dai = rtd->codec_dai;
3650 * dynamic FE links have no fixed DAI mapping.
3651 * CODEC<->CODEC links have no direct connection.
3653 if (rtd->dai_link->dynamic || rtd->dai_link->params)
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, true);
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, true);
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_unlocked - 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.
3772 * Requires external locking.
3774 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3775 * do any widget power switching.
3777 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3780 return snd_soc_dapm_set_pin(dapm, pin, 1);
3782 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3785 * snd_soc_dapm_enable_pin - enable pin.
3786 * @dapm: DAPM context
3789 * Enables input/output pin and its parents or children widgets iff there is
3790 * a valid audio route and active audio stream.
3792 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3793 * do any widget power switching.
3795 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3799 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3801 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3803 mutex_unlock(&dapm->card->dapm_mutex);
3807 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3810 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3811 * @dapm: DAPM context
3814 * Enables input/output pin regardless of any other state. This is
3815 * intended for use with microphone bias supplies used in microphone
3818 * Requires external locking.
3820 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3821 * do any widget power switching.
3823 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3826 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3829 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3833 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3836 dapm_mark_dirty(w, "force enable");
3840 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3843 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3844 * @dapm: DAPM context
3847 * Enables input/output pin regardless of any other state. This is
3848 * intended for use with microphone bias supplies used in microphone
3851 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3852 * do any widget power switching.
3854 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3859 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3861 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
3863 mutex_unlock(&dapm->card->dapm_mutex);
3867 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3870 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3871 * @dapm: DAPM context
3874 * Disables input/output pin and its parents or children widgets.
3876 * Requires external locking.
3878 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3879 * do any widget power switching.
3881 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3884 return snd_soc_dapm_set_pin(dapm, pin, 0);
3886 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3889 * snd_soc_dapm_disable_pin - disable pin.
3890 * @dapm: DAPM context
3893 * Disables input/output pin and its parents or children widgets.
3895 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3896 * do any widget power switching.
3898 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3903 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3905 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3907 mutex_unlock(&dapm->card->dapm_mutex);
3911 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3914 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3915 * @dapm: DAPM context
3918 * Marks the specified pin as being not connected, disabling it along
3919 * any parent or child widgets. At present this is identical to
3920 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3921 * additional things such as disabling controls which only affect
3922 * paths through the pin.
3924 * Requires external locking.
3926 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3927 * do any widget power switching.
3929 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3932 return snd_soc_dapm_set_pin(dapm, pin, 0);
3934 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3937 * snd_soc_dapm_nc_pin - permanently disable pin.
3938 * @dapm: DAPM context
3941 * Marks the specified pin as being not connected, disabling it along
3942 * any parent or child widgets. At present this is identical to
3943 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3944 * additional things such as disabling controls which only affect
3945 * paths through the pin.
3947 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3948 * do any widget power switching.
3950 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3954 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3956 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3958 mutex_unlock(&dapm->card->dapm_mutex);
3962 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3965 * snd_soc_dapm_get_pin_status - get audio pin status
3966 * @dapm: DAPM context
3967 * @pin: audio signal pin endpoint (or start point)
3969 * Get audio pin status - connected or disconnected.
3971 * Returns 1 for connected otherwise 0.
3973 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3976 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3979 return w->connected;
3983 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3986 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3987 * @dapm: DAPM context
3988 * @pin: audio signal pin endpoint (or start point)
3990 * Mark the given endpoint or pin as ignoring suspend. When the
3991 * system is disabled a path between two endpoints flagged as ignoring
3992 * suspend will not be disabled. The path must already be enabled via
3993 * normal means at suspend time, it will not be turned on if it was not
3996 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3999 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4002 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4006 w->ignore_suspend = 1;
4010 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4012 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
4013 struct snd_soc_dapm_widget *w)
4015 struct snd_soc_dapm_path *p;
4017 list_for_each_entry(p, &card->paths, list) {
4018 if ((p->source == w) || (p->sink == w)) {
4020 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
4021 p->source->name, p->source->id, p->source->dapm,
4022 p->sink->name, p->sink->id, p->sink->dapm);
4024 /* Connected to something other than the codec */
4025 if (p->source->dapm != p->sink->dapm)
4028 * Loopback connection from codec external pin to
4029 * codec external pin
4031 if (p->sink->id == snd_soc_dapm_input) {
4032 switch (p->source->id) {
4033 case snd_soc_dapm_output:
4034 case snd_soc_dapm_micbias:
4047 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
4048 * @codec: The codec whose pins should be processed
4050 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
4051 * which are unused. Pins are used if they are connected externally to the
4052 * codec, whether that be to some other device, or a loop-back connection to
4055 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
4057 struct snd_soc_card *card = codec->card;
4058 struct snd_soc_dapm_context *dapm = &codec->dapm;
4059 struct snd_soc_dapm_widget *w;
4061 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
4062 &card->dapm, &codec->dapm);
4064 list_for_each_entry(w, &card->widgets, list) {
4065 if (w->dapm != dapm)
4068 case snd_soc_dapm_input:
4069 case snd_soc_dapm_output:
4070 case snd_soc_dapm_micbias:
4071 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
4073 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
4075 "... Not in map; disabling\n");
4076 snd_soc_dapm_nc_pin(dapm, w->name);
4086 * snd_soc_dapm_free - free dapm resources
4087 * @dapm: DAPM context
4089 * Free all dapm widgets and resources.
4091 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4093 snd_soc_dapm_sys_remove(dapm->dev);
4094 dapm_debugfs_cleanup(dapm);
4095 dapm_free_widgets(dapm);
4096 list_del(&dapm->list);
4098 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4100 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
4102 struct snd_soc_card *card = dapm->card;
4103 struct snd_soc_dapm_widget *w;
4104 LIST_HEAD(down_list);
4107 mutex_lock(&card->dapm_mutex);
4109 list_for_each_entry(w, &dapm->card->widgets, list) {
4110 if (w->dapm != dapm)
4113 dapm_seq_insert(w, &down_list, false);
4119 /* If there were no widgets to power down we're already in
4123 if (dapm->bias_level == SND_SOC_BIAS_ON)
4124 snd_soc_dapm_set_bias_level(dapm,
4125 SND_SOC_BIAS_PREPARE);
4126 dapm_seq_run(card, &down_list, 0, false);
4127 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4128 snd_soc_dapm_set_bias_level(dapm,
4129 SND_SOC_BIAS_STANDBY);
4132 mutex_unlock(&card->dapm_mutex);
4136 * snd_soc_dapm_shutdown - callback for system shutdown
4138 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4140 struct snd_soc_codec *codec;
4142 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
4143 soc_dapm_shutdown_codec(&codec->dapm);
4144 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4145 snd_soc_dapm_set_bias_level(&codec->dapm,
4150 /* Module information */
4151 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4152 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4153 MODULE_LICENSE("GPL");