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_in] = 3,
59 [snd_soc_dapm_dai_out] = 3,
60 [snd_soc_dapm_aif_in] = 3,
61 [snd_soc_dapm_aif_out] = 3,
62 [snd_soc_dapm_mic] = 4,
63 [snd_soc_dapm_mux] = 5,
64 [snd_soc_dapm_virt_mux] = 5,
65 [snd_soc_dapm_value_mux] = 5,
66 [snd_soc_dapm_dac] = 6,
67 [snd_soc_dapm_mixer] = 7,
68 [snd_soc_dapm_mixer_named_ctl] = 7,
69 [snd_soc_dapm_pga] = 8,
70 [snd_soc_dapm_adc] = 9,
71 [snd_soc_dapm_out_drv] = 10,
72 [snd_soc_dapm_hp] = 10,
73 [snd_soc_dapm_spk] = 10,
74 [snd_soc_dapm_line] = 10,
75 [snd_soc_dapm_post] = 11,
78 static int dapm_down_seq[] = {
79 [snd_soc_dapm_pre] = 0,
80 [snd_soc_dapm_adc] = 1,
81 [snd_soc_dapm_hp] = 2,
82 [snd_soc_dapm_spk] = 2,
83 [snd_soc_dapm_line] = 2,
84 [snd_soc_dapm_out_drv] = 2,
85 [snd_soc_dapm_pga] = 4,
86 [snd_soc_dapm_mixer_named_ctl] = 5,
87 [snd_soc_dapm_mixer] = 5,
88 [snd_soc_dapm_dac] = 6,
89 [snd_soc_dapm_mic] = 7,
90 [snd_soc_dapm_micbias] = 8,
91 [snd_soc_dapm_mux] = 9,
92 [snd_soc_dapm_virt_mux] = 9,
93 [snd_soc_dapm_value_mux] = 9,
94 [snd_soc_dapm_aif_in] = 10,
95 [snd_soc_dapm_aif_out] = 10,
96 [snd_soc_dapm_dai_in] = 10,
97 [snd_soc_dapm_dai_out] = 10,
98 [snd_soc_dapm_dai_link] = 11,
99 [snd_soc_dapm_clock_supply] = 12,
100 [snd_soc_dapm_regulator_supply] = 12,
101 [snd_soc_dapm_supply] = 12,
102 [snd_soc_dapm_post] = 13,
105 static void pop_wait(u32 pop_time)
108 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
111 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
119 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
124 vsnprintf(buf, PAGE_SIZE, fmt, args);
125 dev_info(dev, "%s", buf);
131 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
133 return !list_empty(&w->dirty);
136 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
138 if (!dapm_dirty_widget(w)) {
139 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
141 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
144 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
146 void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
148 struct snd_soc_card *card = dapm->card;
149 struct snd_soc_dapm_widget *w;
151 mutex_lock(&card->dapm_mutex);
153 list_for_each_entry(w, &card->widgets, list) {
155 case snd_soc_dapm_input:
156 case snd_soc_dapm_output:
157 dapm_mark_dirty(w, "Rechecking inputs and outputs");
164 mutex_unlock(&card->dapm_mutex);
166 EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
168 /* create a new dapm widget */
169 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
170 const struct snd_soc_dapm_widget *_widget)
172 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
175 /* get snd_card from DAPM context */
176 static inline struct snd_card *dapm_get_snd_card(
177 struct snd_soc_dapm_context *dapm)
180 return dapm->codec->card->snd_card;
181 else if (dapm->platform)
182 return dapm->platform->card->snd_card;
190 /* get soc_card from DAPM context */
191 static inline struct snd_soc_card *dapm_get_soc_card(
192 struct snd_soc_dapm_context *dapm)
195 return dapm->codec->card;
196 else if (dapm->platform)
197 return dapm->platform->card;
205 static void dapm_reset(struct snd_soc_card *card)
207 struct snd_soc_dapm_widget *w;
209 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
211 list_for_each_entry(w, &card->widgets, list) {
212 w->power_checked = false;
218 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
221 return snd_soc_read(w->codec, reg);
222 else if (w->platform)
223 return snd_soc_platform_read(w->platform, reg);
225 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
229 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
232 return snd_soc_write(w->codec, reg, val);
233 else if (w->platform)
234 return snd_soc_platform_write(w->platform, reg, val);
236 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
240 static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
242 if (w->codec && !w->codec->using_regmap)
243 mutex_lock(&w->codec->mutex);
244 else if (w->platform)
245 mutex_lock(&w->platform->mutex);
248 static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
250 if (w->codec && !w->codec->using_regmap)
251 mutex_unlock(&w->codec->mutex);
252 else if (w->platform)
253 mutex_unlock(&w->platform->mutex);
256 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
257 unsigned short reg, unsigned int mask, unsigned int value)
260 unsigned int old, new;
263 if (w->codec && w->codec->using_regmap) {
264 ret = regmap_update_bits_check(w->codec->control_data,
265 reg, mask, value, &change);
270 ret = soc_widget_read(w, reg);
272 soc_widget_unlock(w);
277 new = (old & ~mask) | (value & mask);
280 ret = soc_widget_write(w, reg, new);
282 soc_widget_unlock(w);
286 soc_widget_unlock(w);
293 * snd_soc_dapm_set_bias_level - set the bias level for the system
294 * @dapm: DAPM context
295 * @level: level to configure
297 * Configure the bias (power) levels for the SoC audio device.
299 * Returns 0 for success else error.
301 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
302 enum snd_soc_bias_level level)
304 struct snd_soc_card *card = dapm->card;
307 trace_snd_soc_bias_level_start(card, level);
309 if (card && card->set_bias_level)
310 ret = card->set_bias_level(card, dapm, level);
315 if (dapm->codec->driver->set_bias_level)
316 ret = dapm->codec->driver->set_bias_level(dapm->codec,
319 dapm->bias_level = level;
320 } else if (!card || dapm != &card->dapm) {
321 dapm->bias_level = level;
327 if (card && card->set_bias_level_post)
328 ret = card->set_bias_level_post(card, dapm, level);
330 trace_snd_soc_bias_level_done(card, level);
335 /* set up initial codec paths */
336 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
337 struct snd_soc_dapm_path *p, int i)
340 case snd_soc_dapm_switch:
341 case snd_soc_dapm_mixer:
342 case snd_soc_dapm_mixer_named_ctl: {
344 struct soc_mixer_control *mc = (struct soc_mixer_control *)
345 w->kcontrol_news[i].private_value;
346 unsigned int reg = mc->reg;
347 unsigned int shift = mc->shift;
349 unsigned int mask = (1 << fls(max)) - 1;
350 unsigned int invert = mc->invert;
352 val = soc_widget_read(w, reg);
353 val = (val >> shift) & mask;
360 case snd_soc_dapm_mux: {
361 struct soc_enum *e = (struct soc_enum *)
362 w->kcontrol_news[i].private_value;
365 val = soc_widget_read(w, e->reg);
366 item = (val >> e->shift_l) & e->mask;
369 for (i = 0; i < e->max; i++) {
370 if (!(strcmp(p->name, e->texts[i])) && item == i)
375 case snd_soc_dapm_virt_mux: {
376 struct soc_enum *e = (struct soc_enum *)
377 w->kcontrol_news[i].private_value;
380 /* since a virtual mux has no backing registers to
381 * decide which path to connect, it will try to match
382 * with the first enumeration. This is to ensure
383 * that the default mux choice (the first) will be
384 * correctly powered up during initialization.
386 if (!strcmp(p->name, e->texts[0]))
390 case snd_soc_dapm_value_mux: {
391 struct soc_enum *e = (struct soc_enum *)
392 w->kcontrol_news[i].private_value;
395 val = soc_widget_read(w, e->reg);
396 val = (val >> e->shift_l) & e->mask;
397 for (item = 0; item < e->max; item++) {
398 if (val == e->values[item])
403 for (i = 0; i < e->max; i++) {
404 if (!(strcmp(p->name, e->texts[i])) && item == i)
409 /* does not affect routing - always connected */
410 case snd_soc_dapm_pga:
411 case snd_soc_dapm_out_drv:
412 case snd_soc_dapm_output:
413 case snd_soc_dapm_adc:
414 case snd_soc_dapm_input:
415 case snd_soc_dapm_siggen:
416 case snd_soc_dapm_dac:
417 case snd_soc_dapm_micbias:
418 case snd_soc_dapm_vmid:
419 case snd_soc_dapm_supply:
420 case snd_soc_dapm_regulator_supply:
421 case snd_soc_dapm_clock_supply:
422 case snd_soc_dapm_aif_in:
423 case snd_soc_dapm_aif_out:
424 case snd_soc_dapm_dai_in:
425 case snd_soc_dapm_dai_out:
426 case snd_soc_dapm_hp:
427 case snd_soc_dapm_mic:
428 case snd_soc_dapm_spk:
429 case snd_soc_dapm_line:
430 case snd_soc_dapm_dai_link:
433 /* does affect routing - dynamically connected */
434 case snd_soc_dapm_pre:
435 case snd_soc_dapm_post:
441 /* connect mux widget to its interconnecting audio paths */
442 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
443 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
444 struct snd_soc_dapm_path *path, const char *control_name,
445 const struct snd_kcontrol_new *kcontrol)
447 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
450 for (i = 0; i < e->max; i++) {
451 if (!(strcmp(control_name, e->texts[i]))) {
452 list_add(&path->list, &dapm->card->paths);
453 list_add(&path->list_sink, &dest->sources);
454 list_add(&path->list_source, &src->sinks);
455 path->name = (char*)e->texts[i];
456 dapm_set_path_status(dest, path, 0);
464 /* connect mixer widget to its interconnecting audio paths */
465 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
466 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
467 struct snd_soc_dapm_path *path, const char *control_name)
471 /* search for mixer kcontrol */
472 for (i = 0; i < dest->num_kcontrols; i++) {
473 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
474 list_add(&path->list, &dapm->card->paths);
475 list_add(&path->list_sink, &dest->sources);
476 list_add(&path->list_source, &src->sinks);
477 path->name = dest->kcontrol_news[i].name;
478 dapm_set_path_status(dest, path, i);
485 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
486 struct snd_soc_dapm_widget *kcontrolw,
487 const struct snd_kcontrol_new *kcontrol_new,
488 struct snd_kcontrol **kcontrol)
490 struct snd_soc_dapm_widget *w;
495 list_for_each_entry(w, &dapm->card->widgets, list) {
496 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
498 for (i = 0; i < w->num_kcontrols; i++) {
499 if (&w->kcontrol_news[i] == kcontrol_new) {
501 *kcontrol = w->kcontrols[i];
511 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
512 * create it. Either way, add the widget into the control's widget list
514 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
515 int kci, struct snd_soc_dapm_path *path)
517 struct snd_soc_dapm_context *dapm = w->dapm;
518 struct snd_card *card = dapm->card->snd_card;
522 struct snd_kcontrol *kcontrol;
523 struct snd_soc_dapm_widget_list *wlist;
526 bool wname_in_long_name, kcname_in_long_name;
533 prefix = dapm->codec->name_prefix;
538 prefix_len = strlen(prefix) + 1;
542 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
546 wlist = kcontrol->private_data;
547 wlistentries = wlist->num_widgets + 1;
553 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
554 wlistentries * sizeof(struct snd_soc_dapm_widget *);
555 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
557 dev_err(dapm->dev, "ASoC: can't allocate widget list for %s\n",
561 wlist->num_widgets = wlistentries;
562 wlist->widgets[wlistentries - 1] = w;
566 wname_in_long_name = false;
567 kcname_in_long_name = true;
570 case snd_soc_dapm_switch:
571 case snd_soc_dapm_mixer:
572 wname_in_long_name = true;
573 kcname_in_long_name = true;
575 case snd_soc_dapm_mixer_named_ctl:
576 wname_in_long_name = false;
577 kcname_in_long_name = true;
579 case snd_soc_dapm_mux:
580 case snd_soc_dapm_virt_mux:
581 case snd_soc_dapm_value_mux:
582 wname_in_long_name = true;
583 kcname_in_long_name = false;
591 if (wname_in_long_name && kcname_in_long_name) {
592 name_len = strlen(w->name) - prefix_len + 1 +
593 strlen(w->kcontrol_news[kci].name) + 1;
595 long_name = kmalloc(name_len, GFP_KERNEL);
596 if (long_name == NULL) {
602 * The control will get a prefix from the control
603 * creation process but we're also using the same
604 * prefix for widgets so cut the prefix off the
605 * front of the widget name.
607 snprintf(long_name, name_len, "%s %s",
608 w->name + prefix_len,
609 w->kcontrol_news[kci].name);
610 long_name[name_len - 1] = '\0';
613 } else if (wname_in_long_name) {
615 name = w->name + prefix_len;
618 name = w->kcontrol_news[kci].name;
621 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], wlist, name,
623 ret = snd_ctl_add(card, kcontrol);
626 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
633 path->long_name = long_name;
636 kcontrol->private_data = wlist;
637 w->kcontrols[kci] = kcontrol;
638 path->kcontrol = kcontrol;
643 /* create new dapm mixer control */
644 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
647 struct snd_soc_dapm_path *path;
650 for (i = 0; i < w->num_kcontrols; i++) {
652 list_for_each_entry(path, &w->sources, list_sink) {
653 /* mixer/mux paths name must match control name */
654 if (path->name != (char *)w->kcontrol_news[i].name)
657 if (w->kcontrols[i]) {
658 path->kcontrol = w->kcontrols[i];
662 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
671 /* create new dapm mux control */
672 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
674 struct snd_soc_dapm_context *dapm = w->dapm;
675 struct snd_soc_dapm_path *path;
678 if (w->num_kcontrols != 1) {
680 "ASoC: mux %s has incorrect number of controls\n",
685 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
688 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
692 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
696 list_for_each_entry(path, &w->sources, list_sink)
697 path->kcontrol = w->kcontrols[0];
702 /* create new dapm volume control */
703 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
705 if (w->num_kcontrols)
706 dev_err(w->dapm->dev,
707 "ASoC: PGA controls not supported: '%s'\n", w->name);
712 /* reset 'walked' bit for each dapm path */
713 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
714 struct list_head *sink)
716 struct snd_soc_dapm_path *p;
718 list_for_each_entry(p, sink, list_source) {
721 dapm_clear_walk_output(dapm, &p->sink->sinks);
726 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
727 struct list_head *source)
729 struct snd_soc_dapm_path *p;
731 list_for_each_entry(p, source, list_sink) {
734 dapm_clear_walk_input(dapm, &p->source->sources);
740 /* We implement power down on suspend by checking the power state of
741 * the ALSA card - when we are suspending the ALSA state for the card
744 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
746 int level = snd_power_get_state(widget->dapm->card->snd_card);
749 case SNDRV_CTL_POWER_D3hot:
750 case SNDRV_CTL_POWER_D3cold:
751 if (widget->ignore_suspend)
752 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
754 return widget->ignore_suspend;
760 /* add widget to list if it's not already in the list */
761 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
762 struct snd_soc_dapm_widget *w)
764 struct snd_soc_dapm_widget_list *wlist;
765 int wlistsize, wlistentries, i;
772 /* is this widget already in the list */
773 for (i = 0; i < wlist->num_widgets; i++) {
774 if (wlist->widgets[i] == w)
778 /* allocate some new space */
779 wlistentries = wlist->num_widgets + 1;
780 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
781 wlistentries * sizeof(struct snd_soc_dapm_widget *);
782 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
784 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
790 /* insert the widget */
791 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
792 w->name, wlist->num_widgets);
794 wlist->widgets[wlist->num_widgets] = w;
795 wlist->num_widgets++;
800 * Recursively check for a completed path to an active or physically connected
801 * output widget. Returns number of complete paths.
803 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
804 struct snd_soc_dapm_widget_list **list)
806 struct snd_soc_dapm_path *path;
809 if (widget->outputs >= 0)
810 return widget->outputs;
812 DAPM_UPDATE_STAT(widget, path_checks);
814 switch (widget->id) {
815 case snd_soc_dapm_supply:
816 case snd_soc_dapm_regulator_supply:
817 case snd_soc_dapm_clock_supply:
823 switch (widget->id) {
824 case snd_soc_dapm_adc:
825 case snd_soc_dapm_aif_out:
826 case snd_soc_dapm_dai_out:
827 if (widget->active) {
828 widget->outputs = snd_soc_dapm_suspend_check(widget);
829 return widget->outputs;
835 if (widget->connected) {
836 /* connected pin ? */
837 if (widget->id == snd_soc_dapm_output && !widget->ext) {
838 widget->outputs = snd_soc_dapm_suspend_check(widget);
839 return widget->outputs;
842 /* connected jack or spk ? */
843 if (widget->id == snd_soc_dapm_hp ||
844 widget->id == snd_soc_dapm_spk ||
845 (widget->id == snd_soc_dapm_line &&
846 !list_empty(&widget->sources))) {
847 widget->outputs = snd_soc_dapm_suspend_check(widget);
848 return widget->outputs;
852 list_for_each_entry(path, &widget->sinks, list_source) {
853 DAPM_UPDATE_STAT(widget, neighbour_checks);
864 trace_snd_soc_dapm_output_path(widget, path);
866 if (path->sink && path->connect) {
870 /* do we need to add this widget to the list ? */
873 err = dapm_list_add_widget(list, path->sink);
875 dev_err(widget->dapm->dev,
876 "ASoC: could not add widget %s\n",
883 con += is_connected_output_ep(path->sink, list);
889 widget->outputs = con;
895 * Recursively check for a completed path to an active or physically connected
896 * input widget. Returns number of complete paths.
898 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
899 struct snd_soc_dapm_widget_list **list)
901 struct snd_soc_dapm_path *path;
904 if (widget->inputs >= 0)
905 return widget->inputs;
907 DAPM_UPDATE_STAT(widget, path_checks);
909 switch (widget->id) {
910 case snd_soc_dapm_supply:
911 case snd_soc_dapm_regulator_supply:
912 case snd_soc_dapm_clock_supply:
918 /* active stream ? */
919 switch (widget->id) {
920 case snd_soc_dapm_dac:
921 case snd_soc_dapm_aif_in:
922 case snd_soc_dapm_dai_in:
923 if (widget->active) {
924 widget->inputs = snd_soc_dapm_suspend_check(widget);
925 return widget->inputs;
931 if (widget->connected) {
932 /* connected pin ? */
933 if (widget->id == snd_soc_dapm_input && !widget->ext) {
934 widget->inputs = snd_soc_dapm_suspend_check(widget);
935 return widget->inputs;
938 /* connected VMID/Bias for lower pops */
939 if (widget->id == snd_soc_dapm_vmid) {
940 widget->inputs = snd_soc_dapm_suspend_check(widget);
941 return widget->inputs;
944 /* connected jack ? */
945 if (widget->id == snd_soc_dapm_mic ||
946 (widget->id == snd_soc_dapm_line &&
947 !list_empty(&widget->sinks))) {
948 widget->inputs = snd_soc_dapm_suspend_check(widget);
949 return widget->inputs;
952 /* signal generator */
953 if (widget->id == snd_soc_dapm_siggen) {
954 widget->inputs = snd_soc_dapm_suspend_check(widget);
955 return widget->inputs;
959 list_for_each_entry(path, &widget->sources, list_sink) {
960 DAPM_UPDATE_STAT(widget, neighbour_checks);
971 trace_snd_soc_dapm_input_path(widget, path);
973 if (path->source && path->connect) {
977 /* do we need to add this widget to the list ? */
980 err = dapm_list_add_widget(list, path->source);
982 dev_err(widget->dapm->dev,
983 "ASoC: could not add widget %s\n",
990 con += is_connected_input_ep(path->source, list);
996 widget->inputs = con;
1002 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1003 * @dai: the soc DAI.
1004 * @stream: stream direction.
1005 * @list: list of active widgets for this stream.
1007 * Queries DAPM graph as to whether an valid audio stream path exists for
1008 * the initial stream specified by name. This takes into account
1009 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1011 * Returns the number of valid paths or negative error.
1013 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1014 struct snd_soc_dapm_widget_list **list)
1016 struct snd_soc_card *card = dai->card;
1019 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1022 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1023 paths = is_connected_output_ep(dai->playback_widget, list);
1024 dapm_clear_walk_output(&card->dapm,
1025 &dai->playback_widget->sinks);
1027 paths = is_connected_input_ep(dai->capture_widget, list);
1028 dapm_clear_walk_input(&card->dapm,
1029 &dai->capture_widget->sources);
1032 trace_snd_soc_dapm_connected(paths, stream);
1033 mutex_unlock(&card->dapm_mutex);
1039 * Handler for generic register modifier widget.
1041 int dapm_reg_event(struct snd_soc_dapm_widget *w,
1042 struct snd_kcontrol *kcontrol, int event)
1046 if (SND_SOC_DAPM_EVENT_ON(event))
1051 soc_widget_update_bits_locked(w, -(w->reg + 1),
1052 w->mask << w->shift, val << w->shift);
1056 EXPORT_SYMBOL_GPL(dapm_reg_event);
1059 * Handler for regulator supply widget.
1061 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1062 struct snd_kcontrol *kcontrol, int event)
1066 if (SND_SOC_DAPM_EVENT_ON(event)) {
1067 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1068 ret = regulator_allow_bypass(w->regulator, false);
1070 dev_warn(w->dapm->dev,
1071 "ASoC: Failed to bypass %s: %d\n",
1075 return regulator_enable(w->regulator);
1077 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1078 ret = regulator_allow_bypass(w->regulator, true);
1080 dev_warn(w->dapm->dev,
1081 "ASoC: Failed to unbypass %s: %d\n",
1085 return regulator_disable_deferred(w->regulator, w->shift);
1088 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1091 * Handler for clock supply widget.
1093 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1094 struct snd_kcontrol *kcontrol, int event)
1099 #ifdef CONFIG_HAVE_CLK
1100 if (SND_SOC_DAPM_EVENT_ON(event)) {
1101 return clk_prepare_enable(w->clk);
1103 clk_disable_unprepare(w->clk);
1109 EXPORT_SYMBOL_GPL(dapm_clock_event);
1111 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1113 if (w->power_checked)
1114 return w->new_power;
1119 w->new_power = w->power_check(w);
1121 w->power_checked = true;
1123 return w->new_power;
1126 /* Generic check to see if a widget should be powered.
1128 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1132 DAPM_UPDATE_STAT(w, power_checks);
1134 in = is_connected_input_ep(w, NULL);
1135 dapm_clear_walk_input(w->dapm, &w->sources);
1136 out = is_connected_output_ep(w, NULL);
1137 dapm_clear_walk_output(w->dapm, &w->sinks);
1138 return out != 0 && in != 0;
1141 /* Check to see if an ADC has power */
1142 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1146 DAPM_UPDATE_STAT(w, power_checks);
1149 in = is_connected_input_ep(w, NULL);
1150 dapm_clear_walk_input(w->dapm, &w->sources);
1153 return dapm_generic_check_power(w);
1157 /* Check to see if a DAC has power */
1158 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1162 DAPM_UPDATE_STAT(w, power_checks);
1165 out = is_connected_output_ep(w, NULL);
1166 dapm_clear_walk_output(w->dapm, &w->sinks);
1169 return dapm_generic_check_power(w);
1173 /* Check to see if a power supply is needed */
1174 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1176 struct snd_soc_dapm_path *path;
1178 DAPM_UPDATE_STAT(w, power_checks);
1180 /* Check if one of our outputs is connected */
1181 list_for_each_entry(path, &w->sinks, list_source) {
1182 DAPM_UPDATE_STAT(w, neighbour_checks);
1187 if (path->connected &&
1188 !path->connected(path->source, path->sink))
1194 if (dapm_widget_power_check(path->sink))
1201 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1206 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1207 struct snd_soc_dapm_widget *b,
1215 sort = dapm_down_seq;
1217 if (sort[a->id] != sort[b->id])
1218 return sort[a->id] - sort[b->id];
1219 if (a->subseq != b->subseq) {
1221 return a->subseq - b->subseq;
1223 return b->subseq - a->subseq;
1225 if (a->reg != b->reg)
1226 return a->reg - b->reg;
1227 if (a->dapm != b->dapm)
1228 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1233 /* Insert a widget in order into a DAPM power sequence. */
1234 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1235 struct list_head *list,
1238 struct snd_soc_dapm_widget *w;
1240 list_for_each_entry(w, list, power_list)
1241 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1242 list_add_tail(&new_widget->power_list, &w->power_list);
1246 list_add_tail(&new_widget->power_list, list);
1249 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1250 struct snd_soc_dapm_widget *w, int event)
1252 struct snd_soc_card *card = dapm->card;
1253 const char *ev_name;
1257 case SND_SOC_DAPM_PRE_PMU:
1258 ev_name = "PRE_PMU";
1261 case SND_SOC_DAPM_POST_PMU:
1262 ev_name = "POST_PMU";
1265 case SND_SOC_DAPM_PRE_PMD:
1266 ev_name = "PRE_PMD";
1269 case SND_SOC_DAPM_POST_PMD:
1270 ev_name = "POST_PMD";
1278 if (w->power != power)
1281 if (w->event && (w->event_flags & event)) {
1282 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1284 trace_snd_soc_dapm_widget_event_start(w, event);
1285 ret = w->event(w, NULL, event);
1286 trace_snd_soc_dapm_widget_event_done(w, event);
1288 dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
1289 ev_name, w->name, ret);
1293 /* Apply the coalesced changes from a DAPM sequence */
1294 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1295 struct list_head *pending)
1297 struct snd_soc_card *card = dapm->card;
1298 struct snd_soc_dapm_widget *w;
1300 unsigned int value = 0;
1301 unsigned int mask = 0;
1302 unsigned int cur_mask;
1304 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1307 list_for_each_entry(w, pending, power_list) {
1308 cur_mask = 1 << w->shift;
1309 BUG_ON(reg != w->reg);
1320 pop_dbg(dapm->dev, card->pop_time,
1321 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1322 w->name, reg, value, mask);
1324 /* Check for events */
1325 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1326 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1330 /* Any widget will do, they should all be updating the
1333 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1336 pop_dbg(dapm->dev, card->pop_time,
1337 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1338 value, mask, reg, card->pop_time);
1339 pop_wait(card->pop_time);
1340 soc_widget_update_bits_locked(w, reg, mask, value);
1343 list_for_each_entry(w, pending, power_list) {
1344 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1345 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1349 /* Apply a DAPM power sequence.
1351 * We walk over a pre-sorted list of widgets to apply power to. In
1352 * order to minimise the number of writes to the device required
1353 * multiple widgets will be updated in a single write where possible.
1354 * Currently anything that requires more than a single write is not
1357 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1358 struct list_head *list, int event, bool power_up)
1360 struct snd_soc_dapm_widget *w, *n;
1363 int cur_subseq = -1;
1364 int cur_reg = SND_SOC_NOPM;
1365 struct snd_soc_dapm_context *cur_dapm = NULL;
1372 sort = dapm_down_seq;
1374 list_for_each_entry_safe(w, n, list, power_list) {
1377 /* Do we need to apply any queued changes? */
1378 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1379 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1380 if (!list_empty(&pending))
1381 dapm_seq_run_coalesced(cur_dapm, &pending);
1383 if (cur_dapm && cur_dapm->seq_notifier) {
1384 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1385 if (sort[i] == cur_sort)
1386 cur_dapm->seq_notifier(cur_dapm,
1391 INIT_LIST_HEAD(&pending);
1393 cur_subseq = INT_MIN;
1394 cur_reg = SND_SOC_NOPM;
1399 case snd_soc_dapm_pre:
1401 list_for_each_entry_safe_continue(w, n, list,
1404 if (event == SND_SOC_DAPM_STREAM_START)
1406 NULL, SND_SOC_DAPM_PRE_PMU);
1407 else if (event == SND_SOC_DAPM_STREAM_STOP)
1409 NULL, SND_SOC_DAPM_PRE_PMD);
1412 case snd_soc_dapm_post:
1414 list_for_each_entry_safe_continue(w, n, list,
1417 if (event == SND_SOC_DAPM_STREAM_START)
1419 NULL, SND_SOC_DAPM_POST_PMU);
1420 else if (event == SND_SOC_DAPM_STREAM_STOP)
1422 NULL, SND_SOC_DAPM_POST_PMD);
1426 /* Queue it up for application */
1427 cur_sort = sort[w->id];
1428 cur_subseq = w->subseq;
1431 list_move(&w->power_list, &pending);
1436 dev_err(w->dapm->dev,
1437 "ASoC: Failed to apply widget power: %d\n", ret);
1440 if (!list_empty(&pending))
1441 dapm_seq_run_coalesced(cur_dapm, &pending);
1443 if (cur_dapm && cur_dapm->seq_notifier) {
1444 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1445 if (sort[i] == cur_sort)
1446 cur_dapm->seq_notifier(cur_dapm,
1451 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1453 struct snd_soc_dapm_update *update = dapm->update;
1454 struct snd_soc_dapm_widget *w;
1463 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1464 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1466 dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1470 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1473 dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1477 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1478 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1480 dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1485 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1486 * they're changing state.
1488 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1490 struct snd_soc_dapm_context *d = data;
1493 /* If we're off and we're not supposed to be go into STANDBY */
1494 if (d->bias_level == SND_SOC_BIAS_OFF &&
1495 d->target_bias_level != SND_SOC_BIAS_OFF) {
1497 pm_runtime_get_sync(d->dev);
1499 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1502 "ASoC: Failed to turn on bias: %d\n", ret);
1505 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1506 if (d->bias_level != d->target_bias_level) {
1507 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1510 "ASoC: Failed to prepare bias: %d\n", ret);
1514 /* Async callback run prior to DAPM sequences - brings to their final
1517 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1519 struct snd_soc_dapm_context *d = data;
1522 /* If we just powered the last thing off drop to standby bias */
1523 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1524 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1525 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1526 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1528 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1532 /* If we're in standby and can support bias off then do that */
1533 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1534 d->target_bias_level == SND_SOC_BIAS_OFF) {
1535 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1537 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1541 pm_runtime_put(d->dev);
1544 /* If we just powered up then move to active bias */
1545 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1546 d->target_bias_level == SND_SOC_BIAS_ON) {
1547 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1549 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1554 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1555 bool power, bool connect)
1557 /* If a connection is being made or broken then that update
1558 * will have marked the peer dirty, otherwise the widgets are
1559 * not connected and this update has no impact. */
1563 /* If the peer is already in the state we're moving to then we
1564 * won't have an impact on it. */
1565 if (power != peer->power)
1566 dapm_mark_dirty(peer, "peer state change");
1569 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1570 struct list_head *up_list,
1571 struct list_head *down_list)
1573 struct snd_soc_dapm_path *path;
1575 if (w->power == power)
1578 trace_snd_soc_dapm_widget_power(w, power);
1580 /* If we changed our power state perhaps our neigbours changed
1583 list_for_each_entry(path, &w->sources, list_sink) {
1585 dapm_widget_set_peer_power(path->source, power,
1590 case snd_soc_dapm_supply:
1591 case snd_soc_dapm_regulator_supply:
1592 case snd_soc_dapm_clock_supply:
1593 /* Supplies can't affect their outputs, only their inputs */
1596 list_for_each_entry(path, &w->sinks, list_source) {
1598 dapm_widget_set_peer_power(path->sink, power,
1606 dapm_seq_insert(w, up_list, true);
1608 dapm_seq_insert(w, down_list, false);
1613 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1614 struct list_head *up_list,
1615 struct list_head *down_list)
1620 case snd_soc_dapm_pre:
1621 dapm_seq_insert(w, down_list, false);
1623 case snd_soc_dapm_post:
1624 dapm_seq_insert(w, up_list, true);
1628 power = dapm_widget_power_check(w);
1630 dapm_widget_set_power(w, power, up_list, down_list);
1636 * Scan each dapm widget for complete audio path.
1637 * A complete path is a route that has valid endpoints i.e.:-
1639 * o DAC to output pin.
1640 * o Input Pin to ADC.
1641 * o Input pin to Output pin (bypass, sidetone)
1642 * o DAC to ADC (loopback).
1644 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1646 struct snd_soc_card *card = dapm->card;
1647 struct snd_soc_dapm_widget *w;
1648 struct snd_soc_dapm_context *d;
1650 LIST_HEAD(down_list);
1651 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1652 enum snd_soc_bias_level bias;
1654 trace_snd_soc_dapm_start(card);
1656 list_for_each_entry(d, &card->dapm_list, list) {
1657 if (d->idle_bias_off)
1658 d->target_bias_level = SND_SOC_BIAS_OFF;
1660 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1665 /* Check which widgets we need to power and store them in
1666 * lists indicating if they should be powered up or down. We
1667 * only check widgets that have been flagged as dirty but note
1668 * that new widgets may be added to the dirty list while we
1671 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1672 dapm_power_one_widget(w, &up_list, &down_list);
1675 list_for_each_entry(w, &card->widgets, list) {
1677 case snd_soc_dapm_pre:
1678 case snd_soc_dapm_post:
1679 /* These widgets always need to be powered */
1682 list_del_init(&w->dirty);
1689 /* Supplies and micbiases only bring the
1690 * context up to STANDBY as unless something
1691 * else is active and passing audio they
1692 * generally don't require full power. Signal
1693 * generators are virtual pins and have no
1694 * power impact themselves.
1697 case snd_soc_dapm_siggen:
1699 case snd_soc_dapm_supply:
1700 case snd_soc_dapm_regulator_supply:
1701 case snd_soc_dapm_clock_supply:
1702 case snd_soc_dapm_micbias:
1703 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1704 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1707 d->target_bias_level = SND_SOC_BIAS_ON;
1714 /* Force all contexts in the card to the same bias state if
1715 * they're not ground referenced.
1717 bias = SND_SOC_BIAS_OFF;
1718 list_for_each_entry(d, &card->dapm_list, list)
1719 if (d->target_bias_level > bias)
1720 bias = d->target_bias_level;
1721 list_for_each_entry(d, &card->dapm_list, list)
1722 if (!d->idle_bias_off)
1723 d->target_bias_level = bias;
1725 trace_snd_soc_dapm_walk_done(card);
1727 /* Run all the bias changes in parallel */
1728 list_for_each_entry(d, &dapm->card->dapm_list, list)
1729 async_schedule_domain(dapm_pre_sequence_async, d,
1731 async_synchronize_full_domain(&async_domain);
1733 /* Power down widgets first; try to avoid amplifying pops. */
1734 dapm_seq_run(dapm, &down_list, event, false);
1736 dapm_widget_update(dapm);
1739 dapm_seq_run(dapm, &up_list, event, true);
1741 /* Run all the bias changes in parallel */
1742 list_for_each_entry(d, &dapm->card->dapm_list, list)
1743 async_schedule_domain(dapm_post_sequence_async, d,
1745 async_synchronize_full_domain(&async_domain);
1747 /* do we need to notify any clients that DAPM event is complete */
1748 list_for_each_entry(d, &card->dapm_list, list) {
1749 if (d->stream_event)
1750 d->stream_event(d, event);
1753 pop_dbg(dapm->dev, card->pop_time,
1754 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1755 pop_wait(card->pop_time);
1757 trace_snd_soc_dapm_done(card);
1762 #ifdef CONFIG_DEBUG_FS
1763 static ssize_t dapm_widget_power_read_file(struct file *file,
1764 char __user *user_buf,
1765 size_t count, loff_t *ppos)
1767 struct snd_soc_dapm_widget *w = file->private_data;
1771 struct snd_soc_dapm_path *p = NULL;
1773 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1777 in = is_connected_input_ep(w, NULL);
1778 dapm_clear_walk_input(w->dapm, &w->sources);
1779 out = is_connected_output_ep(w, NULL);
1780 dapm_clear_walk_output(w->dapm, &w->sinks);
1782 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1783 w->name, w->power ? "On" : "Off",
1784 w->force ? " (forced)" : "", in, out);
1787 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1788 " - R%d(0x%x) bit %d",
1789 w->reg, w->reg, w->shift);
1791 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1794 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1796 w->active ? "active" : "inactive");
1798 list_for_each_entry(p, &w->sources, list_sink) {
1799 if (p->connected && !p->connected(w, p->sink))
1803 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1804 " in \"%s\" \"%s\"\n",
1805 p->name ? p->name : "static",
1808 list_for_each_entry(p, &w->sinks, list_source) {
1809 if (p->connected && !p->connected(w, p->sink))
1813 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1814 " out \"%s\" \"%s\"\n",
1815 p->name ? p->name : "static",
1819 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1825 static const struct file_operations dapm_widget_power_fops = {
1826 .open = simple_open,
1827 .read = dapm_widget_power_read_file,
1828 .llseek = default_llseek,
1831 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1832 size_t count, loff_t *ppos)
1834 struct snd_soc_dapm_context *dapm = file->private_data;
1837 switch (dapm->bias_level) {
1838 case SND_SOC_BIAS_ON:
1841 case SND_SOC_BIAS_PREPARE:
1842 level = "Prepare\n";
1844 case SND_SOC_BIAS_STANDBY:
1845 level = "Standby\n";
1847 case SND_SOC_BIAS_OFF:
1852 level = "Unknown\n";
1856 return simple_read_from_buffer(user_buf, count, ppos, level,
1860 static const struct file_operations dapm_bias_fops = {
1861 .open = simple_open,
1862 .read = dapm_bias_read_file,
1863 .llseek = default_llseek,
1866 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1867 struct dentry *parent)
1871 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1873 if (!dapm->debugfs_dapm) {
1875 "ASoC: Failed to create DAPM debugfs directory\n");
1879 d = debugfs_create_file("bias_level", 0444,
1880 dapm->debugfs_dapm, dapm,
1884 "ASoC: Failed to create bias level debugfs file\n");
1887 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1889 struct snd_soc_dapm_context *dapm = w->dapm;
1892 if (!dapm->debugfs_dapm || !w->name)
1895 d = debugfs_create_file(w->name, 0444,
1896 dapm->debugfs_dapm, w,
1897 &dapm_widget_power_fops);
1899 dev_warn(w->dapm->dev,
1900 "ASoC: Failed to create %s debugfs file\n",
1904 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1906 debugfs_remove_recursive(dapm->debugfs_dapm);
1910 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1911 struct dentry *parent)
1915 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1919 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1925 /* test and update the power status of a mux widget */
1926 static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1927 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1929 struct snd_soc_dapm_path *path;
1932 if (widget->id != snd_soc_dapm_mux &&
1933 widget->id != snd_soc_dapm_virt_mux &&
1934 widget->id != snd_soc_dapm_value_mux)
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 if (!path->name || !e->texts[mux])
1946 /* we now need to match the string in the enum to the path */
1947 if (!(strcmp(path->name, e->texts[mux]))) {
1948 path->connect = 1; /* new connection */
1949 dapm_mark_dirty(path->source, "mux connection");
1952 dapm_mark_dirty(path->source,
1953 "mux disconnection");
1954 path->connect = 0; /* old connection must be powered down */
1959 dapm_mark_dirty(widget, "mux change");
1960 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1966 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1967 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1969 struct snd_soc_card *card = widget->dapm->card;
1972 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1973 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1974 mutex_unlock(&card->dapm_mutex);
1976 soc_dpcm_runtime_update(widget);
1979 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1981 /* test and update the power status of a mixer or switch widget */
1982 static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1983 struct snd_kcontrol *kcontrol, int connect)
1985 struct snd_soc_dapm_path *path;
1988 if (widget->id != snd_soc_dapm_mixer &&
1989 widget->id != snd_soc_dapm_mixer_named_ctl &&
1990 widget->id != snd_soc_dapm_switch)
1993 /* find dapm widget path assoc with kcontrol */
1994 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1995 if (path->kcontrol != kcontrol)
1998 /* found, now check type */
2000 path->connect = connect;
2001 dapm_mark_dirty(path->source, "mixer connection");
2005 dapm_mark_dirty(widget, "mixer update");
2006 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
2012 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
2013 struct snd_kcontrol *kcontrol, int connect)
2015 struct snd_soc_card *card = widget->dapm->card;
2018 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2019 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
2020 mutex_unlock(&card->dapm_mutex);
2022 soc_dpcm_runtime_update(widget);
2025 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2027 /* show dapm widget status in sys fs */
2028 static ssize_t dapm_widget_show(struct device *dev,
2029 struct device_attribute *attr, char *buf)
2031 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2032 struct snd_soc_codec *codec =rtd->codec;
2033 struct snd_soc_dapm_widget *w;
2035 char *state = "not set";
2037 list_for_each_entry(w, &codec->card->widgets, list) {
2038 if (w->dapm != &codec->dapm)
2041 /* only display widgets that burnm power */
2043 case snd_soc_dapm_hp:
2044 case snd_soc_dapm_mic:
2045 case snd_soc_dapm_spk:
2046 case snd_soc_dapm_line:
2047 case snd_soc_dapm_micbias:
2048 case snd_soc_dapm_dac:
2049 case snd_soc_dapm_adc:
2050 case snd_soc_dapm_pga:
2051 case snd_soc_dapm_out_drv:
2052 case snd_soc_dapm_mixer:
2053 case snd_soc_dapm_mixer_named_ctl:
2054 case snd_soc_dapm_supply:
2055 case snd_soc_dapm_regulator_supply:
2056 case snd_soc_dapm_clock_supply:
2058 count += sprintf(buf + count, "%s: %s\n",
2059 w->name, w->power ? "On":"Off");
2066 switch (codec->dapm.bias_level) {
2067 case SND_SOC_BIAS_ON:
2070 case SND_SOC_BIAS_PREPARE:
2073 case SND_SOC_BIAS_STANDBY:
2076 case SND_SOC_BIAS_OFF:
2080 count += sprintf(buf + count, "PM State: %s\n", state);
2085 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2087 int snd_soc_dapm_sys_add(struct device *dev)
2089 return device_create_file(dev, &dev_attr_dapm_widget);
2092 static void snd_soc_dapm_sys_remove(struct device *dev)
2094 device_remove_file(dev, &dev_attr_dapm_widget);
2097 /* free all dapm widgets and resources */
2098 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2100 struct snd_soc_dapm_widget *w, *next_w;
2101 struct snd_soc_dapm_path *p, *next_p;
2103 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2104 if (w->dapm != dapm)
2108 * remove source and sink paths associated to this widget.
2109 * While removing the path, remove reference to it from both
2110 * source and sink widgets so that path is removed only once.
2112 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2113 list_del(&p->list_sink);
2114 list_del(&p->list_source);
2116 kfree(p->long_name);
2119 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2120 list_del(&p->list_sink);
2121 list_del(&p->list_source);
2123 kfree(p->long_name);
2126 kfree(w->kcontrols);
2132 static struct snd_soc_dapm_widget *dapm_find_widget(
2133 struct snd_soc_dapm_context *dapm, const char *pin,
2134 bool search_other_contexts)
2136 struct snd_soc_dapm_widget *w;
2137 struct snd_soc_dapm_widget *fallback = NULL;
2139 list_for_each_entry(w, &dapm->card->widgets, list) {
2140 if (!strcmp(w->name, pin)) {
2141 if (w->dapm == dapm)
2148 if (search_other_contexts)
2154 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2155 const char *pin, int status)
2157 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2160 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2164 if (w->connected != status)
2165 dapm_mark_dirty(w, "pin configuration");
2167 w->connected = status;
2175 * snd_soc_dapm_sync - scan and power dapm paths
2176 * @dapm: DAPM context
2178 * Walks all dapm audio paths and powers widgets according to their
2179 * stream or path usage.
2181 * Returns 0 for success.
2183 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2188 * Suppress early reports (eg, jacks syncing their state) to avoid
2189 * silly DAPM runs during card startup.
2191 if (!dapm->card || !dapm->card->instantiated)
2194 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2195 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2196 mutex_unlock(&dapm->card->dapm_mutex);
2199 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2201 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2202 const struct snd_soc_dapm_route *route)
2204 struct snd_soc_dapm_path *path;
2205 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2206 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2208 const char *control = route->control;
2210 char prefixed_sink[80];
2211 char prefixed_source[80];
2214 if (dapm->codec && dapm->codec->name_prefix) {
2215 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2216 dapm->codec->name_prefix, route->sink);
2217 sink = prefixed_sink;
2218 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2219 dapm->codec->name_prefix, route->source);
2220 source = prefixed_source;
2223 source = route->source;
2227 * find src and dest widgets over all widgets but favor a widget from
2228 * current DAPM context
2230 list_for_each_entry(w, &dapm->card->widgets, list) {
2231 if (!wsink && !(strcmp(w->name, sink))) {
2233 if (w->dapm == dapm)
2237 if (!wsource && !(strcmp(w->name, source))) {
2239 if (w->dapm == dapm)
2243 /* use widget from another DAPM context if not found from this */
2249 if (wsource == NULL) {
2250 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2254 if (wsink == NULL) {
2255 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2260 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2264 path->source = wsource;
2266 path->connected = route->connected;
2267 INIT_LIST_HEAD(&path->list);
2268 INIT_LIST_HEAD(&path->list_source);
2269 INIT_LIST_HEAD(&path->list_sink);
2271 /* check for external widgets */
2272 if (wsink->id == snd_soc_dapm_input) {
2273 if (wsource->id == snd_soc_dapm_micbias ||
2274 wsource->id == snd_soc_dapm_mic ||
2275 wsource->id == snd_soc_dapm_line ||
2276 wsource->id == snd_soc_dapm_output)
2279 if (wsource->id == snd_soc_dapm_output) {
2280 if (wsink->id == snd_soc_dapm_spk ||
2281 wsink->id == snd_soc_dapm_hp ||
2282 wsink->id == snd_soc_dapm_line ||
2283 wsink->id == snd_soc_dapm_input)
2287 /* connect static paths */
2288 if (control == NULL) {
2289 list_add(&path->list, &dapm->card->paths);
2290 list_add(&path->list_sink, &wsink->sources);
2291 list_add(&path->list_source, &wsource->sinks);
2296 /* connect dynamic paths */
2297 switch (wsink->id) {
2298 case snd_soc_dapm_adc:
2299 case snd_soc_dapm_dac:
2300 case snd_soc_dapm_pga:
2301 case snd_soc_dapm_out_drv:
2302 case snd_soc_dapm_input:
2303 case snd_soc_dapm_output:
2304 case snd_soc_dapm_siggen:
2305 case snd_soc_dapm_micbias:
2306 case snd_soc_dapm_vmid:
2307 case snd_soc_dapm_pre:
2308 case snd_soc_dapm_post:
2309 case snd_soc_dapm_supply:
2310 case snd_soc_dapm_regulator_supply:
2311 case snd_soc_dapm_clock_supply:
2312 case snd_soc_dapm_aif_in:
2313 case snd_soc_dapm_aif_out:
2314 case snd_soc_dapm_dai_in:
2315 case snd_soc_dapm_dai_out:
2316 case snd_soc_dapm_dai_link:
2317 list_add(&path->list, &dapm->card->paths);
2318 list_add(&path->list_sink, &wsink->sources);
2319 list_add(&path->list_source, &wsource->sinks);
2322 case snd_soc_dapm_mux:
2323 case snd_soc_dapm_virt_mux:
2324 case snd_soc_dapm_value_mux:
2325 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2326 &wsink->kcontrol_news[0]);
2330 case snd_soc_dapm_switch:
2331 case snd_soc_dapm_mixer:
2332 case snd_soc_dapm_mixer_named_ctl:
2333 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2337 case snd_soc_dapm_hp:
2338 case snd_soc_dapm_mic:
2339 case snd_soc_dapm_line:
2340 case snd_soc_dapm_spk:
2341 list_add(&path->list, &dapm->card->paths);
2342 list_add(&path->list_sink, &wsink->sources);
2343 list_add(&path->list_source, &wsource->sinks);
2348 dapm_mark_dirty(wsource, "Route added");
2349 dapm_mark_dirty(wsink, "Route added");
2354 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2355 source, control, sink);
2360 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2361 const struct snd_soc_dapm_route *route)
2363 struct snd_soc_dapm_path *path, *p;
2366 char prefixed_sink[80];
2367 char prefixed_source[80];
2369 if (route->control) {
2371 "ASoC: Removal of routes with controls not supported\n");
2375 if (dapm->codec && dapm->codec->name_prefix) {
2376 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2377 dapm->codec->name_prefix, route->sink);
2378 sink = prefixed_sink;
2379 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2380 dapm->codec->name_prefix, route->source);
2381 source = prefixed_source;
2384 source = route->source;
2388 list_for_each_entry(p, &dapm->card->paths, list) {
2389 if (strcmp(p->source->name, source) != 0)
2391 if (strcmp(p->sink->name, sink) != 0)
2398 dapm_mark_dirty(path->source, "Route removed");
2399 dapm_mark_dirty(path->sink, "Route removed");
2401 list_del(&path->list);
2402 list_del(&path->list_sink);
2403 list_del(&path->list_source);
2406 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2414 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2415 * @dapm: DAPM context
2416 * @route: audio routes
2417 * @num: number of routes
2419 * Connects 2 dapm widgets together via a named audio path. The sink is
2420 * the widget receiving the audio signal, whilst the source is the sender
2421 * of the audio signal.
2423 * Returns 0 for success else error. On error all resources can be freed
2424 * with a call to snd_soc_card_free().
2426 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2427 const struct snd_soc_dapm_route *route, int num)
2431 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2432 for (i = 0; i < num; i++) {
2433 r = snd_soc_dapm_add_route(dapm, route);
2435 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2437 route->control ? route->control : "direct",
2443 mutex_unlock(&dapm->card->dapm_mutex);
2447 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2450 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2451 * @dapm: DAPM context
2452 * @route: audio routes
2453 * @num: number of routes
2455 * Removes routes from the DAPM context.
2457 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2458 const struct snd_soc_dapm_route *route, int num)
2462 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2463 for (i = 0; i < num; i++) {
2464 snd_soc_dapm_del_route(dapm, route);
2467 mutex_unlock(&dapm->card->dapm_mutex);
2471 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2473 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2474 const struct snd_soc_dapm_route *route)
2476 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2479 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2482 struct snd_soc_dapm_path *path;
2486 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2492 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2497 if (route->control || route->connected)
2498 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2499 route->source, route->sink);
2501 list_for_each_entry(path, &source->sinks, list_source) {
2502 if (path->sink == sink) {
2509 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2510 route->source, route->sink);
2512 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2513 count, route->source, route->sink);
2519 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2520 * @dapm: DAPM context
2521 * @route: audio routes
2522 * @num: number of routes
2524 * Mark existing routes matching those specified in the passed array
2525 * as being weak, meaning that they are ignored for the purpose of
2526 * power decisions. The main intended use case is for sidetone paths
2527 * which couple audio between other independent paths if they are both
2528 * active in order to make the combination work better at the user
2529 * level but which aren't intended to be "used".
2531 * Note that CODEC drivers should not use this as sidetone type paths
2532 * can frequently also be used as bypass paths.
2534 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2535 const struct snd_soc_dapm_route *route, int num)
2540 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2541 for (i = 0; i < num; i++) {
2542 err = snd_soc_dapm_weak_route(dapm, route);
2547 mutex_unlock(&dapm->card->dapm_mutex);
2551 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2554 * snd_soc_dapm_new_widgets - add new dapm widgets
2555 * @dapm: DAPM context
2557 * Checks the codec for any new dapm widgets and creates them if found.
2559 * Returns 0 for success.
2561 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2563 struct snd_soc_dapm_widget *w;
2566 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2568 list_for_each_entry(w, &dapm->card->widgets, list)
2573 if (w->num_kcontrols) {
2574 w->kcontrols = kzalloc(w->num_kcontrols *
2575 sizeof(struct snd_kcontrol *),
2577 if (!w->kcontrols) {
2578 mutex_unlock(&dapm->card->dapm_mutex);
2584 case snd_soc_dapm_switch:
2585 case snd_soc_dapm_mixer:
2586 case snd_soc_dapm_mixer_named_ctl:
2589 case snd_soc_dapm_mux:
2590 case snd_soc_dapm_virt_mux:
2591 case snd_soc_dapm_value_mux:
2594 case snd_soc_dapm_pga:
2595 case snd_soc_dapm_out_drv:
2602 /* Read the initial power state from the device */
2604 val = soc_widget_read(w, w->reg);
2605 val &= 1 << w->shift;
2615 dapm_mark_dirty(w, "new widget");
2616 dapm_debugfs_add_widget(w);
2619 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2620 mutex_unlock(&dapm->card->dapm_mutex);
2623 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2626 * snd_soc_dapm_get_volsw - dapm mixer get callback
2627 * @kcontrol: mixer control
2628 * @ucontrol: control element information
2630 * Callback to get the value of a dapm mixer control.
2632 * Returns 0 for success.
2634 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2635 struct snd_ctl_elem_value *ucontrol)
2637 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2638 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2639 struct soc_mixer_control *mc =
2640 (struct soc_mixer_control *)kcontrol->private_value;
2641 unsigned int reg = mc->reg;
2642 unsigned int shift = mc->shift;
2644 unsigned int mask = (1 << fls(max)) - 1;
2645 unsigned int invert = mc->invert;
2647 if (snd_soc_volsw_is_stereo(mc))
2648 dev_warn(widget->dapm->dev,
2649 "ASoC: Control '%s' is stereo, which is not supported\n",
2652 ucontrol->value.integer.value[0] =
2653 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2655 ucontrol->value.integer.value[0] =
2656 max - ucontrol->value.integer.value[0];
2660 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2663 * snd_soc_dapm_put_volsw - dapm mixer set callback
2664 * @kcontrol: mixer control
2665 * @ucontrol: control element information
2667 * Callback to set the value of a dapm mixer control.
2669 * Returns 0 for success.
2671 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2672 struct snd_ctl_elem_value *ucontrol)
2674 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2675 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2676 struct snd_soc_codec *codec = widget->codec;
2677 struct snd_soc_card *card = codec->card;
2678 struct soc_mixer_control *mc =
2679 (struct soc_mixer_control *)kcontrol->private_value;
2680 unsigned int reg = mc->reg;
2681 unsigned int shift = mc->shift;
2683 unsigned int mask = (1 << fls(max)) - 1;
2684 unsigned int invert = mc->invert;
2686 int connect, change;
2687 struct snd_soc_dapm_update update;
2690 if (snd_soc_volsw_is_stereo(mc))
2691 dev_warn(widget->dapm->dev,
2692 "ASoC: Control '%s' is stereo, which is not supported\n",
2695 val = (ucontrol->value.integer.value[0] & mask);
2700 mask = mask << shift;
2703 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2705 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2707 for (wi = 0; wi < wlist->num_widgets; wi++) {
2708 widget = wlist->widgets[wi];
2710 widget->value = val;
2712 update.kcontrol = kcontrol;
2713 update.widget = widget;
2717 widget->dapm->update = &update;
2719 soc_dapm_mixer_update_power(widget, kcontrol, connect);
2721 widget->dapm->update = NULL;
2725 mutex_unlock(&card->dapm_mutex);
2728 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2731 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2732 * @kcontrol: mixer control
2733 * @ucontrol: control element information
2735 * Callback to get the value of a dapm enumerated double mixer control.
2737 * Returns 0 for success.
2739 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2740 struct snd_ctl_elem_value *ucontrol)
2742 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2743 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2744 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2747 val = snd_soc_read(widget->codec, e->reg);
2748 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2749 if (e->shift_l != e->shift_r)
2750 ucontrol->value.enumerated.item[1] =
2751 (val >> e->shift_r) & e->mask;
2755 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2758 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2759 * @kcontrol: mixer control
2760 * @ucontrol: control element information
2762 * Callback to set the value of a dapm enumerated double mixer control.
2764 * Returns 0 for success.
2766 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2767 struct snd_ctl_elem_value *ucontrol)
2769 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2770 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2771 struct snd_soc_codec *codec = widget->codec;
2772 struct snd_soc_card *card = codec->card;
2773 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2774 unsigned int val, mux, change;
2776 struct snd_soc_dapm_update update;
2779 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2781 mux = ucontrol->value.enumerated.item[0];
2782 val = mux << e->shift_l;
2783 mask = e->mask << e->shift_l;
2784 if (e->shift_l != e->shift_r) {
2785 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2787 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2788 mask |= e->mask << e->shift_r;
2791 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2793 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2795 for (wi = 0; wi < wlist->num_widgets; wi++) {
2796 widget = wlist->widgets[wi];
2798 widget->value = val;
2800 update.kcontrol = kcontrol;
2801 update.widget = widget;
2802 update.reg = e->reg;
2805 widget->dapm->update = &update;
2807 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2809 widget->dapm->update = NULL;
2813 mutex_unlock(&card->dapm_mutex);
2816 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2819 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2820 * @kcontrol: mixer control
2821 * @ucontrol: control element information
2823 * Returns 0 for success.
2825 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2826 struct snd_ctl_elem_value *ucontrol)
2828 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2829 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2831 ucontrol->value.enumerated.item[0] = widget->value;
2835 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2838 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2839 * @kcontrol: mixer control
2840 * @ucontrol: control element information
2842 * Returns 0 for success.
2844 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2845 struct snd_ctl_elem_value *ucontrol)
2847 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2848 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2849 struct snd_soc_codec *codec = widget->codec;
2850 struct snd_soc_card *card = codec->card;
2851 struct soc_enum *e =
2852 (struct soc_enum *)kcontrol->private_value;
2857 if (ucontrol->value.enumerated.item[0] >= e->max)
2860 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2862 change = widget->value != ucontrol->value.enumerated.item[0];
2864 for (wi = 0; wi < wlist->num_widgets; wi++) {
2865 widget = wlist->widgets[wi];
2867 widget->value = ucontrol->value.enumerated.item[0];
2869 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
2873 mutex_unlock(&card->dapm_mutex);
2876 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2879 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2881 * @kcontrol: mixer control
2882 * @ucontrol: control element information
2884 * Callback to get the value of a dapm semi enumerated double mixer control.
2886 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2887 * used for handling bitfield coded enumeration for example.
2889 * Returns 0 for success.
2891 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2892 struct snd_ctl_elem_value *ucontrol)
2894 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2895 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2896 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2897 unsigned int reg_val, val, mux;
2899 reg_val = snd_soc_read(widget->codec, e->reg);
2900 val = (reg_val >> e->shift_l) & e->mask;
2901 for (mux = 0; mux < e->max; mux++) {
2902 if (val == e->values[mux])
2905 ucontrol->value.enumerated.item[0] = mux;
2906 if (e->shift_l != e->shift_r) {
2907 val = (reg_val >> e->shift_r) & e->mask;
2908 for (mux = 0; mux < e->max; mux++) {
2909 if (val == e->values[mux])
2912 ucontrol->value.enumerated.item[1] = mux;
2917 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2920 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2922 * @kcontrol: mixer control
2923 * @ucontrol: control element information
2925 * Callback to set the value of a dapm semi enumerated double mixer control.
2927 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2928 * used for handling bitfield coded enumeration for example.
2930 * Returns 0 for success.
2932 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_value *ucontrol)
2935 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2936 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2937 struct snd_soc_codec *codec = widget->codec;
2938 struct snd_soc_card *card = codec->card;
2939 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2940 unsigned int val, mux, change;
2942 struct snd_soc_dapm_update update;
2945 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2947 mux = ucontrol->value.enumerated.item[0];
2948 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2949 mask = e->mask << e->shift_l;
2950 if (e->shift_l != e->shift_r) {
2951 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2953 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2954 mask |= e->mask << e->shift_r;
2957 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2959 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2961 for (wi = 0; wi < wlist->num_widgets; wi++) {
2962 widget = wlist->widgets[wi];
2964 widget->value = val;
2966 update.kcontrol = kcontrol;
2967 update.widget = widget;
2968 update.reg = e->reg;
2971 widget->dapm->update = &update;
2973 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2975 widget->dapm->update = NULL;
2979 mutex_unlock(&card->dapm_mutex);
2982 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2985 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2987 * @kcontrol: mixer control
2988 * @uinfo: control element information
2990 * Callback to provide information about a pin switch control.
2992 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2993 struct snd_ctl_elem_info *uinfo)
2995 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2997 uinfo->value.integer.min = 0;
2998 uinfo->value.integer.max = 1;
3002 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3005 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3007 * @kcontrol: mixer control
3010 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3011 struct snd_ctl_elem_value *ucontrol)
3013 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3014 const char *pin = (const char *)kcontrol->private_value;
3016 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3018 ucontrol->value.integer.value[0] =
3019 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3021 mutex_unlock(&card->dapm_mutex);
3025 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3028 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3030 * @kcontrol: mixer control
3033 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3034 struct snd_ctl_elem_value *ucontrol)
3036 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3037 const char *pin = (const char *)kcontrol->private_value;
3039 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3041 if (ucontrol->value.integer.value[0])
3042 snd_soc_dapm_enable_pin(&card->dapm, pin);
3044 snd_soc_dapm_disable_pin(&card->dapm, pin);
3046 mutex_unlock(&card->dapm_mutex);
3048 snd_soc_dapm_sync(&card->dapm);
3051 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3053 static struct snd_soc_dapm_widget *
3054 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3055 const struct snd_soc_dapm_widget *widget)
3057 struct snd_soc_dapm_widget *w;
3061 if ((w = dapm_cnew_widget(widget)) == NULL)
3065 case snd_soc_dapm_regulator_supply:
3066 w->regulator = devm_regulator_get(dapm->dev, w->name);
3067 if (IS_ERR(w->regulator)) {
3068 ret = PTR_ERR(w->regulator);
3069 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3074 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3075 ret = regulator_allow_bypass(w->regulator, true);
3077 dev_warn(w->dapm->dev,
3078 "ASoC: Failed to unbypass %s: %d\n",
3082 case snd_soc_dapm_clock_supply:
3083 #ifdef CONFIG_CLKDEV_LOOKUP
3084 w->clk = devm_clk_get(dapm->dev, w->name);
3085 if (IS_ERR(w->clk)) {
3086 ret = PTR_ERR(w->clk);
3087 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3099 name_len = strlen(widget->name) + 1;
3100 if (dapm->codec && dapm->codec->name_prefix)
3101 name_len += 1 + strlen(dapm->codec->name_prefix);
3102 w->name = kmalloc(name_len, GFP_KERNEL);
3103 if (w->name == NULL) {
3107 if (dapm->codec && dapm->codec->name_prefix)
3108 snprintf((char *)w->name, name_len, "%s %s",
3109 dapm->codec->name_prefix, widget->name);
3111 snprintf((char *)w->name, name_len, "%s", widget->name);
3114 case snd_soc_dapm_switch:
3115 case snd_soc_dapm_mixer:
3116 case snd_soc_dapm_mixer_named_ctl:
3117 w->power_check = dapm_generic_check_power;
3119 case snd_soc_dapm_mux:
3120 case snd_soc_dapm_virt_mux:
3121 case snd_soc_dapm_value_mux:
3122 w->power_check = dapm_generic_check_power;
3124 case snd_soc_dapm_adc:
3125 case snd_soc_dapm_aif_out:
3126 case snd_soc_dapm_dai_out:
3127 w->power_check = dapm_adc_check_power;
3129 case snd_soc_dapm_dac:
3130 case snd_soc_dapm_aif_in:
3131 case snd_soc_dapm_dai_in:
3132 w->power_check = dapm_dac_check_power;
3134 case snd_soc_dapm_pga:
3135 case snd_soc_dapm_out_drv:
3136 case snd_soc_dapm_input:
3137 case snd_soc_dapm_output:
3138 case snd_soc_dapm_micbias:
3139 case snd_soc_dapm_spk:
3140 case snd_soc_dapm_hp:
3141 case snd_soc_dapm_mic:
3142 case snd_soc_dapm_line:
3143 case snd_soc_dapm_dai_link:
3144 w->power_check = dapm_generic_check_power;
3146 case snd_soc_dapm_supply:
3147 case snd_soc_dapm_regulator_supply:
3148 case snd_soc_dapm_clock_supply:
3149 w->power_check = dapm_supply_check_power;
3152 w->power_check = dapm_always_on_check_power;
3157 w->codec = dapm->codec;
3158 w->platform = dapm->platform;
3159 INIT_LIST_HEAD(&w->sources);
3160 INIT_LIST_HEAD(&w->sinks);
3161 INIT_LIST_HEAD(&w->list);
3162 INIT_LIST_HEAD(&w->dirty);
3163 list_add(&w->list, &dapm->card->widgets);
3165 /* machine layer set ups unconnected pins and insertions */
3171 * snd_soc_dapm_new_controls - create new dapm controls
3172 * @dapm: DAPM context
3173 * @widget: widget array
3174 * @num: number of widgets
3176 * Creates new DAPM controls based upon the templates.
3178 * Returns 0 for success else error.
3180 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3181 const struct snd_soc_dapm_widget *widget,
3184 struct snd_soc_dapm_widget *w;
3188 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3189 for (i = 0; i < num; i++) {
3190 w = snd_soc_dapm_new_control(dapm, widget);
3193 "ASoC: Failed to create DAPM control %s\n",
3200 mutex_unlock(&dapm->card->dapm_mutex);
3203 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3205 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3206 struct snd_kcontrol *kcontrol, int event)
3208 struct snd_soc_dapm_path *source_p, *sink_p;
3209 struct snd_soc_dai *source, *sink;
3210 const struct snd_soc_pcm_stream *config = w->params;
3211 struct snd_pcm_substream substream;
3212 struct snd_pcm_hw_params *params = NULL;
3217 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3219 /* We only support a single source and sink, pick the first */
3220 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3222 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3225 BUG_ON(!source_p || !sink_p);
3226 BUG_ON(!sink_p->source || !source_p->sink);
3227 BUG_ON(!source_p->source || !sink_p->sink);
3229 source = source_p->source->priv;
3230 sink = sink_p->sink->priv;
3232 /* Be a little careful as we don't want to overflow the mask array */
3233 if (config->formats) {
3234 fmt = ffs(config->formats) - 1;
3236 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3241 /* Currently very limited parameter selection */
3242 params = kzalloc(sizeof(*params), GFP_KERNEL);
3247 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3249 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3251 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3254 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3255 = config->channels_min;
3256 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3257 = config->channels_max;
3259 memset(&substream, 0, sizeof(substream));
3262 case SND_SOC_DAPM_PRE_PMU:
3263 if (source->driver->ops && source->driver->ops->hw_params) {
3264 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3265 ret = source->driver->ops->hw_params(&substream,
3268 dev_err(source->dev,
3269 "ASoC: hw_params() failed: %d\n", ret);
3274 if (sink->driver->ops && sink->driver->ops->hw_params) {
3275 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3276 ret = sink->driver->ops->hw_params(&substream, params,
3280 "ASoC: hw_params() failed: %d\n", ret);
3286 case SND_SOC_DAPM_POST_PMU:
3287 ret = snd_soc_dai_digital_mute(sink, 0,
3288 SNDRV_PCM_STREAM_PLAYBACK);
3289 if (ret != 0 && ret != -ENOTSUPP)
3290 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3294 case SND_SOC_DAPM_PRE_PMD:
3295 ret = snd_soc_dai_digital_mute(sink, 1,
3296 SNDRV_PCM_STREAM_PLAYBACK);
3297 if (ret != 0 && ret != -ENOTSUPP)
3298 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3312 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3313 const struct snd_soc_pcm_stream *params,
3314 struct snd_soc_dapm_widget *source,
3315 struct snd_soc_dapm_widget *sink)
3317 struct snd_soc_dapm_route routes[2];
3318 struct snd_soc_dapm_widget template;
3319 struct snd_soc_dapm_widget *w;
3323 len = strlen(source->name) + strlen(sink->name) + 2;
3324 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3327 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3329 memset(&template, 0, sizeof(template));
3330 template.reg = SND_SOC_NOPM;
3331 template.id = snd_soc_dapm_dai_link;
3332 template.name = link_name;
3333 template.event = snd_soc_dai_link_event;
3334 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3335 SND_SOC_DAPM_PRE_PMD;
3337 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3339 w = snd_soc_dapm_new_control(&card->dapm, &template);
3341 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3348 memset(&routes, 0, sizeof(routes));
3350 routes[0].source = source->name;
3351 routes[0].sink = link_name;
3352 routes[1].source = link_name;
3353 routes[1].sink = sink->name;
3355 return snd_soc_dapm_add_routes(&card->dapm, routes,
3356 ARRAY_SIZE(routes));
3359 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3360 struct snd_soc_dai *dai)
3362 struct snd_soc_dapm_widget template;
3363 struct snd_soc_dapm_widget *w;
3365 WARN_ON(dapm->dev != dai->dev);
3367 memset(&template, 0, sizeof(template));
3368 template.reg = SND_SOC_NOPM;
3370 if (dai->driver->playback.stream_name) {
3371 template.id = snd_soc_dapm_dai_in;
3372 template.name = dai->driver->playback.stream_name;
3373 template.sname = dai->driver->playback.stream_name;
3375 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3378 w = snd_soc_dapm_new_control(dapm, &template);
3380 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3381 dai->driver->playback.stream_name);
3385 dai->playback_widget = w;
3388 if (dai->driver->capture.stream_name) {
3389 template.id = snd_soc_dapm_dai_out;
3390 template.name = dai->driver->capture.stream_name;
3391 template.sname = dai->driver->capture.stream_name;
3393 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3396 w = snd_soc_dapm_new_control(dapm, &template);
3398 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3399 dai->driver->capture.stream_name);
3403 dai->capture_widget = w;
3409 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3411 struct snd_soc_dapm_widget *dai_w, *w;
3412 struct snd_soc_dai *dai;
3413 struct snd_soc_dapm_route r;
3415 memset(&r, 0, sizeof(r));
3417 /* For each DAI widget... */
3418 list_for_each_entry(dai_w, &card->widgets, list) {
3419 switch (dai_w->id) {
3420 case snd_soc_dapm_dai_in:
3421 case snd_soc_dapm_dai_out:
3429 /* ...find all widgets with the same stream and link them */
3430 list_for_each_entry(w, &card->widgets, list) {
3431 if (w->dapm != dai_w->dapm)
3435 case snd_soc_dapm_dai_in:
3436 case snd_soc_dapm_dai_out:
3445 if (dai->driver->playback.stream_name &&
3447 dai->driver->playback.stream_name)) {
3448 r.source = dai->playback_widget->name;
3450 dev_dbg(dai->dev, "%s -> %s\n",
3453 snd_soc_dapm_add_route(w->dapm, &r);
3456 if (dai->driver->capture.stream_name &&
3458 dai->driver->capture.stream_name)) {
3460 r.sink = dai->capture_widget->name;
3461 dev_dbg(dai->dev, "%s -> %s\n",
3464 snd_soc_dapm_add_route(w->dapm, &r);
3472 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3476 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3477 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3478 struct snd_soc_dai *codec_dai = rtd->codec_dai;
3480 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3481 w_cpu = cpu_dai->playback_widget;
3482 w_codec = codec_dai->playback_widget;
3484 w_cpu = cpu_dai->capture_widget;
3485 w_codec = codec_dai->capture_widget;
3490 dapm_mark_dirty(w_cpu, "stream event");
3493 case SND_SOC_DAPM_STREAM_START:
3496 case SND_SOC_DAPM_STREAM_STOP:
3499 case SND_SOC_DAPM_STREAM_SUSPEND:
3500 case SND_SOC_DAPM_STREAM_RESUME:
3501 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3502 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3509 dapm_mark_dirty(w_codec, "stream event");
3512 case SND_SOC_DAPM_STREAM_START:
3513 w_codec->active = 1;
3515 case SND_SOC_DAPM_STREAM_STOP:
3516 w_codec->active = 0;
3518 case SND_SOC_DAPM_STREAM_SUSPEND:
3519 case SND_SOC_DAPM_STREAM_RESUME:
3520 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3521 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3526 dapm_power_widgets(&rtd->card->dapm, event);
3530 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3531 * @rtd: PCM runtime data
3532 * @stream: stream name
3533 * @event: stream event
3535 * Sends a stream event to the dapm core. The core then makes any
3536 * necessary widget power changes.
3538 * Returns 0 for success else error.
3540 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3543 struct snd_soc_card *card = rtd->card;
3545 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3546 soc_dapm_stream_event(rtd, stream, event);
3547 mutex_unlock(&card->dapm_mutex);
3551 * snd_soc_dapm_enable_pin - enable pin.
3552 * @dapm: DAPM context
3555 * Enables input/output pin and its parents or children widgets iff there is
3556 * a valid audio route and active audio stream.
3557 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3558 * do any widget power switching.
3560 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3562 return snd_soc_dapm_set_pin(dapm, pin, 1);
3564 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3567 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3568 * @dapm: DAPM context
3571 * Enables input/output pin regardless of any other state. This is
3572 * intended for use with microphone bias supplies used in microphone
3575 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3576 * do any widget power switching.
3578 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3581 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3584 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3588 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3591 dapm_mark_dirty(w, "force enable");
3595 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3598 * snd_soc_dapm_disable_pin - disable pin.
3599 * @dapm: DAPM context
3602 * Disables input/output pin and its parents or children widgets.
3603 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3604 * do any widget power switching.
3606 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3609 return snd_soc_dapm_set_pin(dapm, pin, 0);
3611 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3614 * snd_soc_dapm_nc_pin - permanently disable pin.
3615 * @dapm: DAPM context
3618 * Marks the specified pin as being not connected, disabling it along
3619 * any parent or child widgets. At present this is identical to
3620 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3621 * additional things such as disabling controls which only affect
3622 * paths through the pin.
3624 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3625 * do any widget power switching.
3627 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3629 return snd_soc_dapm_set_pin(dapm, pin, 0);
3631 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3634 * snd_soc_dapm_get_pin_status - get audio pin status
3635 * @dapm: DAPM context
3636 * @pin: audio signal pin endpoint (or start point)
3638 * Get audio pin status - connected or disconnected.
3640 * Returns 1 for connected otherwise 0.
3642 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3645 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3648 return w->connected;
3652 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3655 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3656 * @dapm: DAPM context
3657 * @pin: audio signal pin endpoint (or start point)
3659 * Mark the given endpoint or pin as ignoring suspend. When the
3660 * system is disabled a path between two endpoints flagged as ignoring
3661 * suspend will not be disabled. The path must already be enabled via
3662 * normal means at suspend time, it will not be turned on if it was not
3665 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3668 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3671 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3675 w->ignore_suspend = 1;
3679 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3681 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3682 struct snd_soc_dapm_widget *w)
3684 struct snd_soc_dapm_path *p;
3686 list_for_each_entry(p, &card->paths, list) {
3687 if ((p->source == w) || (p->sink == w)) {
3689 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3690 p->source->name, p->source->id, p->source->dapm,
3691 p->sink->name, p->sink->id, p->sink->dapm);
3693 /* Connected to something other than the codec */
3694 if (p->source->dapm != p->sink->dapm)
3697 * Loopback connection from codec external pin to
3698 * codec external pin
3700 if (p->sink->id == snd_soc_dapm_input) {
3701 switch (p->source->id) {
3702 case snd_soc_dapm_output:
3703 case snd_soc_dapm_micbias:
3716 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3717 * @codec: The codec whose pins should be processed
3719 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3720 * which are unused. Pins are used if they are connected externally to the
3721 * codec, whether that be to some other device, or a loop-back connection to
3724 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3726 struct snd_soc_card *card = codec->card;
3727 struct snd_soc_dapm_context *dapm = &codec->dapm;
3728 struct snd_soc_dapm_widget *w;
3730 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
3731 &card->dapm, &codec->dapm);
3733 list_for_each_entry(w, &card->widgets, list) {
3734 if (w->dapm != dapm)
3737 case snd_soc_dapm_input:
3738 case snd_soc_dapm_output:
3739 case snd_soc_dapm_micbias:
3740 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
3742 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3744 "... Not in map; disabling\n");
3745 snd_soc_dapm_nc_pin(dapm, w->name);
3755 * snd_soc_dapm_free - free dapm resources
3756 * @dapm: DAPM context
3758 * Free all dapm widgets and resources.
3760 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3762 snd_soc_dapm_sys_remove(dapm->dev);
3763 dapm_debugfs_cleanup(dapm);
3764 dapm_free_widgets(dapm);
3765 list_del(&dapm->list);
3767 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3769 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3771 struct snd_soc_card *card = dapm->card;
3772 struct snd_soc_dapm_widget *w;
3773 LIST_HEAD(down_list);
3776 mutex_lock(&card->dapm_mutex);
3778 list_for_each_entry(w, &dapm->card->widgets, list) {
3779 if (w->dapm != dapm)
3782 dapm_seq_insert(w, &down_list, false);
3788 /* If there were no widgets to power down we're already in
3792 if (dapm->bias_level == SND_SOC_BIAS_ON)
3793 snd_soc_dapm_set_bias_level(dapm,
3794 SND_SOC_BIAS_PREPARE);
3795 dapm_seq_run(dapm, &down_list, 0, false);
3796 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3797 snd_soc_dapm_set_bias_level(dapm,
3798 SND_SOC_BIAS_STANDBY);
3801 mutex_unlock(&card->dapm_mutex);
3805 * snd_soc_dapm_shutdown - callback for system shutdown
3807 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3809 struct snd_soc_codec *codec;
3811 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
3812 soc_dapm_shutdown_codec(&codec->dapm);
3813 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3814 snd_soc_dapm_set_bias_level(&codec->dapm,
3819 /* Module information */
3820 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3821 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3822 MODULE_LICENSE("GPL");