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 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
129 if (!dapm_dirty_widget(w)) {
130 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
132 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
135 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
137 /* create a new dapm widget */
138 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
139 const struct snd_soc_dapm_widget *_widget)
141 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
144 /* get snd_card from DAPM context */
145 static inline struct snd_card *dapm_get_snd_card(
146 struct snd_soc_dapm_context *dapm)
149 return dapm->codec->card->snd_card;
150 else if (dapm->platform)
151 return dapm->platform->card->snd_card;
159 /* get soc_card from DAPM context */
160 static inline struct snd_soc_card *dapm_get_soc_card(
161 struct snd_soc_dapm_context *dapm)
164 return dapm->codec->card;
165 else if (dapm->platform)
166 return dapm->platform->card;
174 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
177 return snd_soc_read(w->codec, reg);
178 else if (w->platform)
179 return snd_soc_platform_read(w->platform, reg);
181 dev_err(w->dapm->dev, "no valid widget read method\n");
185 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
188 return snd_soc_write(w->codec, reg, val);
189 else if (w->platform)
190 return snd_soc_platform_write(w->platform, reg, val);
192 dev_err(w->dapm->dev, "no valid widget write method\n");
196 static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
197 unsigned short reg, unsigned int mask, unsigned int value)
200 unsigned int old, new;
203 ret = soc_widget_read(w, reg);
208 new = (old & ~mask) | (value & mask);
211 ret = soc_widget_write(w, reg, new);
220 * snd_soc_dapm_set_bias_level - set the bias level for the system
221 * @dapm: DAPM context
222 * @level: level to configure
224 * Configure the bias (power) levels for the SoC audio device.
226 * Returns 0 for success else error.
228 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
229 enum snd_soc_bias_level level)
231 struct snd_soc_card *card = dapm->card;
234 trace_snd_soc_bias_level_start(card, level);
236 if (card && card->set_bias_level)
237 ret = card->set_bias_level(card, dapm, level);
242 if (dapm->codec->driver->set_bias_level)
243 ret = dapm->codec->driver->set_bias_level(dapm->codec,
246 dapm->bias_level = level;
251 if (card && card->set_bias_level_post)
252 ret = card->set_bias_level_post(card, dapm, level);
254 trace_snd_soc_bias_level_done(card, level);
259 /* set up initial codec paths */
260 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
261 struct snd_soc_dapm_path *p, int i)
264 case snd_soc_dapm_switch:
265 case snd_soc_dapm_mixer:
266 case snd_soc_dapm_mixer_named_ctl: {
268 struct soc_mixer_control *mc = (struct soc_mixer_control *)
269 w->kcontrol_news[i].private_value;
270 unsigned int reg = mc->reg;
271 unsigned int shift = mc->shift;
273 unsigned int mask = (1 << fls(max)) - 1;
274 unsigned int invert = mc->invert;
276 val = soc_widget_read(w, reg);
277 val = (val >> shift) & mask;
279 if ((invert && !val) || (!invert && val))
285 case snd_soc_dapm_mux: {
286 struct soc_enum *e = (struct soc_enum *)
287 w->kcontrol_news[i].private_value;
288 int val, item, bitmask;
290 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
292 val = soc_widget_read(w, e->reg);
293 item = (val >> e->shift_l) & (bitmask - 1);
296 for (i = 0; i < e->max; i++) {
297 if (!(strcmp(p->name, e->texts[i])) && item == i)
302 case snd_soc_dapm_virt_mux: {
303 struct soc_enum *e = (struct soc_enum *)
304 w->kcontrol_news[i].private_value;
307 /* since a virtual mux has no backing registers to
308 * decide which path to connect, it will try to match
309 * with the first enumeration. This is to ensure
310 * that the default mux choice (the first) will be
311 * correctly powered up during initialization.
313 if (!strcmp(p->name, e->texts[0]))
317 case snd_soc_dapm_value_mux: {
318 struct soc_enum *e = (struct soc_enum *)
319 w->kcontrol_news[i].private_value;
322 val = soc_widget_read(w, e->reg);
323 val = (val >> e->shift_l) & e->mask;
324 for (item = 0; item < e->max; item++) {
325 if (val == e->values[item])
330 for (i = 0; i < e->max; i++) {
331 if (!(strcmp(p->name, e->texts[i])) && item == i)
336 /* does not affect routing - always connected */
337 case snd_soc_dapm_pga:
338 case snd_soc_dapm_out_drv:
339 case snd_soc_dapm_output:
340 case snd_soc_dapm_adc:
341 case snd_soc_dapm_input:
342 case snd_soc_dapm_siggen:
343 case snd_soc_dapm_dac:
344 case snd_soc_dapm_micbias:
345 case snd_soc_dapm_vmid:
346 case snd_soc_dapm_supply:
347 case snd_soc_dapm_aif_in:
348 case snd_soc_dapm_aif_out:
349 case snd_soc_dapm_hp:
350 case snd_soc_dapm_mic:
351 case snd_soc_dapm_spk:
352 case snd_soc_dapm_line:
355 /* does affect routing - dynamically connected */
356 case snd_soc_dapm_pre:
357 case snd_soc_dapm_post:
363 /* connect mux widget to its interconnecting audio paths */
364 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
365 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
366 struct snd_soc_dapm_path *path, const char *control_name,
367 const struct snd_kcontrol_new *kcontrol)
369 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
372 for (i = 0; i < e->max; i++) {
373 if (!(strcmp(control_name, e->texts[i]))) {
374 list_add(&path->list, &dapm->card->paths);
375 list_add(&path->list_sink, &dest->sources);
376 list_add(&path->list_source, &src->sinks);
377 path->name = (char*)e->texts[i];
378 dapm_set_path_status(dest, path, 0);
386 /* connect mixer widget to its interconnecting audio paths */
387 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
388 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
389 struct snd_soc_dapm_path *path, const char *control_name)
393 /* search for mixer kcontrol */
394 for (i = 0; i < dest->num_kcontrols; i++) {
395 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
396 list_add(&path->list, &dapm->card->paths);
397 list_add(&path->list_sink, &dest->sources);
398 list_add(&path->list_source, &src->sinks);
399 path->name = dest->kcontrol_news[i].name;
400 dapm_set_path_status(dest, path, i);
407 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
408 struct snd_soc_dapm_widget *kcontrolw,
409 const struct snd_kcontrol_new *kcontrol_new,
410 struct snd_kcontrol **kcontrol)
412 struct snd_soc_dapm_widget *w;
417 list_for_each_entry(w, &dapm->card->widgets, list) {
418 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
420 for (i = 0; i < w->num_kcontrols; i++) {
421 if (&w->kcontrol_news[i] == kcontrol_new) {
423 *kcontrol = w->kcontrols[i];
432 /* create new dapm mixer control */
433 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
435 struct snd_soc_dapm_context *dapm = w->dapm;
437 size_t name_len, prefix_len;
438 struct snd_soc_dapm_path *path;
439 struct snd_card *card = dapm->card->snd_card;
441 struct snd_soc_dapm_widget_list *wlist;
445 prefix = dapm->codec->name_prefix;
450 prefix_len = strlen(prefix) + 1;
455 for (i = 0; i < w->num_kcontrols; i++) {
458 list_for_each_entry(path, &w->sources, list_sink) {
460 /* mixer/mux paths name must match control name */
461 if (path->name != (char *)w->kcontrol_news[i].name)
464 if (w->kcontrols[i]) {
465 path->kcontrol = w->kcontrols[i];
469 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
470 sizeof(struct snd_soc_dapm_widget *),
471 wlist = kzalloc(wlistsize, GFP_KERNEL);
474 "asoc: can't allocate widget list for %s\n",
478 wlist->num_widgets = 1;
479 wlist->widgets[0] = w;
481 /* add dapm control with long name.
482 * for dapm_mixer this is the concatenation of the
483 * mixer and kcontrol name.
484 * for dapm_mixer_named_ctl this is simply the
487 name_len = strlen(w->kcontrol_news[i].name) + 1;
488 if (w->id != snd_soc_dapm_mixer_named_ctl)
489 name_len += 1 + strlen(w->name);
491 path->long_name = kmalloc(name_len, GFP_KERNEL);
493 if (path->long_name == NULL) {
500 /* The control will get a prefix from
501 * the control creation process but
502 * we're also using the same prefix
503 * for widgets so cut the prefix off
504 * the front of the widget name.
506 snprintf(path->long_name, name_len, "%s %s",
507 w->name + prefix_len,
508 w->kcontrol_news[i].name);
510 case snd_soc_dapm_mixer_named_ctl:
511 snprintf(path->long_name, name_len, "%s",
512 w->kcontrol_news[i].name);
516 path->long_name[name_len - 1] = '\0';
518 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
519 wlist, path->long_name,
521 ret = snd_ctl_add(card, path->kcontrol);
524 "asoc: failed to add dapm kcontrol %s: %d\n",
525 path->long_name, ret);
527 kfree(path->long_name);
528 path->long_name = NULL;
531 w->kcontrols[i] = path->kcontrol;
537 /* create new dapm mux control */
538 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
540 struct snd_soc_dapm_context *dapm = w->dapm;
541 struct snd_soc_dapm_path *path = NULL;
542 struct snd_kcontrol *kcontrol;
543 struct snd_card *card = dapm->card->snd_card;
547 struct snd_soc_dapm_widget_list *wlist;
548 int shared, wlistentries;
552 if (w->num_kcontrols != 1) {
554 "asoc: mux %s has incorrect number of controls\n",
559 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
562 wlist = kcontrol->private_data;
563 wlistentries = wlist->num_widgets + 1;
568 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
569 wlistentries * sizeof(struct snd_soc_dapm_widget *),
570 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
573 "asoc: can't allocate widget list for %s\n", w->name);
576 wlist->num_widgets = wlistentries;
577 wlist->widgets[wlistentries - 1] = w;
581 prefix = dapm->codec->name_prefix;
586 name = w->kcontrol_news[0].name;
591 prefix_len = strlen(prefix) + 1;
597 * The control will get a prefix from the control creation
598 * process but we're also using the same prefix for widgets so
599 * cut the prefix off the front of the widget name.
601 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
602 name + prefix_len, prefix);
603 ret = snd_ctl_add(card, kcontrol);
605 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
612 kcontrol->private_data = wlist;
614 w->kcontrols[0] = kcontrol;
616 list_for_each_entry(path, &w->sources, list_sink)
617 path->kcontrol = kcontrol;
622 /* create new dapm volume control */
623 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
625 if (w->num_kcontrols)
626 dev_err(w->dapm->dev,
627 "asoc: PGA controls not supported: '%s'\n", w->name);
632 /* reset 'walked' bit for each dapm path */
633 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
635 struct snd_soc_dapm_path *p;
637 list_for_each_entry(p, &dapm->card->paths, list)
641 /* We implement power down on suspend by checking the power state of
642 * the ALSA card - when we are suspending the ALSA state for the card
645 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
647 int level = snd_power_get_state(widget->dapm->card->snd_card);
650 case SNDRV_CTL_POWER_D3hot:
651 case SNDRV_CTL_POWER_D3cold:
652 if (widget->ignore_suspend)
653 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
655 return widget->ignore_suspend;
662 * Recursively check for a completed path to an active or physically connected
663 * output widget. Returns number of complete paths.
665 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
667 struct snd_soc_dapm_path *path;
670 if (widget->outputs >= 0)
671 return widget->outputs;
673 DAPM_UPDATE_STAT(widget, path_checks);
675 if (widget->id == snd_soc_dapm_supply)
678 switch (widget->id) {
679 case snd_soc_dapm_adc:
680 case snd_soc_dapm_aif_out:
681 if (widget->active) {
682 widget->outputs = snd_soc_dapm_suspend_check(widget);
683 return widget->outputs;
689 if (widget->connected) {
690 /* connected pin ? */
691 if (widget->id == snd_soc_dapm_output && !widget->ext) {
692 widget->outputs = snd_soc_dapm_suspend_check(widget);
693 return widget->outputs;
696 /* connected jack or spk ? */
697 if (widget->id == snd_soc_dapm_hp ||
698 widget->id == snd_soc_dapm_spk ||
699 (widget->id == snd_soc_dapm_line &&
700 !list_empty(&widget->sources))) {
701 widget->outputs = snd_soc_dapm_suspend_check(widget);
702 return widget->outputs;
706 list_for_each_entry(path, &widget->sinks, list_source) {
707 DAPM_UPDATE_STAT(widget, neighbour_checks);
715 if (path->sink && path->connect) {
717 con += is_connected_output_ep(path->sink);
721 widget->outputs = con;
727 * Recursively check for a completed path to an active or physically connected
728 * input widget. Returns number of complete paths.
730 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
732 struct snd_soc_dapm_path *path;
735 if (widget->inputs >= 0)
736 return widget->inputs;
738 DAPM_UPDATE_STAT(widget, path_checks);
740 if (widget->id == snd_soc_dapm_supply)
743 /* active stream ? */
744 switch (widget->id) {
745 case snd_soc_dapm_dac:
746 case snd_soc_dapm_aif_in:
747 if (widget->active) {
748 widget->inputs = snd_soc_dapm_suspend_check(widget);
749 return widget->inputs;
755 if (widget->connected) {
756 /* connected pin ? */
757 if (widget->id == snd_soc_dapm_input && !widget->ext) {
758 widget->inputs = snd_soc_dapm_suspend_check(widget);
759 return widget->inputs;
762 /* connected VMID/Bias for lower pops */
763 if (widget->id == snd_soc_dapm_vmid) {
764 widget->inputs = snd_soc_dapm_suspend_check(widget);
765 return widget->inputs;
768 /* connected jack ? */
769 if (widget->id == snd_soc_dapm_mic ||
770 (widget->id == snd_soc_dapm_line &&
771 !list_empty(&widget->sinks))) {
772 widget->inputs = snd_soc_dapm_suspend_check(widget);
773 return widget->inputs;
776 /* signal generator */
777 if (widget->id == snd_soc_dapm_siggen) {
778 widget->inputs = snd_soc_dapm_suspend_check(widget);
779 return widget->inputs;
783 list_for_each_entry(path, &widget->sources, list_sink) {
784 DAPM_UPDATE_STAT(widget, neighbour_checks);
792 if (path->source && path->connect) {
794 con += is_connected_input_ep(path->source);
798 widget->inputs = con;
804 * Handler for generic register modifier widget.
806 int dapm_reg_event(struct snd_soc_dapm_widget *w,
807 struct snd_kcontrol *kcontrol, int event)
811 if (SND_SOC_DAPM_EVENT_ON(event))
816 soc_widget_update_bits(w, -(w->reg + 1),
817 w->mask << w->shift, val << w->shift);
821 EXPORT_SYMBOL_GPL(dapm_reg_event);
823 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
825 if (w->power_checked)
831 w->new_power = w->power_check(w);
833 w->power_checked = true;
838 /* Generic check to see if a widget should be powered.
840 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
844 DAPM_UPDATE_STAT(w, power_checks);
846 in = is_connected_input_ep(w);
847 dapm_clear_walk(w->dapm);
848 out = is_connected_output_ep(w);
849 dapm_clear_walk(w->dapm);
850 return out != 0 && in != 0;
853 /* Check to see if an ADC has power */
854 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
858 DAPM_UPDATE_STAT(w, power_checks);
861 in = is_connected_input_ep(w);
862 dapm_clear_walk(w->dapm);
865 return dapm_generic_check_power(w);
869 /* Check to see if a DAC has power */
870 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
874 DAPM_UPDATE_STAT(w, power_checks);
877 out = is_connected_output_ep(w);
878 dapm_clear_walk(w->dapm);
881 return dapm_generic_check_power(w);
885 /* Check to see if a power supply is needed */
886 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
888 struct snd_soc_dapm_path *path;
890 DAPM_UPDATE_STAT(w, power_checks);
892 /* Check if one of our outputs is connected */
893 list_for_each_entry(path, &w->sinks, list_source) {
894 DAPM_UPDATE_STAT(w, neighbour_checks);
899 if (path->connected &&
900 !path->connected(path->source, path->sink))
906 if (dapm_widget_power_check(path->sink))
910 dapm_clear_walk(w->dapm);
915 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
920 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
921 struct snd_soc_dapm_widget *b,
929 sort = dapm_down_seq;
931 if (sort[a->id] != sort[b->id])
932 return sort[a->id] - sort[b->id];
933 if (a->subseq != b->subseq) {
935 return a->subseq - b->subseq;
937 return b->subseq - a->subseq;
939 if (a->reg != b->reg)
940 return a->reg - b->reg;
941 if (a->dapm != b->dapm)
942 return (unsigned long)a->dapm - (unsigned long)b->dapm;
947 /* Insert a widget in order into a DAPM power sequence. */
948 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
949 struct list_head *list,
952 struct snd_soc_dapm_widget *w;
954 list_for_each_entry(w, list, power_list)
955 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
956 list_add_tail(&new_widget->power_list, &w->power_list);
960 list_add_tail(&new_widget->power_list, list);
963 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
964 struct snd_soc_dapm_widget *w, int event)
966 struct snd_soc_card *card = dapm->card;
971 case SND_SOC_DAPM_PRE_PMU:
975 case SND_SOC_DAPM_POST_PMU:
976 ev_name = "POST_PMU";
979 case SND_SOC_DAPM_PRE_PMD:
983 case SND_SOC_DAPM_POST_PMD:
984 ev_name = "POST_PMD";
992 if (w->power != power)
995 if (w->event && (w->event_flags & event)) {
996 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
998 trace_snd_soc_dapm_widget_event_start(w, event);
999 ret = w->event(w, NULL, event);
1000 trace_snd_soc_dapm_widget_event_done(w, event);
1002 pr_err("%s: %s event failed: %d\n",
1003 ev_name, w->name, ret);
1007 /* Apply the coalesced changes from a DAPM sequence */
1008 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1009 struct list_head *pending)
1011 struct snd_soc_card *card = dapm->card;
1012 struct snd_soc_dapm_widget *w;
1014 unsigned int value = 0;
1015 unsigned int mask = 0;
1016 unsigned int cur_mask;
1018 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1021 list_for_each_entry(w, pending, power_list) {
1022 cur_mask = 1 << w->shift;
1023 BUG_ON(reg != w->reg);
1034 pop_dbg(dapm->dev, card->pop_time,
1035 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1036 w->name, reg, value, mask);
1038 /* Check for events */
1039 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1040 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1044 /* Any widget will do, they should all be updating the
1047 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1050 pop_dbg(dapm->dev, card->pop_time,
1051 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1052 value, mask, reg, card->pop_time);
1053 pop_wait(card->pop_time);
1054 soc_widget_update_bits(w, reg, mask, value);
1057 list_for_each_entry(w, pending, power_list) {
1058 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1059 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1063 /* Apply a DAPM power sequence.
1065 * We walk over a pre-sorted list of widgets to apply power to. In
1066 * order to minimise the number of writes to the device required
1067 * multiple widgets will be updated in a single write where possible.
1068 * Currently anything that requires more than a single write is not
1071 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1072 struct list_head *list, int event, bool power_up)
1074 struct snd_soc_dapm_widget *w, *n;
1077 int cur_subseq = -1;
1078 int cur_reg = SND_SOC_NOPM;
1079 struct snd_soc_dapm_context *cur_dapm = NULL;
1086 sort = dapm_down_seq;
1088 list_for_each_entry_safe(w, n, list, power_list) {
1091 /* Do we need to apply any queued changes? */
1092 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1093 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1094 if (!list_empty(&pending))
1095 dapm_seq_run_coalesced(cur_dapm, &pending);
1097 if (cur_dapm && cur_dapm->seq_notifier) {
1098 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1099 if (sort[i] == cur_sort)
1100 cur_dapm->seq_notifier(cur_dapm,
1105 INIT_LIST_HEAD(&pending);
1107 cur_subseq = INT_MIN;
1108 cur_reg = SND_SOC_NOPM;
1113 case snd_soc_dapm_pre:
1115 list_for_each_entry_safe_continue(w, n, list,
1118 if (event == SND_SOC_DAPM_STREAM_START)
1120 NULL, SND_SOC_DAPM_PRE_PMU);
1121 else if (event == SND_SOC_DAPM_STREAM_STOP)
1123 NULL, SND_SOC_DAPM_PRE_PMD);
1126 case snd_soc_dapm_post:
1128 list_for_each_entry_safe_continue(w, n, list,
1131 if (event == SND_SOC_DAPM_STREAM_START)
1133 NULL, SND_SOC_DAPM_POST_PMU);
1134 else if (event == SND_SOC_DAPM_STREAM_STOP)
1136 NULL, SND_SOC_DAPM_POST_PMD);
1140 /* Queue it up for application */
1141 cur_sort = sort[w->id];
1142 cur_subseq = w->subseq;
1145 list_move(&w->power_list, &pending);
1150 dev_err(w->dapm->dev,
1151 "Failed to apply widget power: %d\n", ret);
1154 if (!list_empty(&pending))
1155 dapm_seq_run_coalesced(cur_dapm, &pending);
1157 if (cur_dapm && cur_dapm->seq_notifier) {
1158 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1159 if (sort[i] == cur_sort)
1160 cur_dapm->seq_notifier(cur_dapm,
1165 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1167 struct snd_soc_dapm_update *update = dapm->update;
1168 struct snd_soc_dapm_widget *w;
1177 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1178 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1180 pr_err("%s DAPM pre-event failed: %d\n",
1184 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1187 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1190 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1191 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1193 pr_err("%s DAPM post-event failed: %d\n",
1198 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1199 * they're changing state.
1201 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1203 struct snd_soc_dapm_context *d = data;
1206 /* If we're off and we're not supposed to be go into STANDBY */
1207 if (d->bias_level == SND_SOC_BIAS_OFF &&
1208 d->target_bias_level != SND_SOC_BIAS_OFF) {
1209 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1212 "Failed to turn on bias: %d\n", ret);
1215 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1216 if (d->bias_level != d->target_bias_level) {
1217 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1220 "Failed to prepare bias: %d\n", ret);
1224 /* Async callback run prior to DAPM sequences - brings to their final
1227 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1229 struct snd_soc_dapm_context *d = data;
1232 /* If we just powered the last thing off drop to standby bias */
1233 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1234 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1235 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1236 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1238 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1242 /* If we're in standby and can support bias off then do that */
1243 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1244 d->target_bias_level == SND_SOC_BIAS_OFF) {
1245 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1247 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1250 /* If we just powered up then move to active bias */
1251 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1252 d->target_bias_level == SND_SOC_BIAS_ON) {
1253 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1255 dev_err(d->dev, "Failed to apply active bias: %d\n",
1260 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1261 bool power, bool connect)
1263 /* If a connection is being made or broken then that update
1264 * will have marked the peer dirty, otherwise the widgets are
1265 * not connected and this update has no impact. */
1269 /* If the peer is already in the state we're moving to then we
1270 * won't have an impact on it. */
1271 if (power != peer->power)
1272 dapm_mark_dirty(peer, "peer state change");
1275 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1276 struct list_head *up_list,
1277 struct list_head *down_list)
1279 struct snd_soc_dapm_path *path;
1281 if (w->power == power)
1284 trace_snd_soc_dapm_widget_power(w, power);
1286 /* If we changed our power state perhaps our neigbours changed
1289 list_for_each_entry(path, &w->sources, list_sink) {
1291 dapm_widget_set_peer_power(path->source, power,
1296 case snd_soc_dapm_supply:
1297 /* Supplies can't affect their outputs, only their inputs */
1300 list_for_each_entry(path, &w->sinks, list_source) {
1302 dapm_widget_set_peer_power(path->sink, power,
1310 dapm_seq_insert(w, up_list, true);
1312 dapm_seq_insert(w, down_list, false);
1317 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1318 struct list_head *up_list,
1319 struct list_head *down_list)
1324 case snd_soc_dapm_pre:
1325 dapm_seq_insert(w, down_list, false);
1327 case snd_soc_dapm_post:
1328 dapm_seq_insert(w, up_list, true);
1332 power = dapm_widget_power_check(w);
1334 dapm_widget_set_power(w, power, up_list, down_list);
1340 * Scan each dapm widget for complete audio path.
1341 * A complete path is a route that has valid endpoints i.e.:-
1343 * o DAC to output pin.
1344 * o Input Pin to ADC.
1345 * o Input pin to Output pin (bypass, sidetone)
1346 * o DAC to ADC (loopback).
1348 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1350 struct snd_soc_card *card = dapm->card;
1351 struct snd_soc_dapm_widget *w;
1352 struct snd_soc_dapm_context *d;
1354 LIST_HEAD(down_list);
1355 LIST_HEAD(async_domain);
1356 enum snd_soc_bias_level bias;
1358 trace_snd_soc_dapm_start(card);
1360 list_for_each_entry(d, &card->dapm_list, list) {
1361 if (d->n_widgets || d->codec == NULL) {
1362 if (d->idle_bias_off)
1363 d->target_bias_level = SND_SOC_BIAS_OFF;
1365 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1369 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1371 list_for_each_entry(w, &card->widgets, list) {
1372 w->power_checked = false;
1377 /* Check which widgets we need to power and store them in
1378 * lists indicating if they should be powered up or down. We
1379 * only check widgets that have been flagged as dirty but note
1380 * that new widgets may be added to the dirty list while we
1383 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1384 dapm_power_one_widget(w, &up_list, &down_list);
1387 list_for_each_entry(w, &card->widgets, list) {
1388 list_del_init(&w->dirty);
1393 /* Supplies and micbiases only bring the
1394 * context up to STANDBY as unless something
1395 * else is active and passing audio they
1396 * generally don't require full power.
1399 case snd_soc_dapm_supply:
1400 case snd_soc_dapm_micbias:
1401 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1402 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1405 d->target_bias_level = SND_SOC_BIAS_ON;
1412 /* If there are no DAPM widgets then try to figure out power from the
1415 if (!dapm->n_widgets) {
1417 case SND_SOC_DAPM_STREAM_START:
1418 case SND_SOC_DAPM_STREAM_RESUME:
1419 dapm->target_bias_level = SND_SOC_BIAS_ON;
1421 case SND_SOC_DAPM_STREAM_STOP:
1422 if (dapm->codec->active)
1423 dapm->target_bias_level = SND_SOC_BIAS_ON;
1425 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1427 case SND_SOC_DAPM_STREAM_SUSPEND:
1428 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1430 case SND_SOC_DAPM_STREAM_NOP:
1431 dapm->target_bias_level = dapm->bias_level;
1438 /* Force all contexts in the card to the same bias state if
1439 * they're not ground referenced.
1441 bias = SND_SOC_BIAS_OFF;
1442 list_for_each_entry(d, &card->dapm_list, list)
1443 if (d->target_bias_level > bias)
1444 bias = d->target_bias_level;
1445 list_for_each_entry(d, &card->dapm_list, list)
1446 if (!d->idle_bias_off)
1447 d->target_bias_level = bias;
1449 trace_snd_soc_dapm_walk_done(card);
1451 /* Run all the bias changes in parallel */
1452 list_for_each_entry(d, &dapm->card->dapm_list, list)
1453 async_schedule_domain(dapm_pre_sequence_async, d,
1455 async_synchronize_full_domain(&async_domain);
1457 /* Power down widgets first; try to avoid amplifying pops. */
1458 dapm_seq_run(dapm, &down_list, event, false);
1460 dapm_widget_update(dapm);
1463 dapm_seq_run(dapm, &up_list, event, true);
1465 /* Run all the bias changes in parallel */
1466 list_for_each_entry(d, &dapm->card->dapm_list, list)
1467 async_schedule_domain(dapm_post_sequence_async, d,
1469 async_synchronize_full_domain(&async_domain);
1471 pop_dbg(dapm->dev, card->pop_time,
1472 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1473 pop_wait(card->pop_time);
1475 trace_snd_soc_dapm_done(card);
1480 #ifdef CONFIG_DEBUG_FS
1481 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1483 file->private_data = inode->i_private;
1487 static ssize_t dapm_widget_power_read_file(struct file *file,
1488 char __user *user_buf,
1489 size_t count, loff_t *ppos)
1491 struct snd_soc_dapm_widget *w = file->private_data;
1495 struct snd_soc_dapm_path *p = NULL;
1497 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1501 in = is_connected_input_ep(w);
1502 dapm_clear_walk(w->dapm);
1503 out = is_connected_output_ep(w);
1504 dapm_clear_walk(w->dapm);
1506 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1507 w->name, w->power ? "On" : "Off", in, out);
1510 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1511 " - R%d(0x%x) bit %d",
1512 w->reg, w->reg, w->shift);
1514 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1517 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1519 w->active ? "active" : "inactive");
1521 list_for_each_entry(p, &w->sources, list_sink) {
1522 if (p->connected && !p->connected(w, p->sink))
1526 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1527 " in \"%s\" \"%s\"\n",
1528 p->name ? p->name : "static",
1531 list_for_each_entry(p, &w->sinks, list_source) {
1532 if (p->connected && !p->connected(w, p->sink))
1536 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1537 " out \"%s\" \"%s\"\n",
1538 p->name ? p->name : "static",
1542 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1548 static const struct file_operations dapm_widget_power_fops = {
1549 .open = dapm_widget_power_open_file,
1550 .read = dapm_widget_power_read_file,
1551 .llseek = default_llseek,
1554 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1556 file->private_data = inode->i_private;
1560 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1561 size_t count, loff_t *ppos)
1563 struct snd_soc_dapm_context *dapm = file->private_data;
1566 switch (dapm->bias_level) {
1567 case SND_SOC_BIAS_ON:
1570 case SND_SOC_BIAS_PREPARE:
1571 level = "Prepare\n";
1573 case SND_SOC_BIAS_STANDBY:
1574 level = "Standby\n";
1576 case SND_SOC_BIAS_OFF:
1581 level = "Unknown\n";
1585 return simple_read_from_buffer(user_buf, count, ppos, level,
1589 static const struct file_operations dapm_bias_fops = {
1590 .open = dapm_bias_open_file,
1591 .read = dapm_bias_read_file,
1592 .llseek = default_llseek,
1595 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1596 struct dentry *parent)
1600 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1602 if (!dapm->debugfs_dapm) {
1604 "Failed to create DAPM debugfs directory\n");
1608 d = debugfs_create_file("bias_level", 0444,
1609 dapm->debugfs_dapm, dapm,
1613 "ASoC: Failed to create bias level debugfs file\n");
1616 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1618 struct snd_soc_dapm_context *dapm = w->dapm;
1621 if (!dapm->debugfs_dapm || !w->name)
1624 d = debugfs_create_file(w->name, 0444,
1625 dapm->debugfs_dapm, w,
1626 &dapm_widget_power_fops);
1628 dev_warn(w->dapm->dev,
1629 "ASoC: Failed to create %s debugfs file\n",
1633 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1635 debugfs_remove_recursive(dapm->debugfs_dapm);
1639 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1640 struct dentry *parent)
1644 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1648 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1654 /* test and update the power status of a mux widget */
1655 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1656 struct snd_kcontrol *kcontrol, int change,
1657 int mux, struct soc_enum *e)
1659 struct snd_soc_dapm_path *path;
1662 if (widget->id != snd_soc_dapm_mux &&
1663 widget->id != snd_soc_dapm_virt_mux &&
1664 widget->id != snd_soc_dapm_value_mux)
1670 /* find dapm widget path assoc with kcontrol */
1671 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1672 if (path->kcontrol != kcontrol)
1675 if (!path->name || !e->texts[mux])
1679 /* we now need to match the string in the enum to the path */
1680 if (!(strcmp(path->name, e->texts[mux]))) {
1681 path->connect = 1; /* new connection */
1682 dapm_mark_dirty(path->source, "mux connection");
1685 dapm_mark_dirty(path->source,
1686 "mux disconnection");
1687 path->connect = 0; /* old connection must be powered down */
1692 dapm_mark_dirty(widget, "mux change");
1693 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1699 /* test and update the power status of a mixer or switch widget */
1700 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1701 struct snd_kcontrol *kcontrol, int connect)
1703 struct snd_soc_dapm_path *path;
1706 if (widget->id != snd_soc_dapm_mixer &&
1707 widget->id != snd_soc_dapm_mixer_named_ctl &&
1708 widget->id != snd_soc_dapm_switch)
1711 /* find dapm widget path assoc with kcontrol */
1712 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1713 if (path->kcontrol != kcontrol)
1716 /* found, now check type */
1718 path->connect = connect;
1719 dapm_mark_dirty(path->source, "mixer connection");
1723 dapm_mark_dirty(widget, "mixer update");
1724 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1730 /* show dapm widget status in sys fs */
1731 static ssize_t dapm_widget_show(struct device *dev,
1732 struct device_attribute *attr, char *buf)
1734 struct snd_soc_pcm_runtime *rtd =
1735 container_of(dev, struct snd_soc_pcm_runtime, dev);
1736 struct snd_soc_codec *codec =rtd->codec;
1737 struct snd_soc_dapm_widget *w;
1739 char *state = "not set";
1741 list_for_each_entry(w, &codec->card->widgets, list) {
1742 if (w->dapm != &codec->dapm)
1745 /* only display widgets that burnm power */
1747 case snd_soc_dapm_hp:
1748 case snd_soc_dapm_mic:
1749 case snd_soc_dapm_spk:
1750 case snd_soc_dapm_line:
1751 case snd_soc_dapm_micbias:
1752 case snd_soc_dapm_dac:
1753 case snd_soc_dapm_adc:
1754 case snd_soc_dapm_pga:
1755 case snd_soc_dapm_out_drv:
1756 case snd_soc_dapm_mixer:
1757 case snd_soc_dapm_mixer_named_ctl:
1758 case snd_soc_dapm_supply:
1760 count += sprintf(buf + count, "%s: %s\n",
1761 w->name, w->power ? "On":"Off");
1768 switch (codec->dapm.bias_level) {
1769 case SND_SOC_BIAS_ON:
1772 case SND_SOC_BIAS_PREPARE:
1775 case SND_SOC_BIAS_STANDBY:
1778 case SND_SOC_BIAS_OFF:
1782 count += sprintf(buf + count, "PM State: %s\n", state);
1787 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1789 int snd_soc_dapm_sys_add(struct device *dev)
1791 return device_create_file(dev, &dev_attr_dapm_widget);
1794 static void snd_soc_dapm_sys_remove(struct device *dev)
1796 device_remove_file(dev, &dev_attr_dapm_widget);
1799 /* free all dapm widgets and resources */
1800 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1802 struct snd_soc_dapm_widget *w, *next_w;
1803 struct snd_soc_dapm_path *p, *next_p;
1805 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1806 if (w->dapm != dapm)
1810 * remove source and sink paths associated to this widget.
1811 * While removing the path, remove reference to it from both
1812 * source and sink widgets so that path is removed only once.
1814 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1815 list_del(&p->list_sink);
1816 list_del(&p->list_source);
1818 kfree(p->long_name);
1821 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1822 list_del(&p->list_sink);
1823 list_del(&p->list_source);
1825 kfree(p->long_name);
1828 kfree(w->kcontrols);
1834 static struct snd_soc_dapm_widget *dapm_find_widget(
1835 struct snd_soc_dapm_context *dapm, const char *pin,
1836 bool search_other_contexts)
1838 struct snd_soc_dapm_widget *w;
1839 struct snd_soc_dapm_widget *fallback = NULL;
1841 list_for_each_entry(w, &dapm->card->widgets, list) {
1842 if (!strcmp(w->name, pin)) {
1843 if (w->dapm == dapm)
1850 if (search_other_contexts)
1856 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1857 const char *pin, int status)
1859 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1862 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1866 w->connected = status;
1869 dapm_mark_dirty(w, "pin configuration");
1875 * snd_soc_dapm_sync - scan and power dapm paths
1876 * @dapm: DAPM context
1878 * Walks all dapm audio paths and powers widgets according to their
1879 * stream or path usage.
1881 * Returns 0 for success.
1883 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1886 * Suppress early reports (eg, jacks syncing their state) to avoid
1887 * silly DAPM runs during card startup.
1889 if (!dapm->card || !dapm->card->instantiated)
1892 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1894 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1896 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1897 const struct snd_soc_dapm_route *route)
1899 struct snd_soc_dapm_path *path;
1900 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1901 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1903 const char *control = route->control;
1905 char prefixed_sink[80];
1906 char prefixed_source[80];
1909 if (dapm->codec && dapm->codec->name_prefix) {
1910 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1911 dapm->codec->name_prefix, route->sink);
1912 sink = prefixed_sink;
1913 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1914 dapm->codec->name_prefix, route->source);
1915 source = prefixed_source;
1918 source = route->source;
1922 * find src and dest widgets over all widgets but favor a widget from
1923 * current DAPM context
1925 list_for_each_entry(w, &dapm->card->widgets, list) {
1926 if (!wsink && !(strcmp(w->name, sink))) {
1928 if (w->dapm == dapm)
1932 if (!wsource && !(strcmp(w->name, source))) {
1934 if (w->dapm == dapm)
1938 /* use widget from another DAPM context if not found from this */
1944 if (wsource == NULL || wsink == NULL)
1947 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1951 path->source = wsource;
1953 path->connected = route->connected;
1954 INIT_LIST_HEAD(&path->list);
1955 INIT_LIST_HEAD(&path->list_source);
1956 INIT_LIST_HEAD(&path->list_sink);
1958 /* check for external widgets */
1959 if (wsink->id == snd_soc_dapm_input) {
1960 if (wsource->id == snd_soc_dapm_micbias ||
1961 wsource->id == snd_soc_dapm_mic ||
1962 wsource->id == snd_soc_dapm_line ||
1963 wsource->id == snd_soc_dapm_output)
1966 if (wsource->id == snd_soc_dapm_output) {
1967 if (wsink->id == snd_soc_dapm_spk ||
1968 wsink->id == snd_soc_dapm_hp ||
1969 wsink->id == snd_soc_dapm_line ||
1970 wsink->id == snd_soc_dapm_input)
1974 /* connect static paths */
1975 if (control == NULL) {
1976 list_add(&path->list, &dapm->card->paths);
1977 list_add(&path->list_sink, &wsink->sources);
1978 list_add(&path->list_source, &wsource->sinks);
1983 /* connect dynamic paths */
1984 switch (wsink->id) {
1985 case snd_soc_dapm_adc:
1986 case snd_soc_dapm_dac:
1987 case snd_soc_dapm_pga:
1988 case snd_soc_dapm_out_drv:
1989 case snd_soc_dapm_input:
1990 case snd_soc_dapm_output:
1991 case snd_soc_dapm_siggen:
1992 case snd_soc_dapm_micbias:
1993 case snd_soc_dapm_vmid:
1994 case snd_soc_dapm_pre:
1995 case snd_soc_dapm_post:
1996 case snd_soc_dapm_supply:
1997 case snd_soc_dapm_aif_in:
1998 case snd_soc_dapm_aif_out:
1999 list_add(&path->list, &dapm->card->paths);
2000 list_add(&path->list_sink, &wsink->sources);
2001 list_add(&path->list_source, &wsource->sinks);
2004 case snd_soc_dapm_mux:
2005 case snd_soc_dapm_virt_mux:
2006 case snd_soc_dapm_value_mux:
2007 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2008 &wsink->kcontrol_news[0]);
2012 case snd_soc_dapm_switch:
2013 case snd_soc_dapm_mixer:
2014 case snd_soc_dapm_mixer_named_ctl:
2015 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2019 case snd_soc_dapm_hp:
2020 case snd_soc_dapm_mic:
2021 case snd_soc_dapm_line:
2022 case snd_soc_dapm_spk:
2023 list_add(&path->list, &dapm->card->paths);
2024 list_add(&path->list_sink, &wsink->sources);
2025 list_add(&path->list_source, &wsource->sinks);
2032 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2033 source, control, sink);
2039 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2040 * @dapm: DAPM context
2041 * @route: audio routes
2042 * @num: number of routes
2044 * Connects 2 dapm widgets together via a named audio path. The sink is
2045 * the widget receiving the audio signal, whilst the source is the sender
2046 * of the audio signal.
2048 * Returns 0 for success else error. On error all resources can be freed
2049 * with a call to snd_soc_card_free().
2051 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2052 const struct snd_soc_dapm_route *route, int num)
2056 for (i = 0; i < num; i++) {
2057 ret = snd_soc_dapm_add_route(dapm, route);
2059 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2060 route->source, route->sink);
2068 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2070 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2071 const struct snd_soc_dapm_route *route)
2073 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2076 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2079 struct snd_soc_dapm_path *path;
2083 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2089 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2094 if (route->control || route->connected)
2095 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2096 route->source, route->sink);
2098 list_for_each_entry(path, &source->sinks, list_source) {
2099 if (path->sink == sink) {
2106 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2107 route->source, route->sink);
2109 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2110 count, route->source, route->sink);
2116 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2117 * @dapm: DAPM context
2118 * @route: audio routes
2119 * @num: number of routes
2121 * Mark existing routes matching those specified in the passed array
2122 * as being weak, meaning that they are ignored for the purpose of
2123 * power decisions. The main intended use case is for sidetone paths
2124 * which couple audio between other independent paths if they are both
2125 * active in order to make the combination work better at the user
2126 * level but which aren't intended to be "used".
2128 * Note that CODEC drivers should not use this as sidetone type paths
2129 * can frequently also be used as bypass paths.
2131 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2132 const struct snd_soc_dapm_route *route, int num)
2137 for (i = 0; i < num; i++) {
2138 err = snd_soc_dapm_weak_route(dapm, route);
2146 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2149 * snd_soc_dapm_new_widgets - add new dapm widgets
2150 * @dapm: DAPM context
2152 * Checks the codec for any new dapm widgets and creates them if found.
2154 * Returns 0 for success.
2156 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2158 struct snd_soc_dapm_widget *w;
2161 list_for_each_entry(w, &dapm->card->widgets, list)
2166 if (w->num_kcontrols) {
2167 w->kcontrols = kzalloc(w->num_kcontrols *
2168 sizeof(struct snd_kcontrol *),
2175 case snd_soc_dapm_switch:
2176 case snd_soc_dapm_mixer:
2177 case snd_soc_dapm_mixer_named_ctl:
2180 case snd_soc_dapm_mux:
2181 case snd_soc_dapm_virt_mux:
2182 case snd_soc_dapm_value_mux:
2185 case snd_soc_dapm_pga:
2186 case snd_soc_dapm_out_drv:
2193 /* Read the initial power state from the device */
2195 val = soc_widget_read(w, w->reg);
2196 val &= 1 << w->shift;
2206 dapm_mark_dirty(w, "new widget");
2207 dapm_debugfs_add_widget(w);
2210 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2213 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2216 * snd_soc_dapm_get_volsw - dapm mixer get callback
2217 * @kcontrol: mixer control
2218 * @ucontrol: control element information
2220 * Callback to get the value of a dapm mixer control.
2222 * Returns 0 for success.
2224 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2225 struct snd_ctl_elem_value *ucontrol)
2227 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2228 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2229 struct soc_mixer_control *mc =
2230 (struct soc_mixer_control *)kcontrol->private_value;
2231 unsigned int reg = mc->reg;
2232 unsigned int shift = mc->shift;
2233 unsigned int rshift = mc->rshift;
2235 unsigned int invert = mc->invert;
2236 unsigned int mask = (1 << fls(max)) - 1;
2238 ucontrol->value.integer.value[0] =
2239 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2240 if (shift != rshift)
2241 ucontrol->value.integer.value[1] =
2242 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2244 ucontrol->value.integer.value[0] =
2245 max - ucontrol->value.integer.value[0];
2246 if (shift != rshift)
2247 ucontrol->value.integer.value[1] =
2248 max - ucontrol->value.integer.value[1];
2253 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2256 * snd_soc_dapm_put_volsw - dapm mixer set callback
2257 * @kcontrol: mixer control
2258 * @ucontrol: control element information
2260 * Callback to set the value of a dapm mixer control.
2262 * Returns 0 for success.
2264 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2265 struct snd_ctl_elem_value *ucontrol)
2267 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2268 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2269 struct snd_soc_codec *codec = widget->codec;
2270 struct soc_mixer_control *mc =
2271 (struct soc_mixer_control *)kcontrol->private_value;
2272 unsigned int reg = mc->reg;
2273 unsigned int shift = mc->shift;
2275 unsigned int mask = (1 << fls(max)) - 1;
2276 unsigned int invert = mc->invert;
2278 int connect, change;
2279 struct snd_soc_dapm_update update;
2282 val = (ucontrol->value.integer.value[0] & mask);
2286 mask = mask << shift;
2290 /* new connection */
2291 connect = invert ? 0 : 1;
2293 /* old connection must be powered down */
2294 connect = invert ? 1 : 0;
2296 mutex_lock(&codec->mutex);
2298 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2300 for (wi = 0; wi < wlist->num_widgets; wi++) {
2301 widget = wlist->widgets[wi];
2303 widget->value = val;
2305 update.kcontrol = kcontrol;
2306 update.widget = widget;
2310 widget->dapm->update = &update;
2312 dapm_mixer_update_power(widget, kcontrol, connect);
2314 widget->dapm->update = NULL;
2318 mutex_unlock(&codec->mutex);
2321 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2324 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2325 * @kcontrol: mixer control
2326 * @ucontrol: control element information
2328 * Callback to get the value of a dapm enumerated double mixer control.
2330 * Returns 0 for success.
2332 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2333 struct snd_ctl_elem_value *ucontrol)
2335 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2336 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2337 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2338 unsigned int val, bitmask;
2340 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2342 val = snd_soc_read(widget->codec, e->reg);
2343 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2344 if (e->shift_l != e->shift_r)
2345 ucontrol->value.enumerated.item[1] =
2346 (val >> e->shift_r) & (bitmask - 1);
2350 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2353 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2354 * @kcontrol: mixer control
2355 * @ucontrol: control element information
2357 * Callback to set the value of a dapm enumerated double mixer control.
2359 * Returns 0 for success.
2361 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2362 struct snd_ctl_elem_value *ucontrol)
2364 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2365 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2366 struct snd_soc_codec *codec = widget->codec;
2367 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2368 unsigned int val, mux, change;
2369 unsigned int mask, bitmask;
2370 struct snd_soc_dapm_update update;
2373 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2375 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2377 mux = ucontrol->value.enumerated.item[0];
2378 val = mux << e->shift_l;
2379 mask = (bitmask - 1) << e->shift_l;
2380 if (e->shift_l != e->shift_r) {
2381 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2383 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2384 mask |= (bitmask - 1) << e->shift_r;
2387 mutex_lock(&codec->mutex);
2389 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2391 for (wi = 0; wi < wlist->num_widgets; wi++) {
2392 widget = wlist->widgets[wi];
2394 widget->value = val;
2396 update.kcontrol = kcontrol;
2397 update.widget = widget;
2398 update.reg = e->reg;
2401 widget->dapm->update = &update;
2403 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2405 widget->dapm->update = NULL;
2409 mutex_unlock(&codec->mutex);
2412 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2415 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2416 * @kcontrol: mixer control
2417 * @ucontrol: control element information
2419 * Returns 0 for success.
2421 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_value *ucontrol)
2424 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2425 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2427 ucontrol->value.enumerated.item[0] = widget->value;
2431 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2434 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2435 * @kcontrol: mixer control
2436 * @ucontrol: control element information
2438 * Returns 0 for success.
2440 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_value *ucontrol)
2443 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2444 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2445 struct snd_soc_codec *codec = widget->codec;
2446 struct soc_enum *e =
2447 (struct soc_enum *)kcontrol->private_value;
2452 if (ucontrol->value.enumerated.item[0] >= e->max)
2455 mutex_lock(&codec->mutex);
2457 change = widget->value != ucontrol->value.enumerated.item[0];
2459 for (wi = 0; wi < wlist->num_widgets; wi++) {
2460 widget = wlist->widgets[wi];
2462 widget->value = ucontrol->value.enumerated.item[0];
2464 dapm_mux_update_power(widget, kcontrol, change,
2469 mutex_unlock(&codec->mutex);
2472 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2475 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2477 * @kcontrol: mixer control
2478 * @ucontrol: control element information
2480 * Callback to get the value of a dapm semi enumerated double mixer control.
2482 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2483 * used for handling bitfield coded enumeration for example.
2485 * Returns 0 for success.
2487 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2488 struct snd_ctl_elem_value *ucontrol)
2490 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2491 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2492 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2493 unsigned int reg_val, val, mux;
2495 reg_val = snd_soc_read(widget->codec, e->reg);
2496 val = (reg_val >> e->shift_l) & e->mask;
2497 for (mux = 0; mux < e->max; mux++) {
2498 if (val == e->values[mux])
2501 ucontrol->value.enumerated.item[0] = mux;
2502 if (e->shift_l != e->shift_r) {
2503 val = (reg_val >> e->shift_r) & e->mask;
2504 for (mux = 0; mux < e->max; mux++) {
2505 if (val == e->values[mux])
2508 ucontrol->value.enumerated.item[1] = mux;
2513 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2516 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2518 * @kcontrol: mixer control
2519 * @ucontrol: control element information
2521 * Callback to set the value of a dapm semi enumerated double mixer control.
2523 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2524 * used for handling bitfield coded enumeration for example.
2526 * Returns 0 for success.
2528 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2529 struct snd_ctl_elem_value *ucontrol)
2531 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2532 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2533 struct snd_soc_codec *codec = widget->codec;
2534 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2535 unsigned int val, mux, change;
2537 struct snd_soc_dapm_update update;
2540 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2542 mux = ucontrol->value.enumerated.item[0];
2543 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2544 mask = e->mask << e->shift_l;
2545 if (e->shift_l != e->shift_r) {
2546 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2548 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2549 mask |= e->mask << e->shift_r;
2552 mutex_lock(&codec->mutex);
2554 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2556 for (wi = 0; wi < wlist->num_widgets; wi++) {
2557 widget = wlist->widgets[wi];
2559 widget->value = val;
2561 update.kcontrol = kcontrol;
2562 update.widget = widget;
2563 update.reg = e->reg;
2566 widget->dapm->update = &update;
2568 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2570 widget->dapm->update = NULL;
2574 mutex_unlock(&codec->mutex);
2577 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2580 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2582 * @kcontrol: mixer control
2583 * @uinfo: control element information
2585 * Callback to provide information about a pin switch control.
2587 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2588 struct snd_ctl_elem_info *uinfo)
2590 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2592 uinfo->value.integer.min = 0;
2593 uinfo->value.integer.max = 1;
2597 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2600 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2602 * @kcontrol: mixer control
2605 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_value *ucontrol)
2608 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2609 const char *pin = (const char *)kcontrol->private_value;
2611 mutex_lock(&codec->mutex);
2613 ucontrol->value.integer.value[0] =
2614 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2616 mutex_unlock(&codec->mutex);
2620 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2623 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2625 * @kcontrol: mixer control
2628 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_value *ucontrol)
2631 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2632 const char *pin = (const char *)kcontrol->private_value;
2634 mutex_lock(&codec->mutex);
2636 if (ucontrol->value.integer.value[0])
2637 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2639 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2641 snd_soc_dapm_sync(&codec->dapm);
2643 mutex_unlock(&codec->mutex);
2647 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2650 * snd_soc_dapm_new_control - create new dapm control
2651 * @dapm: DAPM context
2652 * @widget: widget template
2654 * Creates a new dapm control based upon the template.
2656 * Returns 0 for success else error.
2658 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2659 const struct snd_soc_dapm_widget *widget)
2661 struct snd_soc_dapm_widget *w;
2664 if ((w = dapm_cnew_widget(widget)) == NULL)
2667 name_len = strlen(widget->name) + 1;
2668 if (dapm->codec && dapm->codec->name_prefix)
2669 name_len += 1 + strlen(dapm->codec->name_prefix);
2670 w->name = kmalloc(name_len, GFP_KERNEL);
2671 if (w->name == NULL) {
2675 if (dapm->codec && dapm->codec->name_prefix)
2676 snprintf(w->name, name_len, "%s %s",
2677 dapm->codec->name_prefix, widget->name);
2679 snprintf(w->name, name_len, "%s", widget->name);
2682 case snd_soc_dapm_switch:
2683 case snd_soc_dapm_mixer:
2684 case snd_soc_dapm_mixer_named_ctl:
2685 w->power_check = dapm_generic_check_power;
2687 case snd_soc_dapm_mux:
2688 case snd_soc_dapm_virt_mux:
2689 case snd_soc_dapm_value_mux:
2690 w->power_check = dapm_generic_check_power;
2692 case snd_soc_dapm_adc:
2693 case snd_soc_dapm_aif_out:
2694 w->power_check = dapm_adc_check_power;
2696 case snd_soc_dapm_dac:
2697 case snd_soc_dapm_aif_in:
2698 w->power_check = dapm_dac_check_power;
2700 case snd_soc_dapm_pga:
2701 case snd_soc_dapm_out_drv:
2702 case snd_soc_dapm_input:
2703 case snd_soc_dapm_output:
2704 case snd_soc_dapm_micbias:
2705 case snd_soc_dapm_spk:
2706 case snd_soc_dapm_hp:
2707 case snd_soc_dapm_mic:
2708 case snd_soc_dapm_line:
2709 w->power_check = dapm_generic_check_power;
2711 case snd_soc_dapm_supply:
2712 w->power_check = dapm_supply_check_power;
2715 w->power_check = dapm_always_on_check_power;
2721 w->codec = dapm->codec;
2722 w->platform = dapm->platform;
2723 INIT_LIST_HEAD(&w->sources);
2724 INIT_LIST_HEAD(&w->sinks);
2725 INIT_LIST_HEAD(&w->list);
2726 INIT_LIST_HEAD(&w->dirty);
2727 list_add(&w->list, &dapm->card->widgets);
2729 /* machine layer set ups unconnected pins and insertions */
2733 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2736 * snd_soc_dapm_new_controls - create new dapm controls
2737 * @dapm: DAPM context
2738 * @widget: widget array
2739 * @num: number of widgets
2741 * Creates new DAPM controls based upon the templates.
2743 * Returns 0 for success else error.
2745 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2746 const struct snd_soc_dapm_widget *widget,
2751 for (i = 0; i < num; i++) {
2752 ret = snd_soc_dapm_new_control(dapm, widget);
2755 "ASoC: Failed to create DAPM control %s: %d\n",
2763 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2765 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2766 const char *stream, int event)
2768 struct snd_soc_dapm_widget *w;
2770 list_for_each_entry(w, &dapm->card->widgets, list)
2772 if (!w->sname || w->dapm != dapm)
2774 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2775 w->name, w->sname, stream, event);
2776 if (strstr(w->sname, stream)) {
2777 dapm_mark_dirty(w, "stream event");
2779 case SND_SOC_DAPM_STREAM_START:
2782 case SND_SOC_DAPM_STREAM_STOP:
2785 case SND_SOC_DAPM_STREAM_SUSPEND:
2786 case SND_SOC_DAPM_STREAM_RESUME:
2787 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2788 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2794 dapm_power_widgets(dapm, event);
2796 /* do we need to notify any clients that DAPM stream is complete */
2797 if (dapm->stream_event)
2798 dapm->stream_event(dapm, event);
2802 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2803 * @rtd: PCM runtime data
2804 * @stream: stream name
2805 * @event: stream event
2807 * Sends a stream event to the dapm core. The core then makes any
2808 * necessary widget power changes.
2810 * Returns 0 for success else error.
2812 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2813 const char *stream, int event)
2815 struct snd_soc_codec *codec = rtd->codec;
2820 mutex_lock(&codec->mutex);
2821 soc_dapm_stream_event(&codec->dapm, stream, event);
2822 mutex_unlock(&codec->mutex);
2827 * snd_soc_dapm_enable_pin - enable pin.
2828 * @dapm: DAPM context
2831 * Enables input/output pin and its parents or children widgets iff there is
2832 * a valid audio route and active audio stream.
2833 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2834 * do any widget power switching.
2836 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2838 return snd_soc_dapm_set_pin(dapm, pin, 1);
2840 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2843 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2844 * @dapm: DAPM context
2847 * Enables input/output pin regardless of any other state. This is
2848 * intended for use with microphone bias supplies used in microphone
2851 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2852 * do any widget power switching.
2854 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2857 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2860 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2864 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2867 dapm_mark_dirty(w, "force enable");
2871 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2874 * snd_soc_dapm_disable_pin - disable pin.
2875 * @dapm: DAPM context
2878 * Disables input/output pin and its parents or children widgets.
2879 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2880 * do any widget power switching.
2882 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2885 return snd_soc_dapm_set_pin(dapm, pin, 0);
2887 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2890 * snd_soc_dapm_nc_pin - permanently disable pin.
2891 * @dapm: DAPM context
2894 * Marks the specified pin as being not connected, disabling it along
2895 * any parent or child widgets. At present this is identical to
2896 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2897 * additional things such as disabling controls which only affect
2898 * paths through the pin.
2900 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2901 * do any widget power switching.
2903 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2905 return snd_soc_dapm_set_pin(dapm, pin, 0);
2907 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2910 * snd_soc_dapm_get_pin_status - get audio pin status
2911 * @dapm: DAPM context
2912 * @pin: audio signal pin endpoint (or start point)
2914 * Get audio pin status - connected or disconnected.
2916 * Returns 1 for connected otherwise 0.
2918 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2921 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2924 return w->connected;
2928 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2931 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2932 * @dapm: DAPM context
2933 * @pin: audio signal pin endpoint (or start point)
2935 * Mark the given endpoint or pin as ignoring suspend. When the
2936 * system is disabled a path between two endpoints flagged as ignoring
2937 * suspend will not be disabled. The path must already be enabled via
2938 * normal means at suspend time, it will not be turned on if it was not
2941 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2944 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2947 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2951 w->ignore_suspend = 1;
2955 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2957 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
2958 struct snd_soc_dapm_widget *w)
2960 struct snd_soc_dapm_path *p;
2962 list_for_each_entry(p, &card->paths, list) {
2963 if ((p->source == w) || (p->sink == w)) {
2965 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
2966 p->source->name, p->source->id, p->source->dapm,
2967 p->sink->name, p->sink->id, p->sink->dapm);
2969 /* Connected to something other than the codec */
2970 if (p->source->dapm != p->sink->dapm)
2973 * Loopback connection from codec external pin to
2974 * codec external pin
2976 if (p->sink->id == snd_soc_dapm_input) {
2977 switch (p->source->id) {
2978 case snd_soc_dapm_output:
2979 case snd_soc_dapm_micbias:
2992 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
2993 * @codec: The codec whose pins should be processed
2995 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
2996 * which are unused. Pins are used if they are connected externally to the
2997 * codec, whether that be to some other device, or a loop-back connection to
3000 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3002 struct snd_soc_card *card = codec->card;
3003 struct snd_soc_dapm_context *dapm = &codec->dapm;
3004 struct snd_soc_dapm_widget *w;
3006 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
3007 &card->dapm, &codec->dapm);
3009 list_for_each_entry(w, &card->widgets, list) {
3010 if (w->dapm != dapm)
3013 case snd_soc_dapm_input:
3014 case snd_soc_dapm_output:
3015 case snd_soc_dapm_micbias:
3016 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
3018 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3020 "... Not in map; disabling\n");
3021 snd_soc_dapm_nc_pin(dapm, w->name);
3031 * snd_soc_dapm_free - free dapm resources
3032 * @dapm: DAPM context
3034 * Free all dapm widgets and resources.
3036 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3038 snd_soc_dapm_sys_remove(dapm->dev);
3039 dapm_debugfs_cleanup(dapm);
3040 dapm_free_widgets(dapm);
3041 list_del(&dapm->list);
3043 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3045 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3047 struct snd_soc_dapm_widget *w;
3048 LIST_HEAD(down_list);
3051 list_for_each_entry(w, &dapm->card->widgets, list) {
3052 if (w->dapm != dapm)
3055 dapm_seq_insert(w, &down_list, false);
3061 /* If there were no widgets to power down we're already in
3065 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
3066 dapm_seq_run(dapm, &down_list, 0, false);
3067 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
3072 * snd_soc_dapm_shutdown - callback for system shutdown
3074 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3076 struct snd_soc_codec *codec;
3078 list_for_each_entry(codec, &card->codec_dev_list, list) {
3079 soc_dapm_shutdown_codec(&codec->dapm);
3080 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
3084 /* Module information */
3085 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3086 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3087 MODULE_LICENSE("GPL");