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;
692 char *long_name = NULL;
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,
764 kcontrol->private_free = dapm_kcontrol_free;
766 ret = dapm_kcontrol_data_alloc(w, kcontrol);
768 snd_ctl_free_one(kcontrol);
772 ret = snd_ctl_add(card, kcontrol);
775 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
781 ret = dapm_kcontrol_add_widget(kcontrol, w);
783 w->kcontrols[kci] = kcontrol;
791 /* create new dapm mixer control */
792 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
795 struct snd_soc_dapm_path *path;
798 for (i = 0; i < w->num_kcontrols; i++) {
800 list_for_each_entry(path, &w->sources, list_sink) {
801 /* mixer/mux paths name must match control name */
802 if (path->name != (char *)w->kcontrol_news[i].name)
805 if (w->kcontrols[i]) {
806 dapm_kcontrol_add_path(w->kcontrols[i], path);
810 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
814 dapm_kcontrol_add_path(w->kcontrols[i], path);
821 /* create new dapm mux control */
822 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
824 struct snd_soc_dapm_context *dapm = w->dapm;
825 struct snd_soc_dapm_path *path;
828 if (w->num_kcontrols != 1) {
830 "ASoC: mux %s has incorrect number of controls\n",
835 if (list_empty(&w->sources)) {
836 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
840 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
844 list_for_each_entry(path, &w->sources, list_sink)
845 dapm_kcontrol_add_path(w->kcontrols[0], path);
850 /* create new dapm volume control */
851 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
853 if (w->num_kcontrols)
854 dev_err(w->dapm->dev,
855 "ASoC: PGA controls not supported: '%s'\n", w->name);
860 /* reset 'walked' bit for each dapm path */
861 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
862 struct list_head *sink)
864 struct snd_soc_dapm_path *p;
866 list_for_each_entry(p, sink, list_source) {
869 dapm_clear_walk_output(dapm, &p->sink->sinks);
874 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
875 struct list_head *source)
877 struct snd_soc_dapm_path *p;
879 list_for_each_entry(p, source, list_sink) {
882 dapm_clear_walk_input(dapm, &p->source->sources);
888 /* We implement power down on suspend by checking the power state of
889 * the ALSA card - when we are suspending the ALSA state for the card
892 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
894 int level = snd_power_get_state(widget->dapm->card->snd_card);
897 case SNDRV_CTL_POWER_D3hot:
898 case SNDRV_CTL_POWER_D3cold:
899 if (widget->ignore_suspend)
900 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
902 return widget->ignore_suspend;
908 /* add widget to list if it's not already in the list */
909 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
910 struct snd_soc_dapm_widget *w)
912 struct snd_soc_dapm_widget_list *wlist;
913 int wlistsize, wlistentries, i;
920 /* is this widget already in the list */
921 for (i = 0; i < wlist->num_widgets; i++) {
922 if (wlist->widgets[i] == w)
926 /* allocate some new space */
927 wlistentries = wlist->num_widgets + 1;
928 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
929 wlistentries * sizeof(struct snd_soc_dapm_widget *);
930 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
932 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
938 /* insert the widget */
939 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
940 w->name, wlist->num_widgets);
942 wlist->widgets[wlist->num_widgets] = w;
943 wlist->num_widgets++;
948 * Recursively check for a completed path to an active or physically connected
949 * output widget. Returns number of complete paths.
951 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
952 struct snd_soc_dapm_widget_list **list)
954 struct snd_soc_dapm_path *path;
957 if (widget->outputs >= 0)
958 return widget->outputs;
960 DAPM_UPDATE_STAT(widget, path_checks);
962 switch (widget->id) {
963 case snd_soc_dapm_supply:
964 case snd_soc_dapm_regulator_supply:
965 case snd_soc_dapm_clock_supply:
966 case snd_soc_dapm_kcontrol:
972 switch (widget->id) {
973 case snd_soc_dapm_adc:
974 case snd_soc_dapm_aif_out:
975 case snd_soc_dapm_dai_out:
976 if (widget->active) {
977 widget->outputs = snd_soc_dapm_suspend_check(widget);
978 return widget->outputs;
984 if (widget->connected) {
985 /* connected pin ? */
986 if (widget->id == snd_soc_dapm_output && !widget->ext) {
987 widget->outputs = snd_soc_dapm_suspend_check(widget);
988 return widget->outputs;
991 /* connected jack or spk ? */
992 if (widget->id == snd_soc_dapm_hp ||
993 widget->id == snd_soc_dapm_spk ||
994 (widget->id == snd_soc_dapm_line &&
995 !list_empty(&widget->sources))) {
996 widget->outputs = snd_soc_dapm_suspend_check(widget);
997 return widget->outputs;
1001 list_for_each_entry(path, &widget->sinks, list_source) {
1002 DAPM_UPDATE_STAT(widget, neighbour_checks);
1013 trace_snd_soc_dapm_output_path(widget, path);
1015 if (path->sink && path->connect) {
1019 /* do we need to add this widget to the list ? */
1022 err = dapm_list_add_widget(list, path->sink);
1024 dev_err(widget->dapm->dev,
1025 "ASoC: could not add widget %s\n",
1032 con += is_connected_output_ep(path->sink, list);
1038 widget->outputs = con;
1044 * Recursively check for a completed path to an active or physically connected
1045 * input widget. Returns number of complete paths.
1047 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1048 struct snd_soc_dapm_widget_list **list)
1050 struct snd_soc_dapm_path *path;
1053 if (widget->inputs >= 0)
1054 return widget->inputs;
1056 DAPM_UPDATE_STAT(widget, path_checks);
1058 switch (widget->id) {
1059 case snd_soc_dapm_supply:
1060 case snd_soc_dapm_regulator_supply:
1061 case snd_soc_dapm_clock_supply:
1062 case snd_soc_dapm_kcontrol:
1068 /* active stream ? */
1069 switch (widget->id) {
1070 case snd_soc_dapm_dac:
1071 case snd_soc_dapm_aif_in:
1072 case snd_soc_dapm_dai_in:
1073 if (widget->active) {
1074 widget->inputs = snd_soc_dapm_suspend_check(widget);
1075 return widget->inputs;
1081 if (widget->connected) {
1082 /* connected pin ? */
1083 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1084 widget->inputs = snd_soc_dapm_suspend_check(widget);
1085 return widget->inputs;
1088 /* connected VMID/Bias for lower pops */
1089 if (widget->id == snd_soc_dapm_vmid) {
1090 widget->inputs = snd_soc_dapm_suspend_check(widget);
1091 return widget->inputs;
1094 /* connected jack ? */
1095 if (widget->id == snd_soc_dapm_mic ||
1096 (widget->id == snd_soc_dapm_line &&
1097 !list_empty(&widget->sinks))) {
1098 widget->inputs = snd_soc_dapm_suspend_check(widget);
1099 return widget->inputs;
1102 /* signal generator */
1103 if (widget->id == snd_soc_dapm_siggen) {
1104 widget->inputs = snd_soc_dapm_suspend_check(widget);
1105 return widget->inputs;
1109 list_for_each_entry(path, &widget->sources, list_sink) {
1110 DAPM_UPDATE_STAT(widget, neighbour_checks);
1121 trace_snd_soc_dapm_input_path(widget, path);
1123 if (path->source && path->connect) {
1127 /* do we need to add this widget to the list ? */
1130 err = dapm_list_add_widget(list, path->source);
1132 dev_err(widget->dapm->dev,
1133 "ASoC: could not add widget %s\n",
1140 con += is_connected_input_ep(path->source, list);
1146 widget->inputs = con;
1152 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1153 * @dai: the soc DAI.
1154 * @stream: stream direction.
1155 * @list: list of active widgets for this stream.
1157 * Queries DAPM graph as to whether an valid audio stream path exists for
1158 * the initial stream specified by name. This takes into account
1159 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1161 * Returns the number of valid paths or negative error.
1163 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1164 struct snd_soc_dapm_widget_list **list)
1166 struct snd_soc_card *card = dai->card;
1169 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1172 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1173 paths = is_connected_output_ep(dai->playback_widget, list);
1174 dapm_clear_walk_output(&card->dapm,
1175 &dai->playback_widget->sinks);
1177 paths = is_connected_input_ep(dai->capture_widget, list);
1178 dapm_clear_walk_input(&card->dapm,
1179 &dai->capture_widget->sources);
1182 trace_snd_soc_dapm_connected(paths, stream);
1183 mutex_unlock(&card->dapm_mutex);
1189 * Handler for generic register modifier widget.
1191 int dapm_reg_event(struct snd_soc_dapm_widget *w,
1192 struct snd_kcontrol *kcontrol, int event)
1196 if (SND_SOC_DAPM_EVENT_ON(event))
1201 soc_widget_update_bits_locked(w, -(w->reg + 1),
1202 w->mask << w->shift, val << w->shift);
1206 EXPORT_SYMBOL_GPL(dapm_reg_event);
1209 * Handler for regulator supply widget.
1211 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1212 struct snd_kcontrol *kcontrol, int event)
1216 soc_dapm_async_complete(w->dapm);
1218 if (SND_SOC_DAPM_EVENT_ON(event)) {
1219 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1220 ret = regulator_allow_bypass(w->regulator, false);
1222 dev_warn(w->dapm->dev,
1223 "ASoC: Failed to unbypass %s: %d\n",
1227 return regulator_enable(w->regulator);
1229 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1230 ret = regulator_allow_bypass(w->regulator, true);
1232 dev_warn(w->dapm->dev,
1233 "ASoC: Failed to bypass %s: %d\n",
1237 return regulator_disable_deferred(w->regulator, w->shift);
1240 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1243 * Handler for clock supply widget.
1245 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1246 struct snd_kcontrol *kcontrol, int event)
1251 soc_dapm_async_complete(w->dapm);
1253 #ifdef CONFIG_HAVE_CLK
1254 if (SND_SOC_DAPM_EVENT_ON(event)) {
1255 return clk_prepare_enable(w->clk);
1257 clk_disable_unprepare(w->clk);
1263 EXPORT_SYMBOL_GPL(dapm_clock_event);
1265 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1267 if (w->power_checked)
1268 return w->new_power;
1273 w->new_power = w->power_check(w);
1275 w->power_checked = true;
1277 return w->new_power;
1280 /* Generic check to see if a widget should be powered.
1282 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1286 DAPM_UPDATE_STAT(w, power_checks);
1288 in = is_connected_input_ep(w, NULL);
1289 dapm_clear_walk_input(w->dapm, &w->sources);
1290 out = is_connected_output_ep(w, NULL);
1291 dapm_clear_walk_output(w->dapm, &w->sinks);
1292 return out != 0 && in != 0;
1295 /* Check to see if an ADC has power */
1296 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1300 DAPM_UPDATE_STAT(w, power_checks);
1303 in = is_connected_input_ep(w, NULL);
1304 dapm_clear_walk_input(w->dapm, &w->sources);
1307 return dapm_generic_check_power(w);
1311 /* Check to see if a DAC has power */
1312 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1316 DAPM_UPDATE_STAT(w, power_checks);
1319 out = is_connected_output_ep(w, NULL);
1320 dapm_clear_walk_output(w->dapm, &w->sinks);
1323 return dapm_generic_check_power(w);
1327 /* Check to see if a power supply is needed */
1328 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1330 struct snd_soc_dapm_path *path;
1332 DAPM_UPDATE_STAT(w, power_checks);
1334 /* Check if one of our outputs is connected */
1335 list_for_each_entry(path, &w->sinks, list_source) {
1336 DAPM_UPDATE_STAT(w, neighbour_checks);
1341 if (path->connected &&
1342 !path->connected(path->source, path->sink))
1348 if (dapm_widget_power_check(path->sink))
1355 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1360 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1361 struct snd_soc_dapm_widget *b,
1369 sort = dapm_down_seq;
1371 if (sort[a->id] != sort[b->id])
1372 return sort[a->id] - sort[b->id];
1373 if (a->subseq != b->subseq) {
1375 return a->subseq - b->subseq;
1377 return b->subseq - a->subseq;
1379 if (a->reg != b->reg)
1380 return a->reg - b->reg;
1381 if (a->dapm != b->dapm)
1382 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1387 /* Insert a widget in order into a DAPM power sequence. */
1388 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1389 struct list_head *list,
1392 struct snd_soc_dapm_widget *w;
1394 list_for_each_entry(w, list, power_list)
1395 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1396 list_add_tail(&new_widget->power_list, &w->power_list);
1400 list_add_tail(&new_widget->power_list, list);
1403 static void dapm_seq_check_event(struct snd_soc_card *card,
1404 struct snd_soc_dapm_widget *w, int event)
1406 const char *ev_name;
1410 case SND_SOC_DAPM_PRE_PMU:
1411 ev_name = "PRE_PMU";
1414 case SND_SOC_DAPM_POST_PMU:
1415 ev_name = "POST_PMU";
1418 case SND_SOC_DAPM_PRE_PMD:
1419 ev_name = "PRE_PMD";
1422 case SND_SOC_DAPM_POST_PMD:
1423 ev_name = "POST_PMD";
1426 case SND_SOC_DAPM_WILL_PMU:
1427 ev_name = "WILL_PMU";
1430 case SND_SOC_DAPM_WILL_PMD:
1431 ev_name = "WILL_PMD";
1435 WARN(1, "Unknown event %d\n", event);
1439 if (w->new_power != power)
1442 if (w->event && (w->event_flags & event)) {
1443 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1445 soc_dapm_async_complete(w->dapm);
1446 trace_snd_soc_dapm_widget_event_start(w, event);
1447 ret = w->event(w, NULL, event);
1448 trace_snd_soc_dapm_widget_event_done(w, event);
1450 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1451 ev_name, w->name, ret);
1455 /* Apply the coalesced changes from a DAPM sequence */
1456 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1457 struct list_head *pending)
1459 struct snd_soc_dapm_widget *w;
1461 unsigned int value = 0;
1462 unsigned int mask = 0;
1464 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1467 list_for_each_entry(w, pending, power_list) {
1468 WARN_ON(reg != w->reg);
1469 w->power = w->new_power;
1471 mask |= w->mask << w->shift;
1473 value |= w->on_val << w->shift;
1475 value |= w->off_val << w->shift;
1477 pop_dbg(w->dapm->dev, card->pop_time,
1478 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1479 w->name, reg, value, mask);
1481 /* Check for events */
1482 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1483 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1487 /* Any widget will do, they should all be updating the
1490 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1493 pop_dbg(w->dapm->dev, card->pop_time,
1494 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1495 value, mask, reg, card->pop_time);
1496 pop_wait(card->pop_time);
1497 soc_widget_update_bits_locked(w, reg, mask, value);
1500 list_for_each_entry(w, pending, power_list) {
1501 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1502 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1506 /* Apply a DAPM power sequence.
1508 * We walk over a pre-sorted list of widgets to apply power to. In
1509 * order to minimise the number of writes to the device required
1510 * multiple widgets will be updated in a single write where possible.
1511 * Currently anything that requires more than a single write is not
1514 static void dapm_seq_run(struct snd_soc_card *card,
1515 struct list_head *list, int event, bool power_up)
1517 struct snd_soc_dapm_widget *w, *n;
1518 struct snd_soc_dapm_context *d;
1521 int cur_subseq = -1;
1522 int cur_reg = SND_SOC_NOPM;
1523 struct snd_soc_dapm_context *cur_dapm = NULL;
1530 sort = dapm_down_seq;
1532 list_for_each_entry_safe(w, n, list, power_list) {
1535 /* Do we need to apply any queued changes? */
1536 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1537 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1538 if (!list_empty(&pending))
1539 dapm_seq_run_coalesced(card, &pending);
1541 if (cur_dapm && cur_dapm->seq_notifier) {
1542 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1543 if (sort[i] == cur_sort)
1544 cur_dapm->seq_notifier(cur_dapm,
1549 if (cur_dapm && w->dapm != cur_dapm)
1550 soc_dapm_async_complete(cur_dapm);
1552 INIT_LIST_HEAD(&pending);
1554 cur_subseq = INT_MIN;
1555 cur_reg = SND_SOC_NOPM;
1560 case snd_soc_dapm_pre:
1562 list_for_each_entry_safe_continue(w, n, list,
1565 if (event == SND_SOC_DAPM_STREAM_START)
1567 NULL, SND_SOC_DAPM_PRE_PMU);
1568 else if (event == SND_SOC_DAPM_STREAM_STOP)
1570 NULL, SND_SOC_DAPM_PRE_PMD);
1573 case snd_soc_dapm_post:
1575 list_for_each_entry_safe_continue(w, n, list,
1578 if (event == SND_SOC_DAPM_STREAM_START)
1580 NULL, SND_SOC_DAPM_POST_PMU);
1581 else if (event == SND_SOC_DAPM_STREAM_STOP)
1583 NULL, SND_SOC_DAPM_POST_PMD);
1587 /* Queue it up for application */
1588 cur_sort = sort[w->id];
1589 cur_subseq = w->subseq;
1592 list_move(&w->power_list, &pending);
1597 dev_err(w->dapm->dev,
1598 "ASoC: Failed to apply widget power: %d\n", ret);
1601 if (!list_empty(&pending))
1602 dapm_seq_run_coalesced(card, &pending);
1604 if (cur_dapm && cur_dapm->seq_notifier) {
1605 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1606 if (sort[i] == cur_sort)
1607 cur_dapm->seq_notifier(cur_dapm,
1611 list_for_each_entry(d, &card->dapm_list, list) {
1612 soc_dapm_async_complete(d);
1616 static void dapm_widget_update(struct snd_soc_card *card)
1618 struct snd_soc_dapm_update *update = card->update;
1619 struct snd_soc_dapm_widget_list *wlist;
1620 struct snd_soc_dapm_widget *w = NULL;
1624 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1627 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1629 for (wi = 0; wi < wlist->num_widgets; wi++) {
1630 w = wlist->widgets[wi];
1632 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1633 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1635 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1643 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1646 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1649 for (wi = 0; wi < wlist->num_widgets; wi++) {
1650 w = wlist->widgets[wi];
1652 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1653 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1655 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1661 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1662 * they're changing state.
1664 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1666 struct snd_soc_dapm_context *d = data;
1669 /* If we're off and we're not supposed to be go into STANDBY */
1670 if (d->bias_level == SND_SOC_BIAS_OFF &&
1671 d->target_bias_level != SND_SOC_BIAS_OFF) {
1673 pm_runtime_get_sync(d->dev);
1675 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1678 "ASoC: Failed to turn on bias: %d\n", ret);
1681 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1682 if (d->bias_level != d->target_bias_level) {
1683 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1686 "ASoC: Failed to prepare bias: %d\n", ret);
1690 /* Async callback run prior to DAPM sequences - brings to their final
1693 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1695 struct snd_soc_dapm_context *d = data;
1698 /* If we just powered the last thing off drop to standby bias */
1699 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1700 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1701 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1702 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1704 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1708 /* If we're in standby and can support bias off then do that */
1709 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1710 d->target_bias_level == SND_SOC_BIAS_OFF) {
1711 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1713 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1717 pm_runtime_put(d->dev);
1720 /* If we just powered up then move to active bias */
1721 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1722 d->target_bias_level == SND_SOC_BIAS_ON) {
1723 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1725 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1730 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1731 bool power, bool connect)
1733 /* If a connection is being made or broken then that update
1734 * will have marked the peer dirty, otherwise the widgets are
1735 * not connected and this update has no impact. */
1739 /* If the peer is already in the state we're moving to then we
1740 * won't have an impact on it. */
1741 if (power != peer->power)
1742 dapm_mark_dirty(peer, "peer state change");
1745 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1746 struct list_head *up_list,
1747 struct list_head *down_list)
1749 struct snd_soc_dapm_path *path;
1751 if (w->power == power)
1754 trace_snd_soc_dapm_widget_power(w, power);
1756 /* If we changed our power state perhaps our neigbours changed
1759 list_for_each_entry(path, &w->sources, list_sink) {
1761 dapm_widget_set_peer_power(path->source, power,
1766 case snd_soc_dapm_supply:
1767 case snd_soc_dapm_regulator_supply:
1768 case snd_soc_dapm_clock_supply:
1769 case snd_soc_dapm_kcontrol:
1770 /* Supplies can't affect their outputs, only their inputs */
1773 list_for_each_entry(path, &w->sinks, list_source) {
1775 dapm_widget_set_peer_power(path->sink, power,
1783 dapm_seq_insert(w, up_list, true);
1785 dapm_seq_insert(w, down_list, false);
1788 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1789 struct list_head *up_list,
1790 struct list_head *down_list)
1795 case snd_soc_dapm_pre:
1796 dapm_seq_insert(w, down_list, false);
1798 case snd_soc_dapm_post:
1799 dapm_seq_insert(w, up_list, true);
1803 power = dapm_widget_power_check(w);
1805 dapm_widget_set_power(w, power, up_list, down_list);
1811 * Scan each dapm widget for complete audio path.
1812 * A complete path is a route that has valid endpoints i.e.:-
1814 * o DAC to output pin.
1815 * o Input Pin to ADC.
1816 * o Input pin to Output pin (bypass, sidetone)
1817 * o DAC to ADC (loopback).
1819 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1821 struct snd_soc_dapm_widget *w;
1822 struct snd_soc_dapm_context *d;
1824 LIST_HEAD(down_list);
1825 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1826 enum snd_soc_bias_level bias;
1828 trace_snd_soc_dapm_start(card);
1830 list_for_each_entry(d, &card->dapm_list, list) {
1831 if (d->idle_bias_off)
1832 d->target_bias_level = SND_SOC_BIAS_OFF;
1834 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1839 /* Check which widgets we need to power and store them in
1840 * lists indicating if they should be powered up or down. We
1841 * only check widgets that have been flagged as dirty but note
1842 * that new widgets may be added to the dirty list while we
1845 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1846 dapm_power_one_widget(w, &up_list, &down_list);
1849 list_for_each_entry(w, &card->widgets, list) {
1851 case snd_soc_dapm_pre:
1852 case snd_soc_dapm_post:
1853 /* These widgets always need to be powered */
1856 list_del_init(&w->dirty);
1863 /* Supplies and micbiases only bring the
1864 * context up to STANDBY as unless something
1865 * else is active and passing audio they
1866 * generally don't require full power. Signal
1867 * generators are virtual pins and have no
1868 * power impact themselves.
1871 case snd_soc_dapm_siggen:
1872 case snd_soc_dapm_vmid:
1874 case snd_soc_dapm_supply:
1875 case snd_soc_dapm_regulator_supply:
1876 case snd_soc_dapm_clock_supply:
1877 case snd_soc_dapm_micbias:
1878 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1879 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1882 d->target_bias_level = SND_SOC_BIAS_ON;
1889 /* Force all contexts in the card to the same bias state if
1890 * they're not ground referenced.
1892 bias = SND_SOC_BIAS_OFF;
1893 list_for_each_entry(d, &card->dapm_list, list)
1894 if (d->target_bias_level > bias)
1895 bias = d->target_bias_level;
1896 list_for_each_entry(d, &card->dapm_list, list)
1897 if (!d->idle_bias_off)
1898 d->target_bias_level = bias;
1900 trace_snd_soc_dapm_walk_done(card);
1902 /* Run all the bias changes in parallel */
1903 list_for_each_entry(d, &card->dapm_list, list)
1904 async_schedule_domain(dapm_pre_sequence_async, d,
1906 async_synchronize_full_domain(&async_domain);
1908 list_for_each_entry(w, &down_list, power_list) {
1909 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1912 list_for_each_entry(w, &up_list, power_list) {
1913 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1916 /* Power down widgets first; try to avoid amplifying pops. */
1917 dapm_seq_run(card, &down_list, event, false);
1919 dapm_widget_update(card);
1922 dapm_seq_run(card, &up_list, event, true);
1924 /* Run all the bias changes in parallel */
1925 list_for_each_entry(d, &card->dapm_list, list)
1926 async_schedule_domain(dapm_post_sequence_async, d,
1928 async_synchronize_full_domain(&async_domain);
1930 /* do we need to notify any clients that DAPM event is complete */
1931 list_for_each_entry(d, &card->dapm_list, list) {
1932 if (d->stream_event)
1933 d->stream_event(d, event);
1936 pop_dbg(card->dev, card->pop_time,
1937 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1938 pop_wait(card->pop_time);
1940 trace_snd_soc_dapm_done(card);
1945 #ifdef CONFIG_DEBUG_FS
1946 static ssize_t dapm_widget_power_read_file(struct file *file,
1947 char __user *user_buf,
1948 size_t count, loff_t *ppos)
1950 struct snd_soc_dapm_widget *w = file->private_data;
1954 struct snd_soc_dapm_path *p = NULL;
1956 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1960 in = is_connected_input_ep(w, NULL);
1961 dapm_clear_walk_input(w->dapm, &w->sources);
1962 out = is_connected_output_ep(w, NULL);
1963 dapm_clear_walk_output(w->dapm, &w->sinks);
1965 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1966 w->name, w->power ? "On" : "Off",
1967 w->force ? " (forced)" : "", in, out);
1970 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1971 " - R%d(0x%x) mask 0x%x",
1972 w->reg, w->reg, w->mask << w->shift);
1974 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1977 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1979 w->active ? "active" : "inactive");
1981 list_for_each_entry(p, &w->sources, list_sink) {
1982 if (p->connected && !p->connected(w, p->source))
1986 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1987 " in \"%s\" \"%s\"\n",
1988 p->name ? p->name : "static",
1991 list_for_each_entry(p, &w->sinks, list_source) {
1992 if (p->connected && !p->connected(w, p->sink))
1996 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1997 " out \"%s\" \"%s\"\n",
1998 p->name ? p->name : "static",
2002 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2008 static const struct file_operations dapm_widget_power_fops = {
2009 .open = simple_open,
2010 .read = dapm_widget_power_read_file,
2011 .llseek = default_llseek,
2014 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2015 size_t count, loff_t *ppos)
2017 struct snd_soc_dapm_context *dapm = file->private_data;
2020 switch (dapm->bias_level) {
2021 case SND_SOC_BIAS_ON:
2024 case SND_SOC_BIAS_PREPARE:
2025 level = "Prepare\n";
2027 case SND_SOC_BIAS_STANDBY:
2028 level = "Standby\n";
2030 case SND_SOC_BIAS_OFF:
2034 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2035 level = "Unknown\n";
2039 return simple_read_from_buffer(user_buf, count, ppos, level,
2043 static const struct file_operations dapm_bias_fops = {
2044 .open = simple_open,
2045 .read = dapm_bias_read_file,
2046 .llseek = default_llseek,
2049 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2050 struct dentry *parent)
2054 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2056 if (!dapm->debugfs_dapm) {
2058 "ASoC: Failed to create DAPM debugfs directory\n");
2062 d = debugfs_create_file("bias_level", 0444,
2063 dapm->debugfs_dapm, dapm,
2067 "ASoC: Failed to create bias level debugfs file\n");
2070 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2072 struct snd_soc_dapm_context *dapm = w->dapm;
2075 if (!dapm->debugfs_dapm || !w->name)
2078 d = debugfs_create_file(w->name, 0444,
2079 dapm->debugfs_dapm, w,
2080 &dapm_widget_power_fops);
2082 dev_warn(w->dapm->dev,
2083 "ASoC: Failed to create %s debugfs file\n",
2087 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2089 debugfs_remove_recursive(dapm->debugfs_dapm);
2093 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2094 struct dentry *parent)
2098 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2102 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2108 /* test and update the power status of a mux widget */
2109 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2110 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2112 struct snd_soc_dapm_path *path;
2115 /* find dapm widget path assoc with kcontrol */
2116 dapm_kcontrol_for_each_path(path, kcontrol) {
2117 if (!path->name || !e->texts[mux])
2121 /* we now need to match the string in the enum to the path */
2122 if (!(strcmp(path->name, e->texts[mux]))) {
2123 path->connect = 1; /* new connection */
2124 dapm_mark_dirty(path->source, "mux connection");
2127 dapm_mark_dirty(path->source,
2128 "mux disconnection");
2129 path->connect = 0; /* old connection must be powered down */
2131 dapm_mark_dirty(path->sink, "mux change");
2135 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2140 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2141 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2142 struct snd_soc_dapm_update *update)
2144 struct snd_soc_card *card = dapm->card;
2147 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2148 card->update = update;
2149 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2150 card->update = NULL;
2151 mutex_unlock(&card->dapm_mutex);
2153 soc_dpcm_runtime_update(card);
2156 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2158 /* test and update the power status of a mixer or switch widget */
2159 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2160 struct snd_kcontrol *kcontrol, int connect)
2162 struct snd_soc_dapm_path *path;
2165 /* find dapm widget path assoc with kcontrol */
2166 dapm_kcontrol_for_each_path(path, kcontrol) {
2168 path->connect = connect;
2169 dapm_mark_dirty(path->source, "mixer connection");
2170 dapm_mark_dirty(path->sink, "mixer update");
2174 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2179 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2180 struct snd_kcontrol *kcontrol, int connect,
2181 struct snd_soc_dapm_update *update)
2183 struct snd_soc_card *card = dapm->card;
2186 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2187 card->update = update;
2188 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2189 card->update = NULL;
2190 mutex_unlock(&card->dapm_mutex);
2192 soc_dpcm_runtime_update(card);
2195 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2197 /* show dapm widget status in sys fs */
2198 static ssize_t dapm_widget_show(struct device *dev,
2199 struct device_attribute *attr, char *buf)
2201 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2202 struct snd_soc_codec *codec =rtd->codec;
2203 struct snd_soc_dapm_widget *w;
2205 char *state = "not set";
2207 list_for_each_entry(w, &codec->card->widgets, list) {
2208 if (w->dapm != &codec->dapm)
2211 /* only display widgets that burnm power */
2213 case snd_soc_dapm_hp:
2214 case snd_soc_dapm_mic:
2215 case snd_soc_dapm_spk:
2216 case snd_soc_dapm_line:
2217 case snd_soc_dapm_micbias:
2218 case snd_soc_dapm_dac:
2219 case snd_soc_dapm_adc:
2220 case snd_soc_dapm_pga:
2221 case snd_soc_dapm_out_drv:
2222 case snd_soc_dapm_mixer:
2223 case snd_soc_dapm_mixer_named_ctl:
2224 case snd_soc_dapm_supply:
2225 case snd_soc_dapm_regulator_supply:
2226 case snd_soc_dapm_clock_supply:
2228 count += sprintf(buf + count, "%s: %s\n",
2229 w->name, w->power ? "On":"Off");
2236 switch (codec->dapm.bias_level) {
2237 case SND_SOC_BIAS_ON:
2240 case SND_SOC_BIAS_PREPARE:
2243 case SND_SOC_BIAS_STANDBY:
2246 case SND_SOC_BIAS_OFF:
2250 count += sprintf(buf + count, "PM State: %s\n", state);
2255 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2257 int snd_soc_dapm_sys_add(struct device *dev)
2259 return device_create_file(dev, &dev_attr_dapm_widget);
2262 static void snd_soc_dapm_sys_remove(struct device *dev)
2264 device_remove_file(dev, &dev_attr_dapm_widget);
2267 static void dapm_free_path(struct snd_soc_dapm_path *path)
2269 list_del(&path->list_sink);
2270 list_del(&path->list_source);
2271 list_del(&path->list_kcontrol);
2272 list_del(&path->list);
2276 /* free all dapm widgets and resources */
2277 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2279 struct snd_soc_dapm_widget *w, *next_w;
2280 struct snd_soc_dapm_path *p, *next_p;
2282 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2283 if (w->dapm != dapm)
2287 * remove source and sink paths associated to this widget.
2288 * While removing the path, remove reference to it from both
2289 * source and sink widgets so that path is removed only once.
2291 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2294 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2297 kfree(w->kcontrols);
2303 static struct snd_soc_dapm_widget *dapm_find_widget(
2304 struct snd_soc_dapm_context *dapm, const char *pin,
2305 bool search_other_contexts)
2307 struct snd_soc_dapm_widget *w;
2308 struct snd_soc_dapm_widget *fallback = NULL;
2310 list_for_each_entry(w, &dapm->card->widgets, list) {
2311 if (!strcmp(w->name, pin)) {
2312 if (w->dapm == dapm)
2319 if (search_other_contexts)
2325 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2326 const char *pin, int status)
2328 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2331 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2335 if (w->connected != status)
2336 dapm_mark_dirty(w, "pin configuration");
2338 w->connected = status;
2346 * snd_soc_dapm_sync - scan and power dapm paths
2347 * @dapm: DAPM context
2349 * Walks all dapm audio paths and powers widgets according to their
2350 * stream or path usage.
2352 * Returns 0 for success.
2354 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2359 * Suppress early reports (eg, jacks syncing their state) to avoid
2360 * silly DAPM runs during card startup.
2362 if (!dapm->card || !dapm->card->instantiated)
2365 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2366 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2367 mutex_unlock(&dapm->card->dapm_mutex);
2370 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2372 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2373 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2374 const char *control,
2375 int (*connected)(struct snd_soc_dapm_widget *source,
2376 struct snd_soc_dapm_widget *sink))
2378 struct snd_soc_dapm_path *path;
2381 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2385 path->source = wsource;
2387 path->connected = connected;
2388 INIT_LIST_HEAD(&path->list);
2389 INIT_LIST_HEAD(&path->list_kcontrol);
2390 INIT_LIST_HEAD(&path->list_source);
2391 INIT_LIST_HEAD(&path->list_sink);
2393 /* check for external widgets */
2394 if (wsink->id == snd_soc_dapm_input) {
2395 if (wsource->id == snd_soc_dapm_micbias ||
2396 wsource->id == snd_soc_dapm_mic ||
2397 wsource->id == snd_soc_dapm_line ||
2398 wsource->id == snd_soc_dapm_output)
2401 if (wsource->id == snd_soc_dapm_output) {
2402 if (wsink->id == snd_soc_dapm_spk ||
2403 wsink->id == snd_soc_dapm_hp ||
2404 wsink->id == snd_soc_dapm_line ||
2405 wsink->id == snd_soc_dapm_input)
2409 dapm_mark_dirty(wsource, "Route added");
2410 dapm_mark_dirty(wsink, "Route added");
2412 /* connect static paths */
2413 if (control == NULL) {
2414 list_add(&path->list, &dapm->card->paths);
2415 list_add(&path->list_sink, &wsink->sources);
2416 list_add(&path->list_source, &wsource->sinks);
2421 /* connect dynamic paths */
2422 switch (wsink->id) {
2423 case snd_soc_dapm_adc:
2424 case snd_soc_dapm_dac:
2425 case snd_soc_dapm_pga:
2426 case snd_soc_dapm_out_drv:
2427 case snd_soc_dapm_input:
2428 case snd_soc_dapm_output:
2429 case snd_soc_dapm_siggen:
2430 case snd_soc_dapm_micbias:
2431 case snd_soc_dapm_vmid:
2432 case snd_soc_dapm_pre:
2433 case snd_soc_dapm_post:
2434 case snd_soc_dapm_supply:
2435 case snd_soc_dapm_regulator_supply:
2436 case snd_soc_dapm_clock_supply:
2437 case snd_soc_dapm_aif_in:
2438 case snd_soc_dapm_aif_out:
2439 case snd_soc_dapm_dai_in:
2440 case snd_soc_dapm_dai_out:
2441 case snd_soc_dapm_dai_link:
2442 case snd_soc_dapm_kcontrol:
2443 list_add(&path->list, &dapm->card->paths);
2444 list_add(&path->list_sink, &wsink->sources);
2445 list_add(&path->list_source, &wsource->sinks);
2448 case snd_soc_dapm_mux:
2449 case snd_soc_dapm_virt_mux:
2450 case snd_soc_dapm_value_mux:
2451 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2452 &wsink->kcontrol_news[0]);
2456 case snd_soc_dapm_switch:
2457 case snd_soc_dapm_mixer:
2458 case snd_soc_dapm_mixer_named_ctl:
2459 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2463 case snd_soc_dapm_hp:
2464 case snd_soc_dapm_mic:
2465 case snd_soc_dapm_line:
2466 case snd_soc_dapm_spk:
2467 list_add(&path->list, &dapm->card->paths);
2468 list_add(&path->list_sink, &wsink->sources);
2469 list_add(&path->list_source, &wsource->sinks);
2480 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2481 const struct snd_soc_dapm_route *route,
2482 unsigned int is_prefixed)
2484 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2485 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2488 char prefixed_sink[80];
2489 char prefixed_source[80];
2492 if (dapm->codec && dapm->codec->name_prefix && !is_prefixed) {
2493 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2494 dapm->codec->name_prefix, route->sink);
2495 sink = prefixed_sink;
2496 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2497 dapm->codec->name_prefix, route->source);
2498 source = prefixed_source;
2501 source = route->source;
2505 * find src and dest widgets over all widgets but favor a widget from
2506 * current DAPM context
2508 list_for_each_entry(w, &dapm->card->widgets, list) {
2509 if (!wsink && !(strcmp(w->name, sink))) {
2511 if (w->dapm == dapm)
2515 if (!wsource && !(strcmp(w->name, source))) {
2517 if (w->dapm == dapm)
2521 /* use widget from another DAPM context if not found from this */
2527 if (wsource == NULL) {
2528 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2532 if (wsink == NULL) {
2533 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2538 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2545 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2546 source, route->control, sink);
2550 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2551 const struct snd_soc_dapm_route *route)
2553 struct snd_soc_dapm_path *path, *p;
2556 char prefixed_sink[80];
2557 char prefixed_source[80];
2559 if (route->control) {
2561 "ASoC: Removal of routes with controls not supported\n");
2565 if (dapm->codec && dapm->codec->name_prefix) {
2566 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2567 dapm->codec->name_prefix, route->sink);
2568 sink = prefixed_sink;
2569 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2570 dapm->codec->name_prefix, route->source);
2571 source = prefixed_source;
2574 source = route->source;
2578 list_for_each_entry(p, &dapm->card->paths, list) {
2579 if (strcmp(p->source->name, source) != 0)
2581 if (strcmp(p->sink->name, sink) != 0)
2588 dapm_mark_dirty(path->source, "Route removed");
2589 dapm_mark_dirty(path->sink, "Route removed");
2591 dapm_free_path(path);
2593 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2601 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2602 * @dapm: DAPM context
2603 * @route: audio routes
2604 * @num: number of routes
2606 * Connects 2 dapm widgets together via a named audio path. The sink is
2607 * the widget receiving the audio signal, whilst the source is the sender
2608 * of the audio signal.
2610 * Returns 0 for success else error. On error all resources can be freed
2611 * with a call to snd_soc_card_free().
2613 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2614 const struct snd_soc_dapm_route *route, int num)
2618 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2619 for (i = 0; i < num; i++) {
2620 r = snd_soc_dapm_add_route(dapm, route, false);
2622 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2624 route->control ? route->control : "direct",
2630 mutex_unlock(&dapm->card->dapm_mutex);
2634 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2637 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2638 * @dapm: DAPM context
2639 * @route: audio routes
2640 * @num: number of routes
2642 * Removes routes from the DAPM context.
2644 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2645 const struct snd_soc_dapm_route *route, int num)
2649 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2650 for (i = 0; i < num; i++) {
2651 snd_soc_dapm_del_route(dapm, route);
2654 mutex_unlock(&dapm->card->dapm_mutex);
2658 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2660 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2661 const struct snd_soc_dapm_route *route)
2663 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2666 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2669 struct snd_soc_dapm_path *path;
2673 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2679 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2684 if (route->control || route->connected)
2685 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2686 route->source, route->sink);
2688 list_for_each_entry(path, &source->sinks, list_source) {
2689 if (path->sink == sink) {
2696 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2697 route->source, route->sink);
2699 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2700 count, route->source, route->sink);
2706 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2707 * @dapm: DAPM context
2708 * @route: audio routes
2709 * @num: number of routes
2711 * Mark existing routes matching those specified in the passed array
2712 * as being weak, meaning that they are ignored for the purpose of
2713 * power decisions. The main intended use case is for sidetone paths
2714 * which couple audio between other independent paths if they are both
2715 * active in order to make the combination work better at the user
2716 * level but which aren't intended to be "used".
2718 * Note that CODEC drivers should not use this as sidetone type paths
2719 * can frequently also be used as bypass paths.
2721 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2722 const struct snd_soc_dapm_route *route, int num)
2727 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2728 for (i = 0; i < num; i++) {
2729 err = snd_soc_dapm_weak_route(dapm, route);
2734 mutex_unlock(&dapm->card->dapm_mutex);
2738 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2741 * snd_soc_dapm_new_widgets - add new dapm widgets
2742 * @dapm: DAPM context
2744 * Checks the codec for any new dapm widgets and creates them if found.
2746 * Returns 0 for success.
2748 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2750 struct snd_soc_dapm_widget *w;
2753 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2755 list_for_each_entry(w, &card->widgets, list)
2760 if (w->num_kcontrols) {
2761 w->kcontrols = kzalloc(w->num_kcontrols *
2762 sizeof(struct snd_kcontrol *),
2764 if (!w->kcontrols) {
2765 mutex_unlock(&card->dapm_mutex);
2771 case snd_soc_dapm_switch:
2772 case snd_soc_dapm_mixer:
2773 case snd_soc_dapm_mixer_named_ctl:
2776 case snd_soc_dapm_mux:
2777 case snd_soc_dapm_virt_mux:
2778 case snd_soc_dapm_value_mux:
2781 case snd_soc_dapm_pga:
2782 case snd_soc_dapm_out_drv:
2789 /* Read the initial power state from the device */
2791 soc_widget_read(w, w->reg, &val);
2792 val = val >> w->shift;
2794 if (val == w->on_val)
2800 dapm_mark_dirty(w, "new widget");
2801 dapm_debugfs_add_widget(w);
2804 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2805 mutex_unlock(&card->dapm_mutex);
2808 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2811 * snd_soc_dapm_get_volsw - dapm mixer get callback
2812 * @kcontrol: mixer control
2813 * @ucontrol: control element information
2815 * Callback to get the value of a dapm mixer control.
2817 * Returns 0 for success.
2819 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2820 struct snd_ctl_elem_value *ucontrol)
2822 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2823 struct snd_soc_card *card = codec->card;
2824 struct soc_mixer_control *mc =
2825 (struct soc_mixer_control *)kcontrol->private_value;
2827 unsigned int shift = mc->shift;
2829 unsigned int mask = (1 << fls(max)) - 1;
2830 unsigned int invert = mc->invert;
2833 if (snd_soc_volsw_is_stereo(mc))
2834 dev_warn(codec->dapm.dev,
2835 "ASoC: Control '%s' is stereo, which is not supported\n",
2838 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2839 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM)
2840 val = (snd_soc_read(codec, reg) >> shift) & mask;
2842 val = dapm_kcontrol_get_value(kcontrol);
2843 mutex_unlock(&card->dapm_mutex);
2846 ucontrol->value.integer.value[0] = max - val;
2848 ucontrol->value.integer.value[0] = val;
2852 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2855 * snd_soc_dapm_put_volsw - dapm mixer set callback
2856 * @kcontrol: mixer control
2857 * @ucontrol: control element information
2859 * Callback to set the value of a dapm mixer control.
2861 * Returns 0 for success.
2863 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2864 struct snd_ctl_elem_value *ucontrol)
2866 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2867 struct snd_soc_card *card = codec->card;
2868 struct soc_mixer_control *mc =
2869 (struct soc_mixer_control *)kcontrol->private_value;
2871 unsigned int shift = mc->shift;
2873 unsigned int mask = (1 << fls(max)) - 1;
2874 unsigned int invert = mc->invert;
2876 int connect, change;
2877 struct snd_soc_dapm_update update;
2880 if (snd_soc_volsw_is_stereo(mc))
2881 dev_warn(codec->dapm.dev,
2882 "ASoC: Control '%s' is stereo, which is not supported\n",
2885 val = (ucontrol->value.integer.value[0] & mask);
2891 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2893 change = dapm_kcontrol_set_value(kcontrol, val);
2895 if (reg != SND_SOC_NOPM) {
2896 mask = mask << shift;
2899 if (snd_soc_test_bits(codec, reg, mask, val)) {
2900 update.kcontrol = kcontrol;
2904 card->update = &update;
2909 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2911 card->update = NULL;
2914 mutex_unlock(&card->dapm_mutex);
2917 soc_dpcm_runtime_update(card);
2921 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2924 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2925 * @kcontrol: mixer control
2926 * @ucontrol: control element information
2928 * Callback to get the value of a dapm enumerated double mixer control.
2930 * Returns 0 for success.
2932 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_value *ucontrol)
2935 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2936 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2939 val = snd_soc_read(codec, e->reg);
2940 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2941 if (e->shift_l != e->shift_r)
2942 ucontrol->value.enumerated.item[1] =
2943 (val >> e->shift_r) & e->mask;
2947 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2950 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2951 * @kcontrol: mixer control
2952 * @ucontrol: control element information
2954 * Callback to set the value of a dapm enumerated double mixer control.
2956 * Returns 0 for success.
2958 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2959 struct snd_ctl_elem_value *ucontrol)
2961 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2962 struct snd_soc_card *card = codec->card;
2963 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2964 unsigned int val, mux, change;
2966 struct snd_soc_dapm_update update;
2969 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2971 mux = ucontrol->value.enumerated.item[0];
2972 val = mux << e->shift_l;
2973 mask = e->mask << e->shift_l;
2974 if (e->shift_l != e->shift_r) {
2975 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2977 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2978 mask |= e->mask << e->shift_r;
2981 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2983 change = snd_soc_test_bits(codec, e->reg, mask, val);
2985 update.kcontrol = kcontrol;
2986 update.reg = e->reg;
2989 card->update = &update;
2991 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2993 card->update = NULL;
2996 mutex_unlock(&card->dapm_mutex);
2999 soc_dpcm_runtime_update(card);
3003 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3006 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
3007 * @kcontrol: mixer control
3008 * @ucontrol: control element information
3010 * Returns 0 for success.
3012 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
3013 struct snd_ctl_elem_value *ucontrol)
3015 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
3018 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
3021 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
3022 * @kcontrol: mixer control
3023 * @ucontrol: control element information
3025 * Returns 0 for success.
3027 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
3028 struct snd_ctl_elem_value *ucontrol)
3030 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3031 struct snd_soc_card *card = codec->card;
3033 struct soc_enum *e =
3034 (struct soc_enum *)kcontrol->private_value;
3038 if (ucontrol->value.enumerated.item[0] >= e->max)
3041 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3043 value = ucontrol->value.enumerated.item[0];
3044 change = dapm_kcontrol_set_value(kcontrol, value);
3046 ret = soc_dapm_mux_update_power(card, kcontrol, value, e);
3048 mutex_unlock(&card->dapm_mutex);
3051 soc_dpcm_runtime_update(card);
3055 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3058 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3060 * @kcontrol: mixer control
3061 * @ucontrol: control element information
3063 * Callback to get the value of a dapm semi enumerated double mixer control.
3065 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3066 * used for handling bitfield coded enumeration for example.
3068 * Returns 0 for success.
3070 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3071 struct snd_ctl_elem_value *ucontrol)
3073 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3074 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3075 unsigned int reg_val, val, mux;
3077 reg_val = snd_soc_read(codec, e->reg);
3078 val = (reg_val >> e->shift_l) & e->mask;
3079 for (mux = 0; mux < e->max; mux++) {
3080 if (val == e->values[mux])
3083 ucontrol->value.enumerated.item[0] = mux;
3084 if (e->shift_l != e->shift_r) {
3085 val = (reg_val >> e->shift_r) & e->mask;
3086 for (mux = 0; mux < e->max; mux++) {
3087 if (val == e->values[mux])
3090 ucontrol->value.enumerated.item[1] = mux;
3095 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3098 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3100 * @kcontrol: mixer control
3101 * @ucontrol: control element information
3103 * Callback to set the value of a dapm semi enumerated double mixer control.
3105 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3106 * used for handling bitfield coded enumeration for example.
3108 * Returns 0 for success.
3110 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_value *ucontrol)
3113 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3114 struct snd_soc_card *card = codec->card;
3115 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3116 unsigned int val, mux, change;
3118 struct snd_soc_dapm_update update;
3121 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3123 mux = ucontrol->value.enumerated.item[0];
3124 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3125 mask = e->mask << e->shift_l;
3126 if (e->shift_l != e->shift_r) {
3127 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3129 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3130 mask |= e->mask << e->shift_r;
3133 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3135 change = snd_soc_test_bits(codec, e->reg, mask, val);
3137 update.kcontrol = kcontrol;
3138 update.reg = e->reg;
3141 card->update = &update;
3143 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
3145 card->update = NULL;
3148 mutex_unlock(&card->dapm_mutex);
3151 soc_dpcm_runtime_update(card);
3155 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3158 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3160 * @kcontrol: mixer control
3161 * @uinfo: control element information
3163 * Callback to provide information about a pin switch control.
3165 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3166 struct snd_ctl_elem_info *uinfo)
3168 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3170 uinfo->value.integer.min = 0;
3171 uinfo->value.integer.max = 1;
3175 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3178 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3180 * @kcontrol: mixer control
3183 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3184 struct snd_ctl_elem_value *ucontrol)
3186 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3187 const char *pin = (const char *)kcontrol->private_value;
3189 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3191 ucontrol->value.integer.value[0] =
3192 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3194 mutex_unlock(&card->dapm_mutex);
3198 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3201 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3203 * @kcontrol: mixer control
3206 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3207 struct snd_ctl_elem_value *ucontrol)
3209 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3210 const char *pin = (const char *)kcontrol->private_value;
3212 if (ucontrol->value.integer.value[0])
3213 snd_soc_dapm_enable_pin(&card->dapm, pin);
3215 snd_soc_dapm_disable_pin(&card->dapm, pin);
3217 snd_soc_dapm_sync(&card->dapm);
3220 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3222 static struct snd_soc_dapm_widget *
3223 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3224 const struct snd_soc_dapm_widget *widget)
3226 struct snd_soc_dapm_widget *w;
3229 if ((w = dapm_cnew_widget(widget)) == NULL)
3233 case snd_soc_dapm_regulator_supply:
3234 w->regulator = devm_regulator_get(dapm->dev, w->name);
3235 if (IS_ERR(w->regulator)) {
3236 ret = PTR_ERR(w->regulator);
3237 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3242 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3243 ret = regulator_allow_bypass(w->regulator, true);
3245 dev_warn(w->dapm->dev,
3246 "ASoC: Failed to bypass %s: %d\n",
3250 case snd_soc_dapm_clock_supply:
3251 #ifdef CONFIG_CLKDEV_LOOKUP
3252 w->clk = devm_clk_get(dapm->dev, w->name);
3253 if (IS_ERR(w->clk)) {
3254 ret = PTR_ERR(w->clk);
3255 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3267 if (dapm->codec && dapm->codec->name_prefix)
3268 w->name = kasprintf(GFP_KERNEL, "%s %s",
3269 dapm->codec->name_prefix, widget->name);
3271 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3273 if (w->name == NULL) {
3279 case snd_soc_dapm_switch:
3280 case snd_soc_dapm_mixer:
3281 case snd_soc_dapm_mixer_named_ctl:
3282 w->power_check = dapm_generic_check_power;
3284 case snd_soc_dapm_mux:
3285 case snd_soc_dapm_virt_mux:
3286 case snd_soc_dapm_value_mux:
3287 w->power_check = dapm_generic_check_power;
3289 case snd_soc_dapm_dai_out:
3290 w->power_check = dapm_adc_check_power;
3292 case snd_soc_dapm_dai_in:
3293 w->power_check = dapm_dac_check_power;
3295 case snd_soc_dapm_adc:
3296 case snd_soc_dapm_aif_out:
3297 case snd_soc_dapm_dac:
3298 case snd_soc_dapm_aif_in:
3299 case snd_soc_dapm_pga:
3300 case snd_soc_dapm_out_drv:
3301 case snd_soc_dapm_input:
3302 case snd_soc_dapm_output:
3303 case snd_soc_dapm_micbias:
3304 case snd_soc_dapm_spk:
3305 case snd_soc_dapm_hp:
3306 case snd_soc_dapm_mic:
3307 case snd_soc_dapm_line:
3308 case snd_soc_dapm_dai_link:
3309 w->power_check = dapm_generic_check_power;
3311 case snd_soc_dapm_supply:
3312 case snd_soc_dapm_regulator_supply:
3313 case snd_soc_dapm_clock_supply:
3314 case snd_soc_dapm_kcontrol:
3315 w->power_check = dapm_supply_check_power;
3318 w->power_check = dapm_always_on_check_power;
3323 w->codec = dapm->codec;
3324 w->platform = dapm->platform;
3325 INIT_LIST_HEAD(&w->sources);
3326 INIT_LIST_HEAD(&w->sinks);
3327 INIT_LIST_HEAD(&w->list);
3328 INIT_LIST_HEAD(&w->dirty);
3329 list_add(&w->list, &dapm->card->widgets);
3331 /* machine layer set ups unconnected pins and insertions */
3337 * snd_soc_dapm_new_controls - create new dapm controls
3338 * @dapm: DAPM context
3339 * @widget: widget array
3340 * @num: number of widgets
3342 * Creates new DAPM controls based upon the templates.
3344 * Returns 0 for success else error.
3346 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3347 const struct snd_soc_dapm_widget *widget,
3350 struct snd_soc_dapm_widget *w;
3354 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3355 for (i = 0; i < num; i++) {
3356 w = snd_soc_dapm_new_control(dapm, widget);
3359 "ASoC: Failed to create DAPM control %s\n",
3366 mutex_unlock(&dapm->card->dapm_mutex);
3369 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3371 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3372 struct snd_kcontrol *kcontrol, int event)
3374 struct snd_soc_dapm_path *source_p, *sink_p;
3375 struct snd_soc_dai *source, *sink;
3376 const struct snd_soc_pcm_stream *config = w->params;
3377 struct snd_pcm_substream substream;
3378 struct snd_pcm_hw_params *params = NULL;
3382 if (WARN_ON(!config) ||
3383 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3386 /* We only support a single source and sink, pick the first */
3387 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3389 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3392 if (WARN_ON(!source_p || !sink_p) ||
3393 WARN_ON(!sink_p->source || !source_p->sink) ||
3394 WARN_ON(!source_p->source || !sink_p->sink))
3397 source = source_p->source->priv;
3398 sink = sink_p->sink->priv;
3400 /* Be a little careful as we don't want to overflow the mask array */
3401 if (config->formats) {
3402 fmt = ffs(config->formats) - 1;
3404 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3409 /* Currently very limited parameter selection */
3410 params = kzalloc(sizeof(*params), GFP_KERNEL);
3415 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3417 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3419 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3422 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3423 = config->channels_min;
3424 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3425 = config->channels_max;
3427 memset(&substream, 0, sizeof(substream));
3430 case SND_SOC_DAPM_PRE_PMU:
3431 if (source->driver->ops && source->driver->ops->hw_params) {
3432 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3433 ret = source->driver->ops->hw_params(&substream,
3436 dev_err(source->dev,
3437 "ASoC: hw_params() failed: %d\n", ret);
3442 if (sink->driver->ops && sink->driver->ops->hw_params) {
3443 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3444 ret = sink->driver->ops->hw_params(&substream, params,
3448 "ASoC: hw_params() failed: %d\n", ret);
3454 case SND_SOC_DAPM_POST_PMU:
3455 ret = snd_soc_dai_digital_mute(sink, 0,
3456 SNDRV_PCM_STREAM_PLAYBACK);
3457 if (ret != 0 && ret != -ENOTSUPP)
3458 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3462 case SND_SOC_DAPM_PRE_PMD:
3463 ret = snd_soc_dai_digital_mute(sink, 1,
3464 SNDRV_PCM_STREAM_PLAYBACK);
3465 if (ret != 0 && ret != -ENOTSUPP)
3466 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3471 WARN(1, "Unknown event %d\n", event);
3480 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3481 const struct snd_soc_pcm_stream *params,
3482 struct snd_soc_dapm_widget *source,
3483 struct snd_soc_dapm_widget *sink)
3485 struct snd_soc_dapm_route routes[2];
3486 struct snd_soc_dapm_widget template;
3487 struct snd_soc_dapm_widget *w;
3491 len = strlen(source->name) + strlen(sink->name) + 2;
3492 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3495 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3497 memset(&template, 0, sizeof(template));
3498 template.reg = SND_SOC_NOPM;
3499 template.id = snd_soc_dapm_dai_link;
3500 template.name = link_name;
3501 template.event = snd_soc_dai_link_event;
3502 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3503 SND_SOC_DAPM_PRE_PMD;
3505 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3507 w = snd_soc_dapm_new_control(&card->dapm, &template);
3509 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3516 memset(&routes, 0, sizeof(routes));
3518 routes[0].source = source->name;
3519 routes[0].sink = link_name;
3520 routes[1].source = link_name;
3521 routes[1].sink = sink->name;
3523 return snd_soc_dapm_add_routes(&card->dapm, routes,
3524 ARRAY_SIZE(routes));
3527 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3528 struct snd_soc_dai *dai)
3530 struct snd_soc_dapm_widget template;
3531 struct snd_soc_dapm_widget *w;
3533 WARN_ON(dapm->dev != dai->dev);
3535 memset(&template, 0, sizeof(template));
3536 template.reg = SND_SOC_NOPM;
3538 if (dai->driver->playback.stream_name) {
3539 template.id = snd_soc_dapm_dai_in;
3540 template.name = dai->driver->playback.stream_name;
3541 template.sname = dai->driver->playback.stream_name;
3543 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3546 w = snd_soc_dapm_new_control(dapm, &template);
3548 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3549 dai->driver->playback.stream_name);
3554 dai->playback_widget = w;
3557 if (dai->driver->capture.stream_name) {
3558 template.id = snd_soc_dapm_dai_out;
3559 template.name = dai->driver->capture.stream_name;
3560 template.sname = dai->driver->capture.stream_name;
3562 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3565 w = snd_soc_dapm_new_control(dapm, &template);
3567 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3568 dai->driver->capture.stream_name);
3573 dai->capture_widget = w;
3579 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3581 struct snd_soc_dapm_widget *dai_w, *w;
3582 struct snd_soc_dai *dai;
3584 /* For each DAI widget... */
3585 list_for_each_entry(dai_w, &card->widgets, list) {
3586 switch (dai_w->id) {
3587 case snd_soc_dapm_dai_in:
3588 case snd_soc_dapm_dai_out:
3596 /* ...find all widgets with the same stream and link them */
3597 list_for_each_entry(w, &card->widgets, list) {
3598 if (w->dapm != dai_w->dapm)
3602 case snd_soc_dapm_dai_in:
3603 case snd_soc_dapm_dai_out:
3609 if (!w->sname || !strstr(w->sname, dai_w->name))
3612 if (dai->driver->playback.stream_name &&
3614 dai->driver->playback.stream_name)) {
3615 dev_dbg(dai->dev, "%s -> %s\n",
3616 dai->playback_widget->name, w->name);
3618 snd_soc_dapm_add_path(w->dapm,
3619 dai->playback_widget, w, NULL, NULL);
3622 if (dai->driver->capture.stream_name &&
3624 dai->driver->capture.stream_name)) {
3625 dev_dbg(dai->dev, "%s -> %s\n",
3626 w->name, dai->capture_widget->name);
3628 snd_soc_dapm_add_path(w->dapm, w,
3629 dai->capture_widget, NULL, NULL);
3637 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3639 struct snd_soc_pcm_runtime *rtd = card->rtd;
3640 struct snd_soc_dai *cpu_dai, *codec_dai;
3641 struct snd_soc_dapm_route r;
3644 memset(&r, 0, sizeof(r));
3646 /* for each BE DAI link... */
3647 for (i = 0; i < card->num_rtd; i++) {
3648 rtd = &card->rtd[i];
3649 cpu_dai = rtd->cpu_dai;
3650 codec_dai = rtd->codec_dai;
3653 * dynamic FE links have no fixed DAI mapping.
3654 * CODEC<->CODEC links have no direct connection.
3656 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3659 /* there is no point in connecting BE DAI links with dummies */
3660 if (snd_soc_dai_is_dummy(codec_dai) ||
3661 snd_soc_dai_is_dummy(cpu_dai))
3664 /* connect BE DAI playback if widgets are valid */
3665 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3666 r.source = cpu_dai->playback_widget->name;
3667 r.sink = codec_dai->playback_widget->name;
3668 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3669 cpu_dai->codec->name, r.source,
3670 codec_dai->platform->name, r.sink);
3672 snd_soc_dapm_add_route(&card->dapm, &r, true);
3675 /* connect BE DAI capture if widgets are valid */
3676 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3677 r.source = codec_dai->capture_widget->name;
3678 r.sink = cpu_dai->capture_widget->name;
3679 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3680 codec_dai->codec->name, r.source,
3681 cpu_dai->platform->name, r.sink);
3683 snd_soc_dapm_add_route(&card->dapm, &r, true);
3689 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3693 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3694 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3695 struct snd_soc_dai *codec_dai = rtd->codec_dai;
3697 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3698 w_cpu = cpu_dai->playback_widget;
3699 w_codec = codec_dai->playback_widget;
3701 w_cpu = cpu_dai->capture_widget;
3702 w_codec = codec_dai->capture_widget;
3707 dapm_mark_dirty(w_cpu, "stream event");
3710 case SND_SOC_DAPM_STREAM_START:
3713 case SND_SOC_DAPM_STREAM_STOP:
3716 case SND_SOC_DAPM_STREAM_SUSPEND:
3717 case SND_SOC_DAPM_STREAM_RESUME:
3718 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3719 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3726 dapm_mark_dirty(w_codec, "stream event");
3729 case SND_SOC_DAPM_STREAM_START:
3730 w_codec->active = 1;
3732 case SND_SOC_DAPM_STREAM_STOP:
3733 w_codec->active = 0;
3735 case SND_SOC_DAPM_STREAM_SUSPEND:
3736 case SND_SOC_DAPM_STREAM_RESUME:
3737 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3738 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3743 dapm_power_widgets(rtd->card, event);
3747 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3748 * @rtd: PCM runtime data
3749 * @stream: stream name
3750 * @event: stream event
3752 * Sends a stream event to the dapm core. The core then makes any
3753 * necessary widget power changes.
3755 * Returns 0 for success else error.
3757 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3760 struct snd_soc_card *card = rtd->card;
3762 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3763 soc_dapm_stream_event(rtd, stream, event);
3764 mutex_unlock(&card->dapm_mutex);
3768 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3769 * @dapm: DAPM context
3772 * Enables input/output pin and its parents or children widgets iff there is
3773 * a valid audio route and active audio stream.
3775 * Requires external locking.
3777 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3778 * do any widget power switching.
3780 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3783 return snd_soc_dapm_set_pin(dapm, pin, 1);
3785 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3788 * snd_soc_dapm_enable_pin - enable pin.
3789 * @dapm: DAPM context
3792 * Enables input/output pin and its parents or children widgets iff there is
3793 * a valid audio route and active audio stream.
3795 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3796 * do any widget power switching.
3798 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3802 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3804 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3806 mutex_unlock(&dapm->card->dapm_mutex);
3810 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3813 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3814 * @dapm: DAPM context
3817 * Enables input/output pin regardless of any other state. This is
3818 * intended for use with microphone bias supplies used in microphone
3821 * Requires external locking.
3823 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3824 * do any widget power switching.
3826 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3829 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3832 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3836 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3839 dapm_mark_dirty(w, "force enable");
3843 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3846 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3847 * @dapm: DAPM context
3850 * Enables input/output pin regardless of any other state. This is
3851 * intended for use with microphone bias supplies used in microphone
3854 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3855 * do any widget power switching.
3857 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3862 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3864 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
3866 mutex_unlock(&dapm->card->dapm_mutex);
3870 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3873 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3874 * @dapm: DAPM context
3877 * Disables input/output pin and its parents or children widgets.
3879 * Requires external locking.
3881 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3882 * do any widget power switching.
3884 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3887 return snd_soc_dapm_set_pin(dapm, pin, 0);
3889 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3892 * snd_soc_dapm_disable_pin - disable pin.
3893 * @dapm: DAPM context
3896 * Disables input/output pin and its parents or children widgets.
3898 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3899 * do any widget power switching.
3901 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3906 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3908 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3910 mutex_unlock(&dapm->card->dapm_mutex);
3914 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3917 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3918 * @dapm: DAPM context
3921 * Marks the specified pin as being not connected, disabling it along
3922 * any parent or child widgets. At present this is identical to
3923 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3924 * additional things such as disabling controls which only affect
3925 * paths through the pin.
3927 * Requires external locking.
3929 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3930 * do any widget power switching.
3932 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3935 return snd_soc_dapm_set_pin(dapm, pin, 0);
3937 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3940 * snd_soc_dapm_nc_pin - permanently disable pin.
3941 * @dapm: DAPM context
3944 * Marks the specified pin as being not connected, disabling it along
3945 * any parent or child widgets. At present this is identical to
3946 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3947 * additional things such as disabling controls which only affect
3948 * paths through the pin.
3950 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3951 * do any widget power switching.
3953 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3957 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3959 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3961 mutex_unlock(&dapm->card->dapm_mutex);
3965 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3968 * snd_soc_dapm_get_pin_status - get audio pin status
3969 * @dapm: DAPM context
3970 * @pin: audio signal pin endpoint (or start point)
3972 * Get audio pin status - connected or disconnected.
3974 * Returns 1 for connected otherwise 0.
3976 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3979 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3982 return w->connected;
3986 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3989 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3990 * @dapm: DAPM context
3991 * @pin: audio signal pin endpoint (or start point)
3993 * Mark the given endpoint or pin as ignoring suspend. When the
3994 * system is disabled a path between two endpoints flagged as ignoring
3995 * suspend will not be disabled. The path must already be enabled via
3996 * normal means at suspend time, it will not be turned on if it was not
3999 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4002 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4005 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4009 w->ignore_suspend = 1;
4013 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4015 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
4016 struct snd_soc_dapm_widget *w)
4018 struct snd_soc_dapm_path *p;
4020 list_for_each_entry(p, &card->paths, list) {
4021 if ((p->source == w) || (p->sink == w)) {
4023 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
4024 p->source->name, p->source->id, p->source->dapm,
4025 p->sink->name, p->sink->id, p->sink->dapm);
4027 /* Connected to something other than the codec */
4028 if (p->source->dapm != p->sink->dapm)
4031 * Loopback connection from codec external pin to
4032 * codec external pin
4034 if (p->sink->id == snd_soc_dapm_input) {
4035 switch (p->source->id) {
4036 case snd_soc_dapm_output:
4037 case snd_soc_dapm_micbias:
4050 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
4051 * @codec: The codec whose pins should be processed
4053 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
4054 * which are unused. Pins are used if they are connected externally to the
4055 * codec, whether that be to some other device, or a loop-back connection to
4058 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
4060 struct snd_soc_card *card = codec->card;
4061 struct snd_soc_dapm_context *dapm = &codec->dapm;
4062 struct snd_soc_dapm_widget *w;
4064 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
4065 &card->dapm, &codec->dapm);
4067 list_for_each_entry(w, &card->widgets, list) {
4068 if (w->dapm != dapm)
4071 case snd_soc_dapm_input:
4072 case snd_soc_dapm_output:
4073 case snd_soc_dapm_micbias:
4074 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
4076 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
4078 "... Not in map; disabling\n");
4079 snd_soc_dapm_nc_pin(dapm, w->name);
4089 * snd_soc_dapm_free - free dapm resources
4090 * @dapm: DAPM context
4092 * Free all dapm widgets and resources.
4094 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4096 snd_soc_dapm_sys_remove(dapm->dev);
4097 dapm_debugfs_cleanup(dapm);
4098 dapm_free_widgets(dapm);
4099 list_del(&dapm->list);
4101 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4103 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
4105 struct snd_soc_card *card = dapm->card;
4106 struct snd_soc_dapm_widget *w;
4107 LIST_HEAD(down_list);
4110 mutex_lock(&card->dapm_mutex);
4112 list_for_each_entry(w, &dapm->card->widgets, list) {
4113 if (w->dapm != dapm)
4116 dapm_seq_insert(w, &down_list, false);
4122 /* If there were no widgets to power down we're already in
4126 if (dapm->bias_level == SND_SOC_BIAS_ON)
4127 snd_soc_dapm_set_bias_level(dapm,
4128 SND_SOC_BIAS_PREPARE);
4129 dapm_seq_run(card, &down_list, 0, false);
4130 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4131 snd_soc_dapm_set_bias_level(dapm,
4132 SND_SOC_BIAS_STANDBY);
4135 mutex_unlock(&card->dapm_mutex);
4139 * snd_soc_dapm_shutdown - callback for system shutdown
4141 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4143 struct snd_soc_codec *codec;
4145 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
4146 soc_dapm_shutdown_codec(&codec->dapm);
4147 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4148 snd_soc_dapm_set_bias_level(&codec->dapm,
4153 /* Module information */
4154 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4155 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4156 MODULE_LICENSE("GPL");