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 /* dapm power sequences - make this per codec in the future */
51 static int dapm_up_seq[] = {
52 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
54 [snd_soc_dapm_regulator_supply] = 1,
55 [snd_soc_dapm_clock_supply] = 1,
56 [snd_soc_dapm_micbias] = 2,
57 [snd_soc_dapm_dai_link] = 2,
58 [snd_soc_dapm_dai] = 3,
59 [snd_soc_dapm_aif_in] = 3,
60 [snd_soc_dapm_aif_out] = 3,
61 [snd_soc_dapm_mic] = 4,
62 [snd_soc_dapm_mux] = 5,
63 [snd_soc_dapm_virt_mux] = 5,
64 [snd_soc_dapm_value_mux] = 5,
65 [snd_soc_dapm_dac] = 6,
66 [snd_soc_dapm_mixer] = 7,
67 [snd_soc_dapm_mixer_named_ctl] = 7,
68 [snd_soc_dapm_pga] = 8,
69 [snd_soc_dapm_adc] = 9,
70 [snd_soc_dapm_out_drv] = 10,
71 [snd_soc_dapm_hp] = 10,
72 [snd_soc_dapm_spk] = 10,
73 [snd_soc_dapm_line] = 10,
74 [snd_soc_dapm_post] = 11,
77 static int dapm_down_seq[] = {
78 [snd_soc_dapm_pre] = 0,
79 [snd_soc_dapm_adc] = 1,
80 [snd_soc_dapm_hp] = 2,
81 [snd_soc_dapm_spk] = 2,
82 [snd_soc_dapm_line] = 2,
83 [snd_soc_dapm_out_drv] = 2,
84 [snd_soc_dapm_pga] = 4,
85 [snd_soc_dapm_mixer_named_ctl] = 5,
86 [snd_soc_dapm_mixer] = 5,
87 [snd_soc_dapm_dac] = 6,
88 [snd_soc_dapm_mic] = 7,
89 [snd_soc_dapm_micbias] = 8,
90 [snd_soc_dapm_mux] = 9,
91 [snd_soc_dapm_virt_mux] = 9,
92 [snd_soc_dapm_value_mux] = 9,
93 [snd_soc_dapm_aif_in] = 10,
94 [snd_soc_dapm_aif_out] = 10,
95 [snd_soc_dapm_dai] = 10,
96 [snd_soc_dapm_dai_link] = 11,
97 [snd_soc_dapm_clock_supply] = 12,
98 [snd_soc_dapm_regulator_supply] = 12,
99 [snd_soc_dapm_supply] = 12,
100 [snd_soc_dapm_post] = 13,
103 static void pop_wait(u32 pop_time)
106 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
109 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
117 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
122 vsnprintf(buf, PAGE_SIZE, fmt, args);
123 dev_info(dev, "%s", buf);
129 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
131 return !list_empty(&w->dirty);
134 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
136 if (!dapm_dirty_widget(w)) {
137 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
139 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
142 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
144 void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
146 struct snd_soc_card *card = dapm->card;
147 struct snd_soc_dapm_widget *w;
149 mutex_lock(&card->dapm_mutex);
151 list_for_each_entry(w, &card->widgets, list) {
153 case snd_soc_dapm_input:
154 case snd_soc_dapm_output:
155 dapm_mark_dirty(w, "Rechecking inputs and outputs");
162 mutex_unlock(&card->dapm_mutex);
164 EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
166 /* create a new dapm widget */
167 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
168 const struct snd_soc_dapm_widget *_widget)
170 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
173 /* get snd_card from DAPM context */
174 static inline struct snd_card *dapm_get_snd_card(
175 struct snd_soc_dapm_context *dapm)
178 return dapm->codec->card->snd_card;
179 else if (dapm->platform)
180 return dapm->platform->card->snd_card;
188 /* get soc_card from DAPM context */
189 static inline struct snd_soc_card *dapm_get_soc_card(
190 struct snd_soc_dapm_context *dapm)
193 return dapm->codec->card;
194 else if (dapm->platform)
195 return dapm->platform->card;
203 static void dapm_reset(struct snd_soc_card *card)
205 struct snd_soc_dapm_widget *w;
207 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
209 list_for_each_entry(w, &card->widgets, list) {
210 w->power_checked = false;
216 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
219 return snd_soc_read(w->codec, reg);
220 else if (w->platform)
221 return snd_soc_platform_read(w->platform, reg);
223 dev_err(w->dapm->dev, "no valid widget read method\n");
227 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
230 return snd_soc_write(w->codec, reg, val);
231 else if (w->platform)
232 return snd_soc_platform_write(w->platform, reg, val);
234 dev_err(w->dapm->dev, "no valid widget write method\n");
238 static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
240 if (w->codec && !w->codec->using_regmap)
241 mutex_lock(&w->codec->mutex);
242 else if (w->platform)
243 mutex_lock(&w->platform->mutex);
246 static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
248 if (w->codec && !w->codec->using_regmap)
249 mutex_unlock(&w->codec->mutex);
250 else if (w->platform)
251 mutex_unlock(&w->platform->mutex);
254 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
255 unsigned short reg, unsigned int mask, unsigned int value)
258 unsigned int old, new;
261 if (w->codec && w->codec->using_regmap) {
262 ret = regmap_update_bits_check(w->codec->control_data,
263 reg, mask, value, &change);
268 ret = soc_widget_read(w, reg);
270 soc_widget_unlock(w);
275 new = (old & ~mask) | (value & mask);
278 ret = soc_widget_write(w, reg, new);
280 soc_widget_unlock(w);
284 soc_widget_unlock(w);
291 * snd_soc_dapm_set_bias_level - set the bias level for the system
292 * @dapm: DAPM context
293 * @level: level to configure
295 * Configure the bias (power) levels for the SoC audio device.
297 * Returns 0 for success else error.
299 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
300 enum snd_soc_bias_level level)
302 struct snd_soc_card *card = dapm->card;
305 trace_snd_soc_bias_level_start(card, level);
307 if (card && card->set_bias_level)
308 ret = card->set_bias_level(card, dapm, level);
313 if (dapm->codec->driver->set_bias_level)
314 ret = dapm->codec->driver->set_bias_level(dapm->codec,
317 dapm->bias_level = level;
322 if (card && card->set_bias_level_post)
323 ret = card->set_bias_level_post(card, dapm, level);
325 trace_snd_soc_bias_level_done(card, level);
330 /* set up initial codec paths */
331 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
332 struct snd_soc_dapm_path *p, int i)
335 case snd_soc_dapm_switch:
336 case snd_soc_dapm_mixer:
337 case snd_soc_dapm_mixer_named_ctl: {
339 struct soc_mixer_control *mc = (struct soc_mixer_control *)
340 w->kcontrol_news[i].private_value;
341 unsigned int reg = mc->reg;
342 unsigned int shift = mc->shift;
344 unsigned int mask = (1 << fls(max)) - 1;
345 unsigned int invert = mc->invert;
347 val = soc_widget_read(w, reg);
348 val = (val >> shift) & mask;
355 case snd_soc_dapm_mux: {
356 struct soc_enum *e = (struct soc_enum *)
357 w->kcontrol_news[i].private_value;
360 val = soc_widget_read(w, e->reg);
361 item = (val >> e->shift_l) & e->mask;
364 for (i = 0; i < e->max; i++) {
365 if (!(strcmp(p->name, e->texts[i])) && item == i)
370 case snd_soc_dapm_virt_mux: {
371 struct soc_enum *e = (struct soc_enum *)
372 w->kcontrol_news[i].private_value;
375 /* since a virtual mux has no backing registers to
376 * decide which path to connect, it will try to match
377 * with the first enumeration. This is to ensure
378 * that the default mux choice (the first) will be
379 * correctly powered up during initialization.
381 if (!strcmp(p->name, e->texts[0]))
385 case snd_soc_dapm_value_mux: {
386 struct soc_enum *e = (struct soc_enum *)
387 w->kcontrol_news[i].private_value;
390 val = soc_widget_read(w, e->reg);
391 val = (val >> e->shift_l) & e->mask;
392 for (item = 0; item < e->max; item++) {
393 if (val == e->values[item])
398 for (i = 0; i < e->max; i++) {
399 if (!(strcmp(p->name, e->texts[i])) && item == i)
404 /* does not affect routing - always connected */
405 case snd_soc_dapm_pga:
406 case snd_soc_dapm_out_drv:
407 case snd_soc_dapm_output:
408 case snd_soc_dapm_adc:
409 case snd_soc_dapm_input:
410 case snd_soc_dapm_siggen:
411 case snd_soc_dapm_dac:
412 case snd_soc_dapm_micbias:
413 case snd_soc_dapm_vmid:
414 case snd_soc_dapm_supply:
415 case snd_soc_dapm_regulator_supply:
416 case snd_soc_dapm_clock_supply:
417 case snd_soc_dapm_aif_in:
418 case snd_soc_dapm_aif_out:
419 case snd_soc_dapm_dai:
420 case snd_soc_dapm_hp:
421 case snd_soc_dapm_mic:
422 case snd_soc_dapm_spk:
423 case snd_soc_dapm_line:
424 case snd_soc_dapm_dai_link:
427 /* does affect routing - dynamically connected */
428 case snd_soc_dapm_pre:
429 case snd_soc_dapm_post:
435 /* connect mux widget to its interconnecting audio paths */
436 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
437 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
438 struct snd_soc_dapm_path *path, const char *control_name,
439 const struct snd_kcontrol_new *kcontrol)
441 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
444 for (i = 0; i < e->max; i++) {
445 if (!(strcmp(control_name, e->texts[i]))) {
446 list_add(&path->list, &dapm->card->paths);
447 list_add(&path->list_sink, &dest->sources);
448 list_add(&path->list_source, &src->sinks);
449 path->name = (char*)e->texts[i];
450 dapm_set_path_status(dest, path, 0);
458 /* connect mixer widget to its interconnecting audio paths */
459 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
460 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
461 struct snd_soc_dapm_path *path, const char *control_name)
465 /* search for mixer kcontrol */
466 for (i = 0; i < dest->num_kcontrols; i++) {
467 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
468 list_add(&path->list, &dapm->card->paths);
469 list_add(&path->list_sink, &dest->sources);
470 list_add(&path->list_source, &src->sinks);
471 path->name = dest->kcontrol_news[i].name;
472 dapm_set_path_status(dest, path, i);
479 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
480 struct snd_soc_dapm_widget *kcontrolw,
481 const struct snd_kcontrol_new *kcontrol_new,
482 struct snd_kcontrol **kcontrol)
484 struct snd_soc_dapm_widget *w;
489 list_for_each_entry(w, &dapm->card->widgets, list) {
490 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
492 for (i = 0; i < w->num_kcontrols; i++) {
493 if (&w->kcontrol_news[i] == kcontrol_new) {
495 *kcontrol = w->kcontrols[i];
504 /* create new dapm mixer control */
505 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
507 struct snd_soc_dapm_context *dapm = w->dapm;
509 size_t name_len, prefix_len;
510 struct snd_soc_dapm_path *path;
511 struct snd_card *card = dapm->card->snd_card;
513 struct snd_soc_dapm_widget_list *wlist;
517 prefix = dapm->codec->name_prefix;
522 prefix_len = strlen(prefix) + 1;
527 for (i = 0; i < w->num_kcontrols; i++) {
530 list_for_each_entry(path, &w->sources, list_sink) {
532 /* mixer/mux paths name must match control name */
533 if (path->name != (char *)w->kcontrol_news[i].name)
536 if (w->kcontrols[i]) {
537 path->kcontrol = w->kcontrols[i];
541 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
542 sizeof(struct snd_soc_dapm_widget *),
543 wlist = kzalloc(wlistsize, GFP_KERNEL);
546 "asoc: can't allocate widget list for %s\n",
550 wlist->num_widgets = 1;
551 wlist->widgets[0] = w;
553 /* add dapm control with long name.
554 * for dapm_mixer this is the concatenation of the
555 * mixer and kcontrol name.
556 * for dapm_mixer_named_ctl this is simply the
559 name_len = strlen(w->kcontrol_news[i].name) + 1;
560 if (w->id != snd_soc_dapm_mixer_named_ctl)
561 name_len += 1 + strlen(w->name);
563 path->long_name = kmalloc(name_len, GFP_KERNEL);
565 if (path->long_name == NULL) {
572 /* The control will get a prefix from
573 * the control creation process but
574 * we're also using the same prefix
575 * for widgets so cut the prefix off
576 * the front of the widget name.
578 snprintf((char *)path->long_name, name_len,
579 "%s %s", w->name + prefix_len,
580 w->kcontrol_news[i].name);
582 case snd_soc_dapm_mixer_named_ctl:
583 snprintf((char *)path->long_name, name_len,
584 "%s", w->kcontrol_news[i].name);
588 ((char *)path->long_name)[name_len - 1] = '\0';
590 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
591 wlist, path->long_name,
593 ret = snd_ctl_add(card, path->kcontrol);
596 "asoc: failed to add dapm kcontrol %s: %d\n",
597 path->long_name, ret);
599 kfree(path->long_name);
600 path->long_name = NULL;
603 w->kcontrols[i] = path->kcontrol;
609 /* create new dapm mux control */
610 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
612 struct snd_soc_dapm_context *dapm = w->dapm;
613 struct snd_soc_dapm_path *path = NULL;
614 struct snd_kcontrol *kcontrol;
615 struct snd_card *card = dapm->card->snd_card;
619 struct snd_soc_dapm_widget_list *wlist;
620 int shared, wlistentries;
624 if (w->num_kcontrols != 1) {
626 "asoc: mux %s has incorrect number of controls\n",
631 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
634 wlist = kcontrol->private_data;
635 wlistentries = wlist->num_widgets + 1;
640 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
641 wlistentries * sizeof(struct snd_soc_dapm_widget *),
642 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
645 "asoc: can't allocate widget list for %s\n", w->name);
648 wlist->num_widgets = wlistentries;
649 wlist->widgets[wlistentries - 1] = w;
653 prefix = dapm->codec->name_prefix;
658 name = w->kcontrol_news[0].name;
663 prefix_len = strlen(prefix) + 1;
669 * The control will get a prefix from the control creation
670 * process but we're also using the same prefix for widgets so
671 * cut the prefix off the front of the widget name.
673 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
674 name + prefix_len, prefix);
675 ret = snd_ctl_add(card, kcontrol);
677 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
684 kcontrol->private_data = wlist;
686 w->kcontrols[0] = kcontrol;
688 list_for_each_entry(path, &w->sources, list_sink)
689 path->kcontrol = kcontrol;
694 /* create new dapm volume control */
695 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
697 if (w->num_kcontrols)
698 dev_err(w->dapm->dev,
699 "asoc: PGA controls not supported: '%s'\n", w->name);
704 /* reset 'walked' bit for each dapm path */
705 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
707 struct snd_soc_dapm_path *p;
709 list_for_each_entry(p, &dapm->card->paths, list)
713 /* We implement power down on suspend by checking the power state of
714 * the ALSA card - when we are suspending the ALSA state for the card
717 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
719 int level = snd_power_get_state(widget->dapm->card->snd_card);
722 case SNDRV_CTL_POWER_D3hot:
723 case SNDRV_CTL_POWER_D3cold:
724 if (widget->ignore_suspend)
725 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
727 return widget->ignore_suspend;
733 /* add widget to list if it's not already in the list */
734 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
735 struct snd_soc_dapm_widget *w)
737 struct snd_soc_dapm_widget_list *wlist;
738 int wlistsize, wlistentries, i;
745 /* is this widget already in the list */
746 for (i = 0; i < wlist->num_widgets; i++) {
747 if (wlist->widgets[i] == w)
751 /* allocate some new space */
752 wlistentries = wlist->num_widgets + 1;
753 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
754 wlistentries * sizeof(struct snd_soc_dapm_widget *);
755 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
757 dev_err(w->dapm->dev, "can't allocate widget list for %s\n",
763 /* insert the widget */
764 dev_dbg(w->dapm->dev, "added %s in widget list pos %d\n",
765 w->name, wlist->num_widgets);
767 wlist->widgets[wlist->num_widgets] = w;
768 wlist->num_widgets++;
773 * Recursively check for a completed path to an active or physically connected
774 * output widget. Returns number of complete paths.
776 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
777 struct snd_soc_dapm_widget_list **list)
779 struct snd_soc_dapm_path *path;
782 if (widget->outputs >= 0)
783 return widget->outputs;
785 DAPM_UPDATE_STAT(widget, path_checks);
787 switch (widget->id) {
788 case snd_soc_dapm_supply:
789 case snd_soc_dapm_regulator_supply:
790 case snd_soc_dapm_clock_supply:
796 switch (widget->id) {
797 case snd_soc_dapm_adc:
798 case snd_soc_dapm_aif_out:
799 case snd_soc_dapm_dai:
800 if (widget->active) {
801 widget->outputs = snd_soc_dapm_suspend_check(widget);
802 return widget->outputs;
808 if (widget->connected) {
809 /* connected pin ? */
810 if (widget->id == snd_soc_dapm_output && !widget->ext) {
811 widget->outputs = snd_soc_dapm_suspend_check(widget);
812 return widget->outputs;
815 /* connected jack or spk ? */
816 if (widget->id == snd_soc_dapm_hp ||
817 widget->id == snd_soc_dapm_spk ||
818 (widget->id == snd_soc_dapm_line &&
819 !list_empty(&widget->sources))) {
820 widget->outputs = snd_soc_dapm_suspend_check(widget);
821 return widget->outputs;
825 list_for_each_entry(path, &widget->sinks, list_source) {
826 DAPM_UPDATE_STAT(widget, neighbour_checks);
834 trace_snd_soc_dapm_output_path(widget, path);
836 if (path->sink && path->connect) {
839 /* do we need to add this widget to the list ? */
842 err = dapm_list_add_widget(list, path->sink);
844 dev_err(widget->dapm->dev, "could not add widget %s\n",
850 con += is_connected_output_ep(path->sink, list);
854 widget->outputs = con;
860 * Recursively check for a completed path to an active or physically connected
861 * input widget. Returns number of complete paths.
863 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
864 struct snd_soc_dapm_widget_list **list)
866 struct snd_soc_dapm_path *path;
869 if (widget->inputs >= 0)
870 return widget->inputs;
872 DAPM_UPDATE_STAT(widget, path_checks);
874 switch (widget->id) {
875 case snd_soc_dapm_supply:
876 case snd_soc_dapm_regulator_supply:
877 case snd_soc_dapm_clock_supply:
883 /* active stream ? */
884 switch (widget->id) {
885 case snd_soc_dapm_dac:
886 case snd_soc_dapm_aif_in:
887 case snd_soc_dapm_dai:
888 if (widget->active) {
889 widget->inputs = snd_soc_dapm_suspend_check(widget);
890 return widget->inputs;
896 if (widget->connected) {
897 /* connected pin ? */
898 if (widget->id == snd_soc_dapm_input && !widget->ext) {
899 widget->inputs = snd_soc_dapm_suspend_check(widget);
900 return widget->inputs;
903 /* connected VMID/Bias for lower pops */
904 if (widget->id == snd_soc_dapm_vmid) {
905 widget->inputs = snd_soc_dapm_suspend_check(widget);
906 return widget->inputs;
909 /* connected jack ? */
910 if (widget->id == snd_soc_dapm_mic ||
911 (widget->id == snd_soc_dapm_line &&
912 !list_empty(&widget->sinks))) {
913 widget->inputs = snd_soc_dapm_suspend_check(widget);
914 return widget->inputs;
917 /* signal generator */
918 if (widget->id == snd_soc_dapm_siggen) {
919 widget->inputs = snd_soc_dapm_suspend_check(widget);
920 return widget->inputs;
924 list_for_each_entry(path, &widget->sources, list_sink) {
925 DAPM_UPDATE_STAT(widget, neighbour_checks);
933 trace_snd_soc_dapm_input_path(widget, path);
935 if (path->source && path->connect) {
938 /* do we need to add this widget to the list ? */
941 err = dapm_list_add_widget(list, path->source);
943 dev_err(widget->dapm->dev, "could not add widget %s\n",
949 con += is_connected_input_ep(path->source, list);
953 widget->inputs = con;
959 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
961 * @stream: stream direction.
962 * @list: list of active widgets for this stream.
964 * Queries DAPM graph as to whether an valid audio stream path exists for
965 * the initial stream specified by name. This takes into account
966 * current mixer and mux kcontrol settings. Creates list of valid widgets.
968 * Returns the number of valid paths or negative error.
970 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
971 struct snd_soc_dapm_widget_list **list)
973 struct snd_soc_card *card = dai->card;
976 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
979 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
980 paths = is_connected_output_ep(dai->playback_widget, list);
982 paths = is_connected_input_ep(dai->capture_widget, list);
984 trace_snd_soc_dapm_connected(paths, stream);
985 dapm_clear_walk(&card->dapm);
986 mutex_unlock(&card->dapm_mutex);
992 * Handler for generic register modifier widget.
994 int dapm_reg_event(struct snd_soc_dapm_widget *w,
995 struct snd_kcontrol *kcontrol, int event)
999 if (SND_SOC_DAPM_EVENT_ON(event))
1004 soc_widget_update_bits_locked(w, -(w->reg + 1),
1005 w->mask << w->shift, val << w->shift);
1009 EXPORT_SYMBOL_GPL(dapm_reg_event);
1012 * Handler for regulator supply widget.
1014 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1015 struct snd_kcontrol *kcontrol, int event)
1017 if (SND_SOC_DAPM_EVENT_ON(event))
1018 return regulator_enable(w->regulator);
1020 return regulator_disable_deferred(w->regulator, w->shift);
1022 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1025 * Handler for clock supply widget.
1027 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1028 struct snd_kcontrol *kcontrol, int event)
1033 #ifdef CONFIG_HAVE_CLK
1034 if (SND_SOC_DAPM_EVENT_ON(event)) {
1035 return clk_enable(w->clk);
1037 clk_disable(w->clk);
1043 EXPORT_SYMBOL_GPL(dapm_clock_event);
1045 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1047 if (w->power_checked)
1048 return w->new_power;
1053 w->new_power = w->power_check(w);
1055 w->power_checked = true;
1057 return w->new_power;
1060 /* Generic check to see if a widget should be powered.
1062 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1066 DAPM_UPDATE_STAT(w, power_checks);
1068 in = is_connected_input_ep(w, NULL);
1069 dapm_clear_walk(w->dapm);
1070 out = is_connected_output_ep(w, NULL);
1071 dapm_clear_walk(w->dapm);
1072 return out != 0 && in != 0;
1075 static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1077 DAPM_UPDATE_STAT(w, power_checks);
1082 return dapm_generic_check_power(w);
1085 /* Check to see if an ADC has power */
1086 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1090 DAPM_UPDATE_STAT(w, power_checks);
1093 in = is_connected_input_ep(w, NULL);
1094 dapm_clear_walk(w->dapm);
1097 return dapm_generic_check_power(w);
1101 /* Check to see if a DAC has power */
1102 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1106 DAPM_UPDATE_STAT(w, power_checks);
1109 out = is_connected_output_ep(w, NULL);
1110 dapm_clear_walk(w->dapm);
1113 return dapm_generic_check_power(w);
1117 /* Check to see if a power supply is needed */
1118 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1120 struct snd_soc_dapm_path *path;
1122 DAPM_UPDATE_STAT(w, power_checks);
1124 /* Check if one of our outputs is connected */
1125 list_for_each_entry(path, &w->sinks, list_source) {
1126 DAPM_UPDATE_STAT(w, neighbour_checks);
1131 if (path->connected &&
1132 !path->connected(path->source, path->sink))
1138 if (dapm_widget_power_check(path->sink))
1142 dapm_clear_walk(w->dapm);
1147 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1152 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1153 struct snd_soc_dapm_widget *b,
1161 sort = dapm_down_seq;
1163 if (sort[a->id] != sort[b->id])
1164 return sort[a->id] - sort[b->id];
1165 if (a->subseq != b->subseq) {
1167 return a->subseq - b->subseq;
1169 return b->subseq - a->subseq;
1171 if (a->reg != b->reg)
1172 return a->reg - b->reg;
1173 if (a->dapm != b->dapm)
1174 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1179 /* Insert a widget in order into a DAPM power sequence. */
1180 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1181 struct list_head *list,
1184 struct snd_soc_dapm_widget *w;
1186 list_for_each_entry(w, list, power_list)
1187 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1188 list_add_tail(&new_widget->power_list, &w->power_list);
1192 list_add_tail(&new_widget->power_list, list);
1195 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1196 struct snd_soc_dapm_widget *w, int event)
1198 struct snd_soc_card *card = dapm->card;
1199 const char *ev_name;
1203 case SND_SOC_DAPM_PRE_PMU:
1204 ev_name = "PRE_PMU";
1207 case SND_SOC_DAPM_POST_PMU:
1208 ev_name = "POST_PMU";
1211 case SND_SOC_DAPM_PRE_PMD:
1212 ev_name = "PRE_PMD";
1215 case SND_SOC_DAPM_POST_PMD:
1216 ev_name = "POST_PMD";
1224 if (w->power != power)
1227 if (w->event && (w->event_flags & event)) {
1228 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1230 trace_snd_soc_dapm_widget_event_start(w, event);
1231 ret = w->event(w, NULL, event);
1232 trace_snd_soc_dapm_widget_event_done(w, event);
1234 pr_err("%s: %s event failed: %d\n",
1235 ev_name, w->name, ret);
1239 /* Apply the coalesced changes from a DAPM sequence */
1240 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1241 struct list_head *pending)
1243 struct snd_soc_card *card = dapm->card;
1244 struct snd_soc_dapm_widget *w;
1246 unsigned int value = 0;
1247 unsigned int mask = 0;
1248 unsigned int cur_mask;
1250 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1253 list_for_each_entry(w, pending, power_list) {
1254 cur_mask = 1 << w->shift;
1255 BUG_ON(reg != w->reg);
1266 pop_dbg(dapm->dev, card->pop_time,
1267 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1268 w->name, reg, value, mask);
1270 /* Check for events */
1271 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1272 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1276 /* Any widget will do, they should all be updating the
1279 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1282 pop_dbg(dapm->dev, card->pop_time,
1283 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1284 value, mask, reg, card->pop_time);
1285 pop_wait(card->pop_time);
1286 soc_widget_update_bits_locked(w, reg, mask, value);
1289 list_for_each_entry(w, pending, power_list) {
1290 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1291 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1295 /* Apply a DAPM power sequence.
1297 * We walk over a pre-sorted list of widgets to apply power to. In
1298 * order to minimise the number of writes to the device required
1299 * multiple widgets will be updated in a single write where possible.
1300 * Currently anything that requires more than a single write is not
1303 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1304 struct list_head *list, int event, bool power_up)
1306 struct snd_soc_dapm_widget *w, *n;
1309 int cur_subseq = -1;
1310 int cur_reg = SND_SOC_NOPM;
1311 struct snd_soc_dapm_context *cur_dapm = NULL;
1318 sort = dapm_down_seq;
1320 list_for_each_entry_safe(w, n, list, power_list) {
1323 /* Do we need to apply any queued changes? */
1324 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1325 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1326 if (!list_empty(&pending))
1327 dapm_seq_run_coalesced(cur_dapm, &pending);
1329 if (cur_dapm && cur_dapm->seq_notifier) {
1330 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1331 if (sort[i] == cur_sort)
1332 cur_dapm->seq_notifier(cur_dapm,
1337 INIT_LIST_HEAD(&pending);
1339 cur_subseq = INT_MIN;
1340 cur_reg = SND_SOC_NOPM;
1345 case snd_soc_dapm_pre:
1347 list_for_each_entry_safe_continue(w, n, list,
1350 if (event == SND_SOC_DAPM_STREAM_START)
1352 NULL, SND_SOC_DAPM_PRE_PMU);
1353 else if (event == SND_SOC_DAPM_STREAM_STOP)
1355 NULL, SND_SOC_DAPM_PRE_PMD);
1358 case snd_soc_dapm_post:
1360 list_for_each_entry_safe_continue(w, n, list,
1363 if (event == SND_SOC_DAPM_STREAM_START)
1365 NULL, SND_SOC_DAPM_POST_PMU);
1366 else if (event == SND_SOC_DAPM_STREAM_STOP)
1368 NULL, SND_SOC_DAPM_POST_PMD);
1372 /* Queue it up for application */
1373 cur_sort = sort[w->id];
1374 cur_subseq = w->subseq;
1377 list_move(&w->power_list, &pending);
1382 dev_err(w->dapm->dev,
1383 "Failed to apply widget power: %d\n", ret);
1386 if (!list_empty(&pending))
1387 dapm_seq_run_coalesced(cur_dapm, &pending);
1389 if (cur_dapm && cur_dapm->seq_notifier) {
1390 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1391 if (sort[i] == cur_sort)
1392 cur_dapm->seq_notifier(cur_dapm,
1397 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1399 struct snd_soc_dapm_update *update = dapm->update;
1400 struct snd_soc_dapm_widget *w;
1409 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1410 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1412 pr_err("%s DAPM pre-event failed: %d\n",
1416 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1419 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1422 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1423 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1425 pr_err("%s DAPM post-event failed: %d\n",
1430 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1431 * they're changing state.
1433 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1435 struct snd_soc_dapm_context *d = data;
1438 /* If we're off and we're not supposed to be go into STANDBY */
1439 if (d->bias_level == SND_SOC_BIAS_OFF &&
1440 d->target_bias_level != SND_SOC_BIAS_OFF) {
1442 pm_runtime_get_sync(d->dev);
1444 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1447 "Failed to turn on bias: %d\n", ret);
1450 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1451 if (d->bias_level != d->target_bias_level) {
1452 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1455 "Failed to prepare bias: %d\n", ret);
1459 /* Async callback run prior to DAPM sequences - brings to their final
1462 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1464 struct snd_soc_dapm_context *d = data;
1467 /* If we just powered the last thing off drop to standby bias */
1468 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1469 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1470 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1471 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1473 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1477 /* If we're in standby and can support bias off then do that */
1478 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1479 d->target_bias_level == SND_SOC_BIAS_OFF) {
1480 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1482 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1485 pm_runtime_put(d->dev);
1488 /* If we just powered up then move to active bias */
1489 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1490 d->target_bias_level == SND_SOC_BIAS_ON) {
1491 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1493 dev_err(d->dev, "Failed to apply active bias: %d\n",
1498 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1499 bool power, bool connect)
1501 /* If a connection is being made or broken then that update
1502 * will have marked the peer dirty, otherwise the widgets are
1503 * not connected and this update has no impact. */
1507 /* If the peer is already in the state we're moving to then we
1508 * won't have an impact on it. */
1509 if (power != peer->power)
1510 dapm_mark_dirty(peer, "peer state change");
1513 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1514 struct list_head *up_list,
1515 struct list_head *down_list)
1517 struct snd_soc_dapm_path *path;
1519 if (w->power == power)
1522 trace_snd_soc_dapm_widget_power(w, power);
1524 /* If we changed our power state perhaps our neigbours changed
1527 list_for_each_entry(path, &w->sources, list_sink) {
1529 dapm_widget_set_peer_power(path->source, power,
1534 case snd_soc_dapm_supply:
1535 case snd_soc_dapm_regulator_supply:
1536 case snd_soc_dapm_clock_supply:
1537 /* Supplies can't affect their outputs, only their inputs */
1540 list_for_each_entry(path, &w->sinks, list_source) {
1542 dapm_widget_set_peer_power(path->sink, power,
1550 dapm_seq_insert(w, up_list, true);
1552 dapm_seq_insert(w, down_list, false);
1557 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1558 struct list_head *up_list,
1559 struct list_head *down_list)
1564 case snd_soc_dapm_pre:
1565 dapm_seq_insert(w, down_list, false);
1567 case snd_soc_dapm_post:
1568 dapm_seq_insert(w, up_list, true);
1572 power = dapm_widget_power_check(w);
1574 dapm_widget_set_power(w, power, up_list, down_list);
1580 * Scan each dapm widget for complete audio path.
1581 * A complete path is a route that has valid endpoints i.e.:-
1583 * o DAC to output pin.
1584 * o Input Pin to ADC.
1585 * o Input pin to Output pin (bypass, sidetone)
1586 * o DAC to ADC (loopback).
1588 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1590 struct snd_soc_card *card = dapm->card;
1591 struct snd_soc_dapm_widget *w;
1592 struct snd_soc_dapm_context *d;
1594 LIST_HEAD(down_list);
1595 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1596 enum snd_soc_bias_level bias;
1598 trace_snd_soc_dapm_start(card);
1600 list_for_each_entry(d, &card->dapm_list, list) {
1601 if (d->idle_bias_off)
1602 d->target_bias_level = SND_SOC_BIAS_OFF;
1604 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1609 /* Check which widgets we need to power and store them in
1610 * lists indicating if they should be powered up or down. We
1611 * only check widgets that have been flagged as dirty but note
1612 * that new widgets may be added to the dirty list while we
1615 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1616 dapm_power_one_widget(w, &up_list, &down_list);
1619 list_for_each_entry(w, &card->widgets, list) {
1621 case snd_soc_dapm_pre:
1622 case snd_soc_dapm_post:
1623 /* These widgets always need to be powered */
1626 list_del_init(&w->dirty);
1633 /* Supplies and micbiases only bring the
1634 * context up to STANDBY as unless something
1635 * else is active and passing audio they
1636 * generally don't require full power. Signal
1637 * generators are virtual pins and have no
1638 * power impact themselves.
1641 case snd_soc_dapm_siggen:
1643 case snd_soc_dapm_supply:
1644 case snd_soc_dapm_regulator_supply:
1645 case snd_soc_dapm_clock_supply:
1646 case snd_soc_dapm_micbias:
1647 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1648 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1651 d->target_bias_level = SND_SOC_BIAS_ON;
1658 /* Force all contexts in the card to the same bias state if
1659 * they're not ground referenced.
1661 bias = SND_SOC_BIAS_OFF;
1662 list_for_each_entry(d, &card->dapm_list, list)
1663 if (d->target_bias_level > bias)
1664 bias = d->target_bias_level;
1665 list_for_each_entry(d, &card->dapm_list, list)
1666 if (!d->idle_bias_off)
1667 d->target_bias_level = bias;
1669 trace_snd_soc_dapm_walk_done(card);
1671 /* Run all the bias changes in parallel */
1672 list_for_each_entry(d, &dapm->card->dapm_list, list)
1673 async_schedule_domain(dapm_pre_sequence_async, d,
1675 async_synchronize_full_domain(&async_domain);
1677 /* Power down widgets first; try to avoid amplifying pops. */
1678 dapm_seq_run(dapm, &down_list, event, false);
1680 dapm_widget_update(dapm);
1683 dapm_seq_run(dapm, &up_list, event, true);
1685 /* Run all the bias changes in parallel */
1686 list_for_each_entry(d, &dapm->card->dapm_list, list)
1687 async_schedule_domain(dapm_post_sequence_async, d,
1689 async_synchronize_full_domain(&async_domain);
1691 /* do we need to notify any clients that DAPM event is complete */
1692 list_for_each_entry(d, &card->dapm_list, list) {
1693 if (d->stream_event)
1694 d->stream_event(d, event);
1697 pop_dbg(dapm->dev, card->pop_time,
1698 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1699 pop_wait(card->pop_time);
1701 trace_snd_soc_dapm_done(card);
1706 #ifdef CONFIG_DEBUG_FS
1707 static ssize_t dapm_widget_power_read_file(struct file *file,
1708 char __user *user_buf,
1709 size_t count, loff_t *ppos)
1711 struct snd_soc_dapm_widget *w = file->private_data;
1715 struct snd_soc_dapm_path *p = NULL;
1717 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1721 in = is_connected_input_ep(w, NULL);
1722 dapm_clear_walk(w->dapm);
1723 out = is_connected_output_ep(w, NULL);
1724 dapm_clear_walk(w->dapm);
1726 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1727 w->name, w->power ? "On" : "Off",
1728 w->force ? " (forced)" : "", in, out);
1731 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1732 " - R%d(0x%x) bit %d",
1733 w->reg, w->reg, w->shift);
1735 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1738 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1740 w->active ? "active" : "inactive");
1742 list_for_each_entry(p, &w->sources, list_sink) {
1743 if (p->connected && !p->connected(w, p->sink))
1747 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1748 " in \"%s\" \"%s\"\n",
1749 p->name ? p->name : "static",
1752 list_for_each_entry(p, &w->sinks, list_source) {
1753 if (p->connected && !p->connected(w, p->sink))
1757 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1758 " out \"%s\" \"%s\"\n",
1759 p->name ? p->name : "static",
1763 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1769 static const struct file_operations dapm_widget_power_fops = {
1770 .open = simple_open,
1771 .read = dapm_widget_power_read_file,
1772 .llseek = default_llseek,
1775 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1776 size_t count, loff_t *ppos)
1778 struct snd_soc_dapm_context *dapm = file->private_data;
1781 switch (dapm->bias_level) {
1782 case SND_SOC_BIAS_ON:
1785 case SND_SOC_BIAS_PREPARE:
1786 level = "Prepare\n";
1788 case SND_SOC_BIAS_STANDBY:
1789 level = "Standby\n";
1791 case SND_SOC_BIAS_OFF:
1796 level = "Unknown\n";
1800 return simple_read_from_buffer(user_buf, count, ppos, level,
1804 static const struct file_operations dapm_bias_fops = {
1805 .open = simple_open,
1806 .read = dapm_bias_read_file,
1807 .llseek = default_llseek,
1810 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1811 struct dentry *parent)
1815 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1817 if (!dapm->debugfs_dapm) {
1819 "Failed to create DAPM debugfs directory\n");
1823 d = debugfs_create_file("bias_level", 0444,
1824 dapm->debugfs_dapm, dapm,
1828 "ASoC: Failed to create bias level debugfs file\n");
1831 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1833 struct snd_soc_dapm_context *dapm = w->dapm;
1836 if (!dapm->debugfs_dapm || !w->name)
1839 d = debugfs_create_file(w->name, 0444,
1840 dapm->debugfs_dapm, w,
1841 &dapm_widget_power_fops);
1843 dev_warn(w->dapm->dev,
1844 "ASoC: Failed to create %s debugfs file\n",
1848 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1850 debugfs_remove_recursive(dapm->debugfs_dapm);
1854 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1855 struct dentry *parent)
1859 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1863 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1869 /* test and update the power status of a mux widget */
1870 static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1871 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1873 struct snd_soc_dapm_path *path;
1876 if (widget->id != snd_soc_dapm_mux &&
1877 widget->id != snd_soc_dapm_virt_mux &&
1878 widget->id != snd_soc_dapm_value_mux)
1881 /* find dapm widget path assoc with kcontrol */
1882 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1883 if (path->kcontrol != kcontrol)
1886 if (!path->name || !e->texts[mux])
1890 /* we now need to match the string in the enum to the path */
1891 if (!(strcmp(path->name, e->texts[mux]))) {
1892 path->connect = 1; /* new connection */
1893 dapm_mark_dirty(path->source, "mux connection");
1896 dapm_mark_dirty(path->source,
1897 "mux disconnection");
1898 path->connect = 0; /* old connection must be powered down */
1903 dapm_mark_dirty(widget, "mux change");
1904 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1910 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1911 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1913 struct snd_soc_card *card = widget->dapm->card;
1916 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1917 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1918 mutex_unlock(&card->dapm_mutex);
1920 soc_dpcm_runtime_update(widget);
1923 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1925 /* test and update the power status of a mixer or switch widget */
1926 static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1927 struct snd_kcontrol *kcontrol, int connect)
1929 struct snd_soc_dapm_path *path;
1932 if (widget->id != snd_soc_dapm_mixer &&
1933 widget->id != snd_soc_dapm_mixer_named_ctl &&
1934 widget->id != snd_soc_dapm_switch)
1937 /* find dapm widget path assoc with kcontrol */
1938 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1939 if (path->kcontrol != kcontrol)
1942 /* found, now check type */
1944 path->connect = connect;
1945 dapm_mark_dirty(path->source, "mixer connection");
1949 dapm_mark_dirty(widget, "mixer update");
1950 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1956 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1957 struct snd_kcontrol *kcontrol, int connect)
1959 struct snd_soc_card *card = widget->dapm->card;
1962 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1963 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1964 mutex_unlock(&card->dapm_mutex);
1966 soc_dpcm_runtime_update(widget);
1969 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
1971 /* show dapm widget status in sys fs */
1972 static ssize_t dapm_widget_show(struct device *dev,
1973 struct device_attribute *attr, char *buf)
1975 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
1976 struct snd_soc_codec *codec =rtd->codec;
1977 struct snd_soc_dapm_widget *w;
1979 char *state = "not set";
1981 list_for_each_entry(w, &codec->card->widgets, list) {
1982 if (w->dapm != &codec->dapm)
1985 /* only display widgets that burnm power */
1987 case snd_soc_dapm_hp:
1988 case snd_soc_dapm_mic:
1989 case snd_soc_dapm_spk:
1990 case snd_soc_dapm_line:
1991 case snd_soc_dapm_micbias:
1992 case snd_soc_dapm_dac:
1993 case snd_soc_dapm_adc:
1994 case snd_soc_dapm_pga:
1995 case snd_soc_dapm_out_drv:
1996 case snd_soc_dapm_mixer:
1997 case snd_soc_dapm_mixer_named_ctl:
1998 case snd_soc_dapm_supply:
1999 case snd_soc_dapm_regulator_supply:
2000 case snd_soc_dapm_clock_supply:
2002 count += sprintf(buf + count, "%s: %s\n",
2003 w->name, w->power ? "On":"Off");
2010 switch (codec->dapm.bias_level) {
2011 case SND_SOC_BIAS_ON:
2014 case SND_SOC_BIAS_PREPARE:
2017 case SND_SOC_BIAS_STANDBY:
2020 case SND_SOC_BIAS_OFF:
2024 count += sprintf(buf + count, "PM State: %s\n", state);
2029 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2031 int snd_soc_dapm_sys_add(struct device *dev)
2033 return device_create_file(dev, &dev_attr_dapm_widget);
2036 static void snd_soc_dapm_sys_remove(struct device *dev)
2038 device_remove_file(dev, &dev_attr_dapm_widget);
2041 /* free all dapm widgets and resources */
2042 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2044 struct snd_soc_dapm_widget *w, *next_w;
2045 struct snd_soc_dapm_path *p, *next_p;
2047 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2048 if (w->dapm != dapm)
2052 * remove source and sink paths associated to this widget.
2053 * While removing the path, remove reference to it from both
2054 * source and sink widgets so that path is removed only once.
2056 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2057 list_del(&p->list_sink);
2058 list_del(&p->list_source);
2060 kfree(p->long_name);
2063 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2064 list_del(&p->list_sink);
2065 list_del(&p->list_source);
2067 kfree(p->long_name);
2070 kfree(w->kcontrols);
2076 static struct snd_soc_dapm_widget *dapm_find_widget(
2077 struct snd_soc_dapm_context *dapm, const char *pin,
2078 bool search_other_contexts)
2080 struct snd_soc_dapm_widget *w;
2081 struct snd_soc_dapm_widget *fallback = NULL;
2083 list_for_each_entry(w, &dapm->card->widgets, list) {
2084 if (!strcmp(w->name, pin)) {
2085 if (w->dapm == dapm)
2092 if (search_other_contexts)
2098 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2099 const char *pin, int status)
2101 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2104 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2108 if (w->connected != status)
2109 dapm_mark_dirty(w, "pin configuration");
2111 w->connected = status;
2119 * snd_soc_dapm_sync - scan and power dapm paths
2120 * @dapm: DAPM context
2122 * Walks all dapm audio paths and powers widgets according to their
2123 * stream or path usage.
2125 * Returns 0 for success.
2127 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2132 * Suppress early reports (eg, jacks syncing their state) to avoid
2133 * silly DAPM runs during card startup.
2135 if (!dapm->card || !dapm->card->instantiated)
2138 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2139 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2140 mutex_unlock(&dapm->card->dapm_mutex);
2143 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2145 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2146 const struct snd_soc_dapm_route *route)
2148 struct snd_soc_dapm_path *path;
2149 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2150 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2152 const char *control = route->control;
2154 char prefixed_sink[80];
2155 char prefixed_source[80];
2158 if (dapm->codec && dapm->codec->name_prefix) {
2159 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2160 dapm->codec->name_prefix, route->sink);
2161 sink = prefixed_sink;
2162 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2163 dapm->codec->name_prefix, route->source);
2164 source = prefixed_source;
2167 source = route->source;
2171 * find src and dest widgets over all widgets but favor a widget from
2172 * current DAPM context
2174 list_for_each_entry(w, &dapm->card->widgets, list) {
2175 if (!wsink && !(strcmp(w->name, sink))) {
2177 if (w->dapm == dapm)
2181 if (!wsource && !(strcmp(w->name, source))) {
2183 if (w->dapm == dapm)
2187 /* use widget from another DAPM context if not found from this */
2193 if (wsource == NULL || wsink == NULL)
2196 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2200 path->source = wsource;
2202 path->connected = route->connected;
2203 INIT_LIST_HEAD(&path->list);
2204 INIT_LIST_HEAD(&path->list_source);
2205 INIT_LIST_HEAD(&path->list_sink);
2207 /* check for external widgets */
2208 if (wsink->id == snd_soc_dapm_input) {
2209 if (wsource->id == snd_soc_dapm_micbias ||
2210 wsource->id == snd_soc_dapm_mic ||
2211 wsource->id == snd_soc_dapm_line ||
2212 wsource->id == snd_soc_dapm_output)
2215 if (wsource->id == snd_soc_dapm_output) {
2216 if (wsink->id == snd_soc_dapm_spk ||
2217 wsink->id == snd_soc_dapm_hp ||
2218 wsink->id == snd_soc_dapm_line ||
2219 wsink->id == snd_soc_dapm_input)
2223 /* connect static paths */
2224 if (control == NULL) {
2225 list_add(&path->list, &dapm->card->paths);
2226 list_add(&path->list_sink, &wsink->sources);
2227 list_add(&path->list_source, &wsource->sinks);
2232 /* connect dynamic paths */
2233 switch (wsink->id) {
2234 case snd_soc_dapm_adc:
2235 case snd_soc_dapm_dac:
2236 case snd_soc_dapm_pga:
2237 case snd_soc_dapm_out_drv:
2238 case snd_soc_dapm_input:
2239 case snd_soc_dapm_output:
2240 case snd_soc_dapm_siggen:
2241 case snd_soc_dapm_micbias:
2242 case snd_soc_dapm_vmid:
2243 case snd_soc_dapm_pre:
2244 case snd_soc_dapm_post:
2245 case snd_soc_dapm_supply:
2246 case snd_soc_dapm_regulator_supply:
2247 case snd_soc_dapm_clock_supply:
2248 case snd_soc_dapm_aif_in:
2249 case snd_soc_dapm_aif_out:
2250 case snd_soc_dapm_dai:
2251 case snd_soc_dapm_dai_link:
2252 list_add(&path->list, &dapm->card->paths);
2253 list_add(&path->list_sink, &wsink->sources);
2254 list_add(&path->list_source, &wsource->sinks);
2257 case snd_soc_dapm_mux:
2258 case snd_soc_dapm_virt_mux:
2259 case snd_soc_dapm_value_mux:
2260 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2261 &wsink->kcontrol_news[0]);
2265 case snd_soc_dapm_switch:
2266 case snd_soc_dapm_mixer:
2267 case snd_soc_dapm_mixer_named_ctl:
2268 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2272 case snd_soc_dapm_hp:
2273 case snd_soc_dapm_mic:
2274 case snd_soc_dapm_line:
2275 case snd_soc_dapm_spk:
2276 list_add(&path->list, &dapm->card->paths);
2277 list_add(&path->list_sink, &wsink->sources);
2278 list_add(&path->list_source, &wsource->sinks);
2283 dapm_mark_dirty(wsource, "Route added");
2284 dapm_mark_dirty(wsink, "Route added");
2289 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2290 source, control, sink);
2295 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2296 const struct snd_soc_dapm_route *route)
2298 struct snd_soc_dapm_path *path, *p;
2301 char prefixed_sink[80];
2302 char prefixed_source[80];
2304 if (route->control) {
2306 "Removal of routes with controls not supported\n");
2310 if (dapm->codec && dapm->codec->name_prefix) {
2311 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2312 dapm->codec->name_prefix, route->sink);
2313 sink = prefixed_sink;
2314 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2315 dapm->codec->name_prefix, route->source);
2316 source = prefixed_source;
2319 source = route->source;
2323 list_for_each_entry(p, &dapm->card->paths, list) {
2324 if (strcmp(p->source->name, source) != 0)
2326 if (strcmp(p->sink->name, sink) != 0)
2333 dapm_mark_dirty(path->source, "Route removed");
2334 dapm_mark_dirty(path->sink, "Route removed");
2336 list_del(&path->list);
2337 list_del(&path->list_sink);
2338 list_del(&path->list_source);
2341 dev_warn(dapm->dev, "Route %s->%s does not exist\n",
2349 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2350 * @dapm: DAPM context
2351 * @route: audio routes
2352 * @num: number of routes
2354 * Connects 2 dapm widgets together via a named audio path. The sink is
2355 * the widget receiving the audio signal, whilst the source is the sender
2356 * of the audio signal.
2358 * Returns 0 for success else error. On error all resources can be freed
2359 * with a call to snd_soc_card_free().
2361 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2362 const struct snd_soc_dapm_route *route, int num)
2366 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2367 for (i = 0; i < num; i++) {
2368 r = snd_soc_dapm_add_route(dapm, route);
2370 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2371 route->source, route->sink);
2376 mutex_unlock(&dapm->card->dapm_mutex);
2380 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2383 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2384 * @dapm: DAPM context
2385 * @route: audio routes
2386 * @num: number of routes
2388 * Removes routes from the DAPM context.
2390 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2391 const struct snd_soc_dapm_route *route, int num)
2395 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2396 for (i = 0; i < num; i++) {
2397 snd_soc_dapm_del_route(dapm, route);
2400 mutex_unlock(&dapm->card->dapm_mutex);
2404 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2406 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2407 const struct snd_soc_dapm_route *route)
2409 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2412 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2415 struct snd_soc_dapm_path *path;
2419 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2425 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2430 if (route->control || route->connected)
2431 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2432 route->source, route->sink);
2434 list_for_each_entry(path, &source->sinks, list_source) {
2435 if (path->sink == sink) {
2442 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2443 route->source, route->sink);
2445 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2446 count, route->source, route->sink);
2452 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2453 * @dapm: DAPM context
2454 * @route: audio routes
2455 * @num: number of routes
2457 * Mark existing routes matching those specified in the passed array
2458 * as being weak, meaning that they are ignored for the purpose of
2459 * power decisions. The main intended use case is for sidetone paths
2460 * which couple audio between other independent paths if they are both
2461 * active in order to make the combination work better at the user
2462 * level but which aren't intended to be "used".
2464 * Note that CODEC drivers should not use this as sidetone type paths
2465 * can frequently also be used as bypass paths.
2467 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2468 const struct snd_soc_dapm_route *route, int num)
2473 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2474 for (i = 0; i < num; i++) {
2475 err = snd_soc_dapm_weak_route(dapm, route);
2480 mutex_unlock(&dapm->card->dapm_mutex);
2484 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2487 * snd_soc_dapm_new_widgets - add new dapm widgets
2488 * @dapm: DAPM context
2490 * Checks the codec for any new dapm widgets and creates them if found.
2492 * Returns 0 for success.
2494 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2496 struct snd_soc_dapm_widget *w;
2499 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2501 list_for_each_entry(w, &dapm->card->widgets, list)
2506 if (w->num_kcontrols) {
2507 w->kcontrols = kzalloc(w->num_kcontrols *
2508 sizeof(struct snd_kcontrol *),
2510 if (!w->kcontrols) {
2511 mutex_unlock(&dapm->card->dapm_mutex);
2517 case snd_soc_dapm_switch:
2518 case snd_soc_dapm_mixer:
2519 case snd_soc_dapm_mixer_named_ctl:
2522 case snd_soc_dapm_mux:
2523 case snd_soc_dapm_virt_mux:
2524 case snd_soc_dapm_value_mux:
2527 case snd_soc_dapm_pga:
2528 case snd_soc_dapm_out_drv:
2535 /* Read the initial power state from the device */
2537 val = soc_widget_read(w, w->reg);
2538 val &= 1 << w->shift;
2548 dapm_mark_dirty(w, "new widget");
2549 dapm_debugfs_add_widget(w);
2552 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2553 mutex_unlock(&dapm->card->dapm_mutex);
2556 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2559 * snd_soc_dapm_get_volsw - dapm mixer get callback
2560 * @kcontrol: mixer control
2561 * @ucontrol: control element information
2563 * Callback to get the value of a dapm mixer control.
2565 * Returns 0 for success.
2567 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2568 struct snd_ctl_elem_value *ucontrol)
2570 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2571 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2572 struct soc_mixer_control *mc =
2573 (struct soc_mixer_control *)kcontrol->private_value;
2574 unsigned int reg = mc->reg;
2575 unsigned int shift = mc->shift;
2577 unsigned int mask = (1 << fls(max)) - 1;
2578 unsigned int invert = mc->invert;
2580 if (snd_soc_volsw_is_stereo(mc))
2581 dev_warn(widget->dapm->dev,
2582 "Control '%s' is stereo, which is not supported\n",
2585 ucontrol->value.integer.value[0] =
2586 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2588 ucontrol->value.integer.value[0] =
2589 max - ucontrol->value.integer.value[0];
2593 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2596 * snd_soc_dapm_put_volsw - dapm mixer set callback
2597 * @kcontrol: mixer control
2598 * @ucontrol: control element information
2600 * Callback to set the value of a dapm mixer control.
2602 * Returns 0 for success.
2604 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2605 struct snd_ctl_elem_value *ucontrol)
2607 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2608 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2609 struct snd_soc_codec *codec = widget->codec;
2610 struct snd_soc_card *card = codec->card;
2611 struct soc_mixer_control *mc =
2612 (struct soc_mixer_control *)kcontrol->private_value;
2613 unsigned int reg = mc->reg;
2614 unsigned int shift = mc->shift;
2616 unsigned int mask = (1 << fls(max)) - 1;
2617 unsigned int invert = mc->invert;
2619 int connect, change;
2620 struct snd_soc_dapm_update update;
2623 if (snd_soc_volsw_is_stereo(mc))
2624 dev_warn(widget->dapm->dev,
2625 "Control '%s' is stereo, which is not supported\n",
2628 val = (ucontrol->value.integer.value[0] & mask);
2633 mask = mask << shift;
2636 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2638 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2640 for (wi = 0; wi < wlist->num_widgets; wi++) {
2641 widget = wlist->widgets[wi];
2643 widget->value = val;
2645 update.kcontrol = kcontrol;
2646 update.widget = widget;
2650 widget->dapm->update = &update;
2652 soc_dapm_mixer_update_power(widget, kcontrol, connect);
2654 widget->dapm->update = NULL;
2658 mutex_unlock(&card->dapm_mutex);
2661 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2664 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2665 * @kcontrol: mixer control
2666 * @ucontrol: control element information
2668 * Callback to get the value of a dapm enumerated double mixer control.
2670 * Returns 0 for success.
2672 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2673 struct snd_ctl_elem_value *ucontrol)
2675 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2676 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2677 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2680 val = snd_soc_read(widget->codec, e->reg);
2681 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2682 if (e->shift_l != e->shift_r)
2683 ucontrol->value.enumerated.item[1] =
2684 (val >> e->shift_r) & e->mask;
2688 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2691 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2692 * @kcontrol: mixer control
2693 * @ucontrol: control element information
2695 * Callback to set the value of a dapm enumerated double mixer control.
2697 * Returns 0 for success.
2699 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2700 struct snd_ctl_elem_value *ucontrol)
2702 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2703 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2704 struct snd_soc_codec *codec = widget->codec;
2705 struct snd_soc_card *card = codec->card;
2706 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2707 unsigned int val, mux, change;
2709 struct snd_soc_dapm_update update;
2712 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2714 mux = ucontrol->value.enumerated.item[0];
2715 val = mux << e->shift_l;
2716 mask = e->mask << e->shift_l;
2717 if (e->shift_l != e->shift_r) {
2718 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2720 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2721 mask |= e->mask << e->shift_r;
2724 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2726 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2728 for (wi = 0; wi < wlist->num_widgets; wi++) {
2729 widget = wlist->widgets[wi];
2731 widget->value = val;
2733 update.kcontrol = kcontrol;
2734 update.widget = widget;
2735 update.reg = e->reg;
2738 widget->dapm->update = &update;
2740 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2742 widget->dapm->update = NULL;
2746 mutex_unlock(&card->dapm_mutex);
2749 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2752 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2753 * @kcontrol: mixer control
2754 * @ucontrol: control element information
2756 * Returns 0 for success.
2758 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_value *ucontrol)
2761 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2762 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2764 ucontrol->value.enumerated.item[0] = widget->value;
2768 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2771 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2772 * @kcontrol: mixer control
2773 * @ucontrol: control element information
2775 * Returns 0 for success.
2777 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2778 struct snd_ctl_elem_value *ucontrol)
2780 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2781 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2782 struct snd_soc_codec *codec = widget->codec;
2783 struct snd_soc_card *card = codec->card;
2784 struct soc_enum *e =
2785 (struct soc_enum *)kcontrol->private_value;
2790 if (ucontrol->value.enumerated.item[0] >= e->max)
2793 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2795 change = widget->value != ucontrol->value.enumerated.item[0];
2797 for (wi = 0; wi < wlist->num_widgets; wi++) {
2798 widget = wlist->widgets[wi];
2800 widget->value = ucontrol->value.enumerated.item[0];
2802 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
2806 mutex_unlock(&card->dapm_mutex);
2809 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2812 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2814 * @kcontrol: mixer control
2815 * @ucontrol: control element information
2817 * Callback to get the value of a dapm semi enumerated double mixer control.
2819 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2820 * used for handling bitfield coded enumeration for example.
2822 * Returns 0 for success.
2824 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2825 struct snd_ctl_elem_value *ucontrol)
2827 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2828 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2829 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2830 unsigned int reg_val, val, mux;
2832 reg_val = snd_soc_read(widget->codec, e->reg);
2833 val = (reg_val >> e->shift_l) & e->mask;
2834 for (mux = 0; mux < e->max; mux++) {
2835 if (val == e->values[mux])
2838 ucontrol->value.enumerated.item[0] = mux;
2839 if (e->shift_l != e->shift_r) {
2840 val = (reg_val >> e->shift_r) & e->mask;
2841 for (mux = 0; mux < e->max; mux++) {
2842 if (val == e->values[mux])
2845 ucontrol->value.enumerated.item[1] = mux;
2850 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2853 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2855 * @kcontrol: mixer control
2856 * @ucontrol: control element information
2858 * Callback to set the value of a dapm semi enumerated double mixer control.
2860 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2861 * used for handling bitfield coded enumeration for example.
2863 * Returns 0 for success.
2865 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2866 struct snd_ctl_elem_value *ucontrol)
2868 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2869 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2870 struct snd_soc_codec *codec = widget->codec;
2871 struct snd_soc_card *card = codec->card;
2872 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2873 unsigned int val, mux, change;
2875 struct snd_soc_dapm_update update;
2878 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2880 mux = ucontrol->value.enumerated.item[0];
2881 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2882 mask = e->mask << e->shift_l;
2883 if (e->shift_l != e->shift_r) {
2884 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2886 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2887 mask |= e->mask << e->shift_r;
2890 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2892 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2894 for (wi = 0; wi < wlist->num_widgets; wi++) {
2895 widget = wlist->widgets[wi];
2897 widget->value = val;
2899 update.kcontrol = kcontrol;
2900 update.widget = widget;
2901 update.reg = e->reg;
2904 widget->dapm->update = &update;
2906 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2908 widget->dapm->update = NULL;
2912 mutex_unlock(&card->dapm_mutex);
2915 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2918 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2920 * @kcontrol: mixer control
2921 * @uinfo: control element information
2923 * Callback to provide information about a pin switch control.
2925 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2926 struct snd_ctl_elem_info *uinfo)
2928 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2930 uinfo->value.integer.min = 0;
2931 uinfo->value.integer.max = 1;
2935 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2938 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2940 * @kcontrol: mixer control
2943 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2944 struct snd_ctl_elem_value *ucontrol)
2946 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2947 const char *pin = (const char *)kcontrol->private_value;
2949 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2951 ucontrol->value.integer.value[0] =
2952 snd_soc_dapm_get_pin_status(&card->dapm, pin);
2954 mutex_unlock(&card->dapm_mutex);
2958 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2961 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2963 * @kcontrol: mixer control
2966 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2967 struct snd_ctl_elem_value *ucontrol)
2969 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2970 const char *pin = (const char *)kcontrol->private_value;
2972 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2974 if (ucontrol->value.integer.value[0])
2975 snd_soc_dapm_enable_pin(&card->dapm, pin);
2977 snd_soc_dapm_disable_pin(&card->dapm, pin);
2979 mutex_unlock(&card->dapm_mutex);
2981 snd_soc_dapm_sync(&card->dapm);
2984 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2986 static struct snd_soc_dapm_widget *
2987 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2988 const struct snd_soc_dapm_widget *widget)
2990 struct snd_soc_dapm_widget *w;
2994 if ((w = dapm_cnew_widget(widget)) == NULL)
2998 case snd_soc_dapm_regulator_supply:
2999 w->regulator = devm_regulator_get(dapm->dev, w->name);
3000 if (IS_ERR(w->regulator)) {
3001 ret = PTR_ERR(w->regulator);
3002 dev_err(dapm->dev, "Failed to request %s: %d\n",
3007 case snd_soc_dapm_clock_supply:
3008 #ifdef CONFIG_CLKDEV_LOOKUP
3009 w->clk = devm_clk_get(dapm->dev, w->name);
3010 if (IS_ERR(w->clk)) {
3011 ret = PTR_ERR(w->clk);
3012 dev_err(dapm->dev, "Failed to request %s: %d\n",
3024 name_len = strlen(widget->name) + 1;
3025 if (dapm->codec && dapm->codec->name_prefix)
3026 name_len += 1 + strlen(dapm->codec->name_prefix);
3027 w->name = kmalloc(name_len, GFP_KERNEL);
3028 if (w->name == NULL) {
3032 if (dapm->codec && dapm->codec->name_prefix)
3033 snprintf((char *)w->name, name_len, "%s %s",
3034 dapm->codec->name_prefix, widget->name);
3036 snprintf((char *)w->name, name_len, "%s", widget->name);
3039 case snd_soc_dapm_switch:
3040 case snd_soc_dapm_mixer:
3041 case snd_soc_dapm_mixer_named_ctl:
3042 w->power_check = dapm_generic_check_power;
3044 case snd_soc_dapm_mux:
3045 case snd_soc_dapm_virt_mux:
3046 case snd_soc_dapm_value_mux:
3047 w->power_check = dapm_generic_check_power;
3049 case snd_soc_dapm_adc:
3050 case snd_soc_dapm_aif_out:
3051 w->power_check = dapm_adc_check_power;
3053 case snd_soc_dapm_dac:
3054 case snd_soc_dapm_aif_in:
3055 w->power_check = dapm_dac_check_power;
3057 case snd_soc_dapm_pga:
3058 case snd_soc_dapm_out_drv:
3059 case snd_soc_dapm_input:
3060 case snd_soc_dapm_output:
3061 case snd_soc_dapm_micbias:
3062 case snd_soc_dapm_spk:
3063 case snd_soc_dapm_hp:
3064 case snd_soc_dapm_mic:
3065 case snd_soc_dapm_line:
3066 case snd_soc_dapm_dai_link:
3067 w->power_check = dapm_generic_check_power;
3069 case snd_soc_dapm_supply:
3070 case snd_soc_dapm_regulator_supply:
3071 case snd_soc_dapm_clock_supply:
3072 w->power_check = dapm_supply_check_power;
3074 case snd_soc_dapm_dai:
3075 w->power_check = dapm_dai_check_power;
3078 w->power_check = dapm_always_on_check_power;
3084 w->codec = dapm->codec;
3085 w->platform = dapm->platform;
3086 INIT_LIST_HEAD(&w->sources);
3087 INIT_LIST_HEAD(&w->sinks);
3088 INIT_LIST_HEAD(&w->list);
3089 INIT_LIST_HEAD(&w->dirty);
3090 list_add(&w->list, &dapm->card->widgets);
3092 /* machine layer set ups unconnected pins and insertions */
3098 * snd_soc_dapm_new_controls - create new dapm controls
3099 * @dapm: DAPM context
3100 * @widget: widget array
3101 * @num: number of widgets
3103 * Creates new DAPM controls based upon the templates.
3105 * Returns 0 for success else error.
3107 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3108 const struct snd_soc_dapm_widget *widget,
3111 struct snd_soc_dapm_widget *w;
3115 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3116 for (i = 0; i < num; i++) {
3117 w = snd_soc_dapm_new_control(dapm, widget);
3120 "ASoC: Failed to create DAPM control %s\n",
3127 mutex_unlock(&dapm->card->dapm_mutex);
3130 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3132 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3133 struct snd_kcontrol *kcontrol, int event)
3135 struct snd_soc_dapm_path *source_p, *sink_p;
3136 struct snd_soc_dai *source, *sink;
3137 const struct snd_soc_pcm_stream *config = w->params;
3138 struct snd_pcm_substream substream;
3139 struct snd_pcm_hw_params *params = NULL;
3144 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3146 /* We only support a single source and sink, pick the first */
3147 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3149 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3152 BUG_ON(!source_p || !sink_p);
3153 BUG_ON(!sink_p->source || !source_p->sink);
3154 BUG_ON(!source_p->source || !sink_p->sink);
3156 source = source_p->source->priv;
3157 sink = sink_p->sink->priv;
3159 /* Be a little careful as we don't want to overflow the mask array */
3160 if (config->formats) {
3161 fmt = ffs(config->formats) - 1;
3163 dev_warn(w->dapm->dev, "Invalid format %llx specified\n",
3168 /* Currently very limited parameter selection */
3169 params = kzalloc(sizeof(*params), GFP_KERNEL);
3174 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3176 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3178 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3181 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3182 = config->channels_min;
3183 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3184 = config->channels_max;
3186 memset(&substream, 0, sizeof(substream));
3189 case SND_SOC_DAPM_PRE_PMU:
3190 if (source->driver->ops && source->driver->ops->hw_params) {
3191 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3192 ret = source->driver->ops->hw_params(&substream,
3195 dev_err(source->dev,
3196 "hw_params() failed: %d\n", ret);
3201 if (sink->driver->ops && sink->driver->ops->hw_params) {
3202 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3203 ret = sink->driver->ops->hw_params(&substream, params,
3207 "hw_params() failed: %d\n", ret);
3213 case SND_SOC_DAPM_POST_PMU:
3214 ret = snd_soc_dai_digital_mute(sink, 0);
3215 if (ret != 0 && ret != -ENOTSUPP)
3216 dev_warn(sink->dev, "Failed to unmute: %d\n", ret);
3220 case SND_SOC_DAPM_PRE_PMD:
3221 ret = snd_soc_dai_digital_mute(sink, 1);
3222 if (ret != 0 && ret != -ENOTSUPP)
3223 dev_warn(sink->dev, "Failed to mute: %d\n", ret);
3237 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3238 const struct snd_soc_pcm_stream *params,
3239 struct snd_soc_dapm_widget *source,
3240 struct snd_soc_dapm_widget *sink)
3242 struct snd_soc_dapm_route routes[2];
3243 struct snd_soc_dapm_widget template;
3244 struct snd_soc_dapm_widget *w;
3248 len = strlen(source->name) + strlen(sink->name) + 2;
3249 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3252 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3254 memset(&template, 0, sizeof(template));
3255 template.reg = SND_SOC_NOPM;
3256 template.id = snd_soc_dapm_dai_link;
3257 template.name = link_name;
3258 template.event = snd_soc_dai_link_event;
3259 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3260 SND_SOC_DAPM_PRE_PMD;
3262 dev_dbg(card->dev, "adding %s widget\n", link_name);
3264 w = snd_soc_dapm_new_control(&card->dapm, &template);
3266 dev_err(card->dev, "Failed to create %s widget\n",
3273 memset(&routes, 0, sizeof(routes));
3275 routes[0].source = source->name;
3276 routes[0].sink = link_name;
3277 routes[1].source = link_name;
3278 routes[1].sink = sink->name;
3280 return snd_soc_dapm_add_routes(&card->dapm, routes,
3281 ARRAY_SIZE(routes));
3284 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3285 struct snd_soc_dai *dai)
3287 struct snd_soc_dapm_widget template;
3288 struct snd_soc_dapm_widget *w;
3290 WARN_ON(dapm->dev != dai->dev);
3292 memset(&template, 0, sizeof(template));
3293 template.reg = SND_SOC_NOPM;
3295 if (dai->driver->playback.stream_name) {
3296 template.id = snd_soc_dapm_dai;
3297 template.name = dai->driver->playback.stream_name;
3298 template.sname = dai->driver->playback.stream_name;
3300 dev_dbg(dai->dev, "adding %s widget\n",
3303 w = snd_soc_dapm_new_control(dapm, &template);
3305 dev_err(dapm->dev, "Failed to create %s widget\n",
3306 dai->driver->playback.stream_name);
3310 dai->playback_widget = w;
3313 if (dai->driver->capture.stream_name) {
3314 template.id = snd_soc_dapm_dai;
3315 template.name = dai->driver->capture.stream_name;
3316 template.sname = dai->driver->capture.stream_name;
3318 dev_dbg(dai->dev, "adding %s widget\n",
3321 w = snd_soc_dapm_new_control(dapm, &template);
3323 dev_err(dapm->dev, "Failed to create %s widget\n",
3324 dai->driver->capture.stream_name);
3328 dai->capture_widget = w;
3334 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3336 struct snd_soc_dapm_widget *dai_w, *w;
3337 struct snd_soc_dai *dai;
3338 struct snd_soc_dapm_route r;
3340 memset(&r, 0, sizeof(r));
3342 /* For each DAI widget... */
3343 list_for_each_entry(dai_w, &card->widgets, list) {
3344 if (dai_w->id != snd_soc_dapm_dai)
3349 /* ...find all widgets with the same stream and link them */
3350 list_for_each_entry(w, &card->widgets, list) {
3351 if (w->dapm != dai_w->dapm)
3354 if (w->id == snd_soc_dapm_dai)
3360 if (dai->driver->playback.stream_name &&
3362 dai->driver->playback.stream_name)) {
3363 r.source = dai->playback_widget->name;
3365 dev_dbg(dai->dev, "%s -> %s\n",
3368 snd_soc_dapm_add_route(w->dapm, &r);
3371 if (dai->driver->capture.stream_name &&
3373 dai->driver->capture.stream_name)) {
3375 r.sink = dai->capture_widget->name;
3376 dev_dbg(dai->dev, "%s -> %s\n",
3379 snd_soc_dapm_add_route(w->dapm, &r);
3387 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3391 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3392 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3393 struct snd_soc_dai *codec_dai = rtd->codec_dai;
3395 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3396 w_cpu = cpu_dai->playback_widget;
3397 w_codec = codec_dai->playback_widget;
3399 w_cpu = cpu_dai->capture_widget;
3400 w_codec = codec_dai->capture_widget;
3405 dapm_mark_dirty(w_cpu, "stream event");
3408 case SND_SOC_DAPM_STREAM_START:
3411 case SND_SOC_DAPM_STREAM_STOP:
3414 case SND_SOC_DAPM_STREAM_SUSPEND:
3415 case SND_SOC_DAPM_STREAM_RESUME:
3416 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3417 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3424 dapm_mark_dirty(w_codec, "stream event");
3427 case SND_SOC_DAPM_STREAM_START:
3428 w_codec->active = 1;
3430 case SND_SOC_DAPM_STREAM_STOP:
3431 w_codec->active = 0;
3433 case SND_SOC_DAPM_STREAM_SUSPEND:
3434 case SND_SOC_DAPM_STREAM_RESUME:
3435 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3436 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3441 dapm_power_widgets(&rtd->card->dapm, event);
3445 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3446 * @rtd: PCM runtime data
3447 * @stream: stream name
3448 * @event: stream event
3450 * Sends a stream event to the dapm core. The core then makes any
3451 * necessary widget power changes.
3453 * Returns 0 for success else error.
3455 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3458 struct snd_soc_card *card = rtd->card;
3460 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3461 soc_dapm_stream_event(rtd, stream, event);
3462 mutex_unlock(&card->dapm_mutex);
3466 * snd_soc_dapm_enable_pin - enable pin.
3467 * @dapm: DAPM context
3470 * Enables input/output pin and its parents or children widgets iff there is
3471 * a valid audio route and active audio stream.
3472 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3473 * do any widget power switching.
3475 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3477 return snd_soc_dapm_set_pin(dapm, pin, 1);
3479 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3482 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3483 * @dapm: DAPM context
3486 * Enables input/output pin regardless of any other state. This is
3487 * intended for use with microphone bias supplies used in microphone
3490 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3491 * do any widget power switching.
3493 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3496 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3499 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3503 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3506 dapm_mark_dirty(w, "force enable");
3510 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3513 * snd_soc_dapm_disable_pin - disable pin.
3514 * @dapm: DAPM context
3517 * Disables input/output pin and its parents or children widgets.
3518 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3519 * do any widget power switching.
3521 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3524 return snd_soc_dapm_set_pin(dapm, pin, 0);
3526 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3529 * snd_soc_dapm_nc_pin - permanently disable pin.
3530 * @dapm: DAPM context
3533 * Marks the specified pin as being not connected, disabling it along
3534 * any parent or child widgets. At present this is identical to
3535 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3536 * additional things such as disabling controls which only affect
3537 * paths through the pin.
3539 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3540 * do any widget power switching.
3542 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3544 return snd_soc_dapm_set_pin(dapm, pin, 0);
3546 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3549 * snd_soc_dapm_get_pin_status - get audio pin status
3550 * @dapm: DAPM context
3551 * @pin: audio signal pin endpoint (or start point)
3553 * Get audio pin status - connected or disconnected.
3555 * Returns 1 for connected otherwise 0.
3557 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3560 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3563 return w->connected;
3567 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3570 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3571 * @dapm: DAPM context
3572 * @pin: audio signal pin endpoint (or start point)
3574 * Mark the given endpoint or pin as ignoring suspend. When the
3575 * system is disabled a path between two endpoints flagged as ignoring
3576 * suspend will not be disabled. The path must already be enabled via
3577 * normal means at suspend time, it will not be turned on if it was not
3580 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3583 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3586 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3590 w->ignore_suspend = 1;
3594 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3596 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3597 struct snd_soc_dapm_widget *w)
3599 struct snd_soc_dapm_path *p;
3601 list_for_each_entry(p, &card->paths, list) {
3602 if ((p->source == w) || (p->sink == w)) {
3604 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3605 p->source->name, p->source->id, p->source->dapm,
3606 p->sink->name, p->sink->id, p->sink->dapm);
3608 /* Connected to something other than the codec */
3609 if (p->source->dapm != p->sink->dapm)
3612 * Loopback connection from codec external pin to
3613 * codec external pin
3615 if (p->sink->id == snd_soc_dapm_input) {
3616 switch (p->source->id) {
3617 case snd_soc_dapm_output:
3618 case snd_soc_dapm_micbias:
3631 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3632 * @codec: The codec whose pins should be processed
3634 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3635 * which are unused. Pins are used if they are connected externally to the
3636 * codec, whether that be to some other device, or a loop-back connection to
3639 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3641 struct snd_soc_card *card = codec->card;
3642 struct snd_soc_dapm_context *dapm = &codec->dapm;
3643 struct snd_soc_dapm_widget *w;
3645 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
3646 &card->dapm, &codec->dapm);
3648 list_for_each_entry(w, &card->widgets, list) {
3649 if (w->dapm != dapm)
3652 case snd_soc_dapm_input:
3653 case snd_soc_dapm_output:
3654 case snd_soc_dapm_micbias:
3655 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
3657 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3659 "... Not in map; disabling\n");
3660 snd_soc_dapm_nc_pin(dapm, w->name);
3670 * snd_soc_dapm_free - free dapm resources
3671 * @dapm: DAPM context
3673 * Free all dapm widgets and resources.
3675 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3677 snd_soc_dapm_sys_remove(dapm->dev);
3678 dapm_debugfs_cleanup(dapm);
3679 dapm_free_widgets(dapm);
3680 list_del(&dapm->list);
3682 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3684 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3686 struct snd_soc_card *card = dapm->card;
3687 struct snd_soc_dapm_widget *w;
3688 LIST_HEAD(down_list);
3691 mutex_lock(&card->dapm_mutex);
3693 list_for_each_entry(w, &dapm->card->widgets, list) {
3694 if (w->dapm != dapm)
3697 dapm_seq_insert(w, &down_list, false);
3703 /* If there were no widgets to power down we're already in
3707 if (dapm->bias_level == SND_SOC_BIAS_ON)
3708 snd_soc_dapm_set_bias_level(dapm,
3709 SND_SOC_BIAS_PREPARE);
3710 dapm_seq_run(dapm, &down_list, 0, false);
3711 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3712 snd_soc_dapm_set_bias_level(dapm,
3713 SND_SOC_BIAS_STANDBY);
3716 mutex_unlock(&card->dapm_mutex);
3720 * snd_soc_dapm_shutdown - callback for system shutdown
3722 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3724 struct snd_soc_codec *codec;
3726 list_for_each_entry(codec, &card->codec_dev_list, list) {
3727 soc_dapm_shutdown_codec(&codec->dapm);
3728 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3729 snd_soc_dapm_set_bias_level(&codec->dapm,
3734 /* Module information */
3735 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3736 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3737 MODULE_LICENSE("GPL");