2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
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/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
25 * o DAPM power change sequencing - allow for configurable per
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/async.h>
36 #include <linux/delay.h>
38 #include <linux/bitops.h>
39 #include <linux/platform_device.h>
40 #include <linux/jiffies.h>
41 #include <linux/debugfs.h>
42 #include <linux/slab.h>
43 #include <sound/core.h>
44 #include <sound/pcm.h>
45 #include <sound/pcm_params.h>
46 #include <sound/soc.h>
47 #include <sound/initval.h>
49 #include <trace/events/asoc.h>
51 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
53 /* dapm power sequences - make this per codec in the future */
54 static int dapm_up_seq[] = {
55 [snd_soc_dapm_pre] = 0,
56 [snd_soc_dapm_supply] = 1,
57 [snd_soc_dapm_micbias] = 2,
58 [snd_soc_dapm_aif_in] = 3,
59 [snd_soc_dapm_aif_out] = 3,
60 [snd_soc_dapm_mic] = 4,
61 [snd_soc_dapm_mux] = 5,
62 [snd_soc_dapm_virt_mux] = 5,
63 [snd_soc_dapm_value_mux] = 5,
64 [snd_soc_dapm_dac] = 6,
65 [snd_soc_dapm_mixer] = 7,
66 [snd_soc_dapm_mixer_named_ctl] = 7,
67 [snd_soc_dapm_pga] = 8,
68 [snd_soc_dapm_adc] = 9,
69 [snd_soc_dapm_out_drv] = 10,
70 [snd_soc_dapm_hp] = 10,
71 [snd_soc_dapm_spk] = 10,
72 [snd_soc_dapm_post] = 11,
75 static int dapm_down_seq[] = {
76 [snd_soc_dapm_pre] = 0,
77 [snd_soc_dapm_adc] = 1,
78 [snd_soc_dapm_hp] = 2,
79 [snd_soc_dapm_spk] = 2,
80 [snd_soc_dapm_out_drv] = 2,
81 [snd_soc_dapm_pga] = 4,
82 [snd_soc_dapm_mixer_named_ctl] = 5,
83 [snd_soc_dapm_mixer] = 5,
84 [snd_soc_dapm_dac] = 6,
85 [snd_soc_dapm_mic] = 7,
86 [snd_soc_dapm_micbias] = 8,
87 [snd_soc_dapm_mux] = 9,
88 [snd_soc_dapm_virt_mux] = 9,
89 [snd_soc_dapm_value_mux] = 9,
90 [snd_soc_dapm_aif_in] = 10,
91 [snd_soc_dapm_aif_out] = 10,
92 [snd_soc_dapm_supply] = 11,
93 [snd_soc_dapm_post] = 12,
96 static void pop_wait(u32 pop_time)
99 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
102 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
110 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
115 vsnprintf(buf, PAGE_SIZE, fmt, args);
116 dev_info(dev, "%s", buf);
122 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
124 return !list_empty(&w->dirty);
127 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
129 if (!dapm_dirty_widget(w)) {
130 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
132 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
135 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
137 /* create a new dapm widget */
138 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
139 const struct snd_soc_dapm_widget *_widget)
141 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
144 /* get snd_card from DAPM context */
145 static inline struct snd_card *dapm_get_snd_card(
146 struct snd_soc_dapm_context *dapm)
149 return dapm->codec->card->snd_card;
150 else if (dapm->platform)
151 return dapm->platform->card->snd_card;
159 /* get soc_card from DAPM context */
160 static inline struct snd_soc_card *dapm_get_soc_card(
161 struct snd_soc_dapm_context *dapm)
164 return dapm->codec->card;
165 else if (dapm->platform)
166 return dapm->platform->card;
174 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
177 return snd_soc_read(w->codec, reg);
178 else if (w->platform)
179 return snd_soc_platform_read(w->platform, reg);
181 dev_err(w->dapm->dev, "no valid widget read method\n");
185 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
188 return snd_soc_write(w->codec, reg, val);
189 else if (w->platform)
190 return snd_soc_platform_write(w->platform, reg, val);
192 dev_err(w->dapm->dev, "no valid widget write method\n");
196 static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
197 unsigned short reg, unsigned int mask, unsigned int value)
200 unsigned int old, new;
203 ret = soc_widget_read(w, reg);
208 new = (old & ~mask) | (value & mask);
211 ret = soc_widget_write(w, reg, new);
220 * snd_soc_dapm_set_bias_level - set the bias level for the system
221 * @dapm: DAPM context
222 * @level: level to configure
224 * Configure the bias (power) levels for the SoC audio device.
226 * Returns 0 for success else error.
228 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
229 enum snd_soc_bias_level level)
231 struct snd_soc_card *card = dapm->card;
234 trace_snd_soc_bias_level_start(card, level);
236 if (card && card->set_bias_level)
237 ret = card->set_bias_level(card, dapm, level);
242 if (dapm->codec->driver->set_bias_level)
243 ret = dapm->codec->driver->set_bias_level(dapm->codec,
246 dapm->bias_level = level;
251 if (card && card->set_bias_level_post)
252 ret = card->set_bias_level_post(card, dapm, level);
254 trace_snd_soc_bias_level_done(card, level);
259 /* set up initial codec paths */
260 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
261 struct snd_soc_dapm_path *p, int i)
264 case snd_soc_dapm_switch:
265 case snd_soc_dapm_mixer:
266 case snd_soc_dapm_mixer_named_ctl: {
268 struct soc_mixer_control *mc = (struct soc_mixer_control *)
269 w->kcontrol_news[i].private_value;
270 unsigned int reg = mc->reg;
271 unsigned int shift = mc->shift;
273 unsigned int mask = (1 << fls(max)) - 1;
274 unsigned int invert = mc->invert;
276 val = soc_widget_read(w, reg);
277 val = (val >> shift) & mask;
279 if ((invert && !val) || (!invert && val))
285 case snd_soc_dapm_mux: {
286 struct soc_enum *e = (struct soc_enum *)
287 w->kcontrol_news[i].private_value;
288 int val, item, bitmask;
290 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
292 val = soc_widget_read(w, e->reg);
293 item = (val >> e->shift_l) & (bitmask - 1);
296 for (i = 0; i < e->max; i++) {
297 if (!(strcmp(p->name, e->texts[i])) && item == i)
302 case snd_soc_dapm_virt_mux: {
303 struct soc_enum *e = (struct soc_enum *)
304 w->kcontrol_news[i].private_value;
307 /* since a virtual mux has no backing registers to
308 * decide which path to connect, it will try to match
309 * with the first enumeration. This is to ensure
310 * that the default mux choice (the first) will be
311 * correctly powered up during initialization.
313 if (!strcmp(p->name, e->texts[0]))
317 case snd_soc_dapm_value_mux: {
318 struct soc_enum *e = (struct soc_enum *)
319 w->kcontrol_news[i].private_value;
322 val = soc_widget_read(w, e->reg);
323 val = (val >> e->shift_l) & e->mask;
324 for (item = 0; item < e->max; item++) {
325 if (val == e->values[item])
330 for (i = 0; i < e->max; i++) {
331 if (!(strcmp(p->name, e->texts[i])) && item == i)
336 /* does not affect routing - always connected */
337 case snd_soc_dapm_pga:
338 case snd_soc_dapm_out_drv:
339 case snd_soc_dapm_output:
340 case snd_soc_dapm_adc:
341 case snd_soc_dapm_input:
342 case snd_soc_dapm_dac:
343 case snd_soc_dapm_micbias:
344 case snd_soc_dapm_vmid:
345 case snd_soc_dapm_supply:
346 case snd_soc_dapm_aif_in:
347 case snd_soc_dapm_aif_out:
348 case snd_soc_dapm_hp:
349 case snd_soc_dapm_mic:
350 case snd_soc_dapm_spk:
351 case snd_soc_dapm_line:
354 /* does affect routing - dynamically connected */
355 case snd_soc_dapm_pre:
356 case snd_soc_dapm_post:
362 /* connect mux widget to its interconnecting audio paths */
363 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
364 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
365 struct snd_soc_dapm_path *path, const char *control_name,
366 const struct snd_kcontrol_new *kcontrol)
368 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
371 for (i = 0; i < e->max; i++) {
372 if (!(strcmp(control_name, e->texts[i]))) {
373 list_add(&path->list, &dapm->card->paths);
374 list_add(&path->list_sink, &dest->sources);
375 list_add(&path->list_source, &src->sinks);
376 path->name = (char*)e->texts[i];
377 dapm_set_path_status(dest, path, 0);
385 /* connect mixer widget to its interconnecting audio paths */
386 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
387 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
388 struct snd_soc_dapm_path *path, const char *control_name)
392 /* search for mixer kcontrol */
393 for (i = 0; i < dest->num_kcontrols; i++) {
394 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
395 list_add(&path->list, &dapm->card->paths);
396 list_add(&path->list_sink, &dest->sources);
397 list_add(&path->list_source, &src->sinks);
398 path->name = dest->kcontrol_news[i].name;
399 dapm_set_path_status(dest, path, i);
406 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
407 struct snd_soc_dapm_widget *kcontrolw,
408 const struct snd_kcontrol_new *kcontrol_new,
409 struct snd_kcontrol **kcontrol)
411 struct snd_soc_dapm_widget *w;
416 list_for_each_entry(w, &dapm->card->widgets, list) {
417 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
419 for (i = 0; i < w->num_kcontrols; i++) {
420 if (&w->kcontrol_news[i] == kcontrol_new) {
422 *kcontrol = w->kcontrols[i];
431 /* create new dapm mixer control */
432 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
434 struct snd_soc_dapm_context *dapm = w->dapm;
436 size_t name_len, prefix_len;
437 struct snd_soc_dapm_path *path;
438 struct snd_card *card = dapm->card->snd_card;
440 struct snd_soc_dapm_widget_list *wlist;
444 prefix = dapm->codec->name_prefix;
449 prefix_len = strlen(prefix) + 1;
454 for (i = 0; i < w->num_kcontrols; i++) {
457 list_for_each_entry(path, &w->sources, list_sink) {
459 /* mixer/mux paths name must match control name */
460 if (path->name != (char *)w->kcontrol_news[i].name)
463 if (w->kcontrols[i]) {
464 path->kcontrol = w->kcontrols[i];
468 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
469 sizeof(struct snd_soc_dapm_widget *),
470 wlist = kzalloc(wlistsize, GFP_KERNEL);
473 "asoc: can't allocate widget list for %s\n",
477 wlist->num_widgets = 1;
478 wlist->widgets[0] = w;
480 /* add dapm control with long name.
481 * for dapm_mixer this is the concatenation of the
482 * mixer and kcontrol name.
483 * for dapm_mixer_named_ctl this is simply the
486 name_len = strlen(w->kcontrol_news[i].name) + 1;
487 if (w->id != snd_soc_dapm_mixer_named_ctl)
488 name_len += 1 + strlen(w->name);
490 path->long_name = kmalloc(name_len, GFP_KERNEL);
492 if (path->long_name == NULL) {
499 /* The control will get a prefix from
500 * the control creation process but
501 * we're also using the same prefix
502 * for widgets so cut the prefix off
503 * the front of the widget name.
505 snprintf(path->long_name, name_len, "%s %s",
506 w->name + prefix_len,
507 w->kcontrol_news[i].name);
509 case snd_soc_dapm_mixer_named_ctl:
510 snprintf(path->long_name, name_len, "%s",
511 w->kcontrol_news[i].name);
515 path->long_name[name_len - 1] = '\0';
517 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
518 wlist, path->long_name,
520 ret = snd_ctl_add(card, path->kcontrol);
523 "asoc: failed to add dapm kcontrol %s: %d\n",
524 path->long_name, ret);
526 kfree(path->long_name);
527 path->long_name = NULL;
530 w->kcontrols[i] = path->kcontrol;
536 /* create new dapm mux control */
537 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
539 struct snd_soc_dapm_context *dapm = w->dapm;
540 struct snd_soc_dapm_path *path = NULL;
541 struct snd_kcontrol *kcontrol;
542 struct snd_card *card = dapm->card->snd_card;
546 struct snd_soc_dapm_widget_list *wlist;
547 int shared, wlistentries;
551 if (w->num_kcontrols != 1) {
553 "asoc: mux %s has incorrect number of controls\n",
558 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
561 wlist = kcontrol->private_data;
562 wlistentries = wlist->num_widgets + 1;
567 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
568 wlistentries * sizeof(struct snd_soc_dapm_widget *),
569 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
572 "asoc: can't allocate widget list for %s\n", w->name);
575 wlist->num_widgets = wlistentries;
576 wlist->widgets[wlistentries - 1] = w;
580 prefix = dapm->codec->name_prefix;
585 name = w->kcontrol_news[0].name;
590 prefix_len = strlen(prefix) + 1;
596 * The control will get a prefix from the control creation
597 * process but we're also using the same prefix for widgets so
598 * cut the prefix off the front of the widget name.
600 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
601 name + prefix_len, prefix);
602 ret = snd_ctl_add(card, kcontrol);
604 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
611 kcontrol->private_data = wlist;
613 w->kcontrols[0] = kcontrol;
615 list_for_each_entry(path, &w->sources, list_sink)
616 path->kcontrol = kcontrol;
621 /* create new dapm volume control */
622 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
624 if (w->num_kcontrols)
625 dev_err(w->dapm->dev,
626 "asoc: PGA controls not supported: '%s'\n", w->name);
631 /* reset 'walked' bit for each dapm path */
632 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
634 struct snd_soc_dapm_path *p;
636 list_for_each_entry(p, &dapm->card->paths, list)
640 /* We implement power down on suspend by checking the power state of
641 * the ALSA card - when we are suspending the ALSA state for the card
644 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
646 int level = snd_power_get_state(widget->dapm->card->snd_card);
649 case SNDRV_CTL_POWER_D3hot:
650 case SNDRV_CTL_POWER_D3cold:
651 if (widget->ignore_suspend)
652 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
654 return widget->ignore_suspend;
661 * Recursively check for a completed path to an active or physically connected
662 * output widget. Returns number of complete paths.
664 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
666 struct snd_soc_dapm_path *path;
669 if (widget->outputs >= 0)
670 return widget->outputs;
672 DAPM_UPDATE_STAT(widget, path_checks);
674 if (widget->id == snd_soc_dapm_supply)
677 switch (widget->id) {
678 case snd_soc_dapm_adc:
679 case snd_soc_dapm_aif_out:
680 if (widget->active) {
681 widget->outputs = snd_soc_dapm_suspend_check(widget);
682 return widget->outputs;
688 if (widget->connected) {
689 /* connected pin ? */
690 if (widget->id == snd_soc_dapm_output && !widget->ext) {
691 widget->outputs = snd_soc_dapm_suspend_check(widget);
692 return widget->outputs;
695 /* connected jack or spk ? */
696 if (widget->id == snd_soc_dapm_hp ||
697 widget->id == snd_soc_dapm_spk ||
698 (widget->id == snd_soc_dapm_line &&
699 !list_empty(&widget->sources))) {
700 widget->outputs = snd_soc_dapm_suspend_check(widget);
701 return widget->outputs;
705 list_for_each_entry(path, &widget->sinks, list_source) {
706 DAPM_UPDATE_STAT(widget, neighbour_checks);
714 if (path->sink && path->connect) {
716 con += is_connected_output_ep(path->sink);
720 widget->outputs = con;
726 * Recursively check for a completed path to an active or physically connected
727 * input widget. Returns number of complete paths.
729 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
731 struct snd_soc_dapm_path *path;
734 if (widget->inputs >= 0)
735 return widget->inputs;
737 DAPM_UPDATE_STAT(widget, path_checks);
739 if (widget->id == snd_soc_dapm_supply)
742 /* active stream ? */
743 switch (widget->id) {
744 case snd_soc_dapm_dac:
745 case snd_soc_dapm_aif_in:
746 if (widget->active) {
747 widget->inputs = snd_soc_dapm_suspend_check(widget);
748 return widget->inputs;
754 if (widget->connected) {
755 /* connected pin ? */
756 if (widget->id == snd_soc_dapm_input && !widget->ext) {
757 widget->inputs = snd_soc_dapm_suspend_check(widget);
758 return widget->inputs;
761 /* connected VMID/Bias for lower pops */
762 if (widget->id == snd_soc_dapm_vmid) {
763 widget->inputs = snd_soc_dapm_suspend_check(widget);
764 return widget->inputs;
767 /* connected jack ? */
768 if (widget->id == snd_soc_dapm_mic ||
769 (widget->id == snd_soc_dapm_line &&
770 !list_empty(&widget->sinks))) {
771 widget->inputs = snd_soc_dapm_suspend_check(widget);
772 return widget->inputs;
777 list_for_each_entry(path, &widget->sources, list_sink) {
778 DAPM_UPDATE_STAT(widget, neighbour_checks);
786 if (path->source && path->connect) {
788 con += is_connected_input_ep(path->source);
792 widget->inputs = con;
798 * Handler for generic register modifier widget.
800 int dapm_reg_event(struct snd_soc_dapm_widget *w,
801 struct snd_kcontrol *kcontrol, int event)
805 if (SND_SOC_DAPM_EVENT_ON(event))
810 soc_widget_update_bits(w, -(w->reg + 1),
811 w->mask << w->shift, val << w->shift);
815 EXPORT_SYMBOL_GPL(dapm_reg_event);
817 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
819 if (w->power_checked)
825 w->new_power = w->power_check(w);
827 w->power_checked = true;
832 /* Generic check to see if a widget should be powered.
834 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
838 DAPM_UPDATE_STAT(w, power_checks);
840 in = is_connected_input_ep(w);
841 dapm_clear_walk(w->dapm);
842 out = is_connected_output_ep(w);
843 dapm_clear_walk(w->dapm);
844 return out != 0 && in != 0;
847 /* Check to see if an ADC has power */
848 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
852 DAPM_UPDATE_STAT(w, power_checks);
855 in = is_connected_input_ep(w);
856 dapm_clear_walk(w->dapm);
859 return dapm_generic_check_power(w);
863 /* Check to see if a DAC has power */
864 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
868 DAPM_UPDATE_STAT(w, power_checks);
871 out = is_connected_output_ep(w);
872 dapm_clear_walk(w->dapm);
875 return dapm_generic_check_power(w);
879 /* Check to see if a power supply is needed */
880 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
882 struct snd_soc_dapm_path *path;
884 DAPM_UPDATE_STAT(w, power_checks);
886 /* Check if one of our outputs is connected */
887 list_for_each_entry(path, &w->sinks, list_source) {
888 DAPM_UPDATE_STAT(w, neighbour_checks);
893 if (path->connected &&
894 !path->connected(path->source, path->sink))
900 if (dapm_widget_power_check(path->sink))
904 dapm_clear_walk(w->dapm);
909 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
914 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
915 struct snd_soc_dapm_widget *b,
923 sort = dapm_down_seq;
925 if (sort[a->id] != sort[b->id])
926 return sort[a->id] - sort[b->id];
927 if (a->subseq != b->subseq) {
929 return a->subseq - b->subseq;
931 return b->subseq - a->subseq;
933 if (a->reg != b->reg)
934 return a->reg - b->reg;
935 if (a->dapm != b->dapm)
936 return (unsigned long)a->dapm - (unsigned long)b->dapm;
941 /* Insert a widget in order into a DAPM power sequence. */
942 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
943 struct list_head *list,
946 struct snd_soc_dapm_widget *w;
948 list_for_each_entry(w, list, power_list)
949 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
950 list_add_tail(&new_widget->power_list, &w->power_list);
954 list_add_tail(&new_widget->power_list, list);
957 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
958 struct snd_soc_dapm_widget *w, int event)
960 struct snd_soc_card *card = dapm->card;
965 case SND_SOC_DAPM_PRE_PMU:
969 case SND_SOC_DAPM_POST_PMU:
970 ev_name = "POST_PMU";
973 case SND_SOC_DAPM_PRE_PMD:
977 case SND_SOC_DAPM_POST_PMD:
978 ev_name = "POST_PMD";
986 if (w->power != power)
989 if (w->event && (w->event_flags & event)) {
990 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
992 trace_snd_soc_dapm_widget_event_start(w, event);
993 ret = w->event(w, NULL, event);
994 trace_snd_soc_dapm_widget_event_done(w, event);
996 pr_err("%s: %s event failed: %d\n",
997 ev_name, w->name, ret);
1001 /* Apply the coalesced changes from a DAPM sequence */
1002 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1003 struct list_head *pending)
1005 struct snd_soc_card *card = dapm->card;
1006 struct snd_soc_dapm_widget *w;
1008 unsigned int value = 0;
1009 unsigned int mask = 0;
1010 unsigned int cur_mask;
1012 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1015 list_for_each_entry(w, pending, power_list) {
1016 cur_mask = 1 << w->shift;
1017 BUG_ON(reg != w->reg);
1028 pop_dbg(dapm->dev, card->pop_time,
1029 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1030 w->name, reg, value, mask);
1032 /* Check for events */
1033 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1034 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1038 /* Any widget will do, they should all be updating the
1041 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1044 pop_dbg(dapm->dev, card->pop_time,
1045 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1046 value, mask, reg, card->pop_time);
1047 pop_wait(card->pop_time);
1048 soc_widget_update_bits(w, reg, mask, value);
1051 list_for_each_entry(w, pending, power_list) {
1052 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1053 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1057 /* Apply a DAPM power sequence.
1059 * We walk over a pre-sorted list of widgets to apply power to. In
1060 * order to minimise the number of writes to the device required
1061 * multiple widgets will be updated in a single write where possible.
1062 * Currently anything that requires more than a single write is not
1065 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1066 struct list_head *list, int event, bool power_up)
1068 struct snd_soc_dapm_widget *w, *n;
1071 int cur_subseq = -1;
1072 int cur_reg = SND_SOC_NOPM;
1073 struct snd_soc_dapm_context *cur_dapm = NULL;
1080 sort = dapm_down_seq;
1082 list_for_each_entry_safe(w, n, list, power_list) {
1085 /* Do we need to apply any queued changes? */
1086 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1087 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1088 if (!list_empty(&pending))
1089 dapm_seq_run_coalesced(cur_dapm, &pending);
1091 if (cur_dapm && cur_dapm->seq_notifier) {
1092 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1093 if (sort[i] == cur_sort)
1094 cur_dapm->seq_notifier(cur_dapm,
1099 INIT_LIST_HEAD(&pending);
1101 cur_subseq = INT_MIN;
1102 cur_reg = SND_SOC_NOPM;
1107 case snd_soc_dapm_pre:
1109 list_for_each_entry_safe_continue(w, n, list,
1112 if (event == SND_SOC_DAPM_STREAM_START)
1114 NULL, SND_SOC_DAPM_PRE_PMU);
1115 else if (event == SND_SOC_DAPM_STREAM_STOP)
1117 NULL, SND_SOC_DAPM_PRE_PMD);
1120 case snd_soc_dapm_post:
1122 list_for_each_entry_safe_continue(w, n, list,
1125 if (event == SND_SOC_DAPM_STREAM_START)
1127 NULL, SND_SOC_DAPM_POST_PMU);
1128 else if (event == SND_SOC_DAPM_STREAM_STOP)
1130 NULL, SND_SOC_DAPM_POST_PMD);
1134 /* Queue it up for application */
1135 cur_sort = sort[w->id];
1136 cur_subseq = w->subseq;
1139 list_move(&w->power_list, &pending);
1144 dev_err(w->dapm->dev,
1145 "Failed to apply widget power: %d\n", ret);
1148 if (!list_empty(&pending))
1149 dapm_seq_run_coalesced(cur_dapm, &pending);
1151 if (cur_dapm && cur_dapm->seq_notifier) {
1152 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1153 if (sort[i] == cur_sort)
1154 cur_dapm->seq_notifier(cur_dapm,
1159 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1161 struct snd_soc_dapm_update *update = dapm->update;
1162 struct snd_soc_dapm_widget *w;
1171 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1172 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1174 pr_err("%s DAPM pre-event failed: %d\n",
1178 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1181 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1184 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1185 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1187 pr_err("%s DAPM post-event failed: %d\n",
1192 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1193 * they're changing state.
1195 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1197 struct snd_soc_dapm_context *d = data;
1200 /* If we're off and we're not supposed to be go into STANDBY */
1201 if (d->bias_level == SND_SOC_BIAS_OFF &&
1202 d->target_bias_level != SND_SOC_BIAS_OFF) {
1203 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1206 "Failed to turn on bias: %d\n", ret);
1209 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1210 if (d->bias_level != d->target_bias_level) {
1211 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1214 "Failed to prepare bias: %d\n", ret);
1218 /* Async callback run prior to DAPM sequences - brings to their final
1221 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1223 struct snd_soc_dapm_context *d = data;
1226 /* If we just powered the last thing off drop to standby bias */
1227 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1228 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1229 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1230 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1232 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1236 /* If we're in standby and can support bias off then do that */
1237 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1238 d->target_bias_level == SND_SOC_BIAS_OFF) {
1239 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1241 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1244 /* If we just powered up then move to active bias */
1245 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1246 d->target_bias_level == SND_SOC_BIAS_ON) {
1247 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1249 dev_err(d->dev, "Failed to apply active bias: %d\n",
1254 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1255 bool power, bool connect)
1257 /* If a connection is being made or broken then that update
1258 * will have marked the peer dirty, otherwise the widgets are
1259 * not connected and this update has no impact. */
1263 /* If the peer is already in the state we're moving to then we
1264 * won't have an impact on it. */
1265 if (power != peer->power)
1266 dapm_mark_dirty(peer, "peer state change");
1269 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1270 struct list_head *up_list,
1271 struct list_head *down_list)
1273 struct snd_soc_dapm_path *path;
1275 if (w->power == power)
1278 trace_snd_soc_dapm_widget_power(w, power);
1280 /* If we changed our power state perhaps our neigbours changed
1283 list_for_each_entry(path, &w->sources, list_sink) {
1285 dapm_widget_set_peer_power(path->source, power,
1290 case snd_soc_dapm_supply:
1291 /* Supplies can't affect their outputs, only their inputs */
1294 list_for_each_entry(path, &w->sinks, list_source) {
1296 dapm_widget_set_peer_power(path->sink, power,
1304 dapm_seq_insert(w, up_list, true);
1306 dapm_seq_insert(w, down_list, false);
1311 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1312 struct list_head *up_list,
1313 struct list_head *down_list)
1318 case snd_soc_dapm_pre:
1319 dapm_seq_insert(w, down_list, false);
1321 case snd_soc_dapm_post:
1322 dapm_seq_insert(w, up_list, true);
1326 power = dapm_widget_power_check(w);
1328 dapm_widget_set_power(w, power, up_list, down_list);
1334 * Scan each dapm widget for complete audio path.
1335 * A complete path is a route that has valid endpoints i.e.:-
1337 * o DAC to output pin.
1338 * o Input Pin to ADC.
1339 * o Input pin to Output pin (bypass, sidetone)
1340 * o DAC to ADC (loopback).
1342 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1344 struct snd_soc_card *card = dapm->card;
1345 struct snd_soc_dapm_widget *w;
1346 struct snd_soc_dapm_context *d;
1348 LIST_HEAD(down_list);
1349 LIST_HEAD(async_domain);
1350 enum snd_soc_bias_level bias;
1352 trace_snd_soc_dapm_start(card);
1354 list_for_each_entry(d, &card->dapm_list, list) {
1355 if (d->n_widgets || d->codec == NULL) {
1356 if (d->idle_bias_off)
1357 d->target_bias_level = SND_SOC_BIAS_OFF;
1359 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1363 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1365 list_for_each_entry(w, &card->widgets, list) {
1366 w->power_checked = false;
1371 /* Check which widgets we need to power and store them in
1372 * lists indicating if they should be powered up or down. We
1373 * only check widgets that have been flagged as dirty but note
1374 * that new widgets may be added to the dirty list while we
1377 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1378 dapm_power_one_widget(w, &up_list, &down_list);
1381 list_for_each_entry(w, &card->widgets, list) {
1382 list_del_init(&w->dirty);
1387 /* Supplies and micbiases only bring the
1388 * context up to STANDBY as unless something
1389 * else is active and passing audio they
1390 * generally don't require full power.
1393 case snd_soc_dapm_supply:
1394 case snd_soc_dapm_micbias:
1395 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1396 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1399 d->target_bias_level = SND_SOC_BIAS_ON;
1406 /* If there are no DAPM widgets then try to figure out power from the
1409 if (!dapm->n_widgets) {
1411 case SND_SOC_DAPM_STREAM_START:
1412 case SND_SOC_DAPM_STREAM_RESUME:
1413 dapm->target_bias_level = SND_SOC_BIAS_ON;
1415 case SND_SOC_DAPM_STREAM_STOP:
1416 if (dapm->codec->active)
1417 dapm->target_bias_level = SND_SOC_BIAS_ON;
1419 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1421 case SND_SOC_DAPM_STREAM_SUSPEND:
1422 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1424 case SND_SOC_DAPM_STREAM_NOP:
1425 dapm->target_bias_level = dapm->bias_level;
1432 /* Force all contexts in the card to the same bias state if
1433 * they're not ground referenced.
1435 bias = SND_SOC_BIAS_OFF;
1436 list_for_each_entry(d, &card->dapm_list, list)
1437 if (d->target_bias_level > bias)
1438 bias = d->target_bias_level;
1439 list_for_each_entry(d, &card->dapm_list, list)
1440 if (!d->idle_bias_off)
1441 d->target_bias_level = bias;
1443 trace_snd_soc_dapm_walk_done(card);
1445 /* Run all the bias changes in parallel */
1446 list_for_each_entry(d, &dapm->card->dapm_list, list)
1447 async_schedule_domain(dapm_pre_sequence_async, d,
1449 async_synchronize_full_domain(&async_domain);
1451 /* Power down widgets first; try to avoid amplifying pops. */
1452 dapm_seq_run(dapm, &down_list, event, false);
1454 dapm_widget_update(dapm);
1457 dapm_seq_run(dapm, &up_list, event, true);
1459 /* Run all the bias changes in parallel */
1460 list_for_each_entry(d, &dapm->card->dapm_list, list)
1461 async_schedule_domain(dapm_post_sequence_async, d,
1463 async_synchronize_full_domain(&async_domain);
1465 pop_dbg(dapm->dev, card->pop_time,
1466 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1467 pop_wait(card->pop_time);
1469 trace_snd_soc_dapm_done(card);
1474 #ifdef CONFIG_DEBUG_FS
1475 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1477 file->private_data = inode->i_private;
1481 static ssize_t dapm_widget_power_read_file(struct file *file,
1482 char __user *user_buf,
1483 size_t count, loff_t *ppos)
1485 struct snd_soc_dapm_widget *w = file->private_data;
1489 struct snd_soc_dapm_path *p = NULL;
1491 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1495 in = is_connected_input_ep(w);
1496 dapm_clear_walk(w->dapm);
1497 out = is_connected_output_ep(w);
1498 dapm_clear_walk(w->dapm);
1500 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1501 w->name, w->power ? "On" : "Off", in, out);
1504 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1505 " - R%d(0x%x) bit %d",
1506 w->reg, w->reg, w->shift);
1508 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1511 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1513 w->active ? "active" : "inactive");
1515 list_for_each_entry(p, &w->sources, list_sink) {
1516 if (p->connected && !p->connected(w, p->sink))
1520 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1521 " in \"%s\" \"%s\"\n",
1522 p->name ? p->name : "static",
1525 list_for_each_entry(p, &w->sinks, list_source) {
1526 if (p->connected && !p->connected(w, p->sink))
1530 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1531 " out \"%s\" \"%s\"\n",
1532 p->name ? p->name : "static",
1536 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1542 static const struct file_operations dapm_widget_power_fops = {
1543 .open = dapm_widget_power_open_file,
1544 .read = dapm_widget_power_read_file,
1545 .llseek = default_llseek,
1548 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1550 file->private_data = inode->i_private;
1554 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1555 size_t count, loff_t *ppos)
1557 struct snd_soc_dapm_context *dapm = file->private_data;
1560 switch (dapm->bias_level) {
1561 case SND_SOC_BIAS_ON:
1564 case SND_SOC_BIAS_PREPARE:
1565 level = "Prepare\n";
1567 case SND_SOC_BIAS_STANDBY:
1568 level = "Standby\n";
1570 case SND_SOC_BIAS_OFF:
1575 level = "Unknown\n";
1579 return simple_read_from_buffer(user_buf, count, ppos, level,
1583 static const struct file_operations dapm_bias_fops = {
1584 .open = dapm_bias_open_file,
1585 .read = dapm_bias_read_file,
1586 .llseek = default_llseek,
1589 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1590 struct dentry *parent)
1594 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1596 if (!dapm->debugfs_dapm) {
1598 "Failed to create DAPM debugfs directory\n");
1602 d = debugfs_create_file("bias_level", 0444,
1603 dapm->debugfs_dapm, dapm,
1607 "ASoC: Failed to create bias level debugfs file\n");
1610 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1612 struct snd_soc_dapm_context *dapm = w->dapm;
1615 if (!dapm->debugfs_dapm || !w->name)
1618 d = debugfs_create_file(w->name, 0444,
1619 dapm->debugfs_dapm, w,
1620 &dapm_widget_power_fops);
1622 dev_warn(w->dapm->dev,
1623 "ASoC: Failed to create %s debugfs file\n",
1627 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1629 debugfs_remove_recursive(dapm->debugfs_dapm);
1633 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1634 struct dentry *parent)
1638 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1642 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1648 /* test and update the power status of a mux widget */
1649 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1650 struct snd_kcontrol *kcontrol, int change,
1651 int mux, struct soc_enum *e)
1653 struct snd_soc_dapm_path *path;
1656 if (widget->id != snd_soc_dapm_mux &&
1657 widget->id != snd_soc_dapm_virt_mux &&
1658 widget->id != snd_soc_dapm_value_mux)
1664 /* find dapm widget path assoc with kcontrol */
1665 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1666 if (path->kcontrol != kcontrol)
1669 if (!path->name || !e->texts[mux])
1673 /* we now need to match the string in the enum to the path */
1674 if (!(strcmp(path->name, e->texts[mux]))) {
1675 path->connect = 1; /* new connection */
1676 dapm_mark_dirty(path->source, "mux connection");
1679 dapm_mark_dirty(path->source,
1680 "mux disconnection");
1681 path->connect = 0; /* old connection must be powered down */
1686 dapm_mark_dirty(widget, "mux change");
1687 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1693 /* test and update the power status of a mixer or switch widget */
1694 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1695 struct snd_kcontrol *kcontrol, int connect)
1697 struct snd_soc_dapm_path *path;
1700 if (widget->id != snd_soc_dapm_mixer &&
1701 widget->id != snd_soc_dapm_mixer_named_ctl &&
1702 widget->id != snd_soc_dapm_switch)
1705 /* find dapm widget path assoc with kcontrol */
1706 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1707 if (path->kcontrol != kcontrol)
1710 /* found, now check type */
1712 path->connect = connect;
1713 dapm_mark_dirty(path->source, "mixer connection");
1717 dapm_mark_dirty(widget, "mixer update");
1718 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1724 /* show dapm widget status in sys fs */
1725 static ssize_t dapm_widget_show(struct device *dev,
1726 struct device_attribute *attr, char *buf)
1728 struct snd_soc_pcm_runtime *rtd =
1729 container_of(dev, struct snd_soc_pcm_runtime, dev);
1730 struct snd_soc_codec *codec =rtd->codec;
1731 struct snd_soc_dapm_widget *w;
1733 char *state = "not set";
1735 list_for_each_entry(w, &codec->card->widgets, list) {
1736 if (w->dapm != &codec->dapm)
1739 /* only display widgets that burnm power */
1741 case snd_soc_dapm_hp:
1742 case snd_soc_dapm_mic:
1743 case snd_soc_dapm_spk:
1744 case snd_soc_dapm_line:
1745 case snd_soc_dapm_micbias:
1746 case snd_soc_dapm_dac:
1747 case snd_soc_dapm_adc:
1748 case snd_soc_dapm_pga:
1749 case snd_soc_dapm_out_drv:
1750 case snd_soc_dapm_mixer:
1751 case snd_soc_dapm_mixer_named_ctl:
1752 case snd_soc_dapm_supply:
1754 count += sprintf(buf + count, "%s: %s\n",
1755 w->name, w->power ? "On":"Off");
1762 switch (codec->dapm.bias_level) {
1763 case SND_SOC_BIAS_ON:
1766 case SND_SOC_BIAS_PREPARE:
1769 case SND_SOC_BIAS_STANDBY:
1772 case SND_SOC_BIAS_OFF:
1776 count += sprintf(buf + count, "PM State: %s\n", state);
1781 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1783 int snd_soc_dapm_sys_add(struct device *dev)
1785 return device_create_file(dev, &dev_attr_dapm_widget);
1788 static void snd_soc_dapm_sys_remove(struct device *dev)
1790 device_remove_file(dev, &dev_attr_dapm_widget);
1793 /* free all dapm widgets and resources */
1794 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1796 struct snd_soc_dapm_widget *w, *next_w;
1797 struct snd_soc_dapm_path *p, *next_p;
1799 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1800 if (w->dapm != dapm)
1804 * remove source and sink paths associated to this widget.
1805 * While removing the path, remove reference to it from both
1806 * source and sink widgets so that path is removed only once.
1808 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1809 list_del(&p->list_sink);
1810 list_del(&p->list_source);
1812 kfree(p->long_name);
1815 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1816 list_del(&p->list_sink);
1817 list_del(&p->list_source);
1819 kfree(p->long_name);
1822 kfree(w->kcontrols);
1828 static struct snd_soc_dapm_widget *dapm_find_widget(
1829 struct snd_soc_dapm_context *dapm, const char *pin,
1830 bool search_other_contexts)
1832 struct snd_soc_dapm_widget *w;
1833 struct snd_soc_dapm_widget *fallback = NULL;
1835 list_for_each_entry(w, &dapm->card->widgets, list) {
1836 if (!strcmp(w->name, pin)) {
1837 if (w->dapm == dapm)
1844 if (search_other_contexts)
1850 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1851 const char *pin, int status)
1853 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1856 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1860 w->connected = status;
1863 dapm_mark_dirty(w, "pin configuration");
1869 * snd_soc_dapm_sync - scan and power dapm paths
1870 * @dapm: DAPM context
1872 * Walks all dapm audio paths and powers widgets according to their
1873 * stream or path usage.
1875 * Returns 0 for success.
1877 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1880 * Suppress early reports (eg, jacks syncing their state) to avoid
1881 * silly DAPM runs during card startup.
1883 if (!dapm->card || !dapm->card->instantiated)
1886 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1888 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1890 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1891 const struct snd_soc_dapm_route *route)
1893 struct snd_soc_dapm_path *path;
1894 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1895 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1897 const char *control = route->control;
1899 char prefixed_sink[80];
1900 char prefixed_source[80];
1903 if (dapm->codec && dapm->codec->name_prefix) {
1904 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1905 dapm->codec->name_prefix, route->sink);
1906 sink = prefixed_sink;
1907 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1908 dapm->codec->name_prefix, route->source);
1909 source = prefixed_source;
1912 source = route->source;
1916 * find src and dest widgets over all widgets but favor a widget from
1917 * current DAPM context
1919 list_for_each_entry(w, &dapm->card->widgets, list) {
1920 if (!wsink && !(strcmp(w->name, sink))) {
1922 if (w->dapm == dapm)
1926 if (!wsource && !(strcmp(w->name, source))) {
1928 if (w->dapm == dapm)
1932 /* use widget from another DAPM context if not found from this */
1938 if (wsource == NULL || wsink == NULL)
1941 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1945 path->source = wsource;
1947 path->connected = route->connected;
1948 INIT_LIST_HEAD(&path->list);
1949 INIT_LIST_HEAD(&path->list_source);
1950 INIT_LIST_HEAD(&path->list_sink);
1952 /* check for external widgets */
1953 if (wsink->id == snd_soc_dapm_input) {
1954 if (wsource->id == snd_soc_dapm_micbias ||
1955 wsource->id == snd_soc_dapm_mic ||
1956 wsource->id == snd_soc_dapm_line ||
1957 wsource->id == snd_soc_dapm_output)
1960 if (wsource->id == snd_soc_dapm_output) {
1961 if (wsink->id == snd_soc_dapm_spk ||
1962 wsink->id == snd_soc_dapm_hp ||
1963 wsink->id == snd_soc_dapm_line ||
1964 wsink->id == snd_soc_dapm_input)
1968 /* connect static paths */
1969 if (control == NULL) {
1970 list_add(&path->list, &dapm->card->paths);
1971 list_add(&path->list_sink, &wsink->sources);
1972 list_add(&path->list_source, &wsource->sinks);
1977 /* connect dynamic paths */
1978 switch (wsink->id) {
1979 case snd_soc_dapm_adc:
1980 case snd_soc_dapm_dac:
1981 case snd_soc_dapm_pga:
1982 case snd_soc_dapm_out_drv:
1983 case snd_soc_dapm_input:
1984 case snd_soc_dapm_output:
1985 case snd_soc_dapm_micbias:
1986 case snd_soc_dapm_vmid:
1987 case snd_soc_dapm_pre:
1988 case snd_soc_dapm_post:
1989 case snd_soc_dapm_supply:
1990 case snd_soc_dapm_aif_in:
1991 case snd_soc_dapm_aif_out:
1992 list_add(&path->list, &dapm->card->paths);
1993 list_add(&path->list_sink, &wsink->sources);
1994 list_add(&path->list_source, &wsource->sinks);
1997 case snd_soc_dapm_mux:
1998 case snd_soc_dapm_virt_mux:
1999 case snd_soc_dapm_value_mux:
2000 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2001 &wsink->kcontrol_news[0]);
2005 case snd_soc_dapm_switch:
2006 case snd_soc_dapm_mixer:
2007 case snd_soc_dapm_mixer_named_ctl:
2008 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2012 case snd_soc_dapm_hp:
2013 case snd_soc_dapm_mic:
2014 case snd_soc_dapm_line:
2015 case snd_soc_dapm_spk:
2016 list_add(&path->list, &dapm->card->paths);
2017 list_add(&path->list_sink, &wsink->sources);
2018 list_add(&path->list_source, &wsource->sinks);
2025 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2026 source, control, sink);
2032 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2033 * @dapm: DAPM context
2034 * @route: audio routes
2035 * @num: number of routes
2037 * Connects 2 dapm widgets together via a named audio path. The sink is
2038 * the widget receiving the audio signal, whilst the source is the sender
2039 * of the audio signal.
2041 * Returns 0 for success else error. On error all resources can be freed
2042 * with a call to snd_soc_card_free().
2044 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2045 const struct snd_soc_dapm_route *route, int num)
2049 for (i = 0; i < num; i++) {
2050 ret = snd_soc_dapm_add_route(dapm, route);
2052 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2053 route->source, route->sink);
2061 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2063 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2064 const struct snd_soc_dapm_route *route)
2066 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2069 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2072 struct snd_soc_dapm_path *path;
2076 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2082 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2087 if (route->control || route->connected)
2088 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2089 route->source, route->sink);
2091 list_for_each_entry(path, &source->sinks, list_source) {
2092 if (path->sink == sink) {
2099 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2100 route->source, route->sink);
2102 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2103 count, route->source, route->sink);
2109 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2110 * @dapm: DAPM context
2111 * @route: audio routes
2112 * @num: number of routes
2114 * Mark existing routes matching those specified in the passed array
2115 * as being weak, meaning that they are ignored for the purpose of
2116 * power decisions. The main intended use case is for sidetone paths
2117 * which couple audio between other independent paths if they are both
2118 * active in order to make the combination work better at the user
2119 * level but which aren't intended to be "used".
2121 * Note that CODEC drivers should not use this as sidetone type paths
2122 * can frequently also be used as bypass paths.
2124 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2125 const struct snd_soc_dapm_route *route, int num)
2130 for (i = 0; i < num; i++) {
2131 err = snd_soc_dapm_weak_route(dapm, route);
2139 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2142 * snd_soc_dapm_new_widgets - add new dapm widgets
2143 * @dapm: DAPM context
2145 * Checks the codec for any new dapm widgets and creates them if found.
2147 * Returns 0 for success.
2149 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2151 struct snd_soc_dapm_widget *w;
2154 list_for_each_entry(w, &dapm->card->widgets, list)
2159 if (w->num_kcontrols) {
2160 w->kcontrols = kzalloc(w->num_kcontrols *
2161 sizeof(struct snd_kcontrol *),
2168 case snd_soc_dapm_switch:
2169 case snd_soc_dapm_mixer:
2170 case snd_soc_dapm_mixer_named_ctl:
2173 case snd_soc_dapm_mux:
2174 case snd_soc_dapm_virt_mux:
2175 case snd_soc_dapm_value_mux:
2178 case snd_soc_dapm_pga:
2179 case snd_soc_dapm_out_drv:
2186 /* Read the initial power state from the device */
2188 val = soc_widget_read(w, w->reg);
2189 val &= 1 << w->shift;
2199 dapm_mark_dirty(w, "new widget");
2200 dapm_debugfs_add_widget(w);
2203 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2206 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2209 * snd_soc_dapm_get_volsw - dapm mixer get callback
2210 * @kcontrol: mixer control
2211 * @ucontrol: control element information
2213 * Callback to get the value of a dapm mixer control.
2215 * Returns 0 for success.
2217 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2218 struct snd_ctl_elem_value *ucontrol)
2220 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2221 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2222 struct soc_mixer_control *mc =
2223 (struct soc_mixer_control *)kcontrol->private_value;
2224 unsigned int reg = mc->reg;
2225 unsigned int shift = mc->shift;
2226 unsigned int rshift = mc->rshift;
2228 unsigned int invert = mc->invert;
2229 unsigned int mask = (1 << fls(max)) - 1;
2231 ucontrol->value.integer.value[0] =
2232 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2233 if (shift != rshift)
2234 ucontrol->value.integer.value[1] =
2235 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2237 ucontrol->value.integer.value[0] =
2238 max - ucontrol->value.integer.value[0];
2239 if (shift != rshift)
2240 ucontrol->value.integer.value[1] =
2241 max - ucontrol->value.integer.value[1];
2246 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2249 * snd_soc_dapm_put_volsw - dapm mixer set callback
2250 * @kcontrol: mixer control
2251 * @ucontrol: control element information
2253 * Callback to set the value of a dapm mixer control.
2255 * Returns 0 for success.
2257 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2258 struct snd_ctl_elem_value *ucontrol)
2260 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2261 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2262 struct snd_soc_codec *codec = widget->codec;
2263 struct soc_mixer_control *mc =
2264 (struct soc_mixer_control *)kcontrol->private_value;
2265 unsigned int reg = mc->reg;
2266 unsigned int shift = mc->shift;
2268 unsigned int mask = (1 << fls(max)) - 1;
2269 unsigned int invert = mc->invert;
2271 int connect, change;
2272 struct snd_soc_dapm_update update;
2275 val = (ucontrol->value.integer.value[0] & mask);
2279 mask = mask << shift;
2283 /* new connection */
2284 connect = invert ? 0 : 1;
2286 /* old connection must be powered down */
2287 connect = invert ? 1 : 0;
2289 mutex_lock(&codec->mutex);
2291 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2293 for (wi = 0; wi < wlist->num_widgets; wi++) {
2294 widget = wlist->widgets[wi];
2296 widget->value = val;
2298 update.kcontrol = kcontrol;
2299 update.widget = widget;
2303 widget->dapm->update = &update;
2305 dapm_mixer_update_power(widget, kcontrol, connect);
2307 widget->dapm->update = NULL;
2311 mutex_unlock(&codec->mutex);
2314 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2317 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2318 * @kcontrol: mixer control
2319 * @ucontrol: control element information
2321 * Callback to get the value of a dapm enumerated double mixer control.
2323 * Returns 0 for success.
2325 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2326 struct snd_ctl_elem_value *ucontrol)
2328 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2329 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2330 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2331 unsigned int val, bitmask;
2333 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2335 val = snd_soc_read(widget->codec, e->reg);
2336 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2337 if (e->shift_l != e->shift_r)
2338 ucontrol->value.enumerated.item[1] =
2339 (val >> e->shift_r) & (bitmask - 1);
2343 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2346 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2347 * @kcontrol: mixer control
2348 * @ucontrol: control element information
2350 * Callback to set the value of a dapm enumerated double mixer control.
2352 * Returns 0 for success.
2354 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_value *ucontrol)
2357 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2358 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2359 struct snd_soc_codec *codec = widget->codec;
2360 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2361 unsigned int val, mux, change;
2362 unsigned int mask, bitmask;
2363 struct snd_soc_dapm_update update;
2366 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2368 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2370 mux = ucontrol->value.enumerated.item[0];
2371 val = mux << e->shift_l;
2372 mask = (bitmask - 1) << e->shift_l;
2373 if (e->shift_l != e->shift_r) {
2374 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2376 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2377 mask |= (bitmask - 1) << e->shift_r;
2380 mutex_lock(&codec->mutex);
2382 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2384 for (wi = 0; wi < wlist->num_widgets; wi++) {
2385 widget = wlist->widgets[wi];
2387 widget->value = val;
2389 update.kcontrol = kcontrol;
2390 update.widget = widget;
2391 update.reg = e->reg;
2394 widget->dapm->update = &update;
2396 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2398 widget->dapm->update = NULL;
2402 mutex_unlock(&codec->mutex);
2405 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2408 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2409 * @kcontrol: mixer control
2410 * @ucontrol: control element information
2412 * Returns 0 for success.
2414 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_value *ucontrol)
2417 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2418 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2420 ucontrol->value.enumerated.item[0] = widget->value;
2424 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2427 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2428 * @kcontrol: mixer control
2429 * @ucontrol: control element information
2431 * Returns 0 for success.
2433 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2434 struct snd_ctl_elem_value *ucontrol)
2436 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2437 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2438 struct snd_soc_codec *codec = widget->codec;
2439 struct soc_enum *e =
2440 (struct soc_enum *)kcontrol->private_value;
2445 if (ucontrol->value.enumerated.item[0] >= e->max)
2448 mutex_lock(&codec->mutex);
2450 change = widget->value != ucontrol->value.enumerated.item[0];
2452 for (wi = 0; wi < wlist->num_widgets; wi++) {
2453 widget = wlist->widgets[wi];
2455 widget->value = ucontrol->value.enumerated.item[0];
2457 dapm_mux_update_power(widget, kcontrol, change,
2462 mutex_unlock(&codec->mutex);
2465 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2468 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2470 * @kcontrol: mixer control
2471 * @ucontrol: control element information
2473 * Callback to get the value of a dapm semi enumerated double mixer control.
2475 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2476 * used for handling bitfield coded enumeration for example.
2478 * Returns 0 for success.
2480 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2481 struct snd_ctl_elem_value *ucontrol)
2483 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2484 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2485 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2486 unsigned int reg_val, val, mux;
2488 reg_val = snd_soc_read(widget->codec, e->reg);
2489 val = (reg_val >> e->shift_l) & e->mask;
2490 for (mux = 0; mux < e->max; mux++) {
2491 if (val == e->values[mux])
2494 ucontrol->value.enumerated.item[0] = mux;
2495 if (e->shift_l != e->shift_r) {
2496 val = (reg_val >> e->shift_r) & e->mask;
2497 for (mux = 0; mux < e->max; mux++) {
2498 if (val == e->values[mux])
2501 ucontrol->value.enumerated.item[1] = mux;
2506 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2509 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2511 * @kcontrol: mixer control
2512 * @ucontrol: control element information
2514 * Callback to set the value of a dapm semi enumerated double mixer control.
2516 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2517 * used for handling bitfield coded enumeration for example.
2519 * Returns 0 for success.
2521 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2522 struct snd_ctl_elem_value *ucontrol)
2524 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2525 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2526 struct snd_soc_codec *codec = widget->codec;
2527 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2528 unsigned int val, mux, change;
2530 struct snd_soc_dapm_update update;
2533 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2535 mux = ucontrol->value.enumerated.item[0];
2536 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2537 mask = e->mask << e->shift_l;
2538 if (e->shift_l != e->shift_r) {
2539 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2541 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2542 mask |= e->mask << e->shift_r;
2545 mutex_lock(&codec->mutex);
2547 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2549 for (wi = 0; wi < wlist->num_widgets; wi++) {
2550 widget = wlist->widgets[wi];
2552 widget->value = val;
2554 update.kcontrol = kcontrol;
2555 update.widget = widget;
2556 update.reg = e->reg;
2559 widget->dapm->update = &update;
2561 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2563 widget->dapm->update = NULL;
2567 mutex_unlock(&codec->mutex);
2570 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2573 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2575 * @kcontrol: mixer control
2576 * @uinfo: control element information
2578 * Callback to provide information about a pin switch control.
2580 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_info *uinfo)
2583 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2585 uinfo->value.integer.min = 0;
2586 uinfo->value.integer.max = 1;
2590 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2593 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2595 * @kcontrol: mixer control
2598 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2601 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2602 const char *pin = (const char *)kcontrol->private_value;
2604 mutex_lock(&codec->mutex);
2606 ucontrol->value.integer.value[0] =
2607 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2609 mutex_unlock(&codec->mutex);
2613 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2616 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2618 * @kcontrol: mixer control
2621 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2622 struct snd_ctl_elem_value *ucontrol)
2624 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2625 const char *pin = (const char *)kcontrol->private_value;
2627 mutex_lock(&codec->mutex);
2629 if (ucontrol->value.integer.value[0])
2630 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2632 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2634 snd_soc_dapm_sync(&codec->dapm);
2636 mutex_unlock(&codec->mutex);
2640 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2643 * snd_soc_dapm_new_control - create new dapm control
2644 * @dapm: DAPM context
2645 * @widget: widget template
2647 * Creates a new dapm control based upon the template.
2649 * Returns 0 for success else error.
2651 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2652 const struct snd_soc_dapm_widget *widget)
2654 struct snd_soc_dapm_widget *w;
2657 if ((w = dapm_cnew_widget(widget)) == NULL)
2660 name_len = strlen(widget->name) + 1;
2661 if (dapm->codec && dapm->codec->name_prefix)
2662 name_len += 1 + strlen(dapm->codec->name_prefix);
2663 w->name = kmalloc(name_len, GFP_KERNEL);
2664 if (w->name == NULL) {
2668 if (dapm->codec && dapm->codec->name_prefix)
2669 snprintf(w->name, name_len, "%s %s",
2670 dapm->codec->name_prefix, widget->name);
2672 snprintf(w->name, name_len, "%s", widget->name);
2675 case snd_soc_dapm_switch:
2676 case snd_soc_dapm_mixer:
2677 case snd_soc_dapm_mixer_named_ctl:
2678 w->power_check = dapm_generic_check_power;
2680 case snd_soc_dapm_mux:
2681 case snd_soc_dapm_virt_mux:
2682 case snd_soc_dapm_value_mux:
2683 w->power_check = dapm_generic_check_power;
2685 case snd_soc_dapm_adc:
2686 case snd_soc_dapm_aif_out:
2687 w->power_check = dapm_adc_check_power;
2689 case snd_soc_dapm_dac:
2690 case snd_soc_dapm_aif_in:
2691 w->power_check = dapm_dac_check_power;
2693 case snd_soc_dapm_pga:
2694 case snd_soc_dapm_out_drv:
2695 case snd_soc_dapm_input:
2696 case snd_soc_dapm_output:
2697 case snd_soc_dapm_micbias:
2698 case snd_soc_dapm_spk:
2699 case snd_soc_dapm_hp:
2700 case snd_soc_dapm_mic:
2701 case snd_soc_dapm_line:
2702 w->power_check = dapm_generic_check_power;
2704 case snd_soc_dapm_supply:
2705 w->power_check = dapm_supply_check_power;
2708 w->power_check = dapm_always_on_check_power;
2714 w->codec = dapm->codec;
2715 w->platform = dapm->platform;
2716 INIT_LIST_HEAD(&w->sources);
2717 INIT_LIST_HEAD(&w->sinks);
2718 INIT_LIST_HEAD(&w->list);
2719 INIT_LIST_HEAD(&w->dirty);
2720 list_add(&w->list, &dapm->card->widgets);
2722 /* machine layer set ups unconnected pins and insertions */
2726 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2729 * snd_soc_dapm_new_controls - create new dapm controls
2730 * @dapm: DAPM context
2731 * @widget: widget array
2732 * @num: number of widgets
2734 * Creates new DAPM controls based upon the templates.
2736 * Returns 0 for success else error.
2738 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2739 const struct snd_soc_dapm_widget *widget,
2744 for (i = 0; i < num; i++) {
2745 ret = snd_soc_dapm_new_control(dapm, widget);
2748 "ASoC: Failed to create DAPM control %s: %d\n",
2756 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2758 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2759 const char *stream, int event)
2761 struct snd_soc_dapm_widget *w;
2763 list_for_each_entry(w, &dapm->card->widgets, list)
2765 if (!w->sname || w->dapm != dapm)
2767 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2768 w->name, w->sname, stream, event);
2769 if (strstr(w->sname, stream)) {
2770 dapm_mark_dirty(w, "stream event");
2772 case SND_SOC_DAPM_STREAM_START:
2775 case SND_SOC_DAPM_STREAM_STOP:
2778 case SND_SOC_DAPM_STREAM_SUSPEND:
2779 case SND_SOC_DAPM_STREAM_RESUME:
2780 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2781 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2787 dapm_power_widgets(dapm, event);
2789 /* do we need to notify any clients that DAPM stream is complete */
2790 if (dapm->stream_event)
2791 dapm->stream_event(dapm, event);
2795 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2796 * @rtd: PCM runtime data
2797 * @stream: stream name
2798 * @event: stream event
2800 * Sends a stream event to the dapm core. The core then makes any
2801 * necessary widget power changes.
2803 * Returns 0 for success else error.
2805 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2806 const char *stream, int event)
2808 struct snd_soc_codec *codec = rtd->codec;
2813 mutex_lock(&codec->mutex);
2814 soc_dapm_stream_event(&codec->dapm, stream, event);
2815 mutex_unlock(&codec->mutex);
2820 * snd_soc_dapm_enable_pin - enable pin.
2821 * @dapm: DAPM context
2824 * Enables input/output pin and its parents or children widgets iff there is
2825 * a valid audio route and active audio stream.
2826 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2827 * do any widget power switching.
2829 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2831 return snd_soc_dapm_set_pin(dapm, pin, 1);
2833 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2836 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2837 * @dapm: DAPM context
2840 * Enables input/output pin regardless of any other state. This is
2841 * intended for use with microphone bias supplies used in microphone
2844 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2845 * do any widget power switching.
2847 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2850 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2853 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2857 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2860 dapm_mark_dirty(w, "force enable");
2864 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2867 * snd_soc_dapm_disable_pin - disable pin.
2868 * @dapm: DAPM context
2871 * Disables input/output pin and its parents or children widgets.
2872 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2873 * do any widget power switching.
2875 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2878 return snd_soc_dapm_set_pin(dapm, pin, 0);
2880 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2883 * snd_soc_dapm_nc_pin - permanently disable pin.
2884 * @dapm: DAPM context
2887 * Marks the specified pin as being not connected, disabling it along
2888 * any parent or child widgets. At present this is identical to
2889 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2890 * additional things such as disabling controls which only affect
2891 * paths through the pin.
2893 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2894 * do any widget power switching.
2896 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2898 return snd_soc_dapm_set_pin(dapm, pin, 0);
2900 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2903 * snd_soc_dapm_get_pin_status - get audio pin status
2904 * @dapm: DAPM context
2905 * @pin: audio signal pin endpoint (or start point)
2907 * Get audio pin status - connected or disconnected.
2909 * Returns 1 for connected otherwise 0.
2911 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2914 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2917 return w->connected;
2921 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2924 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2925 * @dapm: DAPM context
2926 * @pin: audio signal pin endpoint (or start point)
2928 * Mark the given endpoint or pin as ignoring suspend. When the
2929 * system is disabled a path between two endpoints flagged as ignoring
2930 * suspend will not be disabled. The path must already be enabled via
2931 * normal means at suspend time, it will not be turned on if it was not
2934 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2937 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2940 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2944 w->ignore_suspend = 1;
2948 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2951 * snd_soc_dapm_free - free dapm resources
2952 * @dapm: DAPM context
2954 * Free all dapm widgets and resources.
2956 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2958 snd_soc_dapm_sys_remove(dapm->dev);
2959 dapm_debugfs_cleanup(dapm);
2960 dapm_free_widgets(dapm);
2961 list_del(&dapm->list);
2963 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2965 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2967 struct snd_soc_dapm_widget *w;
2968 LIST_HEAD(down_list);
2971 list_for_each_entry(w, &dapm->card->widgets, list) {
2972 if (w->dapm != dapm)
2975 dapm_seq_insert(w, &down_list, false);
2981 /* If there were no widgets to power down we're already in
2985 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2986 dapm_seq_run(dapm, &down_list, 0, false);
2987 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2992 * snd_soc_dapm_shutdown - callback for system shutdown
2994 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2996 struct snd_soc_codec *codec;
2998 list_for_each_entry(codec, &card->codec_dev_list, list) {
2999 soc_dapm_shutdown_codec(&codec->dapm);
3000 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
3004 /* Module information */
3006 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3007 MODULE_LICENSE("GPL");