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_line] = 10,
73 [snd_soc_dapm_post] = 11,
76 static int dapm_down_seq[] = {
77 [snd_soc_dapm_pre] = 0,
78 [snd_soc_dapm_adc] = 1,
79 [snd_soc_dapm_hp] = 2,
80 [snd_soc_dapm_spk] = 2,
81 [snd_soc_dapm_line] = 2,
82 [snd_soc_dapm_out_drv] = 2,
83 [snd_soc_dapm_pga] = 4,
84 [snd_soc_dapm_mixer_named_ctl] = 5,
85 [snd_soc_dapm_mixer] = 5,
86 [snd_soc_dapm_dac] = 6,
87 [snd_soc_dapm_mic] = 7,
88 [snd_soc_dapm_micbias] = 8,
89 [snd_soc_dapm_mux] = 9,
90 [snd_soc_dapm_virt_mux] = 9,
91 [snd_soc_dapm_value_mux] = 9,
92 [snd_soc_dapm_aif_in] = 10,
93 [snd_soc_dapm_aif_out] = 10,
94 [snd_soc_dapm_supply] = 11,
95 [snd_soc_dapm_post] = 12,
98 static void pop_wait(u32 pop_time)
101 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
104 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
112 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
117 vsnprintf(buf, PAGE_SIZE, fmt, args);
118 dev_info(dev, "%s", buf);
124 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
126 return !list_empty(&w->dirty);
129 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
131 if (!dapm_dirty_widget(w)) {
132 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
134 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
137 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
139 /* create a new dapm widget */
140 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
141 const struct snd_soc_dapm_widget *_widget)
143 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
146 /* get snd_card from DAPM context */
147 static inline struct snd_card *dapm_get_snd_card(
148 struct snd_soc_dapm_context *dapm)
151 return dapm->codec->card->snd_card;
152 else if (dapm->platform)
153 return dapm->platform->card->snd_card;
161 /* get soc_card from DAPM context */
162 static inline struct snd_soc_card *dapm_get_soc_card(
163 struct snd_soc_dapm_context *dapm)
166 return dapm->codec->card;
167 else if (dapm->platform)
168 return dapm->platform->card;
176 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
179 return snd_soc_read(w->codec, reg);
180 else if (w->platform)
181 return snd_soc_platform_read(w->platform, reg);
183 dev_err(w->dapm->dev, "no valid widget read method\n");
187 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
190 return snd_soc_write(w->codec, reg, val);
191 else if (w->platform)
192 return snd_soc_platform_write(w->platform, reg, val);
194 dev_err(w->dapm->dev, "no valid widget write method\n");
198 static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
199 unsigned short reg, unsigned int mask, unsigned int value)
202 unsigned int old, new;
205 ret = soc_widget_read(w, reg);
210 new = (old & ~mask) | (value & mask);
213 ret = soc_widget_write(w, reg, new);
222 * snd_soc_dapm_set_bias_level - set the bias level for the system
223 * @dapm: DAPM context
224 * @level: level to configure
226 * Configure the bias (power) levels for the SoC audio device.
228 * Returns 0 for success else error.
230 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
231 enum snd_soc_bias_level level)
233 struct snd_soc_card *card = dapm->card;
236 trace_snd_soc_bias_level_start(card, level);
238 if (card && card->set_bias_level)
239 ret = card->set_bias_level(card, dapm, level);
244 if (dapm->codec->driver->set_bias_level)
245 ret = dapm->codec->driver->set_bias_level(dapm->codec,
248 dapm->bias_level = level;
253 if (card && card->set_bias_level_post)
254 ret = card->set_bias_level_post(card, dapm, level);
256 trace_snd_soc_bias_level_done(card, level);
261 /* set up initial codec paths */
262 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
263 struct snd_soc_dapm_path *p, int i)
266 case snd_soc_dapm_switch:
267 case snd_soc_dapm_mixer:
268 case snd_soc_dapm_mixer_named_ctl: {
270 struct soc_mixer_control *mc = (struct soc_mixer_control *)
271 w->kcontrol_news[i].private_value;
272 unsigned int reg = mc->reg;
273 unsigned int shift = mc->shift;
275 unsigned int mask = (1 << fls(max)) - 1;
276 unsigned int invert = mc->invert;
278 val = soc_widget_read(w, reg);
279 val = (val >> shift) & mask;
281 if ((invert && !val) || (!invert && val))
287 case snd_soc_dapm_mux: {
288 struct soc_enum *e = (struct soc_enum *)
289 w->kcontrol_news[i].private_value;
290 int val, item, bitmask;
292 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
294 val = soc_widget_read(w, e->reg);
295 item = (val >> e->shift_l) & (bitmask - 1);
298 for (i = 0; i < e->max; i++) {
299 if (!(strcmp(p->name, e->texts[i])) && item == i)
304 case snd_soc_dapm_virt_mux: {
305 struct soc_enum *e = (struct soc_enum *)
306 w->kcontrol_news[i].private_value;
309 /* since a virtual mux has no backing registers to
310 * decide which path to connect, it will try to match
311 * with the first enumeration. This is to ensure
312 * that the default mux choice (the first) will be
313 * correctly powered up during initialization.
315 if (!strcmp(p->name, e->texts[0]))
319 case snd_soc_dapm_value_mux: {
320 struct soc_enum *e = (struct soc_enum *)
321 w->kcontrol_news[i].private_value;
324 val = soc_widget_read(w, e->reg);
325 val = (val >> e->shift_l) & e->mask;
326 for (item = 0; item < e->max; item++) {
327 if (val == e->values[item])
332 for (i = 0; i < e->max; i++) {
333 if (!(strcmp(p->name, e->texts[i])) && item == i)
338 /* does not affect routing - always connected */
339 case snd_soc_dapm_pga:
340 case snd_soc_dapm_out_drv:
341 case snd_soc_dapm_output:
342 case snd_soc_dapm_adc:
343 case snd_soc_dapm_input:
344 case snd_soc_dapm_dac:
345 case snd_soc_dapm_micbias:
346 case snd_soc_dapm_vmid:
347 case snd_soc_dapm_supply:
348 case snd_soc_dapm_aif_in:
349 case snd_soc_dapm_aif_out:
350 case snd_soc_dapm_hp:
351 case snd_soc_dapm_mic:
352 case snd_soc_dapm_spk:
353 case snd_soc_dapm_line:
356 /* does affect routing - dynamically connected */
357 case snd_soc_dapm_pre:
358 case snd_soc_dapm_post:
364 /* connect mux widget to its interconnecting audio paths */
365 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
366 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
367 struct snd_soc_dapm_path *path, const char *control_name,
368 const struct snd_kcontrol_new *kcontrol)
370 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
373 for (i = 0; i < e->max; i++) {
374 if (!(strcmp(control_name, e->texts[i]))) {
375 list_add(&path->list, &dapm->card->paths);
376 list_add(&path->list_sink, &dest->sources);
377 list_add(&path->list_source, &src->sinks);
378 path->name = (char*)e->texts[i];
379 dapm_set_path_status(dest, path, 0);
387 /* connect mixer widget to its interconnecting audio paths */
388 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
389 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
390 struct snd_soc_dapm_path *path, const char *control_name)
394 /* search for mixer kcontrol */
395 for (i = 0; i < dest->num_kcontrols; i++) {
396 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
397 list_add(&path->list, &dapm->card->paths);
398 list_add(&path->list_sink, &dest->sources);
399 list_add(&path->list_source, &src->sinks);
400 path->name = dest->kcontrol_news[i].name;
401 dapm_set_path_status(dest, path, i);
408 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
409 struct snd_soc_dapm_widget *kcontrolw,
410 const struct snd_kcontrol_new *kcontrol_new,
411 struct snd_kcontrol **kcontrol)
413 struct snd_soc_dapm_widget *w;
418 list_for_each_entry(w, &dapm->card->widgets, list) {
419 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
421 for (i = 0; i < w->num_kcontrols; i++) {
422 if (&w->kcontrol_news[i] == kcontrol_new) {
424 *kcontrol = w->kcontrols[i];
433 /* create new dapm mixer control */
434 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
436 struct snd_soc_dapm_context *dapm = w->dapm;
438 size_t name_len, prefix_len;
439 struct snd_soc_dapm_path *path;
440 struct snd_card *card = dapm->card->snd_card;
442 struct snd_soc_dapm_widget_list *wlist;
446 prefix = dapm->codec->name_prefix;
451 prefix_len = strlen(prefix) + 1;
456 for (i = 0; i < w->num_kcontrols; i++) {
459 list_for_each_entry(path, &w->sources, list_sink) {
461 /* mixer/mux paths name must match control name */
462 if (path->name != (char *)w->kcontrol_news[i].name)
465 if (w->kcontrols[i]) {
466 path->kcontrol = w->kcontrols[i];
470 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
471 sizeof(struct snd_soc_dapm_widget *),
472 wlist = kzalloc(wlistsize, GFP_KERNEL);
475 "asoc: can't allocate widget list for %s\n",
479 wlist->num_widgets = 1;
480 wlist->widgets[0] = w;
482 /* add dapm control with long name.
483 * for dapm_mixer this is the concatenation of the
484 * mixer and kcontrol name.
485 * for dapm_mixer_named_ctl this is simply the
488 name_len = strlen(w->kcontrol_news[i].name) + 1;
489 if (w->id != snd_soc_dapm_mixer_named_ctl)
490 name_len += 1 + strlen(w->name);
492 path->long_name = kmalloc(name_len, GFP_KERNEL);
494 if (path->long_name == NULL) {
501 /* The control will get a prefix from
502 * the control creation process but
503 * we're also using the same prefix
504 * for widgets so cut the prefix off
505 * the front of the widget name.
507 snprintf(path->long_name, name_len, "%s %s",
508 w->name + prefix_len,
509 w->kcontrol_news[i].name);
511 case snd_soc_dapm_mixer_named_ctl:
512 snprintf(path->long_name, name_len, "%s",
513 w->kcontrol_news[i].name);
517 path->long_name[name_len - 1] = '\0';
519 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
520 wlist, path->long_name,
522 ret = snd_ctl_add(card, path->kcontrol);
525 "asoc: failed to add dapm kcontrol %s: %d\n",
526 path->long_name, ret);
528 kfree(path->long_name);
529 path->long_name = NULL;
532 w->kcontrols[i] = path->kcontrol;
538 /* create new dapm mux control */
539 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
541 struct snd_soc_dapm_context *dapm = w->dapm;
542 struct snd_soc_dapm_path *path = NULL;
543 struct snd_kcontrol *kcontrol;
544 struct snd_card *card = dapm->card->snd_card;
548 struct snd_soc_dapm_widget_list *wlist;
549 int shared, wlistentries;
553 if (w->num_kcontrols != 1) {
555 "asoc: mux %s has incorrect number of controls\n",
560 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
563 wlist = kcontrol->private_data;
564 wlistentries = wlist->num_widgets + 1;
569 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
570 wlistentries * sizeof(struct snd_soc_dapm_widget *),
571 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
574 "asoc: can't allocate widget list for %s\n", w->name);
577 wlist->num_widgets = wlistentries;
578 wlist->widgets[wlistentries - 1] = w;
582 prefix = dapm->codec->name_prefix;
587 name = w->kcontrol_news[0].name;
592 prefix_len = strlen(prefix) + 1;
598 * The control will get a prefix from the control creation
599 * process but we're also using the same prefix for widgets so
600 * cut the prefix off the front of the widget name.
602 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
603 name + prefix_len, prefix);
604 ret = snd_ctl_add(card, kcontrol);
606 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
613 kcontrol->private_data = wlist;
615 w->kcontrols[0] = kcontrol;
617 list_for_each_entry(path, &w->sources, list_sink)
618 path->kcontrol = kcontrol;
623 /* create new dapm volume control */
624 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
626 if (w->num_kcontrols)
627 dev_err(w->dapm->dev,
628 "asoc: PGA controls not supported: '%s'\n", w->name);
633 /* reset 'walked' bit for each dapm path */
634 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
636 struct snd_soc_dapm_path *p;
638 list_for_each_entry(p, &dapm->card->paths, list)
642 /* We implement power down on suspend by checking the power state of
643 * the ALSA card - when we are suspending the ALSA state for the card
646 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
648 int level = snd_power_get_state(widget->dapm->card->snd_card);
651 case SNDRV_CTL_POWER_D3hot:
652 case SNDRV_CTL_POWER_D3cold:
653 if (widget->ignore_suspend)
654 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
656 return widget->ignore_suspend;
663 * Recursively check for a completed path to an active or physically connected
664 * output widget. Returns number of complete paths.
666 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
668 struct snd_soc_dapm_path *path;
671 if (widget->outputs >= 0)
672 return widget->outputs;
674 DAPM_UPDATE_STAT(widget, path_checks);
676 if (widget->id == snd_soc_dapm_supply)
679 switch (widget->id) {
680 case snd_soc_dapm_adc:
681 case snd_soc_dapm_aif_out:
682 if (widget->active) {
683 widget->outputs = snd_soc_dapm_suspend_check(widget);
684 return widget->outputs;
690 if (widget->connected) {
691 /* connected pin ? */
692 if (widget->id == snd_soc_dapm_output && !widget->ext) {
693 widget->outputs = snd_soc_dapm_suspend_check(widget);
694 return widget->outputs;
697 /* connected jack or spk ? */
698 if (widget->id == snd_soc_dapm_hp ||
699 widget->id == snd_soc_dapm_spk ||
700 (widget->id == snd_soc_dapm_line &&
701 !list_empty(&widget->sources))) {
702 widget->outputs = snd_soc_dapm_suspend_check(widget);
703 return widget->outputs;
707 list_for_each_entry(path, &widget->sinks, list_source) {
708 DAPM_UPDATE_STAT(widget, neighbour_checks);
716 if (path->sink && path->connect) {
718 con += is_connected_output_ep(path->sink);
722 widget->outputs = con;
728 * Recursively check for a completed path to an active or physically connected
729 * input widget. Returns number of complete paths.
731 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
733 struct snd_soc_dapm_path *path;
736 if (widget->inputs >= 0)
737 return widget->inputs;
739 DAPM_UPDATE_STAT(widget, path_checks);
741 if (widget->id == snd_soc_dapm_supply)
744 /* active stream ? */
745 switch (widget->id) {
746 case snd_soc_dapm_dac:
747 case snd_soc_dapm_aif_in:
748 if (widget->active) {
749 widget->inputs = snd_soc_dapm_suspend_check(widget);
750 return widget->inputs;
756 if (widget->connected) {
757 /* connected pin ? */
758 if (widget->id == snd_soc_dapm_input && !widget->ext) {
759 widget->inputs = snd_soc_dapm_suspend_check(widget);
760 return widget->inputs;
763 /* connected VMID/Bias for lower pops */
764 if (widget->id == snd_soc_dapm_vmid) {
765 widget->inputs = snd_soc_dapm_suspend_check(widget);
766 return widget->inputs;
769 /* connected jack ? */
770 if (widget->id == snd_soc_dapm_mic ||
771 (widget->id == snd_soc_dapm_line &&
772 !list_empty(&widget->sinks))) {
773 widget->inputs = snd_soc_dapm_suspend_check(widget);
774 return widget->inputs;
779 list_for_each_entry(path, &widget->sources, list_sink) {
780 DAPM_UPDATE_STAT(widget, neighbour_checks);
788 if (path->source && path->connect) {
790 con += is_connected_input_ep(path->source);
794 widget->inputs = con;
800 * Handler for generic register modifier widget.
802 int dapm_reg_event(struct snd_soc_dapm_widget *w,
803 struct snd_kcontrol *kcontrol, int event)
807 if (SND_SOC_DAPM_EVENT_ON(event))
812 soc_widget_update_bits(w, -(w->reg + 1),
813 w->mask << w->shift, val << w->shift);
817 EXPORT_SYMBOL_GPL(dapm_reg_event);
819 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
821 if (w->power_checked)
827 w->new_power = w->power_check(w);
829 w->power_checked = true;
834 /* Generic check to see if a widget should be powered.
836 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
840 DAPM_UPDATE_STAT(w, power_checks);
842 in = is_connected_input_ep(w);
843 dapm_clear_walk(w->dapm);
844 out = is_connected_output_ep(w);
845 dapm_clear_walk(w->dapm);
846 return out != 0 && in != 0;
849 /* Check to see if an ADC has power */
850 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
854 DAPM_UPDATE_STAT(w, power_checks);
857 in = is_connected_input_ep(w);
858 dapm_clear_walk(w->dapm);
861 return dapm_generic_check_power(w);
865 /* Check to see if a DAC has power */
866 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
870 DAPM_UPDATE_STAT(w, power_checks);
873 out = is_connected_output_ep(w);
874 dapm_clear_walk(w->dapm);
877 return dapm_generic_check_power(w);
881 /* Check to see if a power supply is needed */
882 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
884 struct snd_soc_dapm_path *path;
886 DAPM_UPDATE_STAT(w, power_checks);
888 /* Check if one of our outputs is connected */
889 list_for_each_entry(path, &w->sinks, list_source) {
890 DAPM_UPDATE_STAT(w, neighbour_checks);
895 if (path->connected &&
896 !path->connected(path->source, path->sink))
902 if (dapm_widget_power_check(path->sink))
906 dapm_clear_walk(w->dapm);
911 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
916 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
917 struct snd_soc_dapm_widget *b,
925 sort = dapm_down_seq;
927 if (sort[a->id] != sort[b->id])
928 return sort[a->id] - sort[b->id];
929 if (a->subseq != b->subseq) {
931 return a->subseq - b->subseq;
933 return b->subseq - a->subseq;
935 if (a->reg != b->reg)
936 return a->reg - b->reg;
937 if (a->dapm != b->dapm)
938 return (unsigned long)a->dapm - (unsigned long)b->dapm;
943 /* Insert a widget in order into a DAPM power sequence. */
944 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
945 struct list_head *list,
948 struct snd_soc_dapm_widget *w;
950 list_for_each_entry(w, list, power_list)
951 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
952 list_add_tail(&new_widget->power_list, &w->power_list);
956 list_add_tail(&new_widget->power_list, list);
959 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
960 struct snd_soc_dapm_widget *w, int event)
962 struct snd_soc_card *card = dapm->card;
967 case SND_SOC_DAPM_PRE_PMU:
971 case SND_SOC_DAPM_POST_PMU:
972 ev_name = "POST_PMU";
975 case SND_SOC_DAPM_PRE_PMD:
979 case SND_SOC_DAPM_POST_PMD:
980 ev_name = "POST_PMD";
988 if (w->power != power)
991 if (w->event && (w->event_flags & event)) {
992 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
994 trace_snd_soc_dapm_widget_event_start(w, event);
995 ret = w->event(w, NULL, event);
996 trace_snd_soc_dapm_widget_event_done(w, event);
998 pr_err("%s: %s event failed: %d\n",
999 ev_name, w->name, ret);
1003 /* Apply the coalesced changes from a DAPM sequence */
1004 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1005 struct list_head *pending)
1007 struct snd_soc_card *card = dapm->card;
1008 struct snd_soc_dapm_widget *w;
1010 unsigned int value = 0;
1011 unsigned int mask = 0;
1012 unsigned int cur_mask;
1014 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1017 list_for_each_entry(w, pending, power_list) {
1018 cur_mask = 1 << w->shift;
1019 BUG_ON(reg != w->reg);
1030 pop_dbg(dapm->dev, card->pop_time,
1031 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1032 w->name, reg, value, mask);
1034 /* Check for events */
1035 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1036 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1040 /* Any widget will do, they should all be updating the
1043 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1046 pop_dbg(dapm->dev, card->pop_time,
1047 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1048 value, mask, reg, card->pop_time);
1049 pop_wait(card->pop_time);
1050 soc_widget_update_bits(w, reg, mask, value);
1053 list_for_each_entry(w, pending, power_list) {
1054 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1055 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1059 /* Apply a DAPM power sequence.
1061 * We walk over a pre-sorted list of widgets to apply power to. In
1062 * order to minimise the number of writes to the device required
1063 * multiple widgets will be updated in a single write where possible.
1064 * Currently anything that requires more than a single write is not
1067 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1068 struct list_head *list, int event, bool power_up)
1070 struct snd_soc_dapm_widget *w, *n;
1073 int cur_subseq = -1;
1074 int cur_reg = SND_SOC_NOPM;
1075 struct snd_soc_dapm_context *cur_dapm = NULL;
1082 sort = dapm_down_seq;
1084 list_for_each_entry_safe(w, n, list, power_list) {
1087 /* Do we need to apply any queued changes? */
1088 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1089 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1090 if (!list_empty(&pending))
1091 dapm_seq_run_coalesced(cur_dapm, &pending);
1093 if (cur_dapm && cur_dapm->seq_notifier) {
1094 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1095 if (sort[i] == cur_sort)
1096 cur_dapm->seq_notifier(cur_dapm,
1101 INIT_LIST_HEAD(&pending);
1103 cur_subseq = INT_MIN;
1104 cur_reg = SND_SOC_NOPM;
1109 case snd_soc_dapm_pre:
1111 list_for_each_entry_safe_continue(w, n, list,
1114 if (event == SND_SOC_DAPM_STREAM_START)
1116 NULL, SND_SOC_DAPM_PRE_PMU);
1117 else if (event == SND_SOC_DAPM_STREAM_STOP)
1119 NULL, SND_SOC_DAPM_PRE_PMD);
1122 case snd_soc_dapm_post:
1124 list_for_each_entry_safe_continue(w, n, list,
1127 if (event == SND_SOC_DAPM_STREAM_START)
1129 NULL, SND_SOC_DAPM_POST_PMU);
1130 else if (event == SND_SOC_DAPM_STREAM_STOP)
1132 NULL, SND_SOC_DAPM_POST_PMD);
1136 /* Queue it up for application */
1137 cur_sort = sort[w->id];
1138 cur_subseq = w->subseq;
1141 list_move(&w->power_list, &pending);
1146 dev_err(w->dapm->dev,
1147 "Failed to apply widget power: %d\n", ret);
1150 if (!list_empty(&pending))
1151 dapm_seq_run_coalesced(cur_dapm, &pending);
1153 if (cur_dapm && cur_dapm->seq_notifier) {
1154 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1155 if (sort[i] == cur_sort)
1156 cur_dapm->seq_notifier(cur_dapm,
1161 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1163 struct snd_soc_dapm_update *update = dapm->update;
1164 struct snd_soc_dapm_widget *w;
1173 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1174 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1176 pr_err("%s DAPM pre-event failed: %d\n",
1180 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1183 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1186 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1187 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1189 pr_err("%s DAPM post-event failed: %d\n",
1194 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1195 * they're changing state.
1197 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1199 struct snd_soc_dapm_context *d = data;
1202 /* If we're off and we're not supposed to be go into STANDBY */
1203 if (d->bias_level == SND_SOC_BIAS_OFF &&
1204 d->target_bias_level != SND_SOC_BIAS_OFF) {
1205 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1208 "Failed to turn on bias: %d\n", ret);
1211 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1212 if (d->bias_level != d->target_bias_level) {
1213 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1216 "Failed to prepare bias: %d\n", ret);
1220 /* Async callback run prior to DAPM sequences - brings to their final
1223 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1225 struct snd_soc_dapm_context *d = data;
1228 /* If we just powered the last thing off drop to standby bias */
1229 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1230 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1231 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1232 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1234 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1238 /* If we're in standby and can support bias off then do that */
1239 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1240 d->target_bias_level == SND_SOC_BIAS_OFF) {
1241 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1243 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1246 /* If we just powered up then move to active bias */
1247 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1248 d->target_bias_level == SND_SOC_BIAS_ON) {
1249 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1251 dev_err(d->dev, "Failed to apply active bias: %d\n",
1256 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1257 bool power, bool connect)
1259 /* If a connection is being made or broken then that update
1260 * will have marked the peer dirty, otherwise the widgets are
1261 * not connected and this update has no impact. */
1265 /* If the peer is already in the state we're moving to then we
1266 * won't have an impact on it. */
1267 if (power != peer->power)
1268 dapm_mark_dirty(peer, "peer state change");
1271 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1272 struct list_head *up_list,
1273 struct list_head *down_list)
1275 struct snd_soc_dapm_path *path;
1277 if (w->power == power)
1280 trace_snd_soc_dapm_widget_power(w, power);
1282 /* If we changed our power state perhaps our neigbours changed
1285 list_for_each_entry(path, &w->sources, list_sink) {
1287 dapm_widget_set_peer_power(path->source, power,
1292 case snd_soc_dapm_supply:
1293 /* Supplies can't affect their outputs, only their inputs */
1296 list_for_each_entry(path, &w->sinks, list_source) {
1298 dapm_widget_set_peer_power(path->sink, power,
1306 dapm_seq_insert(w, up_list, true);
1308 dapm_seq_insert(w, down_list, false);
1313 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1314 struct list_head *up_list,
1315 struct list_head *down_list)
1320 case snd_soc_dapm_pre:
1321 dapm_seq_insert(w, down_list, false);
1323 case snd_soc_dapm_post:
1324 dapm_seq_insert(w, up_list, true);
1328 power = dapm_widget_power_check(w);
1330 dapm_widget_set_power(w, power, up_list, down_list);
1336 * Scan each dapm widget for complete audio path.
1337 * A complete path is a route that has valid endpoints i.e.:-
1339 * o DAC to output pin.
1340 * o Input Pin to ADC.
1341 * o Input pin to Output pin (bypass, sidetone)
1342 * o DAC to ADC (loopback).
1344 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1346 struct snd_soc_card *card = dapm->card;
1347 struct snd_soc_dapm_widget *w;
1348 struct snd_soc_dapm_context *d;
1350 LIST_HEAD(down_list);
1351 LIST_HEAD(async_domain);
1352 enum snd_soc_bias_level bias;
1354 trace_snd_soc_dapm_start(card);
1356 list_for_each_entry(d, &card->dapm_list, list) {
1357 if (d->n_widgets || d->codec == NULL) {
1358 if (d->idle_bias_off)
1359 d->target_bias_level = SND_SOC_BIAS_OFF;
1361 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1365 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1367 list_for_each_entry(w, &card->widgets, list) {
1368 w->power_checked = false;
1373 /* Check which widgets we need to power and store them in
1374 * lists indicating if they should be powered up or down. We
1375 * only check widgets that have been flagged as dirty but note
1376 * that new widgets may be added to the dirty list while we
1379 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1380 dapm_power_one_widget(w, &up_list, &down_list);
1383 list_for_each_entry(w, &card->widgets, list) {
1385 case snd_soc_dapm_pre:
1386 case snd_soc_dapm_post:
1387 /* These widgets always need to be powered */
1390 list_del_init(&w->dirty);
1397 /* Supplies and micbiases only bring the
1398 * context up to STANDBY as unless something
1399 * else is active and passing audio they
1400 * generally don't require full power.
1403 case snd_soc_dapm_supply:
1404 case snd_soc_dapm_micbias:
1405 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1406 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1409 d->target_bias_level = SND_SOC_BIAS_ON;
1416 /* If there are no DAPM widgets then try to figure out power from the
1419 if (!dapm->n_widgets) {
1421 case SND_SOC_DAPM_STREAM_START:
1422 case SND_SOC_DAPM_STREAM_RESUME:
1423 dapm->target_bias_level = SND_SOC_BIAS_ON;
1425 case SND_SOC_DAPM_STREAM_STOP:
1426 if (dapm->codec->active)
1427 dapm->target_bias_level = SND_SOC_BIAS_ON;
1429 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1431 case SND_SOC_DAPM_STREAM_SUSPEND:
1432 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1434 case SND_SOC_DAPM_STREAM_NOP:
1435 dapm->target_bias_level = dapm->bias_level;
1442 /* Force all contexts in the card to the same bias state if
1443 * they're not ground referenced.
1445 bias = SND_SOC_BIAS_OFF;
1446 list_for_each_entry(d, &card->dapm_list, list)
1447 if (d->target_bias_level > bias)
1448 bias = d->target_bias_level;
1449 list_for_each_entry(d, &card->dapm_list, list)
1450 if (!d->idle_bias_off)
1451 d->target_bias_level = bias;
1453 trace_snd_soc_dapm_walk_done(card);
1455 /* Run all the bias changes in parallel */
1456 list_for_each_entry(d, &dapm->card->dapm_list, list)
1457 async_schedule_domain(dapm_pre_sequence_async, d,
1459 async_synchronize_full_domain(&async_domain);
1461 /* Power down widgets first; try to avoid amplifying pops. */
1462 dapm_seq_run(dapm, &down_list, event, false);
1464 dapm_widget_update(dapm);
1467 dapm_seq_run(dapm, &up_list, event, true);
1469 /* Run all the bias changes in parallel */
1470 list_for_each_entry(d, &dapm->card->dapm_list, list)
1471 async_schedule_domain(dapm_post_sequence_async, d,
1473 async_synchronize_full_domain(&async_domain);
1475 pop_dbg(dapm->dev, card->pop_time,
1476 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1477 pop_wait(card->pop_time);
1479 trace_snd_soc_dapm_done(card);
1484 #ifdef CONFIG_DEBUG_FS
1485 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1487 file->private_data = inode->i_private;
1491 static ssize_t dapm_widget_power_read_file(struct file *file,
1492 char __user *user_buf,
1493 size_t count, loff_t *ppos)
1495 struct snd_soc_dapm_widget *w = file->private_data;
1499 struct snd_soc_dapm_path *p = NULL;
1501 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1505 in = is_connected_input_ep(w);
1506 dapm_clear_walk(w->dapm);
1507 out = is_connected_output_ep(w);
1508 dapm_clear_walk(w->dapm);
1510 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1511 w->name, w->power ? "On" : "Off", in, out);
1514 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1515 " - R%d(0x%x) bit %d",
1516 w->reg, w->reg, w->shift);
1518 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1521 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1523 w->active ? "active" : "inactive");
1525 list_for_each_entry(p, &w->sources, list_sink) {
1526 if (p->connected && !p->connected(w, p->sink))
1530 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1531 " in \"%s\" \"%s\"\n",
1532 p->name ? p->name : "static",
1535 list_for_each_entry(p, &w->sinks, list_source) {
1536 if (p->connected && !p->connected(w, p->sink))
1540 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1541 " out \"%s\" \"%s\"\n",
1542 p->name ? p->name : "static",
1546 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1552 static const struct file_operations dapm_widget_power_fops = {
1553 .open = dapm_widget_power_open_file,
1554 .read = dapm_widget_power_read_file,
1555 .llseek = default_llseek,
1558 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1560 file->private_data = inode->i_private;
1564 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1565 size_t count, loff_t *ppos)
1567 struct snd_soc_dapm_context *dapm = file->private_data;
1570 switch (dapm->bias_level) {
1571 case SND_SOC_BIAS_ON:
1574 case SND_SOC_BIAS_PREPARE:
1575 level = "Prepare\n";
1577 case SND_SOC_BIAS_STANDBY:
1578 level = "Standby\n";
1580 case SND_SOC_BIAS_OFF:
1585 level = "Unknown\n";
1589 return simple_read_from_buffer(user_buf, count, ppos, level,
1593 static const struct file_operations dapm_bias_fops = {
1594 .open = dapm_bias_open_file,
1595 .read = dapm_bias_read_file,
1596 .llseek = default_llseek,
1599 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1600 struct dentry *parent)
1604 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1606 if (!dapm->debugfs_dapm) {
1608 "Failed to create DAPM debugfs directory\n");
1612 d = debugfs_create_file("bias_level", 0444,
1613 dapm->debugfs_dapm, dapm,
1617 "ASoC: Failed to create bias level debugfs file\n");
1620 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1622 struct snd_soc_dapm_context *dapm = w->dapm;
1625 if (!dapm->debugfs_dapm || !w->name)
1628 d = debugfs_create_file(w->name, 0444,
1629 dapm->debugfs_dapm, w,
1630 &dapm_widget_power_fops);
1632 dev_warn(w->dapm->dev,
1633 "ASoC: Failed to create %s debugfs file\n",
1637 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1639 debugfs_remove_recursive(dapm->debugfs_dapm);
1643 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1644 struct dentry *parent)
1648 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1652 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1658 /* test and update the power status of a mux widget */
1659 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1660 struct snd_kcontrol *kcontrol, int change,
1661 int mux, struct soc_enum *e)
1663 struct snd_soc_dapm_path *path;
1666 if (widget->id != snd_soc_dapm_mux &&
1667 widget->id != snd_soc_dapm_virt_mux &&
1668 widget->id != snd_soc_dapm_value_mux)
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 if (!path->name || !e->texts[mux])
1683 /* we now need to match the string in the enum to the path */
1684 if (!(strcmp(path->name, e->texts[mux]))) {
1685 path->connect = 1; /* new connection */
1686 dapm_mark_dirty(path->source, "mux connection");
1689 dapm_mark_dirty(path->source,
1690 "mux disconnection");
1691 path->connect = 0; /* old connection must be powered down */
1696 dapm_mark_dirty(widget, "mux change");
1697 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1703 /* test and update the power status of a mixer or switch widget */
1704 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1705 struct snd_kcontrol *kcontrol, int connect)
1707 struct snd_soc_dapm_path *path;
1710 if (widget->id != snd_soc_dapm_mixer &&
1711 widget->id != snd_soc_dapm_mixer_named_ctl &&
1712 widget->id != snd_soc_dapm_switch)
1715 /* find dapm widget path assoc with kcontrol */
1716 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1717 if (path->kcontrol != kcontrol)
1720 /* found, now check type */
1722 path->connect = connect;
1723 dapm_mark_dirty(path->source, "mixer connection");
1727 dapm_mark_dirty(widget, "mixer update");
1728 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1734 /* show dapm widget status in sys fs */
1735 static ssize_t dapm_widget_show(struct device *dev,
1736 struct device_attribute *attr, char *buf)
1738 struct snd_soc_pcm_runtime *rtd =
1739 container_of(dev, struct snd_soc_pcm_runtime, dev);
1740 struct snd_soc_codec *codec =rtd->codec;
1741 struct snd_soc_dapm_widget *w;
1743 char *state = "not set";
1745 list_for_each_entry(w, &codec->card->widgets, list) {
1746 if (w->dapm != &codec->dapm)
1749 /* only display widgets that burnm power */
1751 case snd_soc_dapm_hp:
1752 case snd_soc_dapm_mic:
1753 case snd_soc_dapm_spk:
1754 case snd_soc_dapm_line:
1755 case snd_soc_dapm_micbias:
1756 case snd_soc_dapm_dac:
1757 case snd_soc_dapm_adc:
1758 case snd_soc_dapm_pga:
1759 case snd_soc_dapm_out_drv:
1760 case snd_soc_dapm_mixer:
1761 case snd_soc_dapm_mixer_named_ctl:
1762 case snd_soc_dapm_supply:
1764 count += sprintf(buf + count, "%s: %s\n",
1765 w->name, w->power ? "On":"Off");
1772 switch (codec->dapm.bias_level) {
1773 case SND_SOC_BIAS_ON:
1776 case SND_SOC_BIAS_PREPARE:
1779 case SND_SOC_BIAS_STANDBY:
1782 case SND_SOC_BIAS_OFF:
1786 count += sprintf(buf + count, "PM State: %s\n", state);
1791 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1793 int snd_soc_dapm_sys_add(struct device *dev)
1795 return device_create_file(dev, &dev_attr_dapm_widget);
1798 static void snd_soc_dapm_sys_remove(struct device *dev)
1800 device_remove_file(dev, &dev_attr_dapm_widget);
1803 /* free all dapm widgets and resources */
1804 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1806 struct snd_soc_dapm_widget *w, *next_w;
1807 struct snd_soc_dapm_path *p, *next_p;
1809 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1810 if (w->dapm != dapm)
1814 * remove source and sink paths associated to this widget.
1815 * While removing the path, remove reference to it from both
1816 * source and sink widgets so that path is removed only once.
1818 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1819 list_del(&p->list_sink);
1820 list_del(&p->list_source);
1822 kfree(p->long_name);
1825 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1826 list_del(&p->list_sink);
1827 list_del(&p->list_source);
1829 kfree(p->long_name);
1832 kfree(w->kcontrols);
1838 static struct snd_soc_dapm_widget *dapm_find_widget(
1839 struct snd_soc_dapm_context *dapm, const char *pin,
1840 bool search_other_contexts)
1842 struct snd_soc_dapm_widget *w;
1843 struct snd_soc_dapm_widget *fallback = NULL;
1845 list_for_each_entry(w, &dapm->card->widgets, list) {
1846 if (!strcmp(w->name, pin)) {
1847 if (w->dapm == dapm)
1854 if (search_other_contexts)
1860 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1861 const char *pin, int status)
1863 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1866 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1870 w->connected = status;
1873 dapm_mark_dirty(w, "pin configuration");
1879 * snd_soc_dapm_sync - scan and power dapm paths
1880 * @dapm: DAPM context
1882 * Walks all dapm audio paths and powers widgets according to their
1883 * stream or path usage.
1885 * Returns 0 for success.
1887 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1890 * Suppress early reports (eg, jacks syncing their state) to avoid
1891 * silly DAPM runs during card startup.
1893 if (!dapm->card || !dapm->card->instantiated)
1896 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1898 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1900 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1901 const struct snd_soc_dapm_route *route)
1903 struct snd_soc_dapm_path *path;
1904 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1905 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1907 const char *control = route->control;
1909 char prefixed_sink[80];
1910 char prefixed_source[80];
1913 if (dapm->codec && dapm->codec->name_prefix) {
1914 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1915 dapm->codec->name_prefix, route->sink);
1916 sink = prefixed_sink;
1917 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1918 dapm->codec->name_prefix, route->source);
1919 source = prefixed_source;
1922 source = route->source;
1926 * find src and dest widgets over all widgets but favor a widget from
1927 * current DAPM context
1929 list_for_each_entry(w, &dapm->card->widgets, list) {
1930 if (!wsink && !(strcmp(w->name, sink))) {
1932 if (w->dapm == dapm)
1936 if (!wsource && !(strcmp(w->name, source))) {
1938 if (w->dapm == dapm)
1942 /* use widget from another DAPM context if not found from this */
1948 if (wsource == NULL || wsink == NULL)
1951 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1955 path->source = wsource;
1957 path->connected = route->connected;
1958 INIT_LIST_HEAD(&path->list);
1959 INIT_LIST_HEAD(&path->list_source);
1960 INIT_LIST_HEAD(&path->list_sink);
1962 /* check for external widgets */
1963 if (wsink->id == snd_soc_dapm_input) {
1964 if (wsource->id == snd_soc_dapm_micbias ||
1965 wsource->id == snd_soc_dapm_mic ||
1966 wsource->id == snd_soc_dapm_line ||
1967 wsource->id == snd_soc_dapm_output)
1970 if (wsource->id == snd_soc_dapm_output) {
1971 if (wsink->id == snd_soc_dapm_spk ||
1972 wsink->id == snd_soc_dapm_hp ||
1973 wsink->id == snd_soc_dapm_line ||
1974 wsink->id == snd_soc_dapm_input)
1978 /* connect static paths */
1979 if (control == NULL) {
1980 list_add(&path->list, &dapm->card->paths);
1981 list_add(&path->list_sink, &wsink->sources);
1982 list_add(&path->list_source, &wsource->sinks);
1987 /* connect dynamic paths */
1988 switch (wsink->id) {
1989 case snd_soc_dapm_adc:
1990 case snd_soc_dapm_dac:
1991 case snd_soc_dapm_pga:
1992 case snd_soc_dapm_out_drv:
1993 case snd_soc_dapm_input:
1994 case snd_soc_dapm_output:
1995 case snd_soc_dapm_micbias:
1996 case snd_soc_dapm_vmid:
1997 case snd_soc_dapm_pre:
1998 case snd_soc_dapm_post:
1999 case snd_soc_dapm_supply:
2000 case snd_soc_dapm_aif_in:
2001 case snd_soc_dapm_aif_out:
2002 list_add(&path->list, &dapm->card->paths);
2003 list_add(&path->list_sink, &wsink->sources);
2004 list_add(&path->list_source, &wsource->sinks);
2007 case snd_soc_dapm_mux:
2008 case snd_soc_dapm_virt_mux:
2009 case snd_soc_dapm_value_mux:
2010 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2011 &wsink->kcontrol_news[0]);
2015 case snd_soc_dapm_switch:
2016 case snd_soc_dapm_mixer:
2017 case snd_soc_dapm_mixer_named_ctl:
2018 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2022 case snd_soc_dapm_hp:
2023 case snd_soc_dapm_mic:
2024 case snd_soc_dapm_line:
2025 case snd_soc_dapm_spk:
2026 list_add(&path->list, &dapm->card->paths);
2027 list_add(&path->list_sink, &wsink->sources);
2028 list_add(&path->list_source, &wsource->sinks);
2035 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2036 source, control, sink);
2042 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2043 * @dapm: DAPM context
2044 * @route: audio routes
2045 * @num: number of routes
2047 * Connects 2 dapm widgets together via a named audio path. The sink is
2048 * the widget receiving the audio signal, whilst the source is the sender
2049 * of the audio signal.
2051 * Returns 0 for success else error. On error all resources can be freed
2052 * with a call to snd_soc_card_free().
2054 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2055 const struct snd_soc_dapm_route *route, int num)
2059 for (i = 0; i < num; i++) {
2060 ret = snd_soc_dapm_add_route(dapm, route);
2062 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2063 route->source, route->sink);
2071 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2073 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2074 const struct snd_soc_dapm_route *route)
2076 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2079 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2082 struct snd_soc_dapm_path *path;
2086 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2092 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2097 if (route->control || route->connected)
2098 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2099 route->source, route->sink);
2101 list_for_each_entry(path, &source->sinks, list_source) {
2102 if (path->sink == sink) {
2109 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2110 route->source, route->sink);
2112 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2113 count, route->source, route->sink);
2119 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2120 * @dapm: DAPM context
2121 * @route: audio routes
2122 * @num: number of routes
2124 * Mark existing routes matching those specified in the passed array
2125 * as being weak, meaning that they are ignored for the purpose of
2126 * power decisions. The main intended use case is for sidetone paths
2127 * which couple audio between other independent paths if they are both
2128 * active in order to make the combination work better at the user
2129 * level but which aren't intended to be "used".
2131 * Note that CODEC drivers should not use this as sidetone type paths
2132 * can frequently also be used as bypass paths.
2134 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2135 const struct snd_soc_dapm_route *route, int num)
2140 for (i = 0; i < num; i++) {
2141 err = snd_soc_dapm_weak_route(dapm, route);
2149 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2152 * snd_soc_dapm_new_widgets - add new dapm widgets
2153 * @dapm: DAPM context
2155 * Checks the codec for any new dapm widgets and creates them if found.
2157 * Returns 0 for success.
2159 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2161 struct snd_soc_dapm_widget *w;
2164 list_for_each_entry(w, &dapm->card->widgets, list)
2169 if (w->num_kcontrols) {
2170 w->kcontrols = kzalloc(w->num_kcontrols *
2171 sizeof(struct snd_kcontrol *),
2178 case snd_soc_dapm_switch:
2179 case snd_soc_dapm_mixer:
2180 case snd_soc_dapm_mixer_named_ctl:
2183 case snd_soc_dapm_mux:
2184 case snd_soc_dapm_virt_mux:
2185 case snd_soc_dapm_value_mux:
2188 case snd_soc_dapm_pga:
2189 case snd_soc_dapm_out_drv:
2196 /* Read the initial power state from the device */
2198 val = soc_widget_read(w, w->reg);
2199 val &= 1 << w->shift;
2209 dapm_mark_dirty(w, "new widget");
2210 dapm_debugfs_add_widget(w);
2213 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2216 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2219 * snd_soc_dapm_get_volsw - dapm mixer get callback
2220 * @kcontrol: mixer control
2221 * @ucontrol: control element information
2223 * Callback to get the value of a dapm mixer control.
2225 * Returns 0 for success.
2227 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
2230 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2231 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2232 struct soc_mixer_control *mc =
2233 (struct soc_mixer_control *)kcontrol->private_value;
2234 unsigned int reg = mc->reg;
2235 unsigned int shift = mc->shift;
2236 unsigned int rshift = mc->rshift;
2238 unsigned int invert = mc->invert;
2239 unsigned int mask = (1 << fls(max)) - 1;
2241 ucontrol->value.integer.value[0] =
2242 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2243 if (shift != rshift)
2244 ucontrol->value.integer.value[1] =
2245 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2247 ucontrol->value.integer.value[0] =
2248 max - ucontrol->value.integer.value[0];
2249 if (shift != rshift)
2250 ucontrol->value.integer.value[1] =
2251 max - ucontrol->value.integer.value[1];
2256 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2259 * snd_soc_dapm_put_volsw - dapm mixer set callback
2260 * @kcontrol: mixer control
2261 * @ucontrol: control element information
2263 * Callback to set the value of a dapm mixer control.
2265 * Returns 0 for success.
2267 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2268 struct snd_ctl_elem_value *ucontrol)
2270 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2271 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2272 struct snd_soc_codec *codec = widget->codec;
2273 struct soc_mixer_control *mc =
2274 (struct soc_mixer_control *)kcontrol->private_value;
2275 unsigned int reg = mc->reg;
2276 unsigned int shift = mc->shift;
2278 unsigned int mask = (1 << fls(max)) - 1;
2279 unsigned int invert = mc->invert;
2281 int connect, change;
2282 struct snd_soc_dapm_update update;
2285 val = (ucontrol->value.integer.value[0] & mask);
2289 mask = mask << shift;
2293 /* new connection */
2294 connect = invert ? 0 : 1;
2296 /* old connection must be powered down */
2297 connect = invert ? 1 : 0;
2299 mutex_lock(&codec->mutex);
2301 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2303 for (wi = 0; wi < wlist->num_widgets; wi++) {
2304 widget = wlist->widgets[wi];
2306 widget->value = val;
2308 update.kcontrol = kcontrol;
2309 update.widget = widget;
2313 widget->dapm->update = &update;
2315 dapm_mixer_update_power(widget, kcontrol, connect);
2317 widget->dapm->update = NULL;
2321 mutex_unlock(&codec->mutex);
2324 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2327 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2328 * @kcontrol: mixer control
2329 * @ucontrol: control element information
2331 * Callback to get the value of a dapm enumerated double mixer control.
2333 * Returns 0 for success.
2335 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_value *ucontrol)
2338 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2339 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2340 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2341 unsigned int val, bitmask;
2343 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2345 val = snd_soc_read(widget->codec, e->reg);
2346 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2347 if (e->shift_l != e->shift_r)
2348 ucontrol->value.enumerated.item[1] =
2349 (val >> e->shift_r) & (bitmask - 1);
2353 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2356 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2357 * @kcontrol: mixer control
2358 * @ucontrol: control element information
2360 * Callback to set the value of a dapm enumerated double mixer control.
2362 * Returns 0 for success.
2364 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2365 struct snd_ctl_elem_value *ucontrol)
2367 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2368 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2369 struct snd_soc_codec *codec = widget->codec;
2370 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2371 unsigned int val, mux, change;
2372 unsigned int mask, bitmask;
2373 struct snd_soc_dapm_update update;
2376 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2378 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2380 mux = ucontrol->value.enumerated.item[0];
2381 val = mux << e->shift_l;
2382 mask = (bitmask - 1) << e->shift_l;
2383 if (e->shift_l != e->shift_r) {
2384 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2386 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2387 mask |= (bitmask - 1) << e->shift_r;
2390 mutex_lock(&codec->mutex);
2392 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2394 for (wi = 0; wi < wlist->num_widgets; wi++) {
2395 widget = wlist->widgets[wi];
2397 widget->value = val;
2399 update.kcontrol = kcontrol;
2400 update.widget = widget;
2401 update.reg = e->reg;
2404 widget->dapm->update = &update;
2406 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2408 widget->dapm->update = NULL;
2412 mutex_unlock(&codec->mutex);
2415 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2418 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2419 * @kcontrol: mixer control
2420 * @ucontrol: control element information
2422 * Returns 0 for success.
2424 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2425 struct snd_ctl_elem_value *ucontrol)
2427 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2428 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2430 ucontrol->value.enumerated.item[0] = widget->value;
2434 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2437 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2438 * @kcontrol: mixer control
2439 * @ucontrol: control element information
2441 * Returns 0 for success.
2443 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2444 struct snd_ctl_elem_value *ucontrol)
2446 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2447 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2448 struct snd_soc_codec *codec = widget->codec;
2449 struct soc_enum *e =
2450 (struct soc_enum *)kcontrol->private_value;
2455 if (ucontrol->value.enumerated.item[0] >= e->max)
2458 mutex_lock(&codec->mutex);
2460 change = widget->value != ucontrol->value.enumerated.item[0];
2462 for (wi = 0; wi < wlist->num_widgets; wi++) {
2463 widget = wlist->widgets[wi];
2465 widget->value = ucontrol->value.enumerated.item[0];
2467 dapm_mux_update_power(widget, kcontrol, change,
2472 mutex_unlock(&codec->mutex);
2475 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2478 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2480 * @kcontrol: mixer control
2481 * @ucontrol: control element information
2483 * Callback to get the value of a dapm semi enumerated double mixer control.
2485 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2486 * used for handling bitfield coded enumeration for example.
2488 * Returns 0 for success.
2490 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2491 struct snd_ctl_elem_value *ucontrol)
2493 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2494 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2495 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2496 unsigned int reg_val, val, mux;
2498 reg_val = snd_soc_read(widget->codec, e->reg);
2499 val = (reg_val >> e->shift_l) & e->mask;
2500 for (mux = 0; mux < e->max; mux++) {
2501 if (val == e->values[mux])
2504 ucontrol->value.enumerated.item[0] = mux;
2505 if (e->shift_l != e->shift_r) {
2506 val = (reg_val >> e->shift_r) & e->mask;
2507 for (mux = 0; mux < e->max; mux++) {
2508 if (val == e->values[mux])
2511 ucontrol->value.enumerated.item[1] = mux;
2516 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2519 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2521 * @kcontrol: mixer control
2522 * @ucontrol: control element information
2524 * Callback to set the value of a dapm semi enumerated double mixer control.
2526 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2527 * used for handling bitfield coded enumeration for example.
2529 * Returns 0 for success.
2531 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2532 struct snd_ctl_elem_value *ucontrol)
2534 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2535 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2536 struct snd_soc_codec *codec = widget->codec;
2537 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2538 unsigned int val, mux, change;
2540 struct snd_soc_dapm_update update;
2543 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2545 mux = ucontrol->value.enumerated.item[0];
2546 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2547 mask = e->mask << e->shift_l;
2548 if (e->shift_l != e->shift_r) {
2549 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2551 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2552 mask |= e->mask << e->shift_r;
2555 mutex_lock(&codec->mutex);
2557 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2559 for (wi = 0; wi < wlist->num_widgets; wi++) {
2560 widget = wlist->widgets[wi];
2562 widget->value = val;
2564 update.kcontrol = kcontrol;
2565 update.widget = widget;
2566 update.reg = e->reg;
2569 widget->dapm->update = &update;
2571 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2573 widget->dapm->update = NULL;
2577 mutex_unlock(&codec->mutex);
2580 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2583 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2585 * @kcontrol: mixer control
2586 * @uinfo: control element information
2588 * Callback to provide information about a pin switch control.
2590 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2591 struct snd_ctl_elem_info *uinfo)
2593 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2595 uinfo->value.integer.min = 0;
2596 uinfo->value.integer.max = 1;
2600 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2603 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2605 * @kcontrol: mixer control
2608 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2609 struct snd_ctl_elem_value *ucontrol)
2611 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2612 const char *pin = (const char *)kcontrol->private_value;
2614 mutex_lock(&codec->mutex);
2616 ucontrol->value.integer.value[0] =
2617 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2619 mutex_unlock(&codec->mutex);
2623 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2626 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2628 * @kcontrol: mixer control
2631 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2632 struct snd_ctl_elem_value *ucontrol)
2634 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2635 const char *pin = (const char *)kcontrol->private_value;
2637 mutex_lock(&codec->mutex);
2639 if (ucontrol->value.integer.value[0])
2640 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2642 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2644 snd_soc_dapm_sync(&codec->dapm);
2646 mutex_unlock(&codec->mutex);
2650 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2653 * snd_soc_dapm_new_control - create new dapm control
2654 * @dapm: DAPM context
2655 * @widget: widget template
2657 * Creates a new dapm control based upon the template.
2659 * Returns 0 for success else error.
2661 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2662 const struct snd_soc_dapm_widget *widget)
2664 struct snd_soc_dapm_widget *w;
2667 if ((w = dapm_cnew_widget(widget)) == NULL)
2670 name_len = strlen(widget->name) + 1;
2671 if (dapm->codec && dapm->codec->name_prefix)
2672 name_len += 1 + strlen(dapm->codec->name_prefix);
2673 w->name = kmalloc(name_len, GFP_KERNEL);
2674 if (w->name == NULL) {
2678 if (dapm->codec && dapm->codec->name_prefix)
2679 snprintf(w->name, name_len, "%s %s",
2680 dapm->codec->name_prefix, widget->name);
2682 snprintf(w->name, name_len, "%s", widget->name);
2685 case snd_soc_dapm_switch:
2686 case snd_soc_dapm_mixer:
2687 case snd_soc_dapm_mixer_named_ctl:
2688 w->power_check = dapm_generic_check_power;
2690 case snd_soc_dapm_mux:
2691 case snd_soc_dapm_virt_mux:
2692 case snd_soc_dapm_value_mux:
2693 w->power_check = dapm_generic_check_power;
2695 case snd_soc_dapm_adc:
2696 case snd_soc_dapm_aif_out:
2697 w->power_check = dapm_adc_check_power;
2699 case snd_soc_dapm_dac:
2700 case snd_soc_dapm_aif_in:
2701 w->power_check = dapm_dac_check_power;
2703 case snd_soc_dapm_pga:
2704 case snd_soc_dapm_out_drv:
2705 case snd_soc_dapm_input:
2706 case snd_soc_dapm_output:
2707 case snd_soc_dapm_micbias:
2708 case snd_soc_dapm_spk:
2709 case snd_soc_dapm_hp:
2710 case snd_soc_dapm_mic:
2711 case snd_soc_dapm_line:
2712 w->power_check = dapm_generic_check_power;
2714 case snd_soc_dapm_supply:
2715 w->power_check = dapm_supply_check_power;
2718 w->power_check = dapm_always_on_check_power;
2724 w->codec = dapm->codec;
2725 w->platform = dapm->platform;
2726 INIT_LIST_HEAD(&w->sources);
2727 INIT_LIST_HEAD(&w->sinks);
2728 INIT_LIST_HEAD(&w->list);
2729 INIT_LIST_HEAD(&w->dirty);
2730 list_add(&w->list, &dapm->card->widgets);
2732 /* machine layer set ups unconnected pins and insertions */
2736 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2739 * snd_soc_dapm_new_controls - create new dapm controls
2740 * @dapm: DAPM context
2741 * @widget: widget array
2742 * @num: number of widgets
2744 * Creates new DAPM controls based upon the templates.
2746 * Returns 0 for success else error.
2748 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2749 const struct snd_soc_dapm_widget *widget,
2754 for (i = 0; i < num; i++) {
2755 ret = snd_soc_dapm_new_control(dapm, widget);
2758 "ASoC: Failed to create DAPM control %s: %d\n",
2766 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2768 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2769 const char *stream, int event)
2771 struct snd_soc_dapm_widget *w;
2773 list_for_each_entry(w, &dapm->card->widgets, list)
2775 if (!w->sname || w->dapm != dapm)
2777 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2778 w->name, w->sname, stream, event);
2779 if (strstr(w->sname, stream)) {
2780 dapm_mark_dirty(w, "stream event");
2782 case SND_SOC_DAPM_STREAM_START:
2785 case SND_SOC_DAPM_STREAM_STOP:
2788 case SND_SOC_DAPM_STREAM_SUSPEND:
2789 case SND_SOC_DAPM_STREAM_RESUME:
2790 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2791 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2797 dapm_power_widgets(dapm, event);
2799 /* do we need to notify any clients that DAPM stream is complete */
2800 if (dapm->stream_event)
2801 dapm->stream_event(dapm, event);
2805 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2806 * @rtd: PCM runtime data
2807 * @stream: stream name
2808 * @event: stream event
2810 * Sends a stream event to the dapm core. The core then makes any
2811 * necessary widget power changes.
2813 * Returns 0 for success else error.
2815 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2816 const char *stream, int event)
2818 struct snd_soc_codec *codec = rtd->codec;
2823 mutex_lock(&codec->mutex);
2824 soc_dapm_stream_event(&codec->dapm, stream, event);
2825 mutex_unlock(&codec->mutex);
2830 * snd_soc_dapm_enable_pin - enable pin.
2831 * @dapm: DAPM context
2834 * Enables input/output pin and its parents or children widgets iff there is
2835 * a valid audio route and active audio stream.
2836 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2837 * do any widget power switching.
2839 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2841 return snd_soc_dapm_set_pin(dapm, pin, 1);
2843 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2846 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2847 * @dapm: DAPM context
2850 * Enables input/output pin regardless of any other state. This is
2851 * intended for use with microphone bias supplies used in microphone
2854 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2855 * do any widget power switching.
2857 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2860 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2863 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2867 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2870 dapm_mark_dirty(w, "force enable");
2874 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2877 * snd_soc_dapm_disable_pin - disable pin.
2878 * @dapm: DAPM context
2881 * Disables input/output pin and its parents or children widgets.
2882 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2883 * do any widget power switching.
2885 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2888 return snd_soc_dapm_set_pin(dapm, pin, 0);
2890 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2893 * snd_soc_dapm_nc_pin - permanently disable pin.
2894 * @dapm: DAPM context
2897 * Marks the specified pin as being not connected, disabling it along
2898 * any parent or child widgets. At present this is identical to
2899 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2900 * additional things such as disabling controls which only affect
2901 * paths through the pin.
2903 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2904 * do any widget power switching.
2906 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2908 return snd_soc_dapm_set_pin(dapm, pin, 0);
2910 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2913 * snd_soc_dapm_get_pin_status - get audio pin status
2914 * @dapm: DAPM context
2915 * @pin: audio signal pin endpoint (or start point)
2917 * Get audio pin status - connected or disconnected.
2919 * Returns 1 for connected otherwise 0.
2921 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2924 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2927 return w->connected;
2931 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2934 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2935 * @dapm: DAPM context
2936 * @pin: audio signal pin endpoint (or start point)
2938 * Mark the given endpoint or pin as ignoring suspend. When the
2939 * system is disabled a path between two endpoints flagged as ignoring
2940 * suspend will not be disabled. The path must already be enabled via
2941 * normal means at suspend time, it will not be turned on if it was not
2944 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2947 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2950 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2954 w->ignore_suspend = 1;
2958 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2961 * snd_soc_dapm_free - free dapm resources
2962 * @dapm: DAPM context
2964 * Free all dapm widgets and resources.
2966 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2968 snd_soc_dapm_sys_remove(dapm->dev);
2969 dapm_debugfs_cleanup(dapm);
2970 dapm_free_widgets(dapm);
2971 list_del(&dapm->list);
2973 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2975 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2977 struct snd_soc_dapm_widget *w;
2978 LIST_HEAD(down_list);
2981 list_for_each_entry(w, &dapm->card->widgets, list) {
2982 if (w->dapm != dapm)
2985 dapm_seq_insert(w, &down_list, false);
2991 /* If there were no widgets to power down we're already in
2995 if (dapm->bias_level == SND_SOC_BIAS_ON)
2996 snd_soc_dapm_set_bias_level(dapm,
2997 SND_SOC_BIAS_PREPARE);
2998 dapm_seq_run(dapm, &down_list, 0, false);
2999 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3000 snd_soc_dapm_set_bias_level(dapm,
3001 SND_SOC_BIAS_STANDBY);
3006 * snd_soc_dapm_shutdown - callback for system shutdown
3008 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3010 struct snd_soc_codec *codec;
3012 list_for_each_entry(codec, &card->codec_dev_list, list) {
3013 soc_dapm_shutdown_codec(&codec->dapm);
3014 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3015 snd_soc_dapm_set_bias_level(&codec->dapm,
3020 /* Module information */
3021 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3022 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3023 MODULE_LICENSE("GPL");