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;
358 int val, item, bitmask;
360 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
362 val = soc_widget_read(w, e->reg);
363 item = (val >> e->shift_l) & (bitmask - 1);
366 for (i = 0; i < e->max; i++) {
367 if (!(strcmp(p->name, e->texts[i])) && item == i)
372 case snd_soc_dapm_virt_mux: {
373 struct soc_enum *e = (struct soc_enum *)
374 w->kcontrol_news[i].private_value;
377 /* since a virtual mux has no backing registers to
378 * decide which path to connect, it will try to match
379 * with the first enumeration. This is to ensure
380 * that the default mux choice (the first) will be
381 * correctly powered up during initialization.
383 if (!strcmp(p->name, e->texts[0]))
387 case snd_soc_dapm_value_mux: {
388 struct soc_enum *e = (struct soc_enum *)
389 w->kcontrol_news[i].private_value;
392 val = soc_widget_read(w, e->reg);
393 val = (val >> e->shift_l) & e->mask;
394 for (item = 0; item < e->max; item++) {
395 if (val == e->values[item])
400 for (i = 0; i < e->max; i++) {
401 if (!(strcmp(p->name, e->texts[i])) && item == i)
406 /* does not affect routing - always connected */
407 case snd_soc_dapm_pga:
408 case snd_soc_dapm_out_drv:
409 case snd_soc_dapm_output:
410 case snd_soc_dapm_adc:
411 case snd_soc_dapm_input:
412 case snd_soc_dapm_siggen:
413 case snd_soc_dapm_dac:
414 case snd_soc_dapm_micbias:
415 case snd_soc_dapm_vmid:
416 case snd_soc_dapm_supply:
417 case snd_soc_dapm_regulator_supply:
418 case snd_soc_dapm_clock_supply:
419 case snd_soc_dapm_aif_in:
420 case snd_soc_dapm_aif_out:
421 case snd_soc_dapm_dai:
422 case snd_soc_dapm_hp:
423 case snd_soc_dapm_mic:
424 case snd_soc_dapm_spk:
425 case snd_soc_dapm_line:
426 case snd_soc_dapm_dai_link:
429 /* does affect routing - dynamically connected */
430 case snd_soc_dapm_pre:
431 case snd_soc_dapm_post:
437 /* connect mux widget to its interconnecting audio paths */
438 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
439 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
440 struct snd_soc_dapm_path *path, const char *control_name,
441 const struct snd_kcontrol_new *kcontrol)
443 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
446 for (i = 0; i < e->max; i++) {
447 if (!(strcmp(control_name, e->texts[i]))) {
448 list_add(&path->list, &dapm->card->paths);
449 list_add(&path->list_sink, &dest->sources);
450 list_add(&path->list_source, &src->sinks);
451 path->name = (char*)e->texts[i];
452 dapm_set_path_status(dest, path, 0);
460 /* connect mixer widget to its interconnecting audio paths */
461 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
462 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
463 struct snd_soc_dapm_path *path, const char *control_name)
467 /* search for mixer kcontrol */
468 for (i = 0; i < dest->num_kcontrols; i++) {
469 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
470 list_add(&path->list, &dapm->card->paths);
471 list_add(&path->list_sink, &dest->sources);
472 list_add(&path->list_source, &src->sinks);
473 path->name = dest->kcontrol_news[i].name;
474 dapm_set_path_status(dest, path, i);
481 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
482 struct snd_soc_dapm_widget *kcontrolw,
483 const struct snd_kcontrol_new *kcontrol_new,
484 struct snd_kcontrol **kcontrol)
486 struct snd_soc_dapm_widget *w;
491 list_for_each_entry(w, &dapm->card->widgets, list) {
492 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
494 for (i = 0; i < w->num_kcontrols; i++) {
495 if (&w->kcontrol_news[i] == kcontrol_new) {
497 *kcontrol = w->kcontrols[i];
506 /* create new dapm mixer control */
507 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
509 struct snd_soc_dapm_context *dapm = w->dapm;
511 size_t name_len, prefix_len;
512 struct snd_soc_dapm_path *path;
513 struct snd_card *card = dapm->card->snd_card;
515 struct snd_soc_dapm_widget_list *wlist;
519 prefix = dapm->codec->name_prefix;
524 prefix_len = strlen(prefix) + 1;
529 for (i = 0; i < w->num_kcontrols; i++) {
532 list_for_each_entry(path, &w->sources, list_sink) {
534 /* mixer/mux paths name must match control name */
535 if (path->name != (char *)w->kcontrol_news[i].name)
538 if (w->kcontrols[i]) {
539 path->kcontrol = w->kcontrols[i];
543 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
544 sizeof(struct snd_soc_dapm_widget *),
545 wlist = kzalloc(wlistsize, GFP_KERNEL);
548 "asoc: can't allocate widget list for %s\n",
552 wlist->num_widgets = 1;
553 wlist->widgets[0] = w;
555 /* add dapm control with long name.
556 * for dapm_mixer this is the concatenation of the
557 * mixer and kcontrol name.
558 * for dapm_mixer_named_ctl this is simply the
561 name_len = strlen(w->kcontrol_news[i].name) + 1;
562 if (w->id != snd_soc_dapm_mixer_named_ctl)
563 name_len += 1 + strlen(w->name);
565 path->long_name = kmalloc(name_len, GFP_KERNEL);
567 if (path->long_name == NULL) {
574 /* The control will get a prefix from
575 * the control creation process but
576 * we're also using the same prefix
577 * for widgets so cut the prefix off
578 * the front of the widget name.
580 snprintf((char *)path->long_name, name_len,
581 "%s %s", w->name + prefix_len,
582 w->kcontrol_news[i].name);
584 case snd_soc_dapm_mixer_named_ctl:
585 snprintf((char *)path->long_name, name_len,
586 "%s", w->kcontrol_news[i].name);
590 ((char *)path->long_name)[name_len - 1] = '\0';
592 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
593 wlist, path->long_name,
595 ret = snd_ctl_add(card, path->kcontrol);
598 "asoc: failed to add dapm kcontrol %s: %d\n",
599 path->long_name, ret);
601 kfree(path->long_name);
602 path->long_name = NULL;
605 w->kcontrols[i] = path->kcontrol;
611 /* create new dapm mux control */
612 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
614 struct snd_soc_dapm_context *dapm = w->dapm;
615 struct snd_soc_dapm_path *path = NULL;
616 struct snd_kcontrol *kcontrol;
617 struct snd_card *card = dapm->card->snd_card;
621 struct snd_soc_dapm_widget_list *wlist;
622 int shared, wlistentries;
626 if (w->num_kcontrols != 1) {
628 "asoc: mux %s has incorrect number of controls\n",
633 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
636 wlist = kcontrol->private_data;
637 wlistentries = wlist->num_widgets + 1;
642 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
643 wlistentries * sizeof(struct snd_soc_dapm_widget *),
644 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
647 "asoc: can't allocate widget list for %s\n", w->name);
650 wlist->num_widgets = wlistentries;
651 wlist->widgets[wlistentries - 1] = w;
655 prefix = dapm->codec->name_prefix;
660 name = w->kcontrol_news[0].name;
665 prefix_len = strlen(prefix) + 1;
671 * The control will get a prefix from the control creation
672 * process but we're also using the same prefix for widgets so
673 * cut the prefix off the front of the widget name.
675 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
676 name + prefix_len, prefix);
677 ret = snd_ctl_add(card, kcontrol);
679 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
686 kcontrol->private_data = wlist;
688 w->kcontrols[0] = kcontrol;
690 list_for_each_entry(path, &w->sources, list_sink)
691 path->kcontrol = kcontrol;
696 /* create new dapm volume control */
697 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
699 if (w->num_kcontrols)
700 dev_err(w->dapm->dev,
701 "asoc: PGA controls not supported: '%s'\n", w->name);
706 /* reset 'walked' bit for each dapm path */
707 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
709 struct snd_soc_dapm_path *p;
711 list_for_each_entry(p, &dapm->card->paths, list)
715 /* We implement power down on suspend by checking the power state of
716 * the ALSA card - when we are suspending the ALSA state for the card
719 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
721 int level = snd_power_get_state(widget->dapm->card->snd_card);
724 case SNDRV_CTL_POWER_D3hot:
725 case SNDRV_CTL_POWER_D3cold:
726 if (widget->ignore_suspend)
727 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
729 return widget->ignore_suspend;
735 /* add widget to list if it's not already in the list */
736 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
737 struct snd_soc_dapm_widget *w)
739 struct snd_soc_dapm_widget_list *wlist;
740 int wlistsize, wlistentries, i;
747 /* is this widget already in the list */
748 for (i = 0; i < wlist->num_widgets; i++) {
749 if (wlist->widgets[i] == w)
753 /* allocate some new space */
754 wlistentries = wlist->num_widgets + 1;
755 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
756 wlistentries * sizeof(struct snd_soc_dapm_widget *);
757 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
759 dev_err(w->dapm->dev, "can't allocate widget list for %s\n",
765 /* insert the widget */
766 dev_dbg(w->dapm->dev, "added %s in widget list pos %d\n",
767 w->name, wlist->num_widgets);
769 wlist->widgets[wlist->num_widgets] = w;
770 wlist->num_widgets++;
775 * Recursively check for a completed path to an active or physically connected
776 * output widget. Returns number of complete paths.
778 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
779 struct snd_soc_dapm_widget_list **list)
781 struct snd_soc_dapm_path *path;
784 if (widget->outputs >= 0)
785 return widget->outputs;
787 DAPM_UPDATE_STAT(widget, path_checks);
789 switch (widget->id) {
790 case snd_soc_dapm_supply:
791 case snd_soc_dapm_regulator_supply:
792 case snd_soc_dapm_clock_supply:
798 switch (widget->id) {
799 case snd_soc_dapm_adc:
800 case snd_soc_dapm_aif_out:
801 case snd_soc_dapm_dai:
802 if (widget->active) {
803 widget->outputs = snd_soc_dapm_suspend_check(widget);
804 return widget->outputs;
810 if (widget->connected) {
811 /* connected pin ? */
812 if (widget->id == snd_soc_dapm_output && !widget->ext) {
813 widget->outputs = snd_soc_dapm_suspend_check(widget);
814 return widget->outputs;
817 /* connected jack or spk ? */
818 if (widget->id == snd_soc_dapm_hp ||
819 widget->id == snd_soc_dapm_spk ||
820 (widget->id == snd_soc_dapm_line &&
821 !list_empty(&widget->sources))) {
822 widget->outputs = snd_soc_dapm_suspend_check(widget);
823 return widget->outputs;
827 list_for_each_entry(path, &widget->sinks, list_source) {
828 DAPM_UPDATE_STAT(widget, neighbour_checks);
836 trace_snd_soc_dapm_output_path(widget, path);
838 if (path->sink && path->connect) {
841 /* do we need to add this widget to the list ? */
844 err = dapm_list_add_widget(list, path->sink);
846 dev_err(widget->dapm->dev, "could not add widget %s\n",
852 con += is_connected_output_ep(path->sink, list);
856 widget->outputs = con;
862 * Recursively check for a completed path to an active or physically connected
863 * input widget. Returns number of complete paths.
865 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
866 struct snd_soc_dapm_widget_list **list)
868 struct snd_soc_dapm_path *path;
871 if (widget->inputs >= 0)
872 return widget->inputs;
874 DAPM_UPDATE_STAT(widget, path_checks);
876 switch (widget->id) {
877 case snd_soc_dapm_supply:
878 case snd_soc_dapm_regulator_supply:
879 case snd_soc_dapm_clock_supply:
885 /* active stream ? */
886 switch (widget->id) {
887 case snd_soc_dapm_dac:
888 case snd_soc_dapm_aif_in:
889 case snd_soc_dapm_dai:
890 if (widget->active) {
891 widget->inputs = snd_soc_dapm_suspend_check(widget);
892 return widget->inputs;
898 if (widget->connected) {
899 /* connected pin ? */
900 if (widget->id == snd_soc_dapm_input && !widget->ext) {
901 widget->inputs = snd_soc_dapm_suspend_check(widget);
902 return widget->inputs;
905 /* connected VMID/Bias for lower pops */
906 if (widget->id == snd_soc_dapm_vmid) {
907 widget->inputs = snd_soc_dapm_suspend_check(widget);
908 return widget->inputs;
911 /* connected jack ? */
912 if (widget->id == snd_soc_dapm_mic ||
913 (widget->id == snd_soc_dapm_line &&
914 !list_empty(&widget->sinks))) {
915 widget->inputs = snd_soc_dapm_suspend_check(widget);
916 return widget->inputs;
919 /* signal generator */
920 if (widget->id == snd_soc_dapm_siggen) {
921 widget->inputs = snd_soc_dapm_suspend_check(widget);
922 return widget->inputs;
926 list_for_each_entry(path, &widget->sources, list_sink) {
927 DAPM_UPDATE_STAT(widget, neighbour_checks);
935 trace_snd_soc_dapm_input_path(widget, path);
937 if (path->source && path->connect) {
940 /* do we need to add this widget to the list ? */
943 err = dapm_list_add_widget(list, path->source);
945 dev_err(widget->dapm->dev, "could not add widget %s\n",
951 con += is_connected_input_ep(path->source, list);
955 widget->inputs = con;
961 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
963 * @stream: stream direction.
964 * @list: list of active widgets for this stream.
966 * Queries DAPM graph as to whether an valid audio stream path exists for
967 * the initial stream specified by name. This takes into account
968 * current mixer and mux kcontrol settings. Creates list of valid widgets.
970 * Returns the number of valid paths or negative error.
972 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
973 struct snd_soc_dapm_widget_list **list)
975 struct snd_soc_card *card = dai->card;
978 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
981 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
982 paths = is_connected_output_ep(dai->playback_widget, list);
984 paths = is_connected_input_ep(dai->capture_widget, list);
986 trace_snd_soc_dapm_connected(paths, stream);
987 dapm_clear_walk(&card->dapm);
988 mutex_unlock(&card->dapm_mutex);
994 * Handler for generic register modifier widget.
996 int dapm_reg_event(struct snd_soc_dapm_widget *w,
997 struct snd_kcontrol *kcontrol, int event)
1001 if (SND_SOC_DAPM_EVENT_ON(event))
1006 soc_widget_update_bits_locked(w, -(w->reg + 1),
1007 w->mask << w->shift, val << w->shift);
1011 EXPORT_SYMBOL_GPL(dapm_reg_event);
1014 * Handler for regulator supply widget.
1016 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1017 struct snd_kcontrol *kcontrol, int event)
1019 if (SND_SOC_DAPM_EVENT_ON(event))
1020 return regulator_enable(w->regulator);
1022 return regulator_disable_deferred(w->regulator, w->shift);
1024 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1027 * Handler for clock supply widget.
1029 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1030 struct snd_kcontrol *kcontrol, int event)
1035 #ifdef CONFIG_HAVE_CLK
1036 if (SND_SOC_DAPM_EVENT_ON(event)) {
1037 return clk_enable(w->clk);
1039 clk_disable(w->clk);
1045 EXPORT_SYMBOL_GPL(dapm_clock_event);
1047 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1049 if (w->power_checked)
1050 return w->new_power;
1055 w->new_power = w->power_check(w);
1057 w->power_checked = true;
1059 return w->new_power;
1062 /* Generic check to see if a widget should be powered.
1064 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1068 DAPM_UPDATE_STAT(w, power_checks);
1070 in = is_connected_input_ep(w, NULL);
1071 dapm_clear_walk(w->dapm);
1072 out = is_connected_output_ep(w, NULL);
1073 dapm_clear_walk(w->dapm);
1074 return out != 0 && in != 0;
1077 static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1079 DAPM_UPDATE_STAT(w, power_checks);
1084 return dapm_generic_check_power(w);
1087 /* Check to see if an ADC has power */
1088 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1092 DAPM_UPDATE_STAT(w, power_checks);
1095 in = is_connected_input_ep(w, NULL);
1096 dapm_clear_walk(w->dapm);
1099 return dapm_generic_check_power(w);
1103 /* Check to see if a DAC has power */
1104 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1108 DAPM_UPDATE_STAT(w, power_checks);
1111 out = is_connected_output_ep(w, NULL);
1112 dapm_clear_walk(w->dapm);
1115 return dapm_generic_check_power(w);
1119 /* Check to see if a power supply is needed */
1120 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1122 struct snd_soc_dapm_path *path;
1124 DAPM_UPDATE_STAT(w, power_checks);
1126 /* Check if one of our outputs is connected */
1127 list_for_each_entry(path, &w->sinks, list_source) {
1128 DAPM_UPDATE_STAT(w, neighbour_checks);
1133 if (path->connected &&
1134 !path->connected(path->source, path->sink))
1140 if (dapm_widget_power_check(path->sink))
1144 dapm_clear_walk(w->dapm);
1149 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1154 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1155 struct snd_soc_dapm_widget *b,
1163 sort = dapm_down_seq;
1165 if (sort[a->id] != sort[b->id])
1166 return sort[a->id] - sort[b->id];
1167 if (a->subseq != b->subseq) {
1169 return a->subseq - b->subseq;
1171 return b->subseq - a->subseq;
1173 if (a->reg != b->reg)
1174 return a->reg - b->reg;
1175 if (a->dapm != b->dapm)
1176 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1181 /* Insert a widget in order into a DAPM power sequence. */
1182 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1183 struct list_head *list,
1186 struct snd_soc_dapm_widget *w;
1188 list_for_each_entry(w, list, power_list)
1189 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1190 list_add_tail(&new_widget->power_list, &w->power_list);
1194 list_add_tail(&new_widget->power_list, list);
1197 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1198 struct snd_soc_dapm_widget *w, int event)
1200 struct snd_soc_card *card = dapm->card;
1201 const char *ev_name;
1205 case SND_SOC_DAPM_PRE_PMU:
1206 ev_name = "PRE_PMU";
1209 case SND_SOC_DAPM_POST_PMU:
1210 ev_name = "POST_PMU";
1213 case SND_SOC_DAPM_PRE_PMD:
1214 ev_name = "PRE_PMD";
1217 case SND_SOC_DAPM_POST_PMD:
1218 ev_name = "POST_PMD";
1226 if (w->power != power)
1229 if (w->event && (w->event_flags & event)) {
1230 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1232 trace_snd_soc_dapm_widget_event_start(w, event);
1233 ret = w->event(w, NULL, event);
1234 trace_snd_soc_dapm_widget_event_done(w, event);
1236 pr_err("%s: %s event failed: %d\n",
1237 ev_name, w->name, ret);
1241 /* Apply the coalesced changes from a DAPM sequence */
1242 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1243 struct list_head *pending)
1245 struct snd_soc_card *card = dapm->card;
1246 struct snd_soc_dapm_widget *w;
1248 unsigned int value = 0;
1249 unsigned int mask = 0;
1250 unsigned int cur_mask;
1252 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1255 list_for_each_entry(w, pending, power_list) {
1256 cur_mask = 1 << w->shift;
1257 BUG_ON(reg != w->reg);
1268 pop_dbg(dapm->dev, card->pop_time,
1269 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1270 w->name, reg, value, mask);
1272 /* Check for events */
1273 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1274 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1278 /* Any widget will do, they should all be updating the
1281 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1284 pop_dbg(dapm->dev, card->pop_time,
1285 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1286 value, mask, reg, card->pop_time);
1287 pop_wait(card->pop_time);
1288 soc_widget_update_bits_locked(w, reg, mask, value);
1291 list_for_each_entry(w, pending, power_list) {
1292 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1293 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1297 /* Apply a DAPM power sequence.
1299 * We walk over a pre-sorted list of widgets to apply power to. In
1300 * order to minimise the number of writes to the device required
1301 * multiple widgets will be updated in a single write where possible.
1302 * Currently anything that requires more than a single write is not
1305 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1306 struct list_head *list, int event, bool power_up)
1308 struct snd_soc_dapm_widget *w, *n;
1311 int cur_subseq = -1;
1312 int cur_reg = SND_SOC_NOPM;
1313 struct snd_soc_dapm_context *cur_dapm = NULL;
1320 sort = dapm_down_seq;
1322 list_for_each_entry_safe(w, n, list, power_list) {
1325 /* Do we need to apply any queued changes? */
1326 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1327 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1328 if (!list_empty(&pending))
1329 dapm_seq_run_coalesced(cur_dapm, &pending);
1331 if (cur_dapm && cur_dapm->seq_notifier) {
1332 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1333 if (sort[i] == cur_sort)
1334 cur_dapm->seq_notifier(cur_dapm,
1339 INIT_LIST_HEAD(&pending);
1341 cur_subseq = INT_MIN;
1342 cur_reg = SND_SOC_NOPM;
1347 case snd_soc_dapm_pre:
1349 list_for_each_entry_safe_continue(w, n, list,
1352 if (event == SND_SOC_DAPM_STREAM_START)
1354 NULL, SND_SOC_DAPM_PRE_PMU);
1355 else if (event == SND_SOC_DAPM_STREAM_STOP)
1357 NULL, SND_SOC_DAPM_PRE_PMD);
1360 case snd_soc_dapm_post:
1362 list_for_each_entry_safe_continue(w, n, list,
1365 if (event == SND_SOC_DAPM_STREAM_START)
1367 NULL, SND_SOC_DAPM_POST_PMU);
1368 else if (event == SND_SOC_DAPM_STREAM_STOP)
1370 NULL, SND_SOC_DAPM_POST_PMD);
1374 /* Queue it up for application */
1375 cur_sort = sort[w->id];
1376 cur_subseq = w->subseq;
1379 list_move(&w->power_list, &pending);
1384 dev_err(w->dapm->dev,
1385 "Failed to apply widget power: %d\n", ret);
1388 if (!list_empty(&pending))
1389 dapm_seq_run_coalesced(cur_dapm, &pending);
1391 if (cur_dapm && cur_dapm->seq_notifier) {
1392 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1393 if (sort[i] == cur_sort)
1394 cur_dapm->seq_notifier(cur_dapm,
1399 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1401 struct snd_soc_dapm_update *update = dapm->update;
1402 struct snd_soc_dapm_widget *w;
1411 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1412 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1414 pr_err("%s DAPM pre-event failed: %d\n",
1418 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1421 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1424 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1425 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1427 pr_err("%s DAPM post-event failed: %d\n",
1432 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1433 * they're changing state.
1435 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1437 struct snd_soc_dapm_context *d = data;
1440 /* If we're off and we're not supposed to be go into STANDBY */
1441 if (d->bias_level == SND_SOC_BIAS_OFF &&
1442 d->target_bias_level != SND_SOC_BIAS_OFF) {
1444 pm_runtime_get_sync(d->dev);
1446 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1449 "Failed to turn on bias: %d\n", ret);
1452 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1453 if (d->bias_level != d->target_bias_level) {
1454 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1457 "Failed to prepare bias: %d\n", ret);
1461 /* Async callback run prior to DAPM sequences - brings to their final
1464 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1466 struct snd_soc_dapm_context *d = data;
1469 /* If we just powered the last thing off drop to standby bias */
1470 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1471 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1472 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1473 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1475 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1479 /* If we're in standby and can support bias off then do that */
1480 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1481 d->target_bias_level == SND_SOC_BIAS_OFF) {
1482 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1484 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1487 pm_runtime_put(d->dev);
1490 /* If we just powered up then move to active bias */
1491 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1492 d->target_bias_level == SND_SOC_BIAS_ON) {
1493 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1495 dev_err(d->dev, "Failed to apply active bias: %d\n",
1500 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1501 bool power, bool connect)
1503 /* If a connection is being made or broken then that update
1504 * will have marked the peer dirty, otherwise the widgets are
1505 * not connected and this update has no impact. */
1509 /* If the peer is already in the state we're moving to then we
1510 * won't have an impact on it. */
1511 if (power != peer->power)
1512 dapm_mark_dirty(peer, "peer state change");
1515 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1516 struct list_head *up_list,
1517 struct list_head *down_list)
1519 struct snd_soc_dapm_path *path;
1521 if (w->power == power)
1524 trace_snd_soc_dapm_widget_power(w, power);
1526 /* If we changed our power state perhaps our neigbours changed
1529 list_for_each_entry(path, &w->sources, list_sink) {
1531 dapm_widget_set_peer_power(path->source, power,
1536 case snd_soc_dapm_supply:
1537 case snd_soc_dapm_regulator_supply:
1538 case snd_soc_dapm_clock_supply:
1539 /* Supplies can't affect their outputs, only their inputs */
1542 list_for_each_entry(path, &w->sinks, list_source) {
1544 dapm_widget_set_peer_power(path->sink, power,
1552 dapm_seq_insert(w, up_list, true);
1554 dapm_seq_insert(w, down_list, false);
1559 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1560 struct list_head *up_list,
1561 struct list_head *down_list)
1566 case snd_soc_dapm_pre:
1567 dapm_seq_insert(w, down_list, false);
1569 case snd_soc_dapm_post:
1570 dapm_seq_insert(w, up_list, true);
1574 power = dapm_widget_power_check(w);
1576 dapm_widget_set_power(w, power, up_list, down_list);
1582 * Scan each dapm widget for complete audio path.
1583 * A complete path is a route that has valid endpoints i.e.:-
1585 * o DAC to output pin.
1586 * o Input Pin to ADC.
1587 * o Input pin to Output pin (bypass, sidetone)
1588 * o DAC to ADC (loopback).
1590 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1592 struct snd_soc_card *card = dapm->card;
1593 struct snd_soc_dapm_widget *w;
1594 struct snd_soc_dapm_context *d;
1596 LIST_HEAD(down_list);
1597 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1598 enum snd_soc_bias_level bias;
1600 trace_snd_soc_dapm_start(card);
1602 list_for_each_entry(d, &card->dapm_list, list) {
1603 if (d->idle_bias_off)
1604 d->target_bias_level = SND_SOC_BIAS_OFF;
1606 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1611 /* Check which widgets we need to power and store them in
1612 * lists indicating if they should be powered up or down. We
1613 * only check widgets that have been flagged as dirty but note
1614 * that new widgets may be added to the dirty list while we
1617 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1618 dapm_power_one_widget(w, &up_list, &down_list);
1621 list_for_each_entry(w, &card->widgets, list) {
1623 case snd_soc_dapm_pre:
1624 case snd_soc_dapm_post:
1625 /* These widgets always need to be powered */
1628 list_del_init(&w->dirty);
1635 /* Supplies and micbiases only bring the
1636 * context up to STANDBY as unless something
1637 * else is active and passing audio they
1638 * generally don't require full power. Signal
1639 * generators are virtual pins and have no
1640 * power impact themselves.
1643 case snd_soc_dapm_siggen:
1645 case snd_soc_dapm_supply:
1646 case snd_soc_dapm_regulator_supply:
1647 case snd_soc_dapm_clock_supply:
1648 case snd_soc_dapm_micbias:
1649 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1650 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1653 d->target_bias_level = SND_SOC_BIAS_ON;
1660 /* Force all contexts in the card to the same bias state if
1661 * they're not ground referenced.
1663 bias = SND_SOC_BIAS_OFF;
1664 list_for_each_entry(d, &card->dapm_list, list)
1665 if (d->target_bias_level > bias)
1666 bias = d->target_bias_level;
1667 list_for_each_entry(d, &card->dapm_list, list)
1668 if (!d->idle_bias_off)
1669 d->target_bias_level = bias;
1671 trace_snd_soc_dapm_walk_done(card);
1673 /* Run all the bias changes in parallel */
1674 list_for_each_entry(d, &dapm->card->dapm_list, list)
1675 async_schedule_domain(dapm_pre_sequence_async, d,
1677 async_synchronize_full_domain(&async_domain);
1679 /* Power down widgets first; try to avoid amplifying pops. */
1680 dapm_seq_run(dapm, &down_list, event, false);
1682 dapm_widget_update(dapm);
1685 dapm_seq_run(dapm, &up_list, event, true);
1687 /* Run all the bias changes in parallel */
1688 list_for_each_entry(d, &dapm->card->dapm_list, list)
1689 async_schedule_domain(dapm_post_sequence_async, d,
1691 async_synchronize_full_domain(&async_domain);
1693 /* do we need to notify any clients that DAPM event is complete */
1694 list_for_each_entry(d, &card->dapm_list, list) {
1695 if (d->stream_event)
1696 d->stream_event(d, event);
1699 pop_dbg(dapm->dev, card->pop_time,
1700 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1701 pop_wait(card->pop_time);
1703 trace_snd_soc_dapm_done(card);
1708 #ifdef CONFIG_DEBUG_FS
1709 static ssize_t dapm_widget_power_read_file(struct file *file,
1710 char __user *user_buf,
1711 size_t count, loff_t *ppos)
1713 struct snd_soc_dapm_widget *w = file->private_data;
1717 struct snd_soc_dapm_path *p = NULL;
1719 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1723 in = is_connected_input_ep(w, NULL);
1724 dapm_clear_walk(w->dapm);
1725 out = is_connected_output_ep(w, NULL);
1726 dapm_clear_walk(w->dapm);
1728 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1729 w->name, w->power ? "On" : "Off",
1730 w->force ? " (forced)" : "", in, out);
1733 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1734 " - R%d(0x%x) bit %d",
1735 w->reg, w->reg, w->shift);
1737 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1740 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1742 w->active ? "active" : "inactive");
1744 list_for_each_entry(p, &w->sources, list_sink) {
1745 if (p->connected && !p->connected(w, p->sink))
1749 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1750 " in \"%s\" \"%s\"\n",
1751 p->name ? p->name : "static",
1754 list_for_each_entry(p, &w->sinks, list_source) {
1755 if (p->connected && !p->connected(w, p->sink))
1759 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1760 " out \"%s\" \"%s\"\n",
1761 p->name ? p->name : "static",
1765 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1771 static const struct file_operations dapm_widget_power_fops = {
1772 .open = simple_open,
1773 .read = dapm_widget_power_read_file,
1774 .llseek = default_llseek,
1777 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1778 size_t count, loff_t *ppos)
1780 struct snd_soc_dapm_context *dapm = file->private_data;
1783 switch (dapm->bias_level) {
1784 case SND_SOC_BIAS_ON:
1787 case SND_SOC_BIAS_PREPARE:
1788 level = "Prepare\n";
1790 case SND_SOC_BIAS_STANDBY:
1791 level = "Standby\n";
1793 case SND_SOC_BIAS_OFF:
1798 level = "Unknown\n";
1802 return simple_read_from_buffer(user_buf, count, ppos, level,
1806 static const struct file_operations dapm_bias_fops = {
1807 .open = simple_open,
1808 .read = dapm_bias_read_file,
1809 .llseek = default_llseek,
1812 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1813 struct dentry *parent)
1817 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1819 if (!dapm->debugfs_dapm) {
1821 "Failed to create DAPM debugfs directory\n");
1825 d = debugfs_create_file("bias_level", 0444,
1826 dapm->debugfs_dapm, dapm,
1830 "ASoC: Failed to create bias level debugfs file\n");
1833 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1835 struct snd_soc_dapm_context *dapm = w->dapm;
1838 if (!dapm->debugfs_dapm || !w->name)
1841 d = debugfs_create_file(w->name, 0444,
1842 dapm->debugfs_dapm, w,
1843 &dapm_widget_power_fops);
1845 dev_warn(w->dapm->dev,
1846 "ASoC: Failed to create %s debugfs file\n",
1850 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1852 debugfs_remove_recursive(dapm->debugfs_dapm);
1856 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1857 struct dentry *parent)
1861 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1865 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1871 /* test and update the power status of a mux widget */
1872 static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1873 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1875 struct snd_soc_dapm_path *path;
1878 if (widget->id != snd_soc_dapm_mux &&
1879 widget->id != snd_soc_dapm_virt_mux &&
1880 widget->id != snd_soc_dapm_value_mux)
1883 /* find dapm widget path assoc with kcontrol */
1884 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1885 if (path->kcontrol != kcontrol)
1888 if (!path->name || !e->texts[mux])
1892 /* we now need to match the string in the enum to the path */
1893 if (!(strcmp(path->name, e->texts[mux]))) {
1894 path->connect = 1; /* new connection */
1895 dapm_mark_dirty(path->source, "mux connection");
1898 dapm_mark_dirty(path->source,
1899 "mux disconnection");
1900 path->connect = 0; /* old connection must be powered down */
1905 dapm_mark_dirty(widget, "mux change");
1906 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1912 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1913 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1915 struct snd_soc_card *card = widget->dapm->card;
1918 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1919 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1920 mutex_unlock(&card->dapm_mutex);
1922 soc_dpcm_runtime_update(widget);
1925 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1927 /* test and update the power status of a mixer or switch widget */
1928 static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1929 struct snd_kcontrol *kcontrol, int connect)
1931 struct snd_soc_dapm_path *path;
1934 if (widget->id != snd_soc_dapm_mixer &&
1935 widget->id != snd_soc_dapm_mixer_named_ctl &&
1936 widget->id != snd_soc_dapm_switch)
1939 /* find dapm widget path assoc with kcontrol */
1940 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1941 if (path->kcontrol != kcontrol)
1944 /* found, now check type */
1946 path->connect = connect;
1947 dapm_mark_dirty(path->source, "mixer connection");
1951 dapm_mark_dirty(widget, "mixer update");
1952 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1958 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1959 struct snd_kcontrol *kcontrol, int connect)
1961 struct snd_soc_card *card = widget->dapm->card;
1964 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1965 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1966 mutex_unlock(&card->dapm_mutex);
1968 soc_dpcm_runtime_update(widget);
1971 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
1973 /* show dapm widget status in sys fs */
1974 static ssize_t dapm_widget_show(struct device *dev,
1975 struct device_attribute *attr, char *buf)
1977 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
1978 struct snd_soc_codec *codec =rtd->codec;
1979 struct snd_soc_dapm_widget *w;
1981 char *state = "not set";
1983 list_for_each_entry(w, &codec->card->widgets, list) {
1984 if (w->dapm != &codec->dapm)
1987 /* only display widgets that burnm power */
1989 case snd_soc_dapm_hp:
1990 case snd_soc_dapm_mic:
1991 case snd_soc_dapm_spk:
1992 case snd_soc_dapm_line:
1993 case snd_soc_dapm_micbias:
1994 case snd_soc_dapm_dac:
1995 case snd_soc_dapm_adc:
1996 case snd_soc_dapm_pga:
1997 case snd_soc_dapm_out_drv:
1998 case snd_soc_dapm_mixer:
1999 case snd_soc_dapm_mixer_named_ctl:
2000 case snd_soc_dapm_supply:
2001 case snd_soc_dapm_regulator_supply:
2002 case snd_soc_dapm_clock_supply:
2004 count += sprintf(buf + count, "%s: %s\n",
2005 w->name, w->power ? "On":"Off");
2012 switch (codec->dapm.bias_level) {
2013 case SND_SOC_BIAS_ON:
2016 case SND_SOC_BIAS_PREPARE:
2019 case SND_SOC_BIAS_STANDBY:
2022 case SND_SOC_BIAS_OFF:
2026 count += sprintf(buf + count, "PM State: %s\n", state);
2031 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2033 int snd_soc_dapm_sys_add(struct device *dev)
2035 return device_create_file(dev, &dev_attr_dapm_widget);
2038 static void snd_soc_dapm_sys_remove(struct device *dev)
2040 device_remove_file(dev, &dev_attr_dapm_widget);
2043 /* free all dapm widgets and resources */
2044 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2046 struct snd_soc_dapm_widget *w, *next_w;
2047 struct snd_soc_dapm_path *p, *next_p;
2049 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2050 if (w->dapm != dapm)
2054 * remove source and sink paths associated to this widget.
2055 * While removing the path, remove reference to it from both
2056 * source and sink widgets so that path is removed only once.
2058 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2059 list_del(&p->list_sink);
2060 list_del(&p->list_source);
2062 kfree(p->long_name);
2065 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2066 list_del(&p->list_sink);
2067 list_del(&p->list_source);
2069 kfree(p->long_name);
2072 kfree(w->kcontrols);
2078 static struct snd_soc_dapm_widget *dapm_find_widget(
2079 struct snd_soc_dapm_context *dapm, const char *pin,
2080 bool search_other_contexts)
2082 struct snd_soc_dapm_widget *w;
2083 struct snd_soc_dapm_widget *fallback = NULL;
2085 list_for_each_entry(w, &dapm->card->widgets, list) {
2086 if (!strcmp(w->name, pin)) {
2087 if (w->dapm == dapm)
2094 if (search_other_contexts)
2100 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2101 const char *pin, int status)
2103 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2106 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2110 if (w->connected != status)
2111 dapm_mark_dirty(w, "pin configuration");
2113 w->connected = status;
2121 * snd_soc_dapm_sync - scan and power dapm paths
2122 * @dapm: DAPM context
2124 * Walks all dapm audio paths and powers widgets according to their
2125 * stream or path usage.
2127 * Returns 0 for success.
2129 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2134 * Suppress early reports (eg, jacks syncing their state) to avoid
2135 * silly DAPM runs during card startup.
2137 if (!dapm->card || !dapm->card->instantiated)
2140 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2141 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2142 mutex_unlock(&dapm->card->dapm_mutex);
2145 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2147 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2148 const struct snd_soc_dapm_route *route)
2150 struct snd_soc_dapm_path *path;
2151 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2152 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2154 const char *control = route->control;
2156 char prefixed_sink[80];
2157 char prefixed_source[80];
2160 if (dapm->codec && dapm->codec->name_prefix) {
2161 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2162 dapm->codec->name_prefix, route->sink);
2163 sink = prefixed_sink;
2164 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2165 dapm->codec->name_prefix, route->source);
2166 source = prefixed_source;
2169 source = route->source;
2173 * find src and dest widgets over all widgets but favor a widget from
2174 * current DAPM context
2176 list_for_each_entry(w, &dapm->card->widgets, list) {
2177 if (!wsink && !(strcmp(w->name, sink))) {
2179 if (w->dapm == dapm)
2183 if (!wsource && !(strcmp(w->name, source))) {
2185 if (w->dapm == dapm)
2189 /* use widget from another DAPM context if not found from this */
2195 if (wsource == NULL || wsink == NULL)
2198 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2202 path->source = wsource;
2204 path->connected = route->connected;
2205 INIT_LIST_HEAD(&path->list);
2206 INIT_LIST_HEAD(&path->list_source);
2207 INIT_LIST_HEAD(&path->list_sink);
2209 /* check for external widgets */
2210 if (wsink->id == snd_soc_dapm_input) {
2211 if (wsource->id == snd_soc_dapm_micbias ||
2212 wsource->id == snd_soc_dapm_mic ||
2213 wsource->id == snd_soc_dapm_line ||
2214 wsource->id == snd_soc_dapm_output)
2217 if (wsource->id == snd_soc_dapm_output) {
2218 if (wsink->id == snd_soc_dapm_spk ||
2219 wsink->id == snd_soc_dapm_hp ||
2220 wsink->id == snd_soc_dapm_line ||
2221 wsink->id == snd_soc_dapm_input)
2225 /* connect static paths */
2226 if (control == NULL) {
2227 list_add(&path->list, &dapm->card->paths);
2228 list_add(&path->list_sink, &wsink->sources);
2229 list_add(&path->list_source, &wsource->sinks);
2234 /* connect dynamic paths */
2235 switch (wsink->id) {
2236 case snd_soc_dapm_adc:
2237 case snd_soc_dapm_dac:
2238 case snd_soc_dapm_pga:
2239 case snd_soc_dapm_out_drv:
2240 case snd_soc_dapm_input:
2241 case snd_soc_dapm_output:
2242 case snd_soc_dapm_siggen:
2243 case snd_soc_dapm_micbias:
2244 case snd_soc_dapm_vmid:
2245 case snd_soc_dapm_pre:
2246 case snd_soc_dapm_post:
2247 case snd_soc_dapm_supply:
2248 case snd_soc_dapm_regulator_supply:
2249 case snd_soc_dapm_clock_supply:
2250 case snd_soc_dapm_aif_in:
2251 case snd_soc_dapm_aif_out:
2252 case snd_soc_dapm_dai:
2253 case snd_soc_dapm_dai_link:
2254 list_add(&path->list, &dapm->card->paths);
2255 list_add(&path->list_sink, &wsink->sources);
2256 list_add(&path->list_source, &wsource->sinks);
2259 case snd_soc_dapm_mux:
2260 case snd_soc_dapm_virt_mux:
2261 case snd_soc_dapm_value_mux:
2262 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2263 &wsink->kcontrol_news[0]);
2267 case snd_soc_dapm_switch:
2268 case snd_soc_dapm_mixer:
2269 case snd_soc_dapm_mixer_named_ctl:
2270 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2274 case snd_soc_dapm_hp:
2275 case snd_soc_dapm_mic:
2276 case snd_soc_dapm_line:
2277 case snd_soc_dapm_spk:
2278 list_add(&path->list, &dapm->card->paths);
2279 list_add(&path->list_sink, &wsink->sources);
2280 list_add(&path->list_source, &wsource->sinks);
2285 dapm_mark_dirty(wsource, "Route added");
2286 dapm_mark_dirty(wsink, "Route added");
2291 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2292 source, control, sink);
2297 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2298 const struct snd_soc_dapm_route *route)
2300 struct snd_soc_dapm_path *path, *p;
2303 char prefixed_sink[80];
2304 char prefixed_source[80];
2306 if (route->control) {
2308 "Removal of routes with controls not supported\n");
2312 if (dapm->codec && dapm->codec->name_prefix) {
2313 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2314 dapm->codec->name_prefix, route->sink);
2315 sink = prefixed_sink;
2316 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2317 dapm->codec->name_prefix, route->source);
2318 source = prefixed_source;
2321 source = route->source;
2325 list_for_each_entry(p, &dapm->card->paths, list) {
2326 if (strcmp(p->source->name, source) != 0)
2328 if (strcmp(p->sink->name, sink) != 0)
2335 dapm_mark_dirty(path->source, "Route removed");
2336 dapm_mark_dirty(path->sink, "Route removed");
2338 list_del(&path->list);
2339 list_del(&path->list_sink);
2340 list_del(&path->list_source);
2343 dev_warn(dapm->dev, "Route %s->%s does not exist\n",
2351 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2352 * @dapm: DAPM context
2353 * @route: audio routes
2354 * @num: number of routes
2356 * Connects 2 dapm widgets together via a named audio path. The sink is
2357 * the widget receiving the audio signal, whilst the source is the sender
2358 * of the audio signal.
2360 * Returns 0 for success else error. On error all resources can be freed
2361 * with a call to snd_soc_card_free().
2363 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2364 const struct snd_soc_dapm_route *route, int num)
2368 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2369 for (i = 0; i < num; i++) {
2370 r = snd_soc_dapm_add_route(dapm, route);
2372 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2373 route->source, route->sink);
2378 mutex_unlock(&dapm->card->dapm_mutex);
2382 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2385 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2386 * @dapm: DAPM context
2387 * @route: audio routes
2388 * @num: number of routes
2390 * Removes routes from the DAPM context.
2392 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2393 const struct snd_soc_dapm_route *route, int num)
2397 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2398 for (i = 0; i < num; i++) {
2399 snd_soc_dapm_del_route(dapm, route);
2402 mutex_unlock(&dapm->card->dapm_mutex);
2406 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2408 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2409 const struct snd_soc_dapm_route *route)
2411 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2414 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2417 struct snd_soc_dapm_path *path;
2421 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2427 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2432 if (route->control || route->connected)
2433 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2434 route->source, route->sink);
2436 list_for_each_entry(path, &source->sinks, list_source) {
2437 if (path->sink == sink) {
2444 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2445 route->source, route->sink);
2447 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2448 count, route->source, route->sink);
2454 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2455 * @dapm: DAPM context
2456 * @route: audio routes
2457 * @num: number of routes
2459 * Mark existing routes matching those specified in the passed array
2460 * as being weak, meaning that they are ignored for the purpose of
2461 * power decisions. The main intended use case is for sidetone paths
2462 * which couple audio between other independent paths if they are both
2463 * active in order to make the combination work better at the user
2464 * level but which aren't intended to be "used".
2466 * Note that CODEC drivers should not use this as sidetone type paths
2467 * can frequently also be used as bypass paths.
2469 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2470 const struct snd_soc_dapm_route *route, int num)
2475 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2476 for (i = 0; i < num; i++) {
2477 err = snd_soc_dapm_weak_route(dapm, route);
2482 mutex_unlock(&dapm->card->dapm_mutex);
2486 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2489 * snd_soc_dapm_new_widgets - add new dapm widgets
2490 * @dapm: DAPM context
2492 * Checks the codec for any new dapm widgets and creates them if found.
2494 * Returns 0 for success.
2496 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2498 struct snd_soc_dapm_widget *w;
2501 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2503 list_for_each_entry(w, &dapm->card->widgets, list)
2508 if (w->num_kcontrols) {
2509 w->kcontrols = kzalloc(w->num_kcontrols *
2510 sizeof(struct snd_kcontrol *),
2512 if (!w->kcontrols) {
2513 mutex_unlock(&dapm->card->dapm_mutex);
2519 case snd_soc_dapm_switch:
2520 case snd_soc_dapm_mixer:
2521 case snd_soc_dapm_mixer_named_ctl:
2524 case snd_soc_dapm_mux:
2525 case snd_soc_dapm_virt_mux:
2526 case snd_soc_dapm_value_mux:
2529 case snd_soc_dapm_pga:
2530 case snd_soc_dapm_out_drv:
2537 /* Read the initial power state from the device */
2539 val = soc_widget_read(w, w->reg);
2540 val &= 1 << w->shift;
2550 dapm_mark_dirty(w, "new widget");
2551 dapm_debugfs_add_widget(w);
2554 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2555 mutex_unlock(&dapm->card->dapm_mutex);
2558 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2561 * snd_soc_dapm_get_volsw - dapm mixer get callback
2562 * @kcontrol: mixer control
2563 * @ucontrol: control element information
2565 * Callback to get the value of a dapm mixer control.
2567 * Returns 0 for success.
2569 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2570 struct snd_ctl_elem_value *ucontrol)
2572 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2573 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2574 struct soc_mixer_control *mc =
2575 (struct soc_mixer_control *)kcontrol->private_value;
2576 unsigned int reg = mc->reg;
2577 unsigned int shift = mc->shift;
2579 unsigned int mask = (1 << fls(max)) - 1;
2580 unsigned int invert = mc->invert;
2582 if (snd_soc_volsw_is_stereo(mc))
2583 dev_warn(widget->dapm->dev,
2584 "Control '%s' is stereo, which is not supported\n",
2587 ucontrol->value.integer.value[0] =
2588 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2590 ucontrol->value.integer.value[0] =
2591 max - ucontrol->value.integer.value[0];
2595 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2598 * snd_soc_dapm_put_volsw - dapm mixer set callback
2599 * @kcontrol: mixer control
2600 * @ucontrol: control element information
2602 * Callback to set the value of a dapm mixer control.
2604 * Returns 0 for success.
2606 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2607 struct snd_ctl_elem_value *ucontrol)
2609 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2610 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2611 struct snd_soc_codec *codec = widget->codec;
2612 struct snd_soc_card *card = codec->card;
2613 struct soc_mixer_control *mc =
2614 (struct soc_mixer_control *)kcontrol->private_value;
2615 unsigned int reg = mc->reg;
2616 unsigned int shift = mc->shift;
2618 unsigned int mask = (1 << fls(max)) - 1;
2619 unsigned int invert = mc->invert;
2621 int connect, change;
2622 struct snd_soc_dapm_update update;
2625 if (snd_soc_volsw_is_stereo(mc))
2626 dev_warn(widget->dapm->dev,
2627 "Control '%s' is stereo, which is not supported\n",
2630 val = (ucontrol->value.integer.value[0] & mask);
2635 mask = mask << shift;
2638 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2640 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2642 for (wi = 0; wi < wlist->num_widgets; wi++) {
2643 widget = wlist->widgets[wi];
2645 widget->value = val;
2647 update.kcontrol = kcontrol;
2648 update.widget = widget;
2652 widget->dapm->update = &update;
2654 soc_dapm_mixer_update_power(widget, kcontrol, connect);
2656 widget->dapm->update = NULL;
2660 mutex_unlock(&card->dapm_mutex);
2663 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2666 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2667 * @kcontrol: mixer control
2668 * @ucontrol: control element information
2670 * Callback to get the value of a dapm enumerated double mixer control.
2672 * Returns 0 for success.
2674 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2675 struct snd_ctl_elem_value *ucontrol)
2677 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2678 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2679 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2680 unsigned int val, bitmask;
2682 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2684 val = snd_soc_read(widget->codec, e->reg);
2685 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2686 if (e->shift_l != e->shift_r)
2687 ucontrol->value.enumerated.item[1] =
2688 (val >> e->shift_r) & (bitmask - 1);
2692 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2695 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2696 * @kcontrol: mixer control
2697 * @ucontrol: control element information
2699 * Callback to set the value of a dapm enumerated double mixer control.
2701 * Returns 0 for success.
2703 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2704 struct snd_ctl_elem_value *ucontrol)
2706 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2707 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2708 struct snd_soc_codec *codec = widget->codec;
2709 struct snd_soc_card *card = codec->card;
2710 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2711 unsigned int val, mux, change;
2712 unsigned int mask, bitmask;
2713 struct snd_soc_dapm_update update;
2716 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2718 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2720 mux = ucontrol->value.enumerated.item[0];
2721 val = mux << e->shift_l;
2722 mask = (bitmask - 1) << e->shift_l;
2723 if (e->shift_l != e->shift_r) {
2724 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2726 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2727 mask |= (bitmask - 1) << e->shift_r;
2730 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2732 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2734 for (wi = 0; wi < wlist->num_widgets; wi++) {
2735 widget = wlist->widgets[wi];
2737 widget->value = val;
2739 update.kcontrol = kcontrol;
2740 update.widget = widget;
2741 update.reg = e->reg;
2744 widget->dapm->update = &update;
2746 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2748 widget->dapm->update = NULL;
2752 mutex_unlock(&card->dapm_mutex);
2755 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2758 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2759 * @kcontrol: mixer control
2760 * @ucontrol: control element information
2762 * Returns 0 for success.
2764 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2765 struct snd_ctl_elem_value *ucontrol)
2767 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2768 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2770 ucontrol->value.enumerated.item[0] = widget->value;
2774 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2777 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2778 * @kcontrol: mixer control
2779 * @ucontrol: control element information
2781 * Returns 0 for success.
2783 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2784 struct snd_ctl_elem_value *ucontrol)
2786 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2787 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2788 struct snd_soc_codec *codec = widget->codec;
2789 struct snd_soc_card *card = codec->card;
2790 struct soc_enum *e =
2791 (struct soc_enum *)kcontrol->private_value;
2796 if (ucontrol->value.enumerated.item[0] >= e->max)
2799 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2801 change = widget->value != ucontrol->value.enumerated.item[0];
2803 for (wi = 0; wi < wlist->num_widgets; wi++) {
2804 widget = wlist->widgets[wi];
2806 widget->value = ucontrol->value.enumerated.item[0];
2808 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
2812 mutex_unlock(&card->dapm_mutex);
2815 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2818 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2820 * @kcontrol: mixer control
2821 * @ucontrol: control element information
2823 * Callback to get the value of a dapm semi enumerated double mixer control.
2825 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2826 * used for handling bitfield coded enumeration for example.
2828 * Returns 0 for success.
2830 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_value *ucontrol)
2833 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2834 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2835 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2836 unsigned int reg_val, val, mux;
2838 reg_val = snd_soc_read(widget->codec, e->reg);
2839 val = (reg_val >> e->shift_l) & e->mask;
2840 for (mux = 0; mux < e->max; mux++) {
2841 if (val == e->values[mux])
2844 ucontrol->value.enumerated.item[0] = mux;
2845 if (e->shift_l != e->shift_r) {
2846 val = (reg_val >> e->shift_r) & e->mask;
2847 for (mux = 0; mux < e->max; mux++) {
2848 if (val == e->values[mux])
2851 ucontrol->value.enumerated.item[1] = mux;
2856 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2859 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2861 * @kcontrol: mixer control
2862 * @ucontrol: control element information
2864 * Callback to set the value of a dapm semi enumerated double mixer control.
2866 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2867 * used for handling bitfield coded enumeration for example.
2869 * Returns 0 for success.
2871 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2872 struct snd_ctl_elem_value *ucontrol)
2874 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2875 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2876 struct snd_soc_codec *codec = widget->codec;
2877 struct snd_soc_card *card = codec->card;
2878 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2879 unsigned int val, mux, change;
2881 struct snd_soc_dapm_update update;
2884 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2886 mux = ucontrol->value.enumerated.item[0];
2887 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2888 mask = e->mask << e->shift_l;
2889 if (e->shift_l != e->shift_r) {
2890 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2892 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2893 mask |= e->mask << e->shift_r;
2896 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2898 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2900 for (wi = 0; wi < wlist->num_widgets; wi++) {
2901 widget = wlist->widgets[wi];
2903 widget->value = val;
2905 update.kcontrol = kcontrol;
2906 update.widget = widget;
2907 update.reg = e->reg;
2910 widget->dapm->update = &update;
2912 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2914 widget->dapm->update = NULL;
2918 mutex_unlock(&card->dapm_mutex);
2921 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2924 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2926 * @kcontrol: mixer control
2927 * @uinfo: control element information
2929 * Callback to provide information about a pin switch control.
2931 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2932 struct snd_ctl_elem_info *uinfo)
2934 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2936 uinfo->value.integer.min = 0;
2937 uinfo->value.integer.max = 1;
2941 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2944 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2946 * @kcontrol: mixer control
2949 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2950 struct snd_ctl_elem_value *ucontrol)
2952 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2953 const char *pin = (const char *)kcontrol->private_value;
2955 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2957 ucontrol->value.integer.value[0] =
2958 snd_soc_dapm_get_pin_status(&card->dapm, pin);
2960 mutex_unlock(&card->dapm_mutex);
2964 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2967 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2969 * @kcontrol: mixer control
2972 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2973 struct snd_ctl_elem_value *ucontrol)
2975 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2976 const char *pin = (const char *)kcontrol->private_value;
2978 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2980 if (ucontrol->value.integer.value[0])
2981 snd_soc_dapm_enable_pin(&card->dapm, pin);
2983 snd_soc_dapm_disable_pin(&card->dapm, pin);
2985 mutex_unlock(&card->dapm_mutex);
2987 snd_soc_dapm_sync(&card->dapm);
2990 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2992 static struct snd_soc_dapm_widget *
2993 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2994 const struct snd_soc_dapm_widget *widget)
2996 struct snd_soc_dapm_widget *w;
3000 if ((w = dapm_cnew_widget(widget)) == NULL)
3004 case snd_soc_dapm_regulator_supply:
3005 w->regulator = devm_regulator_get(dapm->dev, w->name);
3006 if (IS_ERR(w->regulator)) {
3007 ret = PTR_ERR(w->regulator);
3008 dev_err(dapm->dev, "Failed to request %s: %d\n",
3013 case snd_soc_dapm_clock_supply:
3014 #ifdef CONFIG_CLKDEV_LOOKUP
3015 w->clk = devm_clk_get(dapm->dev, w->name);
3016 if (IS_ERR(w->clk)) {
3017 ret = PTR_ERR(w->clk);
3018 dev_err(dapm->dev, "Failed to request %s: %d\n",
3030 name_len = strlen(widget->name) + 1;
3031 if (dapm->codec && dapm->codec->name_prefix)
3032 name_len += 1 + strlen(dapm->codec->name_prefix);
3033 w->name = kmalloc(name_len, GFP_KERNEL);
3034 if (w->name == NULL) {
3038 if (dapm->codec && dapm->codec->name_prefix)
3039 snprintf((char *)w->name, name_len, "%s %s",
3040 dapm->codec->name_prefix, widget->name);
3042 snprintf((char *)w->name, name_len, "%s", widget->name);
3045 case snd_soc_dapm_switch:
3046 case snd_soc_dapm_mixer:
3047 case snd_soc_dapm_mixer_named_ctl:
3048 w->power_check = dapm_generic_check_power;
3050 case snd_soc_dapm_mux:
3051 case snd_soc_dapm_virt_mux:
3052 case snd_soc_dapm_value_mux:
3053 w->power_check = dapm_generic_check_power;
3055 case snd_soc_dapm_adc:
3056 case snd_soc_dapm_aif_out:
3057 w->power_check = dapm_adc_check_power;
3059 case snd_soc_dapm_dac:
3060 case snd_soc_dapm_aif_in:
3061 w->power_check = dapm_dac_check_power;
3063 case snd_soc_dapm_pga:
3064 case snd_soc_dapm_out_drv:
3065 case snd_soc_dapm_input:
3066 case snd_soc_dapm_output:
3067 case snd_soc_dapm_micbias:
3068 case snd_soc_dapm_spk:
3069 case snd_soc_dapm_hp:
3070 case snd_soc_dapm_mic:
3071 case snd_soc_dapm_line:
3072 case snd_soc_dapm_dai_link:
3073 w->power_check = dapm_generic_check_power;
3075 case snd_soc_dapm_supply:
3076 case snd_soc_dapm_regulator_supply:
3077 case snd_soc_dapm_clock_supply:
3078 w->power_check = dapm_supply_check_power;
3080 case snd_soc_dapm_dai:
3081 w->power_check = dapm_dai_check_power;
3084 w->power_check = dapm_always_on_check_power;
3090 w->codec = dapm->codec;
3091 w->platform = dapm->platform;
3092 INIT_LIST_HEAD(&w->sources);
3093 INIT_LIST_HEAD(&w->sinks);
3094 INIT_LIST_HEAD(&w->list);
3095 INIT_LIST_HEAD(&w->dirty);
3096 list_add(&w->list, &dapm->card->widgets);
3098 /* machine layer set ups unconnected pins and insertions */
3104 * snd_soc_dapm_new_controls - create new dapm controls
3105 * @dapm: DAPM context
3106 * @widget: widget array
3107 * @num: number of widgets
3109 * Creates new DAPM controls based upon the templates.
3111 * Returns 0 for success else error.
3113 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3114 const struct snd_soc_dapm_widget *widget,
3117 struct snd_soc_dapm_widget *w;
3121 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3122 for (i = 0; i < num; i++) {
3123 w = snd_soc_dapm_new_control(dapm, widget);
3126 "ASoC: Failed to create DAPM control %s\n",
3133 mutex_unlock(&dapm->card->dapm_mutex);
3136 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3138 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3139 struct snd_kcontrol *kcontrol, int event)
3141 struct snd_soc_dapm_path *source_p, *sink_p;
3142 struct snd_soc_dai *source, *sink;
3143 const struct snd_soc_pcm_stream *config = w->params;
3144 struct snd_pcm_substream substream;
3145 struct snd_pcm_hw_params *params = NULL;
3150 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3152 /* We only support a single source and sink, pick the first */
3153 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3155 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3158 BUG_ON(!source_p || !sink_p);
3159 BUG_ON(!sink_p->source || !source_p->sink);
3160 BUG_ON(!source_p->source || !sink_p->sink);
3162 source = source_p->source->priv;
3163 sink = sink_p->sink->priv;
3165 /* Be a little careful as we don't want to overflow the mask array */
3166 if (config->formats) {
3167 fmt = ffs(config->formats) - 1;
3169 dev_warn(w->dapm->dev, "Invalid format %llx specified\n",
3174 /* Currently very limited parameter selection */
3175 params = kzalloc(sizeof(*params), GFP_KERNEL);
3180 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3182 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3184 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3187 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3188 = config->channels_min;
3189 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3190 = config->channels_max;
3192 memset(&substream, 0, sizeof(substream));
3195 case SND_SOC_DAPM_PRE_PMU:
3196 if (source->driver->ops && source->driver->ops->hw_params) {
3197 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3198 ret = source->driver->ops->hw_params(&substream,
3201 dev_err(source->dev,
3202 "hw_params() failed: %d\n", ret);
3207 if (sink->driver->ops && sink->driver->ops->hw_params) {
3208 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3209 ret = sink->driver->ops->hw_params(&substream, params,
3213 "hw_params() failed: %d\n", ret);
3219 case SND_SOC_DAPM_POST_PMU:
3220 ret = snd_soc_dai_digital_mute(sink, 0);
3221 if (ret != 0 && ret != -ENOTSUPP)
3222 dev_warn(sink->dev, "Failed to unmute: %d\n", ret);
3226 case SND_SOC_DAPM_PRE_PMD:
3227 ret = snd_soc_dai_digital_mute(sink, 1);
3228 if (ret != 0 && ret != -ENOTSUPP)
3229 dev_warn(sink->dev, "Failed to mute: %d\n", ret);
3243 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3244 const struct snd_soc_pcm_stream *params,
3245 struct snd_soc_dapm_widget *source,
3246 struct snd_soc_dapm_widget *sink)
3248 struct snd_soc_dapm_route routes[2];
3249 struct snd_soc_dapm_widget template;
3250 struct snd_soc_dapm_widget *w;
3254 len = strlen(source->name) + strlen(sink->name) + 2;
3255 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3258 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3260 memset(&template, 0, sizeof(template));
3261 template.reg = SND_SOC_NOPM;
3262 template.id = snd_soc_dapm_dai_link;
3263 template.name = link_name;
3264 template.event = snd_soc_dai_link_event;
3265 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3266 SND_SOC_DAPM_PRE_PMD;
3268 dev_dbg(card->dev, "adding %s widget\n", link_name);
3270 w = snd_soc_dapm_new_control(&card->dapm, &template);
3272 dev_err(card->dev, "Failed to create %s widget\n",
3279 memset(&routes, 0, sizeof(routes));
3281 routes[0].source = source->name;
3282 routes[0].sink = link_name;
3283 routes[1].source = link_name;
3284 routes[1].sink = sink->name;
3286 return snd_soc_dapm_add_routes(&card->dapm, routes,
3287 ARRAY_SIZE(routes));
3290 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3291 struct snd_soc_dai *dai)
3293 struct snd_soc_dapm_widget template;
3294 struct snd_soc_dapm_widget *w;
3296 WARN_ON(dapm->dev != dai->dev);
3298 memset(&template, 0, sizeof(template));
3299 template.reg = SND_SOC_NOPM;
3301 if (dai->driver->playback.stream_name) {
3302 template.id = snd_soc_dapm_dai;
3303 template.name = dai->driver->playback.stream_name;
3304 template.sname = dai->driver->playback.stream_name;
3306 dev_dbg(dai->dev, "adding %s widget\n",
3309 w = snd_soc_dapm_new_control(dapm, &template);
3311 dev_err(dapm->dev, "Failed to create %s widget\n",
3312 dai->driver->playback.stream_name);
3316 dai->playback_widget = w;
3319 if (dai->driver->capture.stream_name) {
3320 template.id = snd_soc_dapm_dai;
3321 template.name = dai->driver->capture.stream_name;
3322 template.sname = dai->driver->capture.stream_name;
3324 dev_dbg(dai->dev, "adding %s widget\n",
3327 w = snd_soc_dapm_new_control(dapm, &template);
3329 dev_err(dapm->dev, "Failed to create %s widget\n",
3330 dai->driver->capture.stream_name);
3334 dai->capture_widget = w;
3340 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3342 struct snd_soc_dapm_widget *dai_w, *w;
3343 struct snd_soc_dai *dai;
3344 struct snd_soc_dapm_route r;
3346 memset(&r, 0, sizeof(r));
3348 /* For each DAI widget... */
3349 list_for_each_entry(dai_w, &card->widgets, list) {
3350 if (dai_w->id != snd_soc_dapm_dai)
3355 /* ...find all widgets with the same stream and link them */
3356 list_for_each_entry(w, &card->widgets, list) {
3357 if (w->dapm != dai_w->dapm)
3360 if (w->id == snd_soc_dapm_dai)
3366 if (dai->driver->playback.stream_name &&
3368 dai->driver->playback.stream_name)) {
3369 r.source = dai->playback_widget->name;
3371 dev_dbg(dai->dev, "%s -> %s\n",
3374 snd_soc_dapm_add_route(w->dapm, &r);
3377 if (dai->driver->capture.stream_name &&
3379 dai->driver->capture.stream_name)) {
3381 r.sink = dai->capture_widget->name;
3382 dev_dbg(dai->dev, "%s -> %s\n",
3385 snd_soc_dapm_add_route(w->dapm, &r);
3393 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3397 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3398 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3399 struct snd_soc_dai *codec_dai = rtd->codec_dai;
3401 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3402 w_cpu = cpu_dai->playback_widget;
3403 w_codec = codec_dai->playback_widget;
3405 w_cpu = cpu_dai->capture_widget;
3406 w_codec = codec_dai->capture_widget;
3411 dapm_mark_dirty(w_cpu, "stream event");
3414 case SND_SOC_DAPM_STREAM_START:
3417 case SND_SOC_DAPM_STREAM_STOP:
3420 case SND_SOC_DAPM_STREAM_SUSPEND:
3421 case SND_SOC_DAPM_STREAM_RESUME:
3422 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3423 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3430 dapm_mark_dirty(w_codec, "stream event");
3433 case SND_SOC_DAPM_STREAM_START:
3434 w_codec->active = 1;
3436 case SND_SOC_DAPM_STREAM_STOP:
3437 w_codec->active = 0;
3439 case SND_SOC_DAPM_STREAM_SUSPEND:
3440 case SND_SOC_DAPM_STREAM_RESUME:
3441 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3442 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3447 dapm_power_widgets(&rtd->card->dapm, event);
3451 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3452 * @rtd: PCM runtime data
3453 * @stream: stream name
3454 * @event: stream event
3456 * Sends a stream event to the dapm core. The core then makes any
3457 * necessary widget power changes.
3459 * Returns 0 for success else error.
3461 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3464 struct snd_soc_card *card = rtd->card;
3466 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3467 soc_dapm_stream_event(rtd, stream, event);
3468 mutex_unlock(&card->dapm_mutex);
3472 * snd_soc_dapm_enable_pin - enable pin.
3473 * @dapm: DAPM context
3476 * Enables input/output pin and its parents or children widgets iff there is
3477 * a valid audio route and active audio stream.
3478 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3479 * do any widget power switching.
3481 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3483 return snd_soc_dapm_set_pin(dapm, pin, 1);
3485 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3488 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3489 * @dapm: DAPM context
3492 * Enables input/output pin regardless of any other state. This is
3493 * intended for use with microphone bias supplies used in microphone
3496 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3497 * do any widget power switching.
3499 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3502 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3505 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3509 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3512 dapm_mark_dirty(w, "force enable");
3516 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3519 * snd_soc_dapm_disable_pin - disable pin.
3520 * @dapm: DAPM context
3523 * Disables input/output pin and its parents or children widgets.
3524 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3525 * do any widget power switching.
3527 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3530 return snd_soc_dapm_set_pin(dapm, pin, 0);
3532 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3535 * snd_soc_dapm_nc_pin - permanently disable pin.
3536 * @dapm: DAPM context
3539 * Marks the specified pin as being not connected, disabling it along
3540 * any parent or child widgets. At present this is identical to
3541 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3542 * additional things such as disabling controls which only affect
3543 * paths through the pin.
3545 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3546 * do any widget power switching.
3548 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3550 return snd_soc_dapm_set_pin(dapm, pin, 0);
3552 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3555 * snd_soc_dapm_get_pin_status - get audio pin status
3556 * @dapm: DAPM context
3557 * @pin: audio signal pin endpoint (or start point)
3559 * Get audio pin status - connected or disconnected.
3561 * Returns 1 for connected otherwise 0.
3563 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3566 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3569 return w->connected;
3573 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3576 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3577 * @dapm: DAPM context
3578 * @pin: audio signal pin endpoint (or start point)
3580 * Mark the given endpoint or pin as ignoring suspend. When the
3581 * system is disabled a path between two endpoints flagged as ignoring
3582 * suspend will not be disabled. The path must already be enabled via
3583 * normal means at suspend time, it will not be turned on if it was not
3586 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3589 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3592 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3596 w->ignore_suspend = 1;
3600 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3602 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3603 struct snd_soc_dapm_widget *w)
3605 struct snd_soc_dapm_path *p;
3607 list_for_each_entry(p, &card->paths, list) {
3608 if ((p->source == w) || (p->sink == w)) {
3610 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3611 p->source->name, p->source->id, p->source->dapm,
3612 p->sink->name, p->sink->id, p->sink->dapm);
3614 /* Connected to something other than the codec */
3615 if (p->source->dapm != p->sink->dapm)
3618 * Loopback connection from codec external pin to
3619 * codec external pin
3621 if (p->sink->id == snd_soc_dapm_input) {
3622 switch (p->source->id) {
3623 case snd_soc_dapm_output:
3624 case snd_soc_dapm_micbias:
3637 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3638 * @codec: The codec whose pins should be processed
3640 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3641 * which are unused. Pins are used if they are connected externally to the
3642 * codec, whether that be to some other device, or a loop-back connection to
3645 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3647 struct snd_soc_card *card = codec->card;
3648 struct snd_soc_dapm_context *dapm = &codec->dapm;
3649 struct snd_soc_dapm_widget *w;
3651 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
3652 &card->dapm, &codec->dapm);
3654 list_for_each_entry(w, &card->widgets, list) {
3655 if (w->dapm != dapm)
3658 case snd_soc_dapm_input:
3659 case snd_soc_dapm_output:
3660 case snd_soc_dapm_micbias:
3661 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
3663 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3665 "... Not in map; disabling\n");
3666 snd_soc_dapm_nc_pin(dapm, w->name);
3676 * snd_soc_dapm_free - free dapm resources
3677 * @dapm: DAPM context
3679 * Free all dapm widgets and resources.
3681 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3683 snd_soc_dapm_sys_remove(dapm->dev);
3684 dapm_debugfs_cleanup(dapm);
3685 dapm_free_widgets(dapm);
3686 list_del(&dapm->list);
3688 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3690 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3692 struct snd_soc_card *card = dapm->card;
3693 struct snd_soc_dapm_widget *w;
3694 LIST_HEAD(down_list);
3697 mutex_lock(&card->dapm_mutex);
3699 list_for_each_entry(w, &dapm->card->widgets, list) {
3700 if (w->dapm != dapm)
3703 dapm_seq_insert(w, &down_list, false);
3709 /* If there were no widgets to power down we're already in
3713 if (dapm->bias_level == SND_SOC_BIAS_ON)
3714 snd_soc_dapm_set_bias_level(dapm,
3715 SND_SOC_BIAS_PREPARE);
3716 dapm_seq_run(dapm, &down_list, 0, false);
3717 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3718 snd_soc_dapm_set_bias_level(dapm,
3719 SND_SOC_BIAS_STANDBY);
3722 mutex_unlock(&card->dapm_mutex);
3726 * snd_soc_dapm_shutdown - callback for system shutdown
3728 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3730 struct snd_soc_codec *codec;
3732 list_for_each_entry(codec, &card->codec_dev_list, list) {
3733 soc_dapm_shutdown_codec(&codec->dapm);
3734 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3735 snd_soc_dapm_set_bias_level(&codec->dapm,
3740 /* Module information */
3741 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3742 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3743 MODULE_LICENSE("GPL");