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/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 static 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);
136 /* create a new dapm widget */
137 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
138 const struct snd_soc_dapm_widget *_widget)
140 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
143 /* get snd_card from DAPM context */
144 static inline struct snd_card *dapm_get_snd_card(
145 struct snd_soc_dapm_context *dapm)
148 return dapm->codec->card->snd_card;
149 else if (dapm->platform)
150 return dapm->platform->card->snd_card;
158 /* get soc_card from DAPM context */
159 static inline struct snd_soc_card *dapm_get_soc_card(
160 struct snd_soc_dapm_context *dapm)
163 return dapm->codec->card;
164 else if (dapm->platform)
165 return dapm->platform->card;
173 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
176 return snd_soc_read(w->codec, reg);
177 else if (w->platform)
178 return snd_soc_platform_read(w->platform, reg);
180 dev_err(w->dapm->dev, "no valid widget read method\n");
184 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
187 return snd_soc_write(w->codec, reg, val);
188 else if (w->platform)
189 return snd_soc_platform_write(w->platform, reg, val);
191 dev_err(w->dapm->dev, "no valid widget write method\n");
195 static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
196 unsigned short reg, unsigned int mask, unsigned int value)
199 unsigned int old, new;
202 ret = soc_widget_read(w, reg);
207 new = (old & ~mask) | (value & mask);
210 ret = soc_widget_write(w, reg, new);
219 * snd_soc_dapm_set_bias_level - set the bias level for the system
220 * @dapm: DAPM context
221 * @level: level to configure
223 * Configure the bias (power) levels for the SoC audio device.
225 * Returns 0 for success else error.
227 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
228 enum snd_soc_bias_level level)
230 struct snd_soc_card *card = dapm->card;
233 trace_snd_soc_bias_level_start(card, level);
235 if (card && card->set_bias_level)
236 ret = card->set_bias_level(card, dapm, level);
241 if (dapm->codec->driver->set_bias_level)
242 ret = dapm->codec->driver->set_bias_level(dapm->codec,
245 dapm->bias_level = level;
250 if (card && card->set_bias_level_post)
251 ret = card->set_bias_level_post(card, dapm, level);
253 trace_snd_soc_bias_level_done(card, level);
258 /* set up initial codec paths */
259 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
260 struct snd_soc_dapm_path *p, int i)
263 case snd_soc_dapm_switch:
264 case snd_soc_dapm_mixer:
265 case snd_soc_dapm_mixer_named_ctl: {
267 struct soc_mixer_control *mc = (struct soc_mixer_control *)
268 w->kcontrol_news[i].private_value;
269 unsigned int reg = mc->reg;
270 unsigned int shift = mc->shift;
272 unsigned int mask = (1 << fls(max)) - 1;
273 unsigned int invert = mc->invert;
275 val = soc_widget_read(w, reg);
276 val = (val >> shift) & mask;
278 if ((invert && !val) || (!invert && val))
284 case snd_soc_dapm_mux: {
285 struct soc_enum *e = (struct soc_enum *)
286 w->kcontrol_news[i].private_value;
287 int val, item, bitmask;
289 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
291 val = soc_widget_read(w, e->reg);
292 item = (val >> e->shift_l) & (bitmask - 1);
295 for (i = 0; i < e->max; i++) {
296 if (!(strcmp(p->name, e->texts[i])) && item == i)
301 case snd_soc_dapm_virt_mux: {
302 struct soc_enum *e = (struct soc_enum *)
303 w->kcontrol_news[i].private_value;
306 /* since a virtual mux has no backing registers to
307 * decide which path to connect, it will try to match
308 * with the first enumeration. This is to ensure
309 * that the default mux choice (the first) will be
310 * correctly powered up during initialization.
312 if (!strcmp(p->name, e->texts[0]))
316 case snd_soc_dapm_value_mux: {
317 struct soc_enum *e = (struct soc_enum *)
318 w->kcontrol_news[i].private_value;
321 val = soc_widget_read(w, e->reg);
322 val = (val >> e->shift_l) & e->mask;
323 for (item = 0; item < e->max; item++) {
324 if (val == e->values[item])
329 for (i = 0; i < e->max; i++) {
330 if (!(strcmp(p->name, e->texts[i])) && item == i)
335 /* does not affect routing - always connected */
336 case snd_soc_dapm_pga:
337 case snd_soc_dapm_out_drv:
338 case snd_soc_dapm_output:
339 case snd_soc_dapm_adc:
340 case snd_soc_dapm_input:
341 case snd_soc_dapm_dac:
342 case snd_soc_dapm_micbias:
343 case snd_soc_dapm_vmid:
344 case snd_soc_dapm_supply:
345 case snd_soc_dapm_aif_in:
346 case snd_soc_dapm_aif_out:
347 case snd_soc_dapm_hp:
348 case snd_soc_dapm_mic:
349 case snd_soc_dapm_spk:
350 case snd_soc_dapm_line:
353 /* does affect routing - dynamically connected */
354 case snd_soc_dapm_pre:
355 case snd_soc_dapm_post:
361 /* connect mux widget to its interconnecting audio paths */
362 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
363 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
364 struct snd_soc_dapm_path *path, const char *control_name,
365 const struct snd_kcontrol_new *kcontrol)
367 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
370 for (i = 0; i < e->max; i++) {
371 if (!(strcmp(control_name, e->texts[i]))) {
372 list_add(&path->list, &dapm->card->paths);
373 list_add(&path->list_sink, &dest->sources);
374 list_add(&path->list_source, &src->sinks);
375 path->name = (char*)e->texts[i];
376 dapm_set_path_status(dest, path, 0);
384 /* connect mixer widget to its interconnecting audio paths */
385 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
386 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
387 struct snd_soc_dapm_path *path, const char *control_name)
391 /* search for mixer kcontrol */
392 for (i = 0; i < dest->num_kcontrols; i++) {
393 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
394 list_add(&path->list, &dapm->card->paths);
395 list_add(&path->list_sink, &dest->sources);
396 list_add(&path->list_source, &src->sinks);
397 path->name = dest->kcontrol_news[i].name;
398 dapm_set_path_status(dest, path, i);
405 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
406 struct snd_soc_dapm_widget *kcontrolw,
407 const struct snd_kcontrol_new *kcontrol_new,
408 struct snd_kcontrol **kcontrol)
410 struct snd_soc_dapm_widget *w;
415 list_for_each_entry(w, &dapm->card->widgets, list) {
416 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
418 for (i = 0; i < w->num_kcontrols; i++) {
419 if (&w->kcontrol_news[i] == kcontrol_new) {
421 *kcontrol = w->kcontrols[i];
430 /* create new dapm mixer control */
431 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
433 struct snd_soc_dapm_context *dapm = w->dapm;
435 size_t name_len, prefix_len;
436 struct snd_soc_dapm_path *path;
437 struct snd_card *card = dapm->card->snd_card;
439 struct snd_soc_dapm_widget_list *wlist;
443 prefix = dapm->codec->name_prefix;
448 prefix_len = strlen(prefix) + 1;
453 for (i = 0; i < w->num_kcontrols; i++) {
456 list_for_each_entry(path, &w->sources, list_sink) {
458 /* mixer/mux paths name must match control name */
459 if (path->name != (char *)w->kcontrol_news[i].name)
462 if (w->kcontrols[i]) {
463 path->kcontrol = w->kcontrols[i];
467 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
468 sizeof(struct snd_soc_dapm_widget *),
469 wlist = kzalloc(wlistsize, GFP_KERNEL);
472 "asoc: can't allocate widget list for %s\n",
476 wlist->num_widgets = 1;
477 wlist->widgets[0] = w;
479 /* add dapm control with long name.
480 * for dapm_mixer this is the concatenation of the
481 * mixer and kcontrol name.
482 * for dapm_mixer_named_ctl this is simply the
485 name_len = strlen(w->kcontrol_news[i].name) + 1;
486 if (w->id != snd_soc_dapm_mixer_named_ctl)
487 name_len += 1 + strlen(w->name);
489 path->long_name = kmalloc(name_len, GFP_KERNEL);
491 if (path->long_name == NULL) {
498 /* The control will get a prefix from
499 * the control creation process but
500 * we're also using the same prefix
501 * for widgets so cut the prefix off
502 * the front of the widget name.
504 snprintf(path->long_name, name_len, "%s %s",
505 w->name + prefix_len,
506 w->kcontrol_news[i].name);
508 case snd_soc_dapm_mixer_named_ctl:
509 snprintf(path->long_name, name_len, "%s",
510 w->kcontrol_news[i].name);
514 path->long_name[name_len - 1] = '\0';
516 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
517 wlist, path->long_name,
519 ret = snd_ctl_add(card, path->kcontrol);
522 "asoc: failed to add dapm kcontrol %s: %d\n",
523 path->long_name, ret);
525 kfree(path->long_name);
526 path->long_name = NULL;
529 w->kcontrols[i] = path->kcontrol;
535 /* create new dapm mux control */
536 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
538 struct snd_soc_dapm_context *dapm = w->dapm;
539 struct snd_soc_dapm_path *path = NULL;
540 struct snd_kcontrol *kcontrol;
541 struct snd_card *card = dapm->card->snd_card;
545 struct snd_soc_dapm_widget_list *wlist;
546 int shared, wlistentries;
550 if (w->num_kcontrols != 1) {
552 "asoc: mux %s has incorrect number of controls\n",
557 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
560 wlist = kcontrol->private_data;
561 wlistentries = wlist->num_widgets + 1;
566 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
567 wlistentries * sizeof(struct snd_soc_dapm_widget *),
568 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
571 "asoc: can't allocate widget list for %s\n", w->name);
574 wlist->num_widgets = wlistentries;
575 wlist->widgets[wlistentries - 1] = w;
579 prefix = dapm->codec->name_prefix;
584 name = w->kcontrol_news[0].name;
589 prefix_len = strlen(prefix) + 1;
595 * The control will get a prefix from the control creation
596 * process but we're also using the same prefix for widgets so
597 * cut the prefix off the front of the widget name.
599 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
600 name + prefix_len, prefix);
601 ret = snd_ctl_add(card, kcontrol);
603 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
610 kcontrol->private_data = wlist;
612 w->kcontrols[0] = kcontrol;
614 list_for_each_entry(path, &w->sources, list_sink)
615 path->kcontrol = kcontrol;
620 /* create new dapm volume control */
621 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
623 if (w->num_kcontrols)
624 dev_err(w->dapm->dev,
625 "asoc: PGA controls not supported: '%s'\n", w->name);
630 /* reset 'walked' bit for each dapm path */
631 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
633 struct snd_soc_dapm_path *p;
635 list_for_each_entry(p, &dapm->card->paths, list)
639 /* We implement power down on suspend by checking the power state of
640 * the ALSA card - when we are suspending the ALSA state for the card
643 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
645 int level = snd_power_get_state(widget->dapm->card->snd_card);
648 case SNDRV_CTL_POWER_D3hot:
649 case SNDRV_CTL_POWER_D3cold:
650 if (widget->ignore_suspend)
651 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
653 return widget->ignore_suspend;
660 * Recursively check for a completed path to an active or physically connected
661 * output widget. Returns number of complete paths.
663 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
665 struct snd_soc_dapm_path *path;
668 DAPM_UPDATE_STAT(widget, path_checks);
670 if (widget->id == snd_soc_dapm_supply)
673 switch (widget->id) {
674 case snd_soc_dapm_adc:
675 case snd_soc_dapm_aif_out:
677 return snd_soc_dapm_suspend_check(widget);
682 if (widget->connected) {
683 /* connected pin ? */
684 if (widget->id == snd_soc_dapm_output && !widget->ext)
685 return snd_soc_dapm_suspend_check(widget);
687 /* connected jack or spk ? */
688 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
689 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
690 return snd_soc_dapm_suspend_check(widget);
693 list_for_each_entry(path, &widget->sinks, list_source) {
694 DAPM_UPDATE_STAT(widget, neighbour_checks);
702 if (path->sink && path->connect) {
704 con += is_connected_output_ep(path->sink);
712 * Recursively check for a completed path to an active or physically connected
713 * input widget. Returns number of complete paths.
715 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
717 struct snd_soc_dapm_path *path;
720 DAPM_UPDATE_STAT(widget, path_checks);
722 if (widget->id == snd_soc_dapm_supply)
725 /* active stream ? */
726 switch (widget->id) {
727 case snd_soc_dapm_dac:
728 case snd_soc_dapm_aif_in:
730 return snd_soc_dapm_suspend_check(widget);
735 if (widget->connected) {
736 /* connected pin ? */
737 if (widget->id == snd_soc_dapm_input && !widget->ext)
738 return snd_soc_dapm_suspend_check(widget);
740 /* connected VMID/Bias for lower pops */
741 if (widget->id == snd_soc_dapm_vmid)
742 return snd_soc_dapm_suspend_check(widget);
744 /* connected jack ? */
745 if (widget->id == snd_soc_dapm_mic ||
746 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
747 return snd_soc_dapm_suspend_check(widget);
750 list_for_each_entry(path, &widget->sources, list_sink) {
751 DAPM_UPDATE_STAT(widget, neighbour_checks);
759 if (path->source && path->connect) {
761 con += is_connected_input_ep(path->source);
769 * Handler for generic register modifier widget.
771 int dapm_reg_event(struct snd_soc_dapm_widget *w,
772 struct snd_kcontrol *kcontrol, int event)
776 if (SND_SOC_DAPM_EVENT_ON(event))
781 soc_widget_update_bits(w, -(w->reg + 1),
782 w->mask << w->shift, val << w->shift);
786 EXPORT_SYMBOL_GPL(dapm_reg_event);
788 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
790 if (w->power_checked)
796 w->new_power = w->power_check(w);
798 w->power_checked = true;
803 /* Generic check to see if a widget should be powered.
805 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
809 DAPM_UPDATE_STAT(w, power_checks);
811 in = is_connected_input_ep(w);
812 dapm_clear_walk(w->dapm);
813 out = is_connected_output_ep(w);
814 dapm_clear_walk(w->dapm);
815 return out != 0 && in != 0;
818 /* Check to see if an ADC has power */
819 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
823 DAPM_UPDATE_STAT(w, power_checks);
826 in = is_connected_input_ep(w);
827 dapm_clear_walk(w->dapm);
830 return dapm_generic_check_power(w);
834 /* Check to see if a DAC has power */
835 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
839 DAPM_UPDATE_STAT(w, power_checks);
842 out = is_connected_output_ep(w);
843 dapm_clear_walk(w->dapm);
846 return dapm_generic_check_power(w);
850 /* Check to see if a power supply is needed */
851 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
853 struct snd_soc_dapm_path *path;
855 DAPM_UPDATE_STAT(w, power_checks);
857 /* Check if one of our outputs is connected */
858 list_for_each_entry(path, &w->sinks, list_source) {
859 DAPM_UPDATE_STAT(w, neighbour_checks);
864 if (path->connected &&
865 !path->connected(path->source, path->sink))
871 if (dapm_widget_power_check(path->sink))
875 dapm_clear_walk(w->dapm);
880 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
885 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
886 struct snd_soc_dapm_widget *b,
894 sort = dapm_down_seq;
896 if (sort[a->id] != sort[b->id])
897 return sort[a->id] - sort[b->id];
898 if (a->subseq != b->subseq) {
900 return a->subseq - b->subseq;
902 return b->subseq - a->subseq;
904 if (a->reg != b->reg)
905 return a->reg - b->reg;
906 if (a->dapm != b->dapm)
907 return (unsigned long)a->dapm - (unsigned long)b->dapm;
912 /* Insert a widget in order into a DAPM power sequence. */
913 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
914 struct list_head *list,
917 struct snd_soc_dapm_widget *w;
919 list_for_each_entry(w, list, power_list)
920 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
921 list_add_tail(&new_widget->power_list, &w->power_list);
925 list_add_tail(&new_widget->power_list, list);
928 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
929 struct snd_soc_dapm_widget *w, int event)
931 struct snd_soc_card *card = dapm->card;
936 case SND_SOC_DAPM_PRE_PMU:
940 case SND_SOC_DAPM_POST_PMU:
941 ev_name = "POST_PMU";
944 case SND_SOC_DAPM_PRE_PMD:
948 case SND_SOC_DAPM_POST_PMD:
949 ev_name = "POST_PMD";
957 if (w->power != power)
960 if (w->event && (w->event_flags & event)) {
961 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
963 trace_snd_soc_dapm_widget_event_start(w, event);
964 ret = w->event(w, NULL, event);
965 trace_snd_soc_dapm_widget_event_done(w, event);
967 pr_err("%s: %s event failed: %d\n",
968 ev_name, w->name, ret);
972 /* Apply the coalesced changes from a DAPM sequence */
973 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
974 struct list_head *pending)
976 struct snd_soc_card *card = dapm->card;
977 struct snd_soc_dapm_widget *w;
979 unsigned int value = 0;
980 unsigned int mask = 0;
981 unsigned int cur_mask;
983 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
986 list_for_each_entry(w, pending, power_list) {
987 cur_mask = 1 << w->shift;
988 BUG_ON(reg != w->reg);
999 pop_dbg(dapm->dev, card->pop_time,
1000 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1001 w->name, reg, value, mask);
1003 /* Check for events */
1004 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1005 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1009 /* Any widget will do, they should all be updating the
1012 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1015 pop_dbg(dapm->dev, card->pop_time,
1016 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1017 value, mask, reg, card->pop_time);
1018 pop_wait(card->pop_time);
1019 soc_widget_update_bits(w, reg, mask, value);
1022 list_for_each_entry(w, pending, power_list) {
1023 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1024 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1028 /* Apply a DAPM power sequence.
1030 * We walk over a pre-sorted list of widgets to apply power to. In
1031 * order to minimise the number of writes to the device required
1032 * multiple widgets will be updated in a single write where possible.
1033 * Currently anything that requires more than a single write is not
1036 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1037 struct list_head *list, int event, bool power_up)
1039 struct snd_soc_dapm_widget *w, *n;
1042 int cur_subseq = -1;
1043 int cur_reg = SND_SOC_NOPM;
1044 struct snd_soc_dapm_context *cur_dapm = NULL;
1051 sort = dapm_down_seq;
1053 list_for_each_entry_safe(w, n, list, power_list) {
1056 /* Do we need to apply any queued changes? */
1057 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1058 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1059 if (!list_empty(&pending))
1060 dapm_seq_run_coalesced(cur_dapm, &pending);
1062 if (cur_dapm && cur_dapm->seq_notifier) {
1063 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1064 if (sort[i] == cur_sort)
1065 cur_dapm->seq_notifier(cur_dapm,
1070 INIT_LIST_HEAD(&pending);
1072 cur_subseq = INT_MIN;
1073 cur_reg = SND_SOC_NOPM;
1078 case snd_soc_dapm_pre:
1080 list_for_each_entry_safe_continue(w, n, list,
1083 if (event == SND_SOC_DAPM_STREAM_START)
1085 NULL, SND_SOC_DAPM_PRE_PMU);
1086 else if (event == SND_SOC_DAPM_STREAM_STOP)
1088 NULL, SND_SOC_DAPM_PRE_PMD);
1091 case snd_soc_dapm_post:
1093 list_for_each_entry_safe_continue(w, n, list,
1096 if (event == SND_SOC_DAPM_STREAM_START)
1098 NULL, SND_SOC_DAPM_POST_PMU);
1099 else if (event == SND_SOC_DAPM_STREAM_STOP)
1101 NULL, SND_SOC_DAPM_POST_PMD);
1105 /* Queue it up for application */
1106 cur_sort = sort[w->id];
1107 cur_subseq = w->subseq;
1110 list_move(&w->power_list, &pending);
1115 dev_err(w->dapm->dev,
1116 "Failed to apply widget power: %d\n", ret);
1119 if (!list_empty(&pending))
1120 dapm_seq_run_coalesced(cur_dapm, &pending);
1122 if (cur_dapm && cur_dapm->seq_notifier) {
1123 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1124 if (sort[i] == cur_sort)
1125 cur_dapm->seq_notifier(cur_dapm,
1130 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1132 struct snd_soc_dapm_update *update = dapm->update;
1133 struct snd_soc_dapm_widget *w;
1142 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1143 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1145 pr_err("%s DAPM pre-event failed: %d\n",
1149 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1152 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1155 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1156 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1158 pr_err("%s DAPM post-event failed: %d\n",
1163 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1164 * they're changing state.
1166 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1168 struct snd_soc_dapm_context *d = data;
1171 /* If we're off and we're not supposed to be go into STANDBY */
1172 if (d->bias_level == SND_SOC_BIAS_OFF &&
1173 d->target_bias_level != SND_SOC_BIAS_OFF) {
1174 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1177 "Failed to turn on bias: %d\n", ret);
1180 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1181 if (d->bias_level != d->target_bias_level) {
1182 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1185 "Failed to prepare bias: %d\n", ret);
1189 /* Async callback run prior to DAPM sequences - brings to their final
1192 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1194 struct snd_soc_dapm_context *d = data;
1197 /* If we just powered the last thing off drop to standby bias */
1198 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1199 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1200 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1201 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1203 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1207 /* If we're in standby and can support bias off then do that */
1208 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1209 d->target_bias_level == SND_SOC_BIAS_OFF) {
1210 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1212 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1215 /* If we just powered up then move to active bias */
1216 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1217 d->target_bias_level == SND_SOC_BIAS_ON) {
1218 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1220 dev_err(d->dev, "Failed to apply active bias: %d\n",
1225 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1226 bool power, bool connect)
1228 /* If a connection is being made or broken then that update
1229 * will have marked the peer dirty, otherwise the widgets are
1230 * not connected and this update has no impact. */
1234 /* If the peer is already in the state we're moving to then we
1235 * won't have an impact on it. */
1236 if (power != peer->power)
1237 dapm_mark_dirty(peer, "peer state change");
1240 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1241 struct list_head *up_list,
1242 struct list_head *down_list)
1244 struct snd_soc_dapm_path *path;
1246 if (w->power == power)
1249 trace_snd_soc_dapm_widget_power(w, power);
1251 /* If we changed our power state perhaps our neigbours changed
1254 list_for_each_entry(path, &w->sources, list_sink) {
1256 dapm_widget_set_peer_power(path->source, power,
1261 case snd_soc_dapm_supply:
1262 /* Supplies can't affect their outputs, only their inputs */
1265 list_for_each_entry(path, &w->sinks, list_source) {
1267 dapm_widget_set_peer_power(path->sink, power,
1275 dapm_seq_insert(w, up_list, true);
1277 dapm_seq_insert(w, down_list, false);
1282 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1283 struct list_head *up_list,
1284 struct list_head *down_list)
1289 case snd_soc_dapm_pre:
1290 dapm_seq_insert(w, down_list, false);
1292 case snd_soc_dapm_post:
1293 dapm_seq_insert(w, up_list, true);
1297 power = dapm_widget_power_check(w);
1299 dapm_widget_set_power(w, power, up_list, down_list);
1305 * Scan each dapm widget for complete audio path.
1306 * A complete path is a route that has valid endpoints i.e.:-
1308 * o DAC to output pin.
1309 * o Input Pin to ADC.
1310 * o Input pin to Output pin (bypass, sidetone)
1311 * o DAC to ADC (loopback).
1313 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1315 struct snd_soc_card *card = dapm->card;
1316 struct snd_soc_dapm_widget *w;
1317 struct snd_soc_dapm_context *d;
1319 LIST_HEAD(down_list);
1320 LIST_HEAD(async_domain);
1321 enum snd_soc_bias_level bias;
1323 trace_snd_soc_dapm_start(card);
1325 list_for_each_entry(d, &card->dapm_list, list) {
1326 if (d->n_widgets || d->codec == NULL) {
1327 if (d->idle_bias_off)
1328 d->target_bias_level = SND_SOC_BIAS_OFF;
1330 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1334 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1336 list_for_each_entry(w, &card->widgets, list) {
1337 w->power_checked = false;
1340 /* Check which widgets we need to power and store them in
1341 * lists indicating if they should be powered up or down. We
1342 * only check widgets that have been flagged as dirty but note
1343 * that new widgets may be added to the dirty list while we
1346 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1347 dapm_power_one_widget(w, &up_list, &down_list);
1350 list_for_each_entry(w, &card->widgets, list) {
1351 list_del_init(&w->dirty);
1356 /* Supplies and micbiases only bring the
1357 * context up to STANDBY as unless something
1358 * else is active and passing audio they
1359 * generally don't require full power.
1362 case snd_soc_dapm_supply:
1363 case snd_soc_dapm_micbias:
1364 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1365 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1368 d->target_bias_level = SND_SOC_BIAS_ON;
1375 /* If there are no DAPM widgets then try to figure out power from the
1378 if (!dapm->n_widgets) {
1380 case SND_SOC_DAPM_STREAM_START:
1381 case SND_SOC_DAPM_STREAM_RESUME:
1382 dapm->target_bias_level = SND_SOC_BIAS_ON;
1384 case SND_SOC_DAPM_STREAM_STOP:
1385 if (dapm->codec->active)
1386 dapm->target_bias_level = SND_SOC_BIAS_ON;
1388 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1390 case SND_SOC_DAPM_STREAM_SUSPEND:
1391 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1393 case SND_SOC_DAPM_STREAM_NOP:
1394 dapm->target_bias_level = dapm->bias_level;
1401 /* Force all contexts in the card to the same bias state if
1402 * they're not ground referenced.
1404 bias = SND_SOC_BIAS_OFF;
1405 list_for_each_entry(d, &card->dapm_list, list)
1406 if (d->target_bias_level > bias)
1407 bias = d->target_bias_level;
1408 list_for_each_entry(d, &card->dapm_list, list)
1409 if (!d->idle_bias_off)
1410 d->target_bias_level = bias;
1412 trace_snd_soc_dapm_walk_done(card);
1414 /* Run all the bias changes in parallel */
1415 list_for_each_entry(d, &dapm->card->dapm_list, list)
1416 async_schedule_domain(dapm_pre_sequence_async, d,
1418 async_synchronize_full_domain(&async_domain);
1420 /* Power down widgets first; try to avoid amplifying pops. */
1421 dapm_seq_run(dapm, &down_list, event, false);
1423 dapm_widget_update(dapm);
1426 dapm_seq_run(dapm, &up_list, event, true);
1428 /* Run all the bias changes in parallel */
1429 list_for_each_entry(d, &dapm->card->dapm_list, list)
1430 async_schedule_domain(dapm_post_sequence_async, d,
1432 async_synchronize_full_domain(&async_domain);
1434 pop_dbg(dapm->dev, card->pop_time,
1435 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1436 pop_wait(card->pop_time);
1438 trace_snd_soc_dapm_done(card);
1443 #ifdef CONFIG_DEBUG_FS
1444 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1446 file->private_data = inode->i_private;
1450 static ssize_t dapm_widget_power_read_file(struct file *file,
1451 char __user *user_buf,
1452 size_t count, loff_t *ppos)
1454 struct snd_soc_dapm_widget *w = file->private_data;
1458 struct snd_soc_dapm_path *p = NULL;
1460 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1464 in = is_connected_input_ep(w);
1465 dapm_clear_walk(w->dapm);
1466 out = is_connected_output_ep(w);
1467 dapm_clear_walk(w->dapm);
1469 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1470 w->name, w->power ? "On" : "Off", in, out);
1473 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1474 " - R%d(0x%x) bit %d",
1475 w->reg, w->reg, w->shift);
1477 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1480 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1482 w->active ? "active" : "inactive");
1484 list_for_each_entry(p, &w->sources, list_sink) {
1485 if (p->connected && !p->connected(w, p->sink))
1489 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1490 " in \"%s\" \"%s\"\n",
1491 p->name ? p->name : "static",
1494 list_for_each_entry(p, &w->sinks, list_source) {
1495 if (p->connected && !p->connected(w, p->sink))
1499 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1500 " out \"%s\" \"%s\"\n",
1501 p->name ? p->name : "static",
1505 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1511 static const struct file_operations dapm_widget_power_fops = {
1512 .open = dapm_widget_power_open_file,
1513 .read = dapm_widget_power_read_file,
1514 .llseek = default_llseek,
1517 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1519 file->private_data = inode->i_private;
1523 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1524 size_t count, loff_t *ppos)
1526 struct snd_soc_dapm_context *dapm = file->private_data;
1529 switch (dapm->bias_level) {
1530 case SND_SOC_BIAS_ON:
1533 case SND_SOC_BIAS_PREPARE:
1534 level = "Prepare\n";
1536 case SND_SOC_BIAS_STANDBY:
1537 level = "Standby\n";
1539 case SND_SOC_BIAS_OFF:
1544 level = "Unknown\n";
1548 return simple_read_from_buffer(user_buf, count, ppos, level,
1552 static const struct file_operations dapm_bias_fops = {
1553 .open = dapm_bias_open_file,
1554 .read = dapm_bias_read_file,
1555 .llseek = default_llseek,
1558 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1559 struct dentry *parent)
1563 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1565 if (!dapm->debugfs_dapm) {
1567 "Failed to create DAPM debugfs directory\n");
1571 d = debugfs_create_file("bias_level", 0444,
1572 dapm->debugfs_dapm, dapm,
1576 "ASoC: Failed to create bias level debugfs file\n");
1579 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1581 struct snd_soc_dapm_context *dapm = w->dapm;
1584 if (!dapm->debugfs_dapm || !w->name)
1587 d = debugfs_create_file(w->name, 0444,
1588 dapm->debugfs_dapm, w,
1589 &dapm_widget_power_fops);
1591 dev_warn(w->dapm->dev,
1592 "ASoC: Failed to create %s debugfs file\n",
1596 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1598 debugfs_remove_recursive(dapm->debugfs_dapm);
1602 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1603 struct dentry *parent)
1607 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1611 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1617 /* test and update the power status of a mux widget */
1618 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1619 struct snd_kcontrol *kcontrol, int change,
1620 int mux, struct soc_enum *e)
1622 struct snd_soc_dapm_path *path;
1625 if (widget->id != snd_soc_dapm_mux &&
1626 widget->id != snd_soc_dapm_virt_mux &&
1627 widget->id != snd_soc_dapm_value_mux)
1633 /* find dapm widget path assoc with kcontrol */
1634 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1635 if (path->kcontrol != kcontrol)
1638 if (!path->name || !e->texts[mux])
1642 /* we now need to match the string in the enum to the path */
1643 if (!(strcmp(path->name, e->texts[mux]))) {
1644 path->connect = 1; /* new connection */
1645 dapm_mark_dirty(path->source, "mux connection");
1648 dapm_mark_dirty(path->source,
1649 "mux disconnection");
1650 path->connect = 0; /* old connection must be powered down */
1655 dapm_mark_dirty(widget, "mux change");
1656 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1662 /* test and update the power status of a mixer or switch widget */
1663 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1664 struct snd_kcontrol *kcontrol, int connect)
1666 struct snd_soc_dapm_path *path;
1669 if (widget->id != snd_soc_dapm_mixer &&
1670 widget->id != snd_soc_dapm_mixer_named_ctl &&
1671 widget->id != snd_soc_dapm_switch)
1674 /* find dapm widget path assoc with kcontrol */
1675 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1676 if (path->kcontrol != kcontrol)
1679 /* found, now check type */
1681 path->connect = connect;
1682 dapm_mark_dirty(path->source, "mixer connection");
1686 dapm_mark_dirty(widget, "mixer update");
1687 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1693 /* show dapm widget status in sys fs */
1694 static ssize_t dapm_widget_show(struct device *dev,
1695 struct device_attribute *attr, char *buf)
1697 struct snd_soc_pcm_runtime *rtd =
1698 container_of(dev, struct snd_soc_pcm_runtime, dev);
1699 struct snd_soc_codec *codec =rtd->codec;
1700 struct snd_soc_dapm_widget *w;
1702 char *state = "not set";
1704 list_for_each_entry(w, &codec->card->widgets, list) {
1705 if (w->dapm != &codec->dapm)
1708 /* only display widgets that burnm power */
1710 case snd_soc_dapm_hp:
1711 case snd_soc_dapm_mic:
1712 case snd_soc_dapm_spk:
1713 case snd_soc_dapm_line:
1714 case snd_soc_dapm_micbias:
1715 case snd_soc_dapm_dac:
1716 case snd_soc_dapm_adc:
1717 case snd_soc_dapm_pga:
1718 case snd_soc_dapm_out_drv:
1719 case snd_soc_dapm_mixer:
1720 case snd_soc_dapm_mixer_named_ctl:
1721 case snd_soc_dapm_supply:
1723 count += sprintf(buf + count, "%s: %s\n",
1724 w->name, w->power ? "On":"Off");
1731 switch (codec->dapm.bias_level) {
1732 case SND_SOC_BIAS_ON:
1735 case SND_SOC_BIAS_PREPARE:
1738 case SND_SOC_BIAS_STANDBY:
1741 case SND_SOC_BIAS_OFF:
1745 count += sprintf(buf + count, "PM State: %s\n", state);
1750 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1752 int snd_soc_dapm_sys_add(struct device *dev)
1754 return device_create_file(dev, &dev_attr_dapm_widget);
1757 static void snd_soc_dapm_sys_remove(struct device *dev)
1759 device_remove_file(dev, &dev_attr_dapm_widget);
1762 /* free all dapm widgets and resources */
1763 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1765 struct snd_soc_dapm_widget *w, *next_w;
1766 struct snd_soc_dapm_path *p, *next_p;
1768 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1769 if (w->dapm != dapm)
1773 * remove source and sink paths associated to this widget.
1774 * While removing the path, remove reference to it from both
1775 * source and sink widgets so that path is removed only once.
1777 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1778 list_del(&p->list_sink);
1779 list_del(&p->list_source);
1781 kfree(p->long_name);
1784 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1785 list_del(&p->list_sink);
1786 list_del(&p->list_source);
1788 kfree(p->long_name);
1791 kfree(w->kcontrols);
1797 static struct snd_soc_dapm_widget *dapm_find_widget(
1798 struct snd_soc_dapm_context *dapm, const char *pin,
1799 bool search_other_contexts)
1801 struct snd_soc_dapm_widget *w;
1802 struct snd_soc_dapm_widget *fallback = NULL;
1804 list_for_each_entry(w, &dapm->card->widgets, list) {
1805 if (!strcmp(w->name, pin)) {
1806 if (w->dapm == dapm)
1813 if (search_other_contexts)
1819 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1820 const char *pin, int status)
1822 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1825 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1829 w->connected = status;
1832 dapm_mark_dirty(w, "pin configuration");
1838 * snd_soc_dapm_sync - scan and power dapm paths
1839 * @dapm: DAPM context
1841 * Walks all dapm audio paths and powers widgets according to their
1842 * stream or path usage.
1844 * Returns 0 for success.
1846 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1848 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1850 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1852 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1853 const struct snd_soc_dapm_route *route)
1855 struct snd_soc_dapm_path *path;
1856 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1857 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1859 const char *control = route->control;
1861 char prefixed_sink[80];
1862 char prefixed_source[80];
1865 if (dapm->codec && dapm->codec->name_prefix) {
1866 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1867 dapm->codec->name_prefix, route->sink);
1868 sink = prefixed_sink;
1869 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1870 dapm->codec->name_prefix, route->source);
1871 source = prefixed_source;
1874 source = route->source;
1878 * find src and dest widgets over all widgets but favor a widget from
1879 * current DAPM context
1881 list_for_each_entry(w, &dapm->card->widgets, list) {
1882 if (!wsink && !(strcmp(w->name, sink))) {
1884 if (w->dapm == dapm)
1888 if (!wsource && !(strcmp(w->name, source))) {
1890 if (w->dapm == dapm)
1894 /* use widget from another DAPM context if not found from this */
1900 if (wsource == NULL || wsink == NULL)
1903 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1907 path->source = wsource;
1909 path->connected = route->connected;
1910 INIT_LIST_HEAD(&path->list);
1911 INIT_LIST_HEAD(&path->list_source);
1912 INIT_LIST_HEAD(&path->list_sink);
1914 /* check for external widgets */
1915 if (wsink->id == snd_soc_dapm_input) {
1916 if (wsource->id == snd_soc_dapm_micbias ||
1917 wsource->id == snd_soc_dapm_mic ||
1918 wsource->id == snd_soc_dapm_line ||
1919 wsource->id == snd_soc_dapm_output)
1922 if (wsource->id == snd_soc_dapm_output) {
1923 if (wsink->id == snd_soc_dapm_spk ||
1924 wsink->id == snd_soc_dapm_hp ||
1925 wsink->id == snd_soc_dapm_line ||
1926 wsink->id == snd_soc_dapm_input)
1930 /* connect static paths */
1931 if (control == NULL) {
1932 list_add(&path->list, &dapm->card->paths);
1933 list_add(&path->list_sink, &wsink->sources);
1934 list_add(&path->list_source, &wsource->sinks);
1939 /* connect dynamic paths */
1940 switch (wsink->id) {
1941 case snd_soc_dapm_adc:
1942 case snd_soc_dapm_dac:
1943 case snd_soc_dapm_pga:
1944 case snd_soc_dapm_out_drv:
1945 case snd_soc_dapm_input:
1946 case snd_soc_dapm_output:
1947 case snd_soc_dapm_micbias:
1948 case snd_soc_dapm_vmid:
1949 case snd_soc_dapm_pre:
1950 case snd_soc_dapm_post:
1951 case snd_soc_dapm_supply:
1952 case snd_soc_dapm_aif_in:
1953 case snd_soc_dapm_aif_out:
1954 list_add(&path->list, &dapm->card->paths);
1955 list_add(&path->list_sink, &wsink->sources);
1956 list_add(&path->list_source, &wsource->sinks);
1959 case snd_soc_dapm_mux:
1960 case snd_soc_dapm_virt_mux:
1961 case snd_soc_dapm_value_mux:
1962 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1963 &wsink->kcontrol_news[0]);
1967 case snd_soc_dapm_switch:
1968 case snd_soc_dapm_mixer:
1969 case snd_soc_dapm_mixer_named_ctl:
1970 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1974 case snd_soc_dapm_hp:
1975 case snd_soc_dapm_mic:
1976 case snd_soc_dapm_line:
1977 case snd_soc_dapm_spk:
1978 list_add(&path->list, &dapm->card->paths);
1979 list_add(&path->list_sink, &wsink->sources);
1980 list_add(&path->list_source, &wsource->sinks);
1987 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1988 source, control, sink);
1994 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1995 * @dapm: DAPM context
1996 * @route: audio routes
1997 * @num: number of routes
1999 * Connects 2 dapm widgets together via a named audio path. The sink is
2000 * the widget receiving the audio signal, whilst the source is the sender
2001 * of the audio signal.
2003 * Returns 0 for success else error. On error all resources can be freed
2004 * with a call to snd_soc_card_free().
2006 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2007 const struct snd_soc_dapm_route *route, int num)
2011 for (i = 0; i < num; i++) {
2012 ret = snd_soc_dapm_add_route(dapm, route);
2014 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2015 route->source, route->sink);
2023 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2025 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2026 const struct snd_soc_dapm_route *route)
2028 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2031 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2034 struct snd_soc_dapm_path *path;
2038 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2044 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2049 if (route->control || route->connected)
2050 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2051 route->source, route->sink);
2053 list_for_each_entry(path, &source->sinks, list_source) {
2054 if (path->sink == sink) {
2061 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2062 route->source, route->sink);
2064 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2065 count, route->source, route->sink);
2071 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2072 * @dapm: DAPM context
2073 * @route: audio routes
2074 * @num: number of routes
2076 * Mark existing routes matching those specified in the passed array
2077 * as being weak, meaning that they are ignored for the purpose of
2078 * power decisions. The main intended use case is for sidetone paths
2079 * which couple audio between other independent paths if they are both
2080 * active in order to make the combination work better at the user
2081 * level but which aren't intended to be "used".
2083 * Note that CODEC drivers should not use this as sidetone type paths
2084 * can frequently also be used as bypass paths.
2086 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2087 const struct snd_soc_dapm_route *route, int num)
2092 for (i = 0; i < num; i++) {
2093 err = snd_soc_dapm_weak_route(dapm, route);
2101 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2104 * snd_soc_dapm_new_widgets - add new dapm widgets
2105 * @dapm: DAPM context
2107 * Checks the codec for any new dapm widgets and creates them if found.
2109 * Returns 0 for success.
2111 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2113 struct snd_soc_dapm_widget *w;
2116 list_for_each_entry(w, &dapm->card->widgets, list)
2121 if (w->num_kcontrols) {
2122 w->kcontrols = kzalloc(w->num_kcontrols *
2123 sizeof(struct snd_kcontrol *),
2130 case snd_soc_dapm_switch:
2131 case snd_soc_dapm_mixer:
2132 case snd_soc_dapm_mixer_named_ctl:
2133 w->power_check = dapm_generic_check_power;
2136 case snd_soc_dapm_mux:
2137 case snd_soc_dapm_virt_mux:
2138 case snd_soc_dapm_value_mux:
2139 w->power_check = dapm_generic_check_power;
2142 case snd_soc_dapm_adc:
2143 case snd_soc_dapm_aif_out:
2144 w->power_check = dapm_adc_check_power;
2146 case snd_soc_dapm_dac:
2147 case snd_soc_dapm_aif_in:
2148 w->power_check = dapm_dac_check_power;
2150 case snd_soc_dapm_pga:
2151 case snd_soc_dapm_out_drv:
2152 w->power_check = dapm_generic_check_power;
2155 case snd_soc_dapm_input:
2156 case snd_soc_dapm_output:
2157 case snd_soc_dapm_micbias:
2158 case snd_soc_dapm_spk:
2159 case snd_soc_dapm_hp:
2160 case snd_soc_dapm_mic:
2161 case snd_soc_dapm_line:
2162 w->power_check = dapm_generic_check_power;
2164 case snd_soc_dapm_supply:
2165 w->power_check = dapm_supply_check_power;
2166 case snd_soc_dapm_vmid:
2167 case snd_soc_dapm_pre:
2168 case snd_soc_dapm_post:
2172 if (!w->power_check)
2173 w->power_check = dapm_always_on_check_power;
2175 /* Read the initial power state from the device */
2177 val = soc_widget_read(w, w->reg);
2178 val &= 1 << w->shift;
2188 list_add(&w->dirty, &(w->dapm->card->dapm_dirty));
2189 dapm_debugfs_add_widget(w);
2192 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2195 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2198 * snd_soc_dapm_get_volsw - dapm mixer get callback
2199 * @kcontrol: mixer control
2200 * @ucontrol: control element information
2202 * Callback to get the value of a dapm mixer control.
2204 * Returns 0 for success.
2206 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2207 struct snd_ctl_elem_value *ucontrol)
2209 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2210 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2211 struct soc_mixer_control *mc =
2212 (struct soc_mixer_control *)kcontrol->private_value;
2213 unsigned int reg = mc->reg;
2214 unsigned int shift = mc->shift;
2215 unsigned int rshift = mc->rshift;
2217 unsigned int invert = mc->invert;
2218 unsigned int mask = (1 << fls(max)) - 1;
2220 ucontrol->value.integer.value[0] =
2221 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2222 if (shift != rshift)
2223 ucontrol->value.integer.value[1] =
2224 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2226 ucontrol->value.integer.value[0] =
2227 max - ucontrol->value.integer.value[0];
2228 if (shift != rshift)
2229 ucontrol->value.integer.value[1] =
2230 max - ucontrol->value.integer.value[1];
2235 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2238 * snd_soc_dapm_put_volsw - dapm mixer set callback
2239 * @kcontrol: mixer control
2240 * @ucontrol: control element information
2242 * Callback to set the value of a dapm mixer control.
2244 * Returns 0 for success.
2246 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2247 struct snd_ctl_elem_value *ucontrol)
2249 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2250 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2251 struct snd_soc_codec *codec = widget->codec;
2252 struct soc_mixer_control *mc =
2253 (struct soc_mixer_control *)kcontrol->private_value;
2254 unsigned int reg = mc->reg;
2255 unsigned int shift = mc->shift;
2257 unsigned int mask = (1 << fls(max)) - 1;
2258 unsigned int invert = mc->invert;
2260 int connect, change;
2261 struct snd_soc_dapm_update update;
2264 val = (ucontrol->value.integer.value[0] & mask);
2268 mask = mask << shift;
2272 /* new connection */
2273 connect = invert ? 0 : 1;
2275 /* old connection must be powered down */
2276 connect = invert ? 1 : 0;
2278 mutex_lock(&codec->mutex);
2280 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2282 for (wi = 0; wi < wlist->num_widgets; wi++) {
2283 widget = wlist->widgets[wi];
2285 widget->value = val;
2287 update.kcontrol = kcontrol;
2288 update.widget = widget;
2292 widget->dapm->update = &update;
2294 dapm_mixer_update_power(widget, kcontrol, connect);
2296 widget->dapm->update = NULL;
2300 mutex_unlock(&codec->mutex);
2303 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2306 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2307 * @kcontrol: mixer control
2308 * @ucontrol: control element information
2310 * Callback to get the value of a dapm enumerated double mixer control.
2312 * Returns 0 for success.
2314 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2315 struct snd_ctl_elem_value *ucontrol)
2317 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2318 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2319 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2320 unsigned int val, bitmask;
2322 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2324 val = snd_soc_read(widget->codec, e->reg);
2325 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2326 if (e->shift_l != e->shift_r)
2327 ucontrol->value.enumerated.item[1] =
2328 (val >> e->shift_r) & (bitmask - 1);
2332 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2335 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2336 * @kcontrol: mixer control
2337 * @ucontrol: control element information
2339 * Callback to set the value of a dapm enumerated double mixer control.
2341 * Returns 0 for success.
2343 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2344 struct snd_ctl_elem_value *ucontrol)
2346 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2347 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2348 struct snd_soc_codec *codec = widget->codec;
2349 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2350 unsigned int val, mux, change;
2351 unsigned int mask, bitmask;
2352 struct snd_soc_dapm_update update;
2355 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2357 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2359 mux = ucontrol->value.enumerated.item[0];
2360 val = mux << e->shift_l;
2361 mask = (bitmask - 1) << e->shift_l;
2362 if (e->shift_l != e->shift_r) {
2363 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2365 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2366 mask |= (bitmask - 1) << e->shift_r;
2369 mutex_lock(&codec->mutex);
2371 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2373 for (wi = 0; wi < wlist->num_widgets; wi++) {
2374 widget = wlist->widgets[wi];
2376 widget->value = val;
2378 update.kcontrol = kcontrol;
2379 update.widget = widget;
2380 update.reg = e->reg;
2383 widget->dapm->update = &update;
2385 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2387 widget->dapm->update = NULL;
2391 mutex_unlock(&codec->mutex);
2394 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2397 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2398 * @kcontrol: mixer control
2399 * @ucontrol: control element information
2401 * Returns 0 for success.
2403 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_value *ucontrol)
2406 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2407 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2409 ucontrol->value.enumerated.item[0] = widget->value;
2413 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2416 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2417 * @kcontrol: mixer control
2418 * @ucontrol: control element information
2420 * Returns 0 for success.
2422 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2423 struct snd_ctl_elem_value *ucontrol)
2425 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2426 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2427 struct snd_soc_codec *codec = widget->codec;
2428 struct soc_enum *e =
2429 (struct soc_enum *)kcontrol->private_value;
2434 if (ucontrol->value.enumerated.item[0] >= e->max)
2437 mutex_lock(&codec->mutex);
2439 change = widget->value != ucontrol->value.enumerated.item[0];
2441 for (wi = 0; wi < wlist->num_widgets; wi++) {
2442 widget = wlist->widgets[wi];
2444 widget->value = ucontrol->value.enumerated.item[0];
2446 dapm_mux_update_power(widget, kcontrol, change,
2451 mutex_unlock(&codec->mutex);
2454 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2457 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2459 * @kcontrol: mixer control
2460 * @ucontrol: control element information
2462 * Callback to get the value of a dapm semi enumerated double mixer control.
2464 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2465 * used for handling bitfield coded enumeration for example.
2467 * Returns 0 for success.
2469 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2470 struct snd_ctl_elem_value *ucontrol)
2472 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2473 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2474 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2475 unsigned int reg_val, val, mux;
2477 reg_val = snd_soc_read(widget->codec, e->reg);
2478 val = (reg_val >> e->shift_l) & e->mask;
2479 for (mux = 0; mux < e->max; mux++) {
2480 if (val == e->values[mux])
2483 ucontrol->value.enumerated.item[0] = mux;
2484 if (e->shift_l != e->shift_r) {
2485 val = (reg_val >> e->shift_r) & e->mask;
2486 for (mux = 0; mux < e->max; mux++) {
2487 if (val == e->values[mux])
2490 ucontrol->value.enumerated.item[1] = mux;
2495 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2498 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2500 * @kcontrol: mixer control
2501 * @ucontrol: control element information
2503 * Callback to set the value of a dapm semi enumerated double mixer control.
2505 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2506 * used for handling bitfield coded enumeration for example.
2508 * Returns 0 for success.
2510 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2511 struct snd_ctl_elem_value *ucontrol)
2513 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2514 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2515 struct snd_soc_codec *codec = widget->codec;
2516 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2517 unsigned int val, mux, change;
2519 struct snd_soc_dapm_update update;
2522 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2524 mux = ucontrol->value.enumerated.item[0];
2525 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2526 mask = e->mask << e->shift_l;
2527 if (e->shift_l != e->shift_r) {
2528 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2530 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2531 mask |= e->mask << e->shift_r;
2534 mutex_lock(&codec->mutex);
2536 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2538 for (wi = 0; wi < wlist->num_widgets; wi++) {
2539 widget = wlist->widgets[wi];
2541 widget->value = val;
2543 update.kcontrol = kcontrol;
2544 update.widget = widget;
2545 update.reg = e->reg;
2548 widget->dapm->update = &update;
2550 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2552 widget->dapm->update = NULL;
2556 mutex_unlock(&codec->mutex);
2559 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2562 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2564 * @kcontrol: mixer control
2565 * @uinfo: control element information
2567 * Callback to provide information about a pin switch control.
2569 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2570 struct snd_ctl_elem_info *uinfo)
2572 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2574 uinfo->value.integer.min = 0;
2575 uinfo->value.integer.max = 1;
2579 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2582 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2584 * @kcontrol: mixer control
2587 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2588 struct snd_ctl_elem_value *ucontrol)
2590 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2591 const char *pin = (const char *)kcontrol->private_value;
2593 mutex_lock(&codec->mutex);
2595 ucontrol->value.integer.value[0] =
2596 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2598 mutex_unlock(&codec->mutex);
2602 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2605 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2607 * @kcontrol: mixer control
2610 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_value *ucontrol)
2613 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2614 const char *pin = (const char *)kcontrol->private_value;
2616 mutex_lock(&codec->mutex);
2618 if (ucontrol->value.integer.value[0])
2619 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2621 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2623 snd_soc_dapm_sync(&codec->dapm);
2625 mutex_unlock(&codec->mutex);
2629 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2632 * snd_soc_dapm_new_control - create new dapm control
2633 * @dapm: DAPM context
2634 * @widget: widget template
2636 * Creates a new dapm control based upon the template.
2638 * Returns 0 for success else error.
2640 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2641 const struct snd_soc_dapm_widget *widget)
2643 struct snd_soc_dapm_widget *w;
2646 if ((w = dapm_cnew_widget(widget)) == NULL)
2649 name_len = strlen(widget->name) + 1;
2650 if (dapm->codec && dapm->codec->name_prefix)
2651 name_len += 1 + strlen(dapm->codec->name_prefix);
2652 w->name = kmalloc(name_len, GFP_KERNEL);
2653 if (w->name == NULL) {
2657 if (dapm->codec && dapm->codec->name_prefix)
2658 snprintf(w->name, name_len, "%s %s",
2659 dapm->codec->name_prefix, widget->name);
2661 snprintf(w->name, name_len, "%s", widget->name);
2665 w->codec = dapm->codec;
2666 w->platform = dapm->platform;
2667 INIT_LIST_HEAD(&w->sources);
2668 INIT_LIST_HEAD(&w->sinks);
2669 INIT_LIST_HEAD(&w->list);
2670 INIT_LIST_HEAD(&w->dirty);
2671 list_add(&w->list, &dapm->card->widgets);
2673 /* machine layer set ups unconnected pins and insertions */
2677 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2680 * snd_soc_dapm_new_controls - create new dapm controls
2681 * @dapm: DAPM context
2682 * @widget: widget array
2683 * @num: number of widgets
2685 * Creates new DAPM controls based upon the templates.
2687 * Returns 0 for success else error.
2689 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2690 const struct snd_soc_dapm_widget *widget,
2695 for (i = 0; i < num; i++) {
2696 ret = snd_soc_dapm_new_control(dapm, widget);
2699 "ASoC: Failed to create DAPM control %s: %d\n",
2707 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2709 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2710 const char *stream, int event)
2712 struct snd_soc_dapm_widget *w;
2714 list_for_each_entry(w, &dapm->card->widgets, list)
2716 if (!w->sname || w->dapm != dapm)
2718 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2719 w->name, w->sname, stream, event);
2720 if (strstr(w->sname, stream)) {
2721 dapm_mark_dirty(w, "stream event");
2723 case SND_SOC_DAPM_STREAM_START:
2726 case SND_SOC_DAPM_STREAM_STOP:
2729 case SND_SOC_DAPM_STREAM_SUSPEND:
2730 case SND_SOC_DAPM_STREAM_RESUME:
2731 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2732 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2738 dapm_power_widgets(dapm, event);
2740 /* do we need to notify any clients that DAPM stream is complete */
2741 if (dapm->stream_event)
2742 dapm->stream_event(dapm, event);
2746 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2747 * @rtd: PCM runtime data
2748 * @stream: stream name
2749 * @event: stream event
2751 * Sends a stream event to the dapm core. The core then makes any
2752 * necessary widget power changes.
2754 * Returns 0 for success else error.
2756 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2757 const char *stream, int event)
2759 struct snd_soc_codec *codec = rtd->codec;
2764 mutex_lock(&codec->mutex);
2765 soc_dapm_stream_event(&codec->dapm, stream, event);
2766 mutex_unlock(&codec->mutex);
2771 * snd_soc_dapm_enable_pin - enable pin.
2772 * @dapm: DAPM context
2775 * Enables input/output pin and its parents or children widgets iff there is
2776 * a valid audio route and active audio stream.
2777 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2778 * do any widget power switching.
2780 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2782 return snd_soc_dapm_set_pin(dapm, pin, 1);
2784 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2787 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2788 * @dapm: DAPM context
2791 * Enables input/output pin regardless of any other state. This is
2792 * intended for use with microphone bias supplies used in microphone
2795 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2796 * do any widget power switching.
2798 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2801 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2804 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2808 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2811 dapm_mark_dirty(w, "force enable");
2815 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2818 * snd_soc_dapm_disable_pin - disable pin.
2819 * @dapm: DAPM context
2822 * Disables input/output pin and its parents or children widgets.
2823 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2824 * do any widget power switching.
2826 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2829 return snd_soc_dapm_set_pin(dapm, pin, 0);
2831 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2834 * snd_soc_dapm_nc_pin - permanently disable pin.
2835 * @dapm: DAPM context
2838 * Marks the specified pin as being not connected, disabling it along
2839 * any parent or child widgets. At present this is identical to
2840 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2841 * additional things such as disabling controls which only affect
2842 * paths through the pin.
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_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2849 return snd_soc_dapm_set_pin(dapm, pin, 0);
2851 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2854 * snd_soc_dapm_get_pin_status - get audio pin status
2855 * @dapm: DAPM context
2856 * @pin: audio signal pin endpoint (or start point)
2858 * Get audio pin status - connected or disconnected.
2860 * Returns 1 for connected otherwise 0.
2862 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2865 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2868 return w->connected;
2872 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2875 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2876 * @dapm: DAPM context
2877 * @pin: audio signal pin endpoint (or start point)
2879 * Mark the given endpoint or pin as ignoring suspend. When the
2880 * system is disabled a path between two endpoints flagged as ignoring
2881 * suspend will not be disabled. The path must already be enabled via
2882 * normal means at suspend time, it will not be turned on if it was not
2885 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2888 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2891 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2895 w->ignore_suspend = 1;
2899 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2902 * snd_soc_dapm_free - free dapm resources
2903 * @dapm: DAPM context
2905 * Free all dapm widgets and resources.
2907 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2909 snd_soc_dapm_sys_remove(dapm->dev);
2910 dapm_debugfs_cleanup(dapm);
2911 dapm_free_widgets(dapm);
2912 list_del(&dapm->list);
2914 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2916 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2918 struct snd_soc_dapm_widget *w;
2919 LIST_HEAD(down_list);
2922 list_for_each_entry(w, &dapm->card->widgets, list) {
2923 if (w->dapm != dapm)
2926 dapm_seq_insert(w, &down_list, false);
2932 /* If there were no widgets to power down we're already in
2936 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2937 dapm_seq_run(dapm, &down_list, 0, false);
2938 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2943 * snd_soc_dapm_shutdown - callback for system shutdown
2945 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2947 struct snd_soc_codec *codec;
2949 list_for_each_entry(codec, &card->codec_dev_list, list) {
2950 soc_dapm_shutdown_codec(&codec->dapm);
2951 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
2955 /* Module information */
2956 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2957 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2958 MODULE_LICENSE("GPL");