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_supply] = 1,
63 [snd_soc_dapm_regulator_supply] = 1,
64 [snd_soc_dapm_clock_supply] = 1,
65 [snd_soc_dapm_micbias] = 2,
66 [snd_soc_dapm_dai_link] = 2,
67 [snd_soc_dapm_dai_in] = 3,
68 [snd_soc_dapm_dai_out] = 3,
69 [snd_soc_dapm_aif_in] = 3,
70 [snd_soc_dapm_aif_out] = 3,
71 [snd_soc_dapm_mic] = 4,
72 [snd_soc_dapm_mux] = 5,
73 [snd_soc_dapm_virt_mux] = 5,
74 [snd_soc_dapm_value_mux] = 5,
75 [snd_soc_dapm_dac] = 6,
76 [snd_soc_dapm_switch] = 7,
77 [snd_soc_dapm_mixer] = 7,
78 [snd_soc_dapm_mixer_named_ctl] = 7,
79 [snd_soc_dapm_pga] = 8,
80 [snd_soc_dapm_adc] = 9,
81 [snd_soc_dapm_out_drv] = 10,
82 [snd_soc_dapm_hp] = 10,
83 [snd_soc_dapm_spk] = 10,
84 [snd_soc_dapm_line] = 10,
85 [snd_soc_dapm_kcontrol] = 11,
86 [snd_soc_dapm_post] = 12,
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_clock_supply] = 12,
113 [snd_soc_dapm_regulator_supply] = 12,
114 [snd_soc_dapm_supply] = 12,
115 [snd_soc_dapm_post] = 13,
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->widget = snd_soc_dapm_new_control(widget->dapm,
244 kcontrol->private_data = data;
249 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
251 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
257 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
258 const struct snd_kcontrol *kcontrol)
260 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
265 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
266 struct snd_soc_dapm_widget *widget)
268 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
269 struct snd_soc_dapm_widget_list *new_wlist;
273 n = data->wlist->num_widgets + 1;
277 new_wlist = krealloc(data->wlist,
278 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
282 new_wlist->widgets[n - 1] = widget;
283 new_wlist->num_widgets = n;
285 data->wlist = new_wlist;
290 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
291 struct snd_soc_dapm_path *path)
293 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
295 list_add_tail(&path->list_kcontrol, &data->paths);
298 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
299 path->source, NULL, NULL);
303 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
305 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
310 return data->widget->power;
313 static struct list_head *dapm_kcontrol_get_path_list(
314 const struct snd_kcontrol *kcontrol)
316 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
321 #define dapm_kcontrol_for_each_path(path, kcontrol) \
322 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
325 static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
327 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
332 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
335 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
337 if (data->value == value)
341 data->widget->on_val = value;
349 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
350 * @kcontrol: The kcontrol
352 struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
354 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->codec;
356 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
358 static void dapm_reset(struct snd_soc_card *card)
360 struct snd_soc_dapm_widget *w;
362 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
364 list_for_each_entry(w, &card->widgets, list) {
365 w->new_power = w->power;
366 w->power_checked = false;
372 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
375 return snd_soc_read(w->codec, reg);
376 else if (w->platform)
377 return snd_soc_platform_read(w->platform, reg);
379 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
383 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
386 return snd_soc_write(w->codec, reg, val);
387 else if (w->platform)
388 return snd_soc_platform_write(w->platform, reg, val);
390 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
394 static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
396 if (w->codec && !w->codec->using_regmap)
397 mutex_lock(&w->codec->mutex);
398 else if (w->platform)
399 mutex_lock(&w->platform->mutex);
402 static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
404 if (w->codec && !w->codec->using_regmap)
405 mutex_unlock(&w->codec->mutex);
406 else if (w->platform)
407 mutex_unlock(&w->platform->mutex);
410 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
411 unsigned short reg, unsigned int mask, unsigned int value)
414 unsigned int old, new;
417 if (w->codec && w->codec->using_regmap) {
418 ret = regmap_update_bits_check(w->codec->control_data,
419 reg, mask, value, &change);
424 ret = soc_widget_read(w, reg);
426 soc_widget_unlock(w);
431 new = (old & ~mask) | (value & mask);
434 ret = soc_widget_write(w, reg, new);
436 soc_widget_unlock(w);
440 soc_widget_unlock(w);
447 * snd_soc_dapm_set_bias_level - set the bias level for the system
448 * @dapm: DAPM context
449 * @level: level to configure
451 * Configure the bias (power) levels for the SoC audio device.
453 * Returns 0 for success else error.
455 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
456 enum snd_soc_bias_level level)
458 struct snd_soc_card *card = dapm->card;
461 trace_snd_soc_bias_level_start(card, level);
463 if (card && card->set_bias_level)
464 ret = card->set_bias_level(card, dapm, level);
469 if (dapm->codec->driver->set_bias_level)
470 ret = dapm->codec->driver->set_bias_level(dapm->codec,
473 dapm->bias_level = level;
474 } else if (!card || dapm != &card->dapm) {
475 dapm->bias_level = level;
481 if (card && card->set_bias_level_post)
482 ret = card->set_bias_level_post(card, dapm, level);
484 trace_snd_soc_bias_level_done(card, level);
489 /* set up initial codec paths */
490 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
491 struct snd_soc_dapm_path *p, int i)
494 case snd_soc_dapm_switch:
495 case snd_soc_dapm_mixer:
496 case snd_soc_dapm_mixer_named_ctl: {
498 struct soc_mixer_control *mc = (struct soc_mixer_control *)
499 w->kcontrol_news[i].private_value;
500 unsigned int reg = mc->reg;
501 unsigned int shift = mc->shift;
503 unsigned int mask = (1 << fls(max)) - 1;
504 unsigned int invert = mc->invert;
506 val = soc_widget_read(w, reg);
507 val = (val >> shift) & mask;
514 case snd_soc_dapm_mux: {
515 struct soc_enum *e = (struct soc_enum *)
516 w->kcontrol_news[i].private_value;
519 val = soc_widget_read(w, e->reg);
520 item = (val >> e->shift_l) & e->mask;
522 if (item < e->max && !strcmp(p->name, e->texts[item]))
528 case snd_soc_dapm_virt_mux: {
529 struct soc_enum *e = (struct soc_enum *)
530 w->kcontrol_news[i].private_value;
533 /* since a virtual mux has no backing registers to
534 * decide which path to connect, it will try to match
535 * with the first enumeration. This is to ensure
536 * that the default mux choice (the first) will be
537 * correctly powered up during initialization.
539 if (!strcmp(p->name, e->texts[0]))
543 case snd_soc_dapm_value_mux: {
544 struct soc_enum *e = (struct soc_enum *)
545 w->kcontrol_news[i].private_value;
548 val = soc_widget_read(w, e->reg);
549 val = (val >> e->shift_l) & e->mask;
550 for (item = 0; item < e->max; item++) {
551 if (val == e->values[item])
555 if (item < e->max && !strcmp(p->name, e->texts[item]))
561 /* does not affect routing - always connected */
562 case snd_soc_dapm_pga:
563 case snd_soc_dapm_out_drv:
564 case snd_soc_dapm_output:
565 case snd_soc_dapm_adc:
566 case snd_soc_dapm_input:
567 case snd_soc_dapm_siggen:
568 case snd_soc_dapm_dac:
569 case snd_soc_dapm_micbias:
570 case snd_soc_dapm_vmid:
571 case snd_soc_dapm_supply:
572 case snd_soc_dapm_regulator_supply:
573 case snd_soc_dapm_clock_supply:
574 case snd_soc_dapm_aif_in:
575 case snd_soc_dapm_aif_out:
576 case snd_soc_dapm_dai_in:
577 case snd_soc_dapm_dai_out:
578 case snd_soc_dapm_hp:
579 case snd_soc_dapm_mic:
580 case snd_soc_dapm_spk:
581 case snd_soc_dapm_line:
582 case snd_soc_dapm_dai_link:
583 case snd_soc_dapm_kcontrol:
586 /* does affect routing - dynamically connected */
587 case snd_soc_dapm_pre:
588 case snd_soc_dapm_post:
594 /* connect mux widget to its interconnecting audio paths */
595 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
596 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
597 struct snd_soc_dapm_path *path, const char *control_name,
598 const struct snd_kcontrol_new *kcontrol)
600 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
603 for (i = 0; i < e->max; i++) {
604 if (!(strcmp(control_name, e->texts[i]))) {
605 list_add(&path->list, &dapm->card->paths);
606 list_add(&path->list_sink, &dest->sources);
607 list_add(&path->list_source, &src->sinks);
608 path->name = (char*)e->texts[i];
609 dapm_set_path_status(dest, path, 0);
617 /* connect mixer widget to its interconnecting audio paths */
618 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
619 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
620 struct snd_soc_dapm_path *path, const char *control_name)
624 /* search for mixer kcontrol */
625 for (i = 0; i < dest->num_kcontrols; i++) {
626 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
627 list_add(&path->list, &dapm->card->paths);
628 list_add(&path->list_sink, &dest->sources);
629 list_add(&path->list_source, &src->sinks);
630 path->name = dest->kcontrol_news[i].name;
631 dapm_set_path_status(dest, path, i);
638 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
639 struct snd_soc_dapm_widget *kcontrolw,
640 const struct snd_kcontrol_new *kcontrol_new,
641 struct snd_kcontrol **kcontrol)
643 struct snd_soc_dapm_widget *w;
648 list_for_each_entry(w, &dapm->card->widgets, list) {
649 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
651 for (i = 0; i < w->num_kcontrols; i++) {
652 if (&w->kcontrol_news[i] == kcontrol_new) {
654 *kcontrol = w->kcontrols[i];
664 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
665 * create it. Either way, add the widget into the control's widget list
667 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
670 struct snd_soc_dapm_context *dapm = w->dapm;
671 struct snd_card *card = dapm->card->snd_card;
675 struct snd_kcontrol *kcontrol;
676 bool wname_in_long_name, kcname_in_long_name;
682 prefix = dapm->codec->name_prefix;
687 prefix_len = strlen(prefix) + 1;
691 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
696 wname_in_long_name = false;
697 kcname_in_long_name = true;
700 case snd_soc_dapm_switch:
701 case snd_soc_dapm_mixer:
702 wname_in_long_name = true;
703 kcname_in_long_name = true;
705 case snd_soc_dapm_mixer_named_ctl:
706 wname_in_long_name = false;
707 kcname_in_long_name = true;
709 case snd_soc_dapm_mux:
710 case snd_soc_dapm_virt_mux:
711 case snd_soc_dapm_value_mux:
712 wname_in_long_name = true;
713 kcname_in_long_name = false;
720 if (wname_in_long_name && kcname_in_long_name) {
722 * The control will get a prefix from the control
723 * creation process but we're also using the same
724 * prefix for widgets so cut the prefix off the
725 * front of the widget name.
727 long_name = kasprintf(GFP_KERNEL, "%s %s",
728 w->name + prefix_len,
729 w->kcontrol_news[kci].name);
730 if (long_name == NULL)
734 } else if (wname_in_long_name) {
736 name = w->name + prefix_len;
739 name = w->kcontrol_news[kci].name;
742 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
747 kcontrol->private_free = dapm_kcontrol_free;
749 ret = dapm_kcontrol_data_alloc(w, kcontrol);
751 snd_ctl_free_one(kcontrol);
755 ret = snd_ctl_add(card, kcontrol);
758 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
764 ret = dapm_kcontrol_add_widget(kcontrol, w);
768 w->kcontrols[kci] = kcontrol;
773 /* create new dapm mixer control */
774 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
777 struct snd_soc_dapm_path *path;
780 for (i = 0; i < w->num_kcontrols; i++) {
782 list_for_each_entry(path, &w->sources, list_sink) {
783 /* mixer/mux paths name must match control name */
784 if (path->name != (char *)w->kcontrol_news[i].name)
787 if (w->kcontrols[i]) {
788 dapm_kcontrol_add_path(w->kcontrols[i], path);
792 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
796 dapm_kcontrol_add_path(w->kcontrols[i], path);
803 /* create new dapm mux control */
804 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
806 struct snd_soc_dapm_context *dapm = w->dapm;
807 struct snd_soc_dapm_path *path;
810 if (w->num_kcontrols != 1) {
812 "ASoC: mux %s has incorrect number of controls\n",
817 if (list_empty(&w->sources)) {
818 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
822 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
826 list_for_each_entry(path, &w->sources, list_sink)
827 dapm_kcontrol_add_path(w->kcontrols[0], path);
832 /* create new dapm volume control */
833 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
835 if (w->num_kcontrols)
836 dev_err(w->dapm->dev,
837 "ASoC: PGA controls not supported: '%s'\n", w->name);
842 /* reset 'walked' bit for each dapm path */
843 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
844 struct list_head *sink)
846 struct snd_soc_dapm_path *p;
848 list_for_each_entry(p, sink, list_source) {
851 dapm_clear_walk_output(dapm, &p->sink->sinks);
856 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
857 struct list_head *source)
859 struct snd_soc_dapm_path *p;
861 list_for_each_entry(p, source, list_sink) {
864 dapm_clear_walk_input(dapm, &p->source->sources);
870 /* We implement power down on suspend by checking the power state of
871 * the ALSA card - when we are suspending the ALSA state for the card
874 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
876 int level = snd_power_get_state(widget->dapm->card->snd_card);
879 case SNDRV_CTL_POWER_D3hot:
880 case SNDRV_CTL_POWER_D3cold:
881 if (widget->ignore_suspend)
882 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
884 return widget->ignore_suspend;
890 /* add widget to list if it's not already in the list */
891 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
892 struct snd_soc_dapm_widget *w)
894 struct snd_soc_dapm_widget_list *wlist;
895 int wlistsize, wlistentries, i;
902 /* is this widget already in the list */
903 for (i = 0; i < wlist->num_widgets; i++) {
904 if (wlist->widgets[i] == w)
908 /* allocate some new space */
909 wlistentries = wlist->num_widgets + 1;
910 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
911 wlistentries * sizeof(struct snd_soc_dapm_widget *);
912 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
914 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
920 /* insert the widget */
921 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
922 w->name, wlist->num_widgets);
924 wlist->widgets[wlist->num_widgets] = w;
925 wlist->num_widgets++;
930 * Recursively check for a completed path to an active or physically connected
931 * output widget. Returns number of complete paths.
933 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
934 struct snd_soc_dapm_widget_list **list)
936 struct snd_soc_dapm_path *path;
939 if (widget->outputs >= 0)
940 return widget->outputs;
942 DAPM_UPDATE_STAT(widget, path_checks);
944 switch (widget->id) {
945 case snd_soc_dapm_supply:
946 case snd_soc_dapm_regulator_supply:
947 case snd_soc_dapm_clock_supply:
948 case snd_soc_dapm_kcontrol:
954 switch (widget->id) {
955 case snd_soc_dapm_adc:
956 case snd_soc_dapm_aif_out:
957 case snd_soc_dapm_dai_out:
958 if (widget->active) {
959 widget->outputs = snd_soc_dapm_suspend_check(widget);
960 return widget->outputs;
966 if (widget->connected) {
967 /* connected pin ? */
968 if (widget->id == snd_soc_dapm_output && !widget->ext) {
969 widget->outputs = snd_soc_dapm_suspend_check(widget);
970 return widget->outputs;
973 /* connected jack or spk ? */
974 if (widget->id == snd_soc_dapm_hp ||
975 widget->id == snd_soc_dapm_spk ||
976 (widget->id == snd_soc_dapm_line &&
977 !list_empty(&widget->sources))) {
978 widget->outputs = snd_soc_dapm_suspend_check(widget);
979 return widget->outputs;
983 list_for_each_entry(path, &widget->sinks, list_source) {
984 DAPM_UPDATE_STAT(widget, neighbour_checks);
995 trace_snd_soc_dapm_output_path(widget, path);
997 if (path->sink && path->connect) {
1001 /* do we need to add this widget to the list ? */
1004 err = dapm_list_add_widget(list, path->sink);
1006 dev_err(widget->dapm->dev,
1007 "ASoC: could not add widget %s\n",
1014 con += is_connected_output_ep(path->sink, list);
1020 widget->outputs = con;
1026 * Recursively check for a completed path to an active or physically connected
1027 * input widget. Returns number of complete paths.
1029 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1030 struct snd_soc_dapm_widget_list **list)
1032 struct snd_soc_dapm_path *path;
1035 if (widget->inputs >= 0)
1036 return widget->inputs;
1038 DAPM_UPDATE_STAT(widget, path_checks);
1040 switch (widget->id) {
1041 case snd_soc_dapm_supply:
1042 case snd_soc_dapm_regulator_supply:
1043 case snd_soc_dapm_clock_supply:
1044 case snd_soc_dapm_kcontrol:
1050 /* active stream ? */
1051 switch (widget->id) {
1052 case snd_soc_dapm_dac:
1053 case snd_soc_dapm_aif_in:
1054 case snd_soc_dapm_dai_in:
1055 if (widget->active) {
1056 widget->inputs = snd_soc_dapm_suspend_check(widget);
1057 return widget->inputs;
1063 if (widget->connected) {
1064 /* connected pin ? */
1065 if (widget->id == snd_soc_dapm_input && !widget->ext) {
1066 widget->inputs = snd_soc_dapm_suspend_check(widget);
1067 return widget->inputs;
1070 /* connected VMID/Bias for lower pops */
1071 if (widget->id == snd_soc_dapm_vmid) {
1072 widget->inputs = snd_soc_dapm_suspend_check(widget);
1073 return widget->inputs;
1076 /* connected jack ? */
1077 if (widget->id == snd_soc_dapm_mic ||
1078 (widget->id == snd_soc_dapm_line &&
1079 !list_empty(&widget->sinks))) {
1080 widget->inputs = snd_soc_dapm_suspend_check(widget);
1081 return widget->inputs;
1084 /* signal generator */
1085 if (widget->id == snd_soc_dapm_siggen) {
1086 widget->inputs = snd_soc_dapm_suspend_check(widget);
1087 return widget->inputs;
1091 list_for_each_entry(path, &widget->sources, list_sink) {
1092 DAPM_UPDATE_STAT(widget, neighbour_checks);
1103 trace_snd_soc_dapm_input_path(widget, path);
1105 if (path->source && path->connect) {
1109 /* do we need to add this widget to the list ? */
1112 err = dapm_list_add_widget(list, path->source);
1114 dev_err(widget->dapm->dev,
1115 "ASoC: could not add widget %s\n",
1122 con += is_connected_input_ep(path->source, list);
1128 widget->inputs = con;
1134 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1135 * @dai: the soc DAI.
1136 * @stream: stream direction.
1137 * @list: list of active widgets for this stream.
1139 * Queries DAPM graph as to whether an valid audio stream path exists for
1140 * the initial stream specified by name. This takes into account
1141 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1143 * Returns the number of valid paths or negative error.
1145 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1146 struct snd_soc_dapm_widget_list **list)
1148 struct snd_soc_card *card = dai->card;
1151 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1154 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1155 paths = is_connected_output_ep(dai->playback_widget, list);
1156 dapm_clear_walk_output(&card->dapm,
1157 &dai->playback_widget->sinks);
1159 paths = is_connected_input_ep(dai->capture_widget, list);
1160 dapm_clear_walk_input(&card->dapm,
1161 &dai->capture_widget->sources);
1164 trace_snd_soc_dapm_connected(paths, stream);
1165 mutex_unlock(&card->dapm_mutex);
1171 * Handler for generic register modifier widget.
1173 int dapm_reg_event(struct snd_soc_dapm_widget *w,
1174 struct snd_kcontrol *kcontrol, int event)
1178 if (SND_SOC_DAPM_EVENT_ON(event))
1183 soc_widget_update_bits_locked(w, -(w->reg + 1),
1184 w->mask << w->shift, val << w->shift);
1188 EXPORT_SYMBOL_GPL(dapm_reg_event);
1191 * Handler for regulator supply widget.
1193 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1194 struct snd_kcontrol *kcontrol, int event)
1198 if (SND_SOC_DAPM_EVENT_ON(event)) {
1199 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1200 ret = regulator_allow_bypass(w->regulator, false);
1202 dev_warn(w->dapm->dev,
1203 "ASoC: Failed to bypass %s: %d\n",
1207 return regulator_enable(w->regulator);
1209 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1210 ret = regulator_allow_bypass(w->regulator, true);
1212 dev_warn(w->dapm->dev,
1213 "ASoC: Failed to unbypass %s: %d\n",
1217 return regulator_disable_deferred(w->regulator, w->shift);
1220 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1223 * Handler for clock supply widget.
1225 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1226 struct snd_kcontrol *kcontrol, int event)
1231 #ifdef CONFIG_HAVE_CLK
1232 if (SND_SOC_DAPM_EVENT_ON(event)) {
1233 return clk_prepare_enable(w->clk);
1235 clk_disable_unprepare(w->clk);
1241 EXPORT_SYMBOL_GPL(dapm_clock_event);
1243 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1245 if (w->power_checked)
1246 return w->new_power;
1251 w->new_power = w->power_check(w);
1253 w->power_checked = true;
1255 return w->new_power;
1258 /* Generic check to see if a widget should be powered.
1260 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1264 DAPM_UPDATE_STAT(w, power_checks);
1266 in = is_connected_input_ep(w, NULL);
1267 dapm_clear_walk_input(w->dapm, &w->sources);
1268 out = is_connected_output_ep(w, NULL);
1269 dapm_clear_walk_output(w->dapm, &w->sinks);
1270 return out != 0 && in != 0;
1273 /* Check to see if an ADC has power */
1274 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1278 DAPM_UPDATE_STAT(w, power_checks);
1281 in = is_connected_input_ep(w, NULL);
1282 dapm_clear_walk_input(w->dapm, &w->sources);
1285 return dapm_generic_check_power(w);
1289 /* Check to see if a DAC has power */
1290 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1294 DAPM_UPDATE_STAT(w, power_checks);
1297 out = is_connected_output_ep(w, NULL);
1298 dapm_clear_walk_output(w->dapm, &w->sinks);
1301 return dapm_generic_check_power(w);
1305 /* Check to see if a power supply is needed */
1306 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1308 struct snd_soc_dapm_path *path;
1310 DAPM_UPDATE_STAT(w, power_checks);
1312 /* Check if one of our outputs is connected */
1313 list_for_each_entry(path, &w->sinks, list_source) {
1314 DAPM_UPDATE_STAT(w, neighbour_checks);
1319 if (path->connected &&
1320 !path->connected(path->source, path->sink))
1326 if (dapm_widget_power_check(path->sink))
1333 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1338 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1339 struct snd_soc_dapm_widget *b,
1347 sort = dapm_down_seq;
1349 if (sort[a->id] != sort[b->id])
1350 return sort[a->id] - sort[b->id];
1351 if (a->subseq != b->subseq) {
1353 return a->subseq - b->subseq;
1355 return b->subseq - a->subseq;
1357 if (a->reg != b->reg)
1358 return a->reg - b->reg;
1359 if (a->dapm != b->dapm)
1360 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1365 /* Insert a widget in order into a DAPM power sequence. */
1366 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1367 struct list_head *list,
1370 struct snd_soc_dapm_widget *w;
1372 list_for_each_entry(w, list, power_list)
1373 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1374 list_add_tail(&new_widget->power_list, &w->power_list);
1378 list_add_tail(&new_widget->power_list, list);
1381 static void dapm_seq_check_event(struct snd_soc_card *card,
1382 struct snd_soc_dapm_widget *w, int event)
1384 const char *ev_name;
1388 case SND_SOC_DAPM_PRE_PMU:
1389 ev_name = "PRE_PMU";
1392 case SND_SOC_DAPM_POST_PMU:
1393 ev_name = "POST_PMU";
1396 case SND_SOC_DAPM_PRE_PMD:
1397 ev_name = "PRE_PMD";
1400 case SND_SOC_DAPM_POST_PMD:
1401 ev_name = "POST_PMD";
1404 case SND_SOC_DAPM_WILL_PMU:
1405 ev_name = "WILL_PMU";
1408 case SND_SOC_DAPM_WILL_PMD:
1409 ev_name = "WILL_PMD";
1417 if (w->new_power != power)
1420 if (w->event && (w->event_flags & event)) {
1421 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1423 trace_snd_soc_dapm_widget_event_start(w, event);
1424 ret = w->event(w, NULL, event);
1425 trace_snd_soc_dapm_widget_event_done(w, event);
1427 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1428 ev_name, w->name, ret);
1432 /* Apply the coalesced changes from a DAPM sequence */
1433 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1434 struct list_head *pending)
1436 struct snd_soc_dapm_widget *w;
1438 unsigned int value = 0;
1439 unsigned int mask = 0;
1441 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1444 list_for_each_entry(w, pending, power_list) {
1445 BUG_ON(reg != w->reg);
1446 w->power = w->new_power;
1448 mask |= w->mask << w->shift;
1450 value |= w->on_val << w->shift;
1452 value |= w->off_val << w->shift;
1454 pop_dbg(w->dapm->dev, card->pop_time,
1455 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1456 w->name, reg, value, mask);
1458 /* Check for events */
1459 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1460 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1464 /* Any widget will do, they should all be updating the
1467 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1470 pop_dbg(w->dapm->dev, card->pop_time,
1471 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1472 value, mask, reg, card->pop_time);
1473 pop_wait(card->pop_time);
1474 soc_widget_update_bits_locked(w, reg, mask, value);
1477 list_for_each_entry(w, pending, power_list) {
1478 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1479 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1483 /* Apply a DAPM power sequence.
1485 * We walk over a pre-sorted list of widgets to apply power to. In
1486 * order to minimise the number of writes to the device required
1487 * multiple widgets will be updated in a single write where possible.
1488 * Currently anything that requires more than a single write is not
1491 static void dapm_seq_run(struct snd_soc_card *card,
1492 struct list_head *list, int event, bool power_up)
1494 struct snd_soc_dapm_widget *w, *n;
1497 int cur_subseq = -1;
1498 int cur_reg = SND_SOC_NOPM;
1499 struct snd_soc_dapm_context *cur_dapm = NULL;
1506 sort = dapm_down_seq;
1508 list_for_each_entry_safe(w, n, list, power_list) {
1511 /* Do we need to apply any queued changes? */
1512 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1513 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1514 if (!list_empty(&pending))
1515 dapm_seq_run_coalesced(card, &pending);
1517 if (cur_dapm && cur_dapm->seq_notifier) {
1518 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1519 if (sort[i] == cur_sort)
1520 cur_dapm->seq_notifier(cur_dapm,
1525 INIT_LIST_HEAD(&pending);
1527 cur_subseq = INT_MIN;
1528 cur_reg = SND_SOC_NOPM;
1533 case snd_soc_dapm_pre:
1535 list_for_each_entry_safe_continue(w, n, list,
1538 if (event == SND_SOC_DAPM_STREAM_START)
1540 NULL, SND_SOC_DAPM_PRE_PMU);
1541 else if (event == SND_SOC_DAPM_STREAM_STOP)
1543 NULL, SND_SOC_DAPM_PRE_PMD);
1546 case snd_soc_dapm_post:
1548 list_for_each_entry_safe_continue(w, n, list,
1551 if (event == SND_SOC_DAPM_STREAM_START)
1553 NULL, SND_SOC_DAPM_POST_PMU);
1554 else if (event == SND_SOC_DAPM_STREAM_STOP)
1556 NULL, SND_SOC_DAPM_POST_PMD);
1560 /* Queue it up for application */
1561 cur_sort = sort[w->id];
1562 cur_subseq = w->subseq;
1565 list_move(&w->power_list, &pending);
1570 dev_err(w->dapm->dev,
1571 "ASoC: Failed to apply widget power: %d\n", ret);
1574 if (!list_empty(&pending))
1575 dapm_seq_run_coalesced(card, &pending);
1577 if (cur_dapm && cur_dapm->seq_notifier) {
1578 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1579 if (sort[i] == cur_sort)
1580 cur_dapm->seq_notifier(cur_dapm,
1585 static void dapm_widget_update(struct snd_soc_card *card)
1587 struct snd_soc_dapm_update *update = card->update;
1588 struct snd_soc_dapm_widget_list *wlist;
1589 struct snd_soc_dapm_widget *w = NULL;
1593 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1596 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1598 for (wi = 0; wi < wlist->num_widgets; wi++) {
1599 w = wlist->widgets[wi];
1601 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1602 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1604 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1612 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1615 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1618 for (wi = 0; wi < wlist->num_widgets; wi++) {
1619 w = wlist->widgets[wi];
1621 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1622 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1624 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1630 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1631 * they're changing state.
1633 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1635 struct snd_soc_dapm_context *d = data;
1638 /* If we're off and we're not supposed to be go into STANDBY */
1639 if (d->bias_level == SND_SOC_BIAS_OFF &&
1640 d->target_bias_level != SND_SOC_BIAS_OFF) {
1642 pm_runtime_get_sync(d->dev);
1644 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1647 "ASoC: Failed to turn on bias: %d\n", ret);
1650 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1651 if (d->bias_level != d->target_bias_level) {
1652 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1655 "ASoC: Failed to prepare bias: %d\n", ret);
1659 /* Async callback run prior to DAPM sequences - brings to their final
1662 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1664 struct snd_soc_dapm_context *d = data;
1667 /* If we just powered the last thing off drop to standby bias */
1668 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1669 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1670 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1671 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1673 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1677 /* If we're in standby and can support bias off then do that */
1678 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1679 d->target_bias_level == SND_SOC_BIAS_OFF) {
1680 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1682 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1686 pm_runtime_put(d->dev);
1689 /* If we just powered up then move to active bias */
1690 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1691 d->target_bias_level == SND_SOC_BIAS_ON) {
1692 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1694 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1699 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1700 bool power, bool connect)
1702 /* If a connection is being made or broken then that update
1703 * will have marked the peer dirty, otherwise the widgets are
1704 * not connected and this update has no impact. */
1708 /* If the peer is already in the state we're moving to then we
1709 * won't have an impact on it. */
1710 if (power != peer->power)
1711 dapm_mark_dirty(peer, "peer state change");
1714 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1715 struct list_head *up_list,
1716 struct list_head *down_list)
1718 struct snd_soc_dapm_path *path;
1720 if (w->power == power)
1723 trace_snd_soc_dapm_widget_power(w, power);
1725 /* If we changed our power state perhaps our neigbours changed
1728 list_for_each_entry(path, &w->sources, list_sink) {
1730 dapm_widget_set_peer_power(path->source, power,
1735 case snd_soc_dapm_supply:
1736 case snd_soc_dapm_regulator_supply:
1737 case snd_soc_dapm_clock_supply:
1738 case snd_soc_dapm_kcontrol:
1739 /* Supplies can't affect their outputs, only their inputs */
1742 list_for_each_entry(path, &w->sinks, list_source) {
1744 dapm_widget_set_peer_power(path->sink, power,
1752 dapm_seq_insert(w, up_list, true);
1754 dapm_seq_insert(w, down_list, false);
1757 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1758 struct list_head *up_list,
1759 struct list_head *down_list)
1764 case snd_soc_dapm_pre:
1765 dapm_seq_insert(w, down_list, false);
1767 case snd_soc_dapm_post:
1768 dapm_seq_insert(w, up_list, true);
1772 power = dapm_widget_power_check(w);
1774 dapm_widget_set_power(w, power, up_list, down_list);
1780 * Scan each dapm widget for complete audio path.
1781 * A complete path is a route that has valid endpoints i.e.:-
1783 * o DAC to output pin.
1784 * o Input Pin to ADC.
1785 * o Input pin to Output pin (bypass, sidetone)
1786 * o DAC to ADC (loopback).
1788 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1790 struct snd_soc_dapm_widget *w;
1791 struct snd_soc_dapm_context *d;
1793 LIST_HEAD(down_list);
1794 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1795 enum snd_soc_bias_level bias;
1797 trace_snd_soc_dapm_start(card);
1799 list_for_each_entry(d, &card->dapm_list, list) {
1800 if (d->idle_bias_off)
1801 d->target_bias_level = SND_SOC_BIAS_OFF;
1803 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1808 /* Check which widgets we need to power and store them in
1809 * lists indicating if they should be powered up or down. We
1810 * only check widgets that have been flagged as dirty but note
1811 * that new widgets may be added to the dirty list while we
1814 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1815 dapm_power_one_widget(w, &up_list, &down_list);
1818 list_for_each_entry(w, &card->widgets, list) {
1820 case snd_soc_dapm_pre:
1821 case snd_soc_dapm_post:
1822 /* These widgets always need to be powered */
1825 list_del_init(&w->dirty);
1832 /* Supplies and micbiases only bring the
1833 * context up to STANDBY as unless something
1834 * else is active and passing audio they
1835 * generally don't require full power. Signal
1836 * generators are virtual pins and have no
1837 * power impact themselves.
1840 case snd_soc_dapm_siggen:
1842 case snd_soc_dapm_supply:
1843 case snd_soc_dapm_regulator_supply:
1844 case snd_soc_dapm_clock_supply:
1845 case snd_soc_dapm_micbias:
1846 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1847 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1850 d->target_bias_level = SND_SOC_BIAS_ON;
1857 /* Force all contexts in the card to the same bias state if
1858 * they're not ground referenced.
1860 bias = SND_SOC_BIAS_OFF;
1861 list_for_each_entry(d, &card->dapm_list, list)
1862 if (d->target_bias_level > bias)
1863 bias = d->target_bias_level;
1864 list_for_each_entry(d, &card->dapm_list, list)
1865 if (!d->idle_bias_off)
1866 d->target_bias_level = bias;
1868 trace_snd_soc_dapm_walk_done(card);
1870 /* Run all the bias changes in parallel */
1871 list_for_each_entry(d, &card->dapm_list, list)
1872 async_schedule_domain(dapm_pre_sequence_async, d,
1874 async_synchronize_full_domain(&async_domain);
1876 list_for_each_entry(w, &down_list, power_list) {
1877 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1880 list_for_each_entry(w, &up_list, power_list) {
1881 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1884 /* Power down widgets first; try to avoid amplifying pops. */
1885 dapm_seq_run(card, &down_list, event, false);
1887 dapm_widget_update(card);
1890 dapm_seq_run(card, &up_list, event, true);
1892 /* Run all the bias changes in parallel */
1893 list_for_each_entry(d, &card->dapm_list, list)
1894 async_schedule_domain(dapm_post_sequence_async, d,
1896 async_synchronize_full_domain(&async_domain);
1898 /* do we need to notify any clients that DAPM event is complete */
1899 list_for_each_entry(d, &card->dapm_list, list) {
1900 if (d->stream_event)
1901 d->stream_event(d, event);
1904 pop_dbg(card->dev, card->pop_time,
1905 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1906 pop_wait(card->pop_time);
1908 trace_snd_soc_dapm_done(card);
1913 #ifdef CONFIG_DEBUG_FS
1914 static ssize_t dapm_widget_power_read_file(struct file *file,
1915 char __user *user_buf,
1916 size_t count, loff_t *ppos)
1918 struct snd_soc_dapm_widget *w = file->private_data;
1922 struct snd_soc_dapm_path *p = NULL;
1924 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1928 in = is_connected_input_ep(w, NULL);
1929 dapm_clear_walk_input(w->dapm, &w->sources);
1930 out = is_connected_output_ep(w, NULL);
1931 dapm_clear_walk_output(w->dapm, &w->sinks);
1933 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1934 w->name, w->power ? "On" : "Off",
1935 w->force ? " (forced)" : "", in, out);
1938 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1939 " - R%d(0x%x) mask 0x%x",
1940 w->reg, w->reg, w->mask << w->shift);
1942 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1945 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1947 w->active ? "active" : "inactive");
1949 list_for_each_entry(p, &w->sources, list_sink) {
1950 if (p->connected && !p->connected(w, p->sink))
1954 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1955 " in \"%s\" \"%s\"\n",
1956 p->name ? p->name : "static",
1959 list_for_each_entry(p, &w->sinks, list_source) {
1960 if (p->connected && !p->connected(w, p->sink))
1964 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1965 " out \"%s\" \"%s\"\n",
1966 p->name ? p->name : "static",
1970 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1976 static const struct file_operations dapm_widget_power_fops = {
1977 .open = simple_open,
1978 .read = dapm_widget_power_read_file,
1979 .llseek = default_llseek,
1982 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1983 size_t count, loff_t *ppos)
1985 struct snd_soc_dapm_context *dapm = file->private_data;
1988 switch (dapm->bias_level) {
1989 case SND_SOC_BIAS_ON:
1992 case SND_SOC_BIAS_PREPARE:
1993 level = "Prepare\n";
1995 case SND_SOC_BIAS_STANDBY:
1996 level = "Standby\n";
1998 case SND_SOC_BIAS_OFF:
2003 level = "Unknown\n";
2007 return simple_read_from_buffer(user_buf, count, ppos, level,
2011 static const struct file_operations dapm_bias_fops = {
2012 .open = simple_open,
2013 .read = dapm_bias_read_file,
2014 .llseek = default_llseek,
2017 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2018 struct dentry *parent)
2022 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2024 if (!dapm->debugfs_dapm) {
2026 "ASoC: Failed to create DAPM debugfs directory\n");
2030 d = debugfs_create_file("bias_level", 0444,
2031 dapm->debugfs_dapm, dapm,
2035 "ASoC: Failed to create bias level debugfs file\n");
2038 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2040 struct snd_soc_dapm_context *dapm = w->dapm;
2043 if (!dapm->debugfs_dapm || !w->name)
2046 d = debugfs_create_file(w->name, 0444,
2047 dapm->debugfs_dapm, w,
2048 &dapm_widget_power_fops);
2050 dev_warn(w->dapm->dev,
2051 "ASoC: Failed to create %s debugfs file\n",
2055 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2057 debugfs_remove_recursive(dapm->debugfs_dapm);
2061 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2062 struct dentry *parent)
2066 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2070 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2076 /* test and update the power status of a mux widget */
2077 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2078 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2080 struct snd_soc_dapm_path *path;
2083 /* find dapm widget path assoc with kcontrol */
2084 dapm_kcontrol_for_each_path(path, kcontrol) {
2085 if (!path->name || !e->texts[mux])
2089 /* we now need to match the string in the enum to the path */
2090 if (!(strcmp(path->name, e->texts[mux]))) {
2091 path->connect = 1; /* new connection */
2092 dapm_mark_dirty(path->source, "mux connection");
2095 dapm_mark_dirty(path->source,
2096 "mux disconnection");
2097 path->connect = 0; /* old connection must be powered down */
2099 dapm_mark_dirty(path->sink, "mux change");
2103 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2108 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2109 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2110 struct snd_soc_dapm_update *update)
2112 struct snd_soc_card *card = dapm->card;
2115 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2116 card->update = update;
2117 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2118 card->update = NULL;
2119 mutex_unlock(&card->dapm_mutex);
2121 soc_dpcm_runtime_update(card);
2124 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2126 /* test and update the power status of a mixer or switch widget */
2127 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2128 struct snd_kcontrol *kcontrol, int connect)
2130 struct snd_soc_dapm_path *path;
2133 /* find dapm widget path assoc with kcontrol */
2134 dapm_kcontrol_for_each_path(path, kcontrol) {
2136 path->connect = connect;
2137 dapm_mark_dirty(path->source, "mixer connection");
2138 dapm_mark_dirty(path->sink, "mixer update");
2142 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2147 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2148 struct snd_kcontrol *kcontrol, int connect,
2149 struct snd_soc_dapm_update *update)
2151 struct snd_soc_card *card = dapm->card;
2154 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2155 card->update = update;
2156 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2157 card->update = NULL;
2158 mutex_unlock(&card->dapm_mutex);
2160 soc_dpcm_runtime_update(card);
2163 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2165 /* show dapm widget status in sys fs */
2166 static ssize_t dapm_widget_show(struct device *dev,
2167 struct device_attribute *attr, char *buf)
2169 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2170 struct snd_soc_codec *codec =rtd->codec;
2171 struct snd_soc_dapm_widget *w;
2173 char *state = "not set";
2175 list_for_each_entry(w, &codec->card->widgets, list) {
2176 if (w->dapm != &codec->dapm)
2179 /* only display widgets that burnm power */
2181 case snd_soc_dapm_hp:
2182 case snd_soc_dapm_mic:
2183 case snd_soc_dapm_spk:
2184 case snd_soc_dapm_line:
2185 case snd_soc_dapm_micbias:
2186 case snd_soc_dapm_dac:
2187 case snd_soc_dapm_adc:
2188 case snd_soc_dapm_pga:
2189 case snd_soc_dapm_out_drv:
2190 case snd_soc_dapm_mixer:
2191 case snd_soc_dapm_mixer_named_ctl:
2192 case snd_soc_dapm_supply:
2193 case snd_soc_dapm_regulator_supply:
2194 case snd_soc_dapm_clock_supply:
2196 count += sprintf(buf + count, "%s: %s\n",
2197 w->name, w->power ? "On":"Off");
2204 switch (codec->dapm.bias_level) {
2205 case SND_SOC_BIAS_ON:
2208 case SND_SOC_BIAS_PREPARE:
2211 case SND_SOC_BIAS_STANDBY:
2214 case SND_SOC_BIAS_OFF:
2218 count += sprintf(buf + count, "PM State: %s\n", state);
2223 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2225 int snd_soc_dapm_sys_add(struct device *dev)
2227 return device_create_file(dev, &dev_attr_dapm_widget);
2230 static void snd_soc_dapm_sys_remove(struct device *dev)
2232 device_remove_file(dev, &dev_attr_dapm_widget);
2235 static void dapm_free_path(struct snd_soc_dapm_path *path)
2237 list_del(&path->list_sink);
2238 list_del(&path->list_source);
2239 list_del(&path->list_kcontrol);
2240 list_del(&path->list);
2244 /* free all dapm widgets and resources */
2245 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2247 struct snd_soc_dapm_widget *w, *next_w;
2248 struct snd_soc_dapm_path *p, *next_p;
2250 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2251 if (w->dapm != dapm)
2255 * remove source and sink paths associated to this widget.
2256 * While removing the path, remove reference to it from both
2257 * source and sink widgets so that path is removed only once.
2259 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2262 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2265 kfree(w->kcontrols);
2271 static struct snd_soc_dapm_widget *dapm_find_widget(
2272 struct snd_soc_dapm_context *dapm, const char *pin,
2273 bool search_other_contexts)
2275 struct snd_soc_dapm_widget *w;
2276 struct snd_soc_dapm_widget *fallback = NULL;
2278 list_for_each_entry(w, &dapm->card->widgets, list) {
2279 if (!strcmp(w->name, pin)) {
2280 if (w->dapm == dapm)
2287 if (search_other_contexts)
2293 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2294 const char *pin, int status)
2296 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2299 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2303 if (w->connected != status)
2304 dapm_mark_dirty(w, "pin configuration");
2306 w->connected = status;
2314 * snd_soc_dapm_sync - scan and power dapm paths
2315 * @dapm: DAPM context
2317 * Walks all dapm audio paths and powers widgets according to their
2318 * stream or path usage.
2320 * Returns 0 for success.
2322 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2327 * Suppress early reports (eg, jacks syncing their state) to avoid
2328 * silly DAPM runs during card startup.
2330 if (!dapm->card || !dapm->card->instantiated)
2333 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2334 ret = dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2335 mutex_unlock(&dapm->card->dapm_mutex);
2338 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2340 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2341 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2342 const char *control,
2343 int (*connected)(struct snd_soc_dapm_widget *source,
2344 struct snd_soc_dapm_widget *sink))
2346 struct snd_soc_dapm_path *path;
2349 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2353 path->source = wsource;
2355 path->connected = connected;
2356 INIT_LIST_HEAD(&path->list);
2357 INIT_LIST_HEAD(&path->list_kcontrol);
2358 INIT_LIST_HEAD(&path->list_source);
2359 INIT_LIST_HEAD(&path->list_sink);
2361 /* check for external widgets */
2362 if (wsink->id == snd_soc_dapm_input) {
2363 if (wsource->id == snd_soc_dapm_micbias ||
2364 wsource->id == snd_soc_dapm_mic ||
2365 wsource->id == snd_soc_dapm_line ||
2366 wsource->id == snd_soc_dapm_output)
2369 if (wsource->id == snd_soc_dapm_output) {
2370 if (wsink->id == snd_soc_dapm_spk ||
2371 wsink->id == snd_soc_dapm_hp ||
2372 wsink->id == snd_soc_dapm_line ||
2373 wsink->id == snd_soc_dapm_input)
2377 dapm_mark_dirty(wsource, "Route added");
2378 dapm_mark_dirty(wsink, "Route added");
2380 /* connect static paths */
2381 if (control == NULL) {
2382 list_add(&path->list, &dapm->card->paths);
2383 list_add(&path->list_sink, &wsink->sources);
2384 list_add(&path->list_source, &wsource->sinks);
2389 /* connect dynamic paths */
2390 switch (wsink->id) {
2391 case snd_soc_dapm_adc:
2392 case snd_soc_dapm_dac:
2393 case snd_soc_dapm_pga:
2394 case snd_soc_dapm_out_drv:
2395 case snd_soc_dapm_input:
2396 case snd_soc_dapm_output:
2397 case snd_soc_dapm_siggen:
2398 case snd_soc_dapm_micbias:
2399 case snd_soc_dapm_vmid:
2400 case snd_soc_dapm_pre:
2401 case snd_soc_dapm_post:
2402 case snd_soc_dapm_supply:
2403 case snd_soc_dapm_regulator_supply:
2404 case snd_soc_dapm_clock_supply:
2405 case snd_soc_dapm_aif_in:
2406 case snd_soc_dapm_aif_out:
2407 case snd_soc_dapm_dai_in:
2408 case snd_soc_dapm_dai_out:
2409 case snd_soc_dapm_dai_link:
2410 case snd_soc_dapm_kcontrol:
2411 list_add(&path->list, &dapm->card->paths);
2412 list_add(&path->list_sink, &wsink->sources);
2413 list_add(&path->list_source, &wsource->sinks);
2416 case snd_soc_dapm_mux:
2417 case snd_soc_dapm_virt_mux:
2418 case snd_soc_dapm_value_mux:
2419 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2420 &wsink->kcontrol_news[0]);
2424 case snd_soc_dapm_switch:
2425 case snd_soc_dapm_mixer:
2426 case snd_soc_dapm_mixer_named_ctl:
2427 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2431 case snd_soc_dapm_hp:
2432 case snd_soc_dapm_mic:
2433 case snd_soc_dapm_line:
2434 case snd_soc_dapm_spk:
2435 list_add(&path->list, &dapm->card->paths);
2436 list_add(&path->list_sink, &wsink->sources);
2437 list_add(&path->list_source, &wsource->sinks);
2448 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2449 const struct snd_soc_dapm_route *route)
2451 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2452 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2455 char prefixed_sink[80];
2456 char prefixed_source[80];
2459 if (dapm->codec && dapm->codec->name_prefix) {
2460 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2461 dapm->codec->name_prefix, route->sink);
2462 sink = prefixed_sink;
2463 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2464 dapm->codec->name_prefix, route->source);
2465 source = prefixed_source;
2468 source = route->source;
2472 * find src and dest widgets over all widgets but favor a widget from
2473 * current DAPM context
2475 list_for_each_entry(w, &dapm->card->widgets, list) {
2476 if (!wsink && !(strcmp(w->name, sink))) {
2478 if (w->dapm == dapm)
2482 if (!wsource && !(strcmp(w->name, source))) {
2484 if (w->dapm == dapm)
2488 /* use widget from another DAPM context if not found from this */
2494 if (wsource == NULL) {
2495 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2499 if (wsink == NULL) {
2500 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2505 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2512 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2513 source, route->control, sink);
2517 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2518 const struct snd_soc_dapm_route *route)
2520 struct snd_soc_dapm_path *path, *p;
2523 char prefixed_sink[80];
2524 char prefixed_source[80];
2526 if (route->control) {
2528 "ASoC: Removal of routes with controls not supported\n");
2532 if (dapm->codec && dapm->codec->name_prefix) {
2533 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2534 dapm->codec->name_prefix, route->sink);
2535 sink = prefixed_sink;
2536 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2537 dapm->codec->name_prefix, route->source);
2538 source = prefixed_source;
2541 source = route->source;
2545 list_for_each_entry(p, &dapm->card->paths, list) {
2546 if (strcmp(p->source->name, source) != 0)
2548 if (strcmp(p->sink->name, sink) != 0)
2555 dapm_mark_dirty(path->source, "Route removed");
2556 dapm_mark_dirty(path->sink, "Route removed");
2558 dapm_free_path(path);
2560 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2568 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2569 * @dapm: DAPM context
2570 * @route: audio routes
2571 * @num: number of routes
2573 * Connects 2 dapm widgets together via a named audio path. The sink is
2574 * the widget receiving the audio signal, whilst the source is the sender
2575 * of the audio signal.
2577 * Returns 0 for success else error. On error all resources can be freed
2578 * with a call to snd_soc_card_free().
2580 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2581 const struct snd_soc_dapm_route *route, int num)
2585 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2586 for (i = 0; i < num; i++) {
2587 r = snd_soc_dapm_add_route(dapm, route);
2589 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2591 route->control ? route->control : "direct",
2597 mutex_unlock(&dapm->card->dapm_mutex);
2601 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2604 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2605 * @dapm: DAPM context
2606 * @route: audio routes
2607 * @num: number of routes
2609 * Removes routes from the DAPM context.
2611 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2612 const struct snd_soc_dapm_route *route, int num)
2616 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2617 for (i = 0; i < num; i++) {
2618 snd_soc_dapm_del_route(dapm, route);
2621 mutex_unlock(&dapm->card->dapm_mutex);
2625 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2627 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2628 const struct snd_soc_dapm_route *route)
2630 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2633 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2636 struct snd_soc_dapm_path *path;
2640 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2646 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2651 if (route->control || route->connected)
2652 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2653 route->source, route->sink);
2655 list_for_each_entry(path, &source->sinks, list_source) {
2656 if (path->sink == sink) {
2663 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2664 route->source, route->sink);
2666 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2667 count, route->source, route->sink);
2673 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2674 * @dapm: DAPM context
2675 * @route: audio routes
2676 * @num: number of routes
2678 * Mark existing routes matching those specified in the passed array
2679 * as being weak, meaning that they are ignored for the purpose of
2680 * power decisions. The main intended use case is for sidetone paths
2681 * which couple audio between other independent paths if they are both
2682 * active in order to make the combination work better at the user
2683 * level but which aren't intended to be "used".
2685 * Note that CODEC drivers should not use this as sidetone type paths
2686 * can frequently also be used as bypass paths.
2688 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2689 const struct snd_soc_dapm_route *route, int num)
2694 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2695 for (i = 0; i < num; i++) {
2696 err = snd_soc_dapm_weak_route(dapm, route);
2701 mutex_unlock(&dapm->card->dapm_mutex);
2705 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2708 * snd_soc_dapm_new_widgets - add new dapm widgets
2709 * @dapm: DAPM context
2711 * Checks the codec for any new dapm widgets and creates them if found.
2713 * Returns 0 for success.
2715 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2717 struct snd_soc_card *card = dapm->card;
2718 struct snd_soc_dapm_widget *w;
2721 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2723 list_for_each_entry(w, &card->widgets, list)
2728 if (w->num_kcontrols) {
2729 w->kcontrols = kzalloc(w->num_kcontrols *
2730 sizeof(struct snd_kcontrol *),
2732 if (!w->kcontrols) {
2733 mutex_unlock(&card->dapm_mutex);
2739 case snd_soc_dapm_switch:
2740 case snd_soc_dapm_mixer:
2741 case snd_soc_dapm_mixer_named_ctl:
2744 case snd_soc_dapm_mux:
2745 case snd_soc_dapm_virt_mux:
2746 case snd_soc_dapm_value_mux:
2749 case snd_soc_dapm_pga:
2750 case snd_soc_dapm_out_drv:
2757 /* Read the initial power state from the device */
2759 val = soc_widget_read(w, w->reg) >> w->shift;
2761 if (val == w->on_val)
2767 dapm_mark_dirty(w, "new widget");
2768 dapm_debugfs_add_widget(w);
2771 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2772 mutex_unlock(&card->dapm_mutex);
2775 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2778 * snd_soc_dapm_get_volsw - dapm mixer get callback
2779 * @kcontrol: mixer control
2780 * @ucontrol: control element information
2782 * Callback to get the value of a dapm mixer control.
2784 * Returns 0 for success.
2786 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2787 struct snd_ctl_elem_value *ucontrol)
2789 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2790 struct snd_soc_card *card = codec->card;
2791 struct soc_mixer_control *mc =
2792 (struct soc_mixer_control *)kcontrol->private_value;
2793 unsigned int reg = mc->reg;
2794 unsigned int shift = mc->shift;
2796 unsigned int mask = (1 << fls(max)) - 1;
2797 unsigned int invert = mc->invert;
2800 if (snd_soc_volsw_is_stereo(mc))
2801 dev_warn(codec->dapm.dev,
2802 "ASoC: Control '%s' is stereo, which is not supported\n",
2805 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2806 if (dapm_kcontrol_is_powered(kcontrol))
2807 val = (snd_soc_read(codec, reg) >> shift) & mask;
2809 val = dapm_kcontrol_get_value(kcontrol);
2810 mutex_unlock(&card->dapm_mutex);
2813 ucontrol->value.integer.value[0] = max - val;
2815 ucontrol->value.integer.value[0] = val;
2819 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2822 * snd_soc_dapm_put_volsw - dapm mixer set callback
2823 * @kcontrol: mixer control
2824 * @ucontrol: control element information
2826 * Callback to set the value of a dapm mixer control.
2828 * Returns 0 for success.
2830 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_value *ucontrol)
2833 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2834 struct snd_soc_card *card = codec->card;
2835 struct soc_mixer_control *mc =
2836 (struct soc_mixer_control *)kcontrol->private_value;
2837 unsigned int reg = mc->reg;
2838 unsigned int shift = mc->shift;
2840 unsigned int mask = (1 << fls(max)) - 1;
2841 unsigned int invert = mc->invert;
2843 int connect, change;
2844 struct snd_soc_dapm_update update;
2846 if (snd_soc_volsw_is_stereo(mc))
2847 dev_warn(codec->dapm.dev,
2848 "ASoC: Control '%s' is stereo, which is not supported\n",
2851 val = (ucontrol->value.integer.value[0] & mask);
2857 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2859 dapm_kcontrol_set_value(kcontrol, val);
2861 mask = mask << shift;
2864 change = snd_soc_test_bits(codec, reg, mask, val);
2866 update.kcontrol = kcontrol;
2871 card->update = &update;
2873 soc_dapm_mixer_update_power(card, kcontrol, connect);
2875 card->update = NULL;
2878 mutex_unlock(&card->dapm_mutex);
2881 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2884 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2885 * @kcontrol: mixer control
2886 * @ucontrol: control element information
2888 * Callback to get the value of a dapm enumerated double mixer control.
2890 * Returns 0 for success.
2892 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2893 struct snd_ctl_elem_value *ucontrol)
2895 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2896 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2899 val = snd_soc_read(codec, e->reg);
2900 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2901 if (e->shift_l != e->shift_r)
2902 ucontrol->value.enumerated.item[1] =
2903 (val >> e->shift_r) & e->mask;
2907 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2910 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2911 * @kcontrol: mixer control
2912 * @ucontrol: control element information
2914 * Callback to set the value of a dapm enumerated double mixer control.
2916 * Returns 0 for success.
2918 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
2921 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2922 struct snd_soc_card *card = codec->card;
2923 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2924 unsigned int val, mux, change;
2926 struct snd_soc_dapm_update update;
2928 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2930 mux = ucontrol->value.enumerated.item[0];
2931 val = mux << e->shift_l;
2932 mask = e->mask << e->shift_l;
2933 if (e->shift_l != e->shift_r) {
2934 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2936 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2937 mask |= e->mask << e->shift_r;
2940 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2942 change = snd_soc_test_bits(codec, e->reg, mask, val);
2944 update.kcontrol = kcontrol;
2945 update.reg = e->reg;
2948 card->update = &update;
2950 soc_dapm_mux_update_power(card, kcontrol, mux, e);
2952 card->update = NULL;
2955 mutex_unlock(&card->dapm_mutex);
2958 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2961 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2962 * @kcontrol: mixer control
2963 * @ucontrol: control element information
2965 * Returns 0 for success.
2967 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2968 struct snd_ctl_elem_value *ucontrol)
2970 ucontrol->value.enumerated.item[0] = dapm_kcontrol_get_value(kcontrol);
2973 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2976 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2977 * @kcontrol: mixer control
2978 * @ucontrol: control element information
2980 * Returns 0 for success.
2982 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2983 struct snd_ctl_elem_value *ucontrol)
2985 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
2986 struct snd_soc_card *card = codec->card;
2988 struct soc_enum *e =
2989 (struct soc_enum *)kcontrol->private_value;
2992 if (ucontrol->value.enumerated.item[0] >= e->max)
2995 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2997 value = ucontrol->value.enumerated.item[0];
2998 change = dapm_kcontrol_set_value(kcontrol, value);
3000 soc_dapm_mux_update_power(card, kcontrol, value, e);
3002 mutex_unlock(&card->dapm_mutex);
3005 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
3008 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
3010 * @kcontrol: mixer control
3011 * @ucontrol: control element information
3013 * Callback to get the value of a dapm semi enumerated double mixer control.
3015 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3016 * used for handling bitfield coded enumeration for example.
3018 * Returns 0 for success.
3020 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
3021 struct snd_ctl_elem_value *ucontrol)
3023 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3024 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3025 unsigned int reg_val, val, mux;
3027 reg_val = snd_soc_read(codec, e->reg);
3028 val = (reg_val >> e->shift_l) & e->mask;
3029 for (mux = 0; mux < e->max; mux++) {
3030 if (val == e->values[mux])
3033 ucontrol->value.enumerated.item[0] = mux;
3034 if (e->shift_l != e->shift_r) {
3035 val = (reg_val >> e->shift_r) & e->mask;
3036 for (mux = 0; mux < e->max; mux++) {
3037 if (val == e->values[mux])
3040 ucontrol->value.enumerated.item[1] = mux;
3045 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
3048 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
3050 * @kcontrol: mixer control
3051 * @ucontrol: control element information
3053 * Callback to set the value of a dapm semi enumerated double mixer control.
3055 * Semi enumerated mixer: the enumerated items are referred as values. Can be
3056 * used for handling bitfield coded enumeration for example.
3058 * Returns 0 for success.
3060 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
3061 struct snd_ctl_elem_value *ucontrol)
3063 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
3064 struct snd_soc_card *card = codec->card;
3065 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3066 unsigned int val, mux, change;
3068 struct snd_soc_dapm_update update;
3070 if (ucontrol->value.enumerated.item[0] > e->max - 1)
3072 mux = ucontrol->value.enumerated.item[0];
3073 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
3074 mask = e->mask << e->shift_l;
3075 if (e->shift_l != e->shift_r) {
3076 if (ucontrol->value.enumerated.item[1] > e->max - 1)
3078 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
3079 mask |= e->mask << e->shift_r;
3082 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3084 change = snd_soc_test_bits(codec, e->reg, mask, val);
3086 update.kcontrol = kcontrol;
3087 update.reg = e->reg;
3090 card->update = &update;
3092 soc_dapm_mux_update_power(card, kcontrol, mux, e);
3094 card->update = NULL;
3097 mutex_unlock(&card->dapm_mutex);
3100 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
3103 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3105 * @kcontrol: mixer control
3106 * @uinfo: control element information
3108 * Callback to provide information about a pin switch control.
3110 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_info *uinfo)
3113 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3115 uinfo->value.integer.min = 0;
3116 uinfo->value.integer.max = 1;
3120 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3123 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3125 * @kcontrol: mixer control
3128 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3129 struct snd_ctl_elem_value *ucontrol)
3131 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3132 const char *pin = (const char *)kcontrol->private_value;
3134 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3136 ucontrol->value.integer.value[0] =
3137 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3139 mutex_unlock(&card->dapm_mutex);
3143 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3146 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3148 * @kcontrol: mixer control
3151 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3152 struct snd_ctl_elem_value *ucontrol)
3154 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3155 const char *pin = (const char *)kcontrol->private_value;
3157 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3159 if (ucontrol->value.integer.value[0])
3160 snd_soc_dapm_enable_pin(&card->dapm, pin);
3162 snd_soc_dapm_disable_pin(&card->dapm, pin);
3164 mutex_unlock(&card->dapm_mutex);
3166 snd_soc_dapm_sync(&card->dapm);
3169 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3171 static struct snd_soc_dapm_widget *
3172 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3173 const struct snd_soc_dapm_widget *widget)
3175 struct snd_soc_dapm_widget *w;
3178 if ((w = dapm_cnew_widget(widget)) == NULL)
3182 case snd_soc_dapm_regulator_supply:
3183 w->regulator = devm_regulator_get(dapm->dev, w->name);
3184 if (IS_ERR(w->regulator)) {
3185 ret = PTR_ERR(w->regulator);
3186 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3191 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3192 ret = regulator_allow_bypass(w->regulator, true);
3194 dev_warn(w->dapm->dev,
3195 "ASoC: Failed to unbypass %s: %d\n",
3199 case snd_soc_dapm_clock_supply:
3200 #ifdef CONFIG_CLKDEV_LOOKUP
3201 w->clk = devm_clk_get(dapm->dev, w->name);
3202 if (IS_ERR(w->clk)) {
3203 ret = PTR_ERR(w->clk);
3204 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3216 if (dapm->codec && dapm->codec->name_prefix)
3217 w->name = kasprintf(GFP_KERNEL, "%s %s",
3218 dapm->codec->name_prefix, widget->name);
3220 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3222 if (w->name == NULL) {
3228 case snd_soc_dapm_switch:
3229 case snd_soc_dapm_mixer:
3230 case snd_soc_dapm_mixer_named_ctl:
3231 w->power_check = dapm_generic_check_power;
3233 case snd_soc_dapm_mux:
3234 case snd_soc_dapm_virt_mux:
3235 case snd_soc_dapm_value_mux:
3236 w->power_check = dapm_generic_check_power;
3238 case snd_soc_dapm_dai_out:
3239 w->power_check = dapm_adc_check_power;
3241 case snd_soc_dapm_dai_in:
3242 w->power_check = dapm_dac_check_power;
3244 case snd_soc_dapm_adc:
3245 case snd_soc_dapm_aif_out:
3246 case snd_soc_dapm_dac:
3247 case snd_soc_dapm_aif_in:
3248 case snd_soc_dapm_pga:
3249 case snd_soc_dapm_out_drv:
3250 case snd_soc_dapm_input:
3251 case snd_soc_dapm_output:
3252 case snd_soc_dapm_micbias:
3253 case snd_soc_dapm_spk:
3254 case snd_soc_dapm_hp:
3255 case snd_soc_dapm_mic:
3256 case snd_soc_dapm_line:
3257 case snd_soc_dapm_dai_link:
3258 w->power_check = dapm_generic_check_power;
3260 case snd_soc_dapm_supply:
3261 case snd_soc_dapm_regulator_supply:
3262 case snd_soc_dapm_clock_supply:
3263 case snd_soc_dapm_kcontrol:
3264 w->power_check = dapm_supply_check_power;
3267 w->power_check = dapm_always_on_check_power;
3272 w->codec = dapm->codec;
3273 w->platform = dapm->platform;
3274 INIT_LIST_HEAD(&w->sources);
3275 INIT_LIST_HEAD(&w->sinks);
3276 INIT_LIST_HEAD(&w->list);
3277 INIT_LIST_HEAD(&w->dirty);
3278 list_add(&w->list, &dapm->card->widgets);
3280 /* machine layer set ups unconnected pins and insertions */
3286 * snd_soc_dapm_new_controls - create new dapm controls
3287 * @dapm: DAPM context
3288 * @widget: widget array
3289 * @num: number of widgets
3291 * Creates new DAPM controls based upon the templates.
3293 * Returns 0 for success else error.
3295 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3296 const struct snd_soc_dapm_widget *widget,
3299 struct snd_soc_dapm_widget *w;
3303 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3304 for (i = 0; i < num; i++) {
3305 w = snd_soc_dapm_new_control(dapm, widget);
3308 "ASoC: Failed to create DAPM control %s\n",
3315 mutex_unlock(&dapm->card->dapm_mutex);
3318 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3320 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3321 struct snd_kcontrol *kcontrol, int event)
3323 struct snd_soc_dapm_path *source_p, *sink_p;
3324 struct snd_soc_dai *source, *sink;
3325 const struct snd_soc_pcm_stream *config = w->params;
3326 struct snd_pcm_substream substream;
3327 struct snd_pcm_hw_params *params = NULL;
3332 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3334 /* We only support a single source and sink, pick the first */
3335 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3337 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3340 BUG_ON(!source_p || !sink_p);
3341 BUG_ON(!sink_p->source || !source_p->sink);
3342 BUG_ON(!source_p->source || !sink_p->sink);
3344 source = source_p->source->priv;
3345 sink = sink_p->sink->priv;
3347 /* Be a little careful as we don't want to overflow the mask array */
3348 if (config->formats) {
3349 fmt = ffs(config->formats) - 1;
3351 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3356 /* Currently very limited parameter selection */
3357 params = kzalloc(sizeof(*params), GFP_KERNEL);
3362 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3364 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3366 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3369 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3370 = config->channels_min;
3371 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3372 = config->channels_max;
3374 memset(&substream, 0, sizeof(substream));
3377 case SND_SOC_DAPM_PRE_PMU:
3378 if (source->driver->ops && source->driver->ops->hw_params) {
3379 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3380 ret = source->driver->ops->hw_params(&substream,
3383 dev_err(source->dev,
3384 "ASoC: hw_params() failed: %d\n", ret);
3389 if (sink->driver->ops && sink->driver->ops->hw_params) {
3390 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3391 ret = sink->driver->ops->hw_params(&substream, params,
3395 "ASoC: hw_params() failed: %d\n", ret);
3401 case SND_SOC_DAPM_POST_PMU:
3402 ret = snd_soc_dai_digital_mute(sink, 0,
3403 SNDRV_PCM_STREAM_PLAYBACK);
3404 if (ret != 0 && ret != -ENOTSUPP)
3405 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3409 case SND_SOC_DAPM_PRE_PMD:
3410 ret = snd_soc_dai_digital_mute(sink, 1,
3411 SNDRV_PCM_STREAM_PLAYBACK);
3412 if (ret != 0 && ret != -ENOTSUPP)
3413 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3427 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3428 const struct snd_soc_pcm_stream *params,
3429 struct snd_soc_dapm_widget *source,
3430 struct snd_soc_dapm_widget *sink)
3432 struct snd_soc_dapm_route routes[2];
3433 struct snd_soc_dapm_widget template;
3434 struct snd_soc_dapm_widget *w;
3438 len = strlen(source->name) + strlen(sink->name) + 2;
3439 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3442 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3444 memset(&template, 0, sizeof(template));
3445 template.reg = SND_SOC_NOPM;
3446 template.id = snd_soc_dapm_dai_link;
3447 template.name = link_name;
3448 template.event = snd_soc_dai_link_event;
3449 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3450 SND_SOC_DAPM_PRE_PMD;
3452 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3454 w = snd_soc_dapm_new_control(&card->dapm, &template);
3456 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3463 memset(&routes, 0, sizeof(routes));
3465 routes[0].source = source->name;
3466 routes[0].sink = link_name;
3467 routes[1].source = link_name;
3468 routes[1].sink = sink->name;
3470 return snd_soc_dapm_add_routes(&card->dapm, routes,
3471 ARRAY_SIZE(routes));
3474 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3475 struct snd_soc_dai *dai)
3477 struct snd_soc_dapm_widget template;
3478 struct snd_soc_dapm_widget *w;
3480 WARN_ON(dapm->dev != dai->dev);
3482 memset(&template, 0, sizeof(template));
3483 template.reg = SND_SOC_NOPM;
3485 if (dai->driver->playback.stream_name) {
3486 template.id = snd_soc_dapm_dai_in;
3487 template.name = dai->driver->playback.stream_name;
3488 template.sname = dai->driver->playback.stream_name;
3490 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3493 w = snd_soc_dapm_new_control(dapm, &template);
3495 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3496 dai->driver->playback.stream_name);
3500 dai->playback_widget = w;
3503 if (dai->driver->capture.stream_name) {
3504 template.id = snd_soc_dapm_dai_out;
3505 template.name = dai->driver->capture.stream_name;
3506 template.sname = dai->driver->capture.stream_name;
3508 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3511 w = snd_soc_dapm_new_control(dapm, &template);
3513 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3514 dai->driver->capture.stream_name);
3518 dai->capture_widget = w;
3524 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3526 struct snd_soc_dapm_widget *dai_w, *w;
3527 struct snd_soc_dai *dai;
3529 /* For each DAI widget... */
3530 list_for_each_entry(dai_w, &card->widgets, list) {
3531 switch (dai_w->id) {
3532 case snd_soc_dapm_dai_in:
3533 case snd_soc_dapm_dai_out:
3541 /* ...find all widgets with the same stream and link them */
3542 list_for_each_entry(w, &card->widgets, list) {
3543 if (w->dapm != dai_w->dapm)
3547 case snd_soc_dapm_dai_in:
3548 case snd_soc_dapm_dai_out:
3554 if (!w->sname || !strstr(w->sname, dai_w->name))
3557 if (dai->driver->playback.stream_name &&
3559 dai->driver->playback.stream_name)) {
3560 dev_dbg(dai->dev, "%s -> %s\n",
3561 dai->playback_widget->name, w->name);
3563 snd_soc_dapm_add_path(w->dapm,
3564 dai->playback_widget, w, NULL, NULL);
3567 if (dai->driver->capture.stream_name &&
3569 dai->driver->capture.stream_name)) {
3570 dev_dbg(dai->dev, "%s -> %s\n",
3571 w->name, dai->capture_widget->name);
3573 snd_soc_dapm_add_path(w->dapm, w,
3574 dai->capture_widget, NULL, NULL);
3582 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3586 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3587 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3588 struct snd_soc_dai *codec_dai = rtd->codec_dai;
3590 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3591 w_cpu = cpu_dai->playback_widget;
3592 w_codec = codec_dai->playback_widget;
3594 w_cpu = cpu_dai->capture_widget;
3595 w_codec = codec_dai->capture_widget;
3600 dapm_mark_dirty(w_cpu, "stream event");
3603 case SND_SOC_DAPM_STREAM_START:
3606 case SND_SOC_DAPM_STREAM_STOP:
3609 case SND_SOC_DAPM_STREAM_SUSPEND:
3610 case SND_SOC_DAPM_STREAM_RESUME:
3611 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3612 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3619 dapm_mark_dirty(w_codec, "stream event");
3622 case SND_SOC_DAPM_STREAM_START:
3623 w_codec->active = 1;
3625 case SND_SOC_DAPM_STREAM_STOP:
3626 w_codec->active = 0;
3628 case SND_SOC_DAPM_STREAM_SUSPEND:
3629 case SND_SOC_DAPM_STREAM_RESUME:
3630 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3631 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3636 dapm_power_widgets(rtd->card, event);
3640 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3641 * @rtd: PCM runtime data
3642 * @stream: stream name
3643 * @event: stream event
3645 * Sends a stream event to the dapm core. The core then makes any
3646 * necessary widget power changes.
3648 * Returns 0 for success else error.
3650 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3653 struct snd_soc_card *card = rtd->card;
3655 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3656 soc_dapm_stream_event(rtd, stream, event);
3657 mutex_unlock(&card->dapm_mutex);
3661 * snd_soc_dapm_enable_pin - enable pin.
3662 * @dapm: DAPM context
3665 * Enables input/output pin and its parents or children widgets iff there is
3666 * a valid audio route and active audio stream.
3667 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3668 * do any widget power switching.
3670 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3672 return snd_soc_dapm_set_pin(dapm, pin, 1);
3674 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3677 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3678 * @dapm: DAPM context
3681 * Enables input/output pin regardless of any other state. This is
3682 * intended for use with microphone bias supplies used in microphone
3685 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3686 * do any widget power switching.
3688 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3691 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3694 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3698 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3701 dapm_mark_dirty(w, "force enable");
3705 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3708 * snd_soc_dapm_disable_pin - disable pin.
3709 * @dapm: DAPM context
3712 * Disables input/output pin and its parents or children widgets.
3713 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3714 * do any widget power switching.
3716 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3719 return snd_soc_dapm_set_pin(dapm, pin, 0);
3721 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3724 * snd_soc_dapm_nc_pin - permanently disable pin.
3725 * @dapm: DAPM context
3728 * Marks the specified pin as being not connected, disabling it along
3729 * any parent or child widgets. At present this is identical to
3730 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3731 * additional things such as disabling controls which only affect
3732 * paths through the pin.
3734 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3735 * do any widget power switching.
3737 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3739 return snd_soc_dapm_set_pin(dapm, pin, 0);
3741 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3744 * snd_soc_dapm_get_pin_status - get audio pin status
3745 * @dapm: DAPM context
3746 * @pin: audio signal pin endpoint (or start point)
3748 * Get audio pin status - connected or disconnected.
3750 * Returns 1 for connected otherwise 0.
3752 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3755 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3758 return w->connected;
3762 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3765 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3766 * @dapm: DAPM context
3767 * @pin: audio signal pin endpoint (or start point)
3769 * Mark the given endpoint or pin as ignoring suspend. When the
3770 * system is disabled a path between two endpoints flagged as ignoring
3771 * suspend will not be disabled. The path must already be enabled via
3772 * normal means at suspend time, it will not be turned on if it was not
3775 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3778 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3781 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3785 w->ignore_suspend = 1;
3789 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3791 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3792 struct snd_soc_dapm_widget *w)
3794 struct snd_soc_dapm_path *p;
3796 list_for_each_entry(p, &card->paths, list) {
3797 if ((p->source == w) || (p->sink == w)) {
3799 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3800 p->source->name, p->source->id, p->source->dapm,
3801 p->sink->name, p->sink->id, p->sink->dapm);
3803 /* Connected to something other than the codec */
3804 if (p->source->dapm != p->sink->dapm)
3807 * Loopback connection from codec external pin to
3808 * codec external pin
3810 if (p->sink->id == snd_soc_dapm_input) {
3811 switch (p->source->id) {
3812 case snd_soc_dapm_output:
3813 case snd_soc_dapm_micbias:
3826 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3827 * @codec: The codec whose pins should be processed
3829 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3830 * which are unused. Pins are used if they are connected externally to the
3831 * codec, whether that be to some other device, or a loop-back connection to
3834 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3836 struct snd_soc_card *card = codec->card;
3837 struct snd_soc_dapm_context *dapm = &codec->dapm;
3838 struct snd_soc_dapm_widget *w;
3840 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
3841 &card->dapm, &codec->dapm);
3843 list_for_each_entry(w, &card->widgets, list) {
3844 if (w->dapm != dapm)
3847 case snd_soc_dapm_input:
3848 case snd_soc_dapm_output:
3849 case snd_soc_dapm_micbias:
3850 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
3852 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3854 "... Not in map; disabling\n");
3855 snd_soc_dapm_nc_pin(dapm, w->name);
3865 * snd_soc_dapm_free - free dapm resources
3866 * @dapm: DAPM context
3868 * Free all dapm widgets and resources.
3870 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3872 snd_soc_dapm_sys_remove(dapm->dev);
3873 dapm_debugfs_cleanup(dapm);
3874 dapm_free_widgets(dapm);
3875 list_del(&dapm->list);
3877 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3879 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3881 struct snd_soc_card *card = dapm->card;
3882 struct snd_soc_dapm_widget *w;
3883 LIST_HEAD(down_list);
3886 mutex_lock(&card->dapm_mutex);
3888 list_for_each_entry(w, &dapm->card->widgets, list) {
3889 if (w->dapm != dapm)
3892 dapm_seq_insert(w, &down_list, false);
3898 /* If there were no widgets to power down we're already in
3902 if (dapm->bias_level == SND_SOC_BIAS_ON)
3903 snd_soc_dapm_set_bias_level(dapm,
3904 SND_SOC_BIAS_PREPARE);
3905 dapm_seq_run(card, &down_list, 0, false);
3906 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3907 snd_soc_dapm_set_bias_level(dapm,
3908 SND_SOC_BIAS_STANDBY);
3911 mutex_unlock(&card->dapm_mutex);
3915 * snd_soc_dapm_shutdown - callback for system shutdown
3917 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3919 struct snd_soc_codec *codec;
3921 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
3922 soc_dapm_shutdown_codec(&codec->dapm);
3923 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3924 snd_soc_dapm_set_bias_level(&codec->dapm,
3929 /* Module information */
3930 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3931 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3932 MODULE_LICENSE("GPL");