2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/headphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed power down of audio subsystem to reduce pops between a quick
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/async.h>
30 #include <linux/delay.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/debugfs.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/slab.h>
39 #include <sound/core.h>
40 #include <sound/pcm.h>
41 #include <sound/pcm_params.h>
42 #include <sound/soc.h>
43 #include <sound/initval.h>
45 #include <trace/events/asoc.h>
47 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49 /* dapm power sequences - make this per codec in the future */
50 static int dapm_up_seq[] = {
51 [snd_soc_dapm_pre] = 0,
52 [snd_soc_dapm_supply] = 1,
53 [snd_soc_dapm_regulator_supply] = 1,
54 [snd_soc_dapm_micbias] = 2,
55 [snd_soc_dapm_dai] = 3,
56 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
60 [snd_soc_dapm_virt_mux] = 5,
61 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
67 [snd_soc_dapm_out_drv] = 10,
68 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
70 [snd_soc_dapm_line] = 10,
71 [snd_soc_dapm_post] = 11,
74 static int dapm_down_seq[] = {
75 [snd_soc_dapm_pre] = 0,
76 [snd_soc_dapm_adc] = 1,
77 [snd_soc_dapm_hp] = 2,
78 [snd_soc_dapm_spk] = 2,
79 [snd_soc_dapm_line] = 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_dai] = 10,
93 [snd_soc_dapm_regulator_supply] = 11,
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 void dapm_reset(struct snd_soc_card *card)
178 struct snd_soc_dapm_widget *w;
180 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
182 list_for_each_entry(w, &card->widgets, list) {
183 w->power_checked = false;
189 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
192 return snd_soc_read(w->codec, reg);
193 else if (w->platform)
194 return snd_soc_platform_read(w->platform, reg);
196 dev_err(w->dapm->dev, "no valid widget read method\n");
200 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
203 return snd_soc_write(w->codec, reg, val);
204 else if (w->platform)
205 return snd_soc_platform_write(w->platform, reg, val);
207 dev_err(w->dapm->dev, "no valid widget write method\n");
211 static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
212 unsigned short reg, unsigned int mask, unsigned int value)
215 unsigned int old, new;
218 if (w->codec && w->codec->using_regmap) {
219 ret = regmap_update_bits_check(w->codec->control_data,
220 reg, mask, value, &change);
224 ret = soc_widget_read(w, reg);
229 new = (old & ~mask) | (value & mask);
232 ret = soc_widget_write(w, reg, new);
242 * snd_soc_dapm_set_bias_level - set the bias level for the system
243 * @dapm: DAPM context
244 * @level: level to configure
246 * Configure the bias (power) levels for the SoC audio device.
248 * Returns 0 for success else error.
250 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
251 enum snd_soc_bias_level level)
253 struct snd_soc_card *card = dapm->card;
256 trace_snd_soc_bias_level_start(card, level);
258 if (card && card->set_bias_level)
259 ret = card->set_bias_level(card, dapm, level);
264 if (dapm->codec->driver->set_bias_level)
265 ret = dapm->codec->driver->set_bias_level(dapm->codec,
268 dapm->bias_level = level;
273 if (card && card->set_bias_level_post)
274 ret = card->set_bias_level_post(card, dapm, level);
276 trace_snd_soc_bias_level_done(card, level);
281 /* set up initial codec paths */
282 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
283 struct snd_soc_dapm_path *p, int i)
286 case snd_soc_dapm_switch:
287 case snd_soc_dapm_mixer:
288 case snd_soc_dapm_mixer_named_ctl: {
290 struct soc_mixer_control *mc = (struct soc_mixer_control *)
291 w->kcontrol_news[i].private_value;
292 unsigned int reg = mc->reg;
293 unsigned int shift = mc->shift;
295 unsigned int mask = (1 << fls(max)) - 1;
296 unsigned int invert = mc->invert;
298 val = soc_widget_read(w, reg);
299 val = (val >> shift) & mask;
301 if ((invert && !val) || (!invert && val))
307 case snd_soc_dapm_mux: {
308 struct soc_enum *e = (struct soc_enum *)
309 w->kcontrol_news[i].private_value;
310 int val, item, bitmask;
312 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
314 val = soc_widget_read(w, e->reg);
315 item = (val >> e->shift_l) & (bitmask - 1);
318 for (i = 0; i < e->max; i++) {
319 if (!(strcmp(p->name, e->texts[i])) && item == i)
324 case snd_soc_dapm_virt_mux: {
325 struct soc_enum *e = (struct soc_enum *)
326 w->kcontrol_news[i].private_value;
329 /* since a virtual mux has no backing registers to
330 * decide which path to connect, it will try to match
331 * with the first enumeration. This is to ensure
332 * that the default mux choice (the first) will be
333 * correctly powered up during initialization.
335 if (!strcmp(p->name, e->texts[0]))
339 case snd_soc_dapm_value_mux: {
340 struct soc_enum *e = (struct soc_enum *)
341 w->kcontrol_news[i].private_value;
344 val = soc_widget_read(w, e->reg);
345 val = (val >> e->shift_l) & e->mask;
346 for (item = 0; item < e->max; item++) {
347 if (val == e->values[item])
352 for (i = 0; i < e->max; i++) {
353 if (!(strcmp(p->name, e->texts[i])) && item == i)
358 /* does not affect routing - always connected */
359 case snd_soc_dapm_pga:
360 case snd_soc_dapm_out_drv:
361 case snd_soc_dapm_output:
362 case snd_soc_dapm_adc:
363 case snd_soc_dapm_input:
364 case snd_soc_dapm_siggen:
365 case snd_soc_dapm_dac:
366 case snd_soc_dapm_micbias:
367 case snd_soc_dapm_vmid:
368 case snd_soc_dapm_supply:
369 case snd_soc_dapm_regulator_supply:
370 case snd_soc_dapm_aif_in:
371 case snd_soc_dapm_aif_out:
372 case snd_soc_dapm_dai:
373 case snd_soc_dapm_hp:
374 case snd_soc_dapm_mic:
375 case snd_soc_dapm_spk:
376 case snd_soc_dapm_line:
379 /* does affect routing - dynamically connected */
380 case snd_soc_dapm_pre:
381 case snd_soc_dapm_post:
387 /* connect mux widget to its interconnecting audio paths */
388 static int dapm_connect_mux(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,
391 const struct snd_kcontrol_new *kcontrol)
393 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
396 for (i = 0; i < e->max; i++) {
397 if (!(strcmp(control_name, e->texts[i]))) {
398 list_add(&path->list, &dapm->card->paths);
399 list_add(&path->list_sink, &dest->sources);
400 list_add(&path->list_source, &src->sinks);
401 path->name = (char*)e->texts[i];
402 dapm_set_path_status(dest, path, 0);
410 /* connect mixer widget to its interconnecting audio paths */
411 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
412 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
413 struct snd_soc_dapm_path *path, const char *control_name)
417 /* search for mixer kcontrol */
418 for (i = 0; i < dest->num_kcontrols; i++) {
419 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
420 list_add(&path->list, &dapm->card->paths);
421 list_add(&path->list_sink, &dest->sources);
422 list_add(&path->list_source, &src->sinks);
423 path->name = dest->kcontrol_news[i].name;
424 dapm_set_path_status(dest, path, i);
431 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
432 struct snd_soc_dapm_widget *kcontrolw,
433 const struct snd_kcontrol_new *kcontrol_new,
434 struct snd_kcontrol **kcontrol)
436 struct snd_soc_dapm_widget *w;
441 list_for_each_entry(w, &dapm->card->widgets, list) {
442 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
444 for (i = 0; i < w->num_kcontrols; i++) {
445 if (&w->kcontrol_news[i] == kcontrol_new) {
447 *kcontrol = w->kcontrols[i];
456 /* create new dapm mixer control */
457 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
459 struct snd_soc_dapm_context *dapm = w->dapm;
461 size_t name_len, prefix_len;
462 struct snd_soc_dapm_path *path;
463 struct snd_card *card = dapm->card->snd_card;
465 struct snd_soc_dapm_widget_list *wlist;
469 prefix = dapm->codec->name_prefix;
474 prefix_len = strlen(prefix) + 1;
479 for (i = 0; i < w->num_kcontrols; i++) {
482 list_for_each_entry(path, &w->sources, list_sink) {
484 /* mixer/mux paths name must match control name */
485 if (path->name != (char *)w->kcontrol_news[i].name)
488 if (w->kcontrols[i]) {
489 path->kcontrol = w->kcontrols[i];
493 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
494 sizeof(struct snd_soc_dapm_widget *),
495 wlist = kzalloc(wlistsize, GFP_KERNEL);
498 "asoc: can't allocate widget list for %s\n",
502 wlist->num_widgets = 1;
503 wlist->widgets[0] = w;
505 /* add dapm control with long name.
506 * for dapm_mixer this is the concatenation of the
507 * mixer and kcontrol name.
508 * for dapm_mixer_named_ctl this is simply the
511 name_len = strlen(w->kcontrol_news[i].name) + 1;
512 if (w->id != snd_soc_dapm_mixer_named_ctl)
513 name_len += 1 + strlen(w->name);
515 path->long_name = kmalloc(name_len, GFP_KERNEL);
517 if (path->long_name == NULL) {
524 /* The control will get a prefix from
525 * the control creation process but
526 * we're also using the same prefix
527 * for widgets so cut the prefix off
528 * the front of the widget name.
530 snprintf((char *)path->long_name, name_len,
531 "%s %s", w->name + prefix_len,
532 w->kcontrol_news[i].name);
534 case snd_soc_dapm_mixer_named_ctl:
535 snprintf((char *)path->long_name, name_len,
536 "%s", w->kcontrol_news[i].name);
540 ((char *)path->long_name)[name_len - 1] = '\0';
542 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
543 wlist, path->long_name,
545 ret = snd_ctl_add(card, path->kcontrol);
548 "asoc: failed to add dapm kcontrol %s: %d\n",
549 path->long_name, ret);
551 kfree(path->long_name);
552 path->long_name = NULL;
555 w->kcontrols[i] = path->kcontrol;
561 /* create new dapm mux control */
562 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
564 struct snd_soc_dapm_context *dapm = w->dapm;
565 struct snd_soc_dapm_path *path = NULL;
566 struct snd_kcontrol *kcontrol;
567 struct snd_card *card = dapm->card->snd_card;
571 struct snd_soc_dapm_widget_list *wlist;
572 int shared, wlistentries;
576 if (w->num_kcontrols != 1) {
578 "asoc: mux %s has incorrect number of controls\n",
583 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
586 wlist = kcontrol->private_data;
587 wlistentries = wlist->num_widgets + 1;
592 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
593 wlistentries * sizeof(struct snd_soc_dapm_widget *),
594 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
597 "asoc: can't allocate widget list for %s\n", w->name);
600 wlist->num_widgets = wlistentries;
601 wlist->widgets[wlistentries - 1] = w;
605 prefix = dapm->codec->name_prefix;
610 name = w->kcontrol_news[0].name;
615 prefix_len = strlen(prefix) + 1;
621 * The control will get a prefix from the control creation
622 * process but we're also using the same prefix for widgets so
623 * cut the prefix off the front of the widget name.
625 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
626 name + prefix_len, prefix);
627 ret = snd_ctl_add(card, kcontrol);
629 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
636 kcontrol->private_data = wlist;
638 w->kcontrols[0] = kcontrol;
640 list_for_each_entry(path, &w->sources, list_sink)
641 path->kcontrol = kcontrol;
646 /* create new dapm volume control */
647 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
649 if (w->num_kcontrols)
650 dev_err(w->dapm->dev,
651 "asoc: PGA controls not supported: '%s'\n", w->name);
656 /* reset 'walked' bit for each dapm path */
657 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
659 struct snd_soc_dapm_path *p;
661 list_for_each_entry(p, &dapm->card->paths, list)
665 /* We implement power down on suspend by checking the power state of
666 * the ALSA card - when we are suspending the ALSA state for the card
669 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
671 int level = snd_power_get_state(widget->dapm->card->snd_card);
674 case SNDRV_CTL_POWER_D3hot:
675 case SNDRV_CTL_POWER_D3cold:
676 if (widget->ignore_suspend)
677 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
679 return widget->ignore_suspend;
686 * Recursively check for a completed path to an active or physically connected
687 * output widget. Returns number of complete paths.
689 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
691 struct snd_soc_dapm_path *path;
694 if (widget->outputs >= 0)
695 return widget->outputs;
697 DAPM_UPDATE_STAT(widget, path_checks);
699 switch (widget->id) {
700 case snd_soc_dapm_supply:
701 case snd_soc_dapm_regulator_supply:
707 switch (widget->id) {
708 case snd_soc_dapm_adc:
709 case snd_soc_dapm_aif_out:
710 case snd_soc_dapm_dai:
711 if (widget->active) {
712 widget->outputs = snd_soc_dapm_suspend_check(widget);
713 return widget->outputs;
719 if (widget->connected) {
720 /* connected pin ? */
721 if (widget->id == snd_soc_dapm_output && !widget->ext) {
722 widget->outputs = snd_soc_dapm_suspend_check(widget);
723 return widget->outputs;
726 /* connected jack or spk ? */
727 if (widget->id == snd_soc_dapm_hp ||
728 widget->id == snd_soc_dapm_spk ||
729 (widget->id == snd_soc_dapm_line &&
730 !list_empty(&widget->sources))) {
731 widget->outputs = snd_soc_dapm_suspend_check(widget);
732 return widget->outputs;
736 list_for_each_entry(path, &widget->sinks, list_source) {
737 DAPM_UPDATE_STAT(widget, neighbour_checks);
745 if (path->sink && path->connect) {
747 con += is_connected_output_ep(path->sink);
751 widget->outputs = con;
757 * Recursively check for a completed path to an active or physically connected
758 * input widget. Returns number of complete paths.
760 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
762 struct snd_soc_dapm_path *path;
765 if (widget->inputs >= 0)
766 return widget->inputs;
768 DAPM_UPDATE_STAT(widget, path_checks);
770 switch (widget->id) {
771 case snd_soc_dapm_supply:
772 case snd_soc_dapm_regulator_supply:
778 /* active stream ? */
779 switch (widget->id) {
780 case snd_soc_dapm_dac:
781 case snd_soc_dapm_aif_in:
782 case snd_soc_dapm_dai:
783 if (widget->active) {
784 widget->inputs = snd_soc_dapm_suspend_check(widget);
785 return widget->inputs;
791 if (widget->connected) {
792 /* connected pin ? */
793 if (widget->id == snd_soc_dapm_input && !widget->ext) {
794 widget->inputs = snd_soc_dapm_suspend_check(widget);
795 return widget->inputs;
798 /* connected VMID/Bias for lower pops */
799 if (widget->id == snd_soc_dapm_vmid) {
800 widget->inputs = snd_soc_dapm_suspend_check(widget);
801 return widget->inputs;
804 /* connected jack ? */
805 if (widget->id == snd_soc_dapm_mic ||
806 (widget->id == snd_soc_dapm_line &&
807 !list_empty(&widget->sinks))) {
808 widget->inputs = snd_soc_dapm_suspend_check(widget);
809 return widget->inputs;
812 /* signal generator */
813 if (widget->id == snd_soc_dapm_siggen) {
814 widget->inputs = snd_soc_dapm_suspend_check(widget);
815 return widget->inputs;
819 list_for_each_entry(path, &widget->sources, list_sink) {
820 DAPM_UPDATE_STAT(widget, neighbour_checks);
828 if (path->source && path->connect) {
830 con += is_connected_input_ep(path->source);
834 widget->inputs = con;
840 * Handler for generic register modifier widget.
842 int dapm_reg_event(struct snd_soc_dapm_widget *w,
843 struct snd_kcontrol *kcontrol, int event)
847 if (SND_SOC_DAPM_EVENT_ON(event))
852 soc_widget_update_bits(w, -(w->reg + 1),
853 w->mask << w->shift, val << w->shift);
857 EXPORT_SYMBOL_GPL(dapm_reg_event);
860 * Handler for regulator supply widget.
862 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
863 struct snd_kcontrol *kcontrol, int event)
865 if (SND_SOC_DAPM_EVENT_ON(event))
866 return regulator_enable(w->priv);
868 return regulator_disable_deferred(w->priv, w->shift);
870 EXPORT_SYMBOL_GPL(dapm_regulator_event);
872 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
874 if (w->power_checked)
880 w->new_power = w->power_check(w);
882 w->power_checked = true;
887 /* Generic check to see if a widget should be powered.
889 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
893 DAPM_UPDATE_STAT(w, power_checks);
895 in = is_connected_input_ep(w);
896 dapm_clear_walk(w->dapm);
897 out = is_connected_output_ep(w);
898 dapm_clear_walk(w->dapm);
899 return out != 0 && in != 0;
902 static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
904 DAPM_UPDATE_STAT(w, power_checks);
909 /* Check to see if an ADC has power */
910 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
914 DAPM_UPDATE_STAT(w, power_checks);
917 in = is_connected_input_ep(w);
918 dapm_clear_walk(w->dapm);
921 return dapm_generic_check_power(w);
925 /* Check to see if a DAC has power */
926 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
930 DAPM_UPDATE_STAT(w, power_checks);
933 out = is_connected_output_ep(w);
934 dapm_clear_walk(w->dapm);
937 return dapm_generic_check_power(w);
941 /* Check to see if a power supply is needed */
942 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
944 struct snd_soc_dapm_path *path;
946 DAPM_UPDATE_STAT(w, power_checks);
948 /* Check if one of our outputs is connected */
949 list_for_each_entry(path, &w->sinks, list_source) {
950 DAPM_UPDATE_STAT(w, neighbour_checks);
955 if (path->connected &&
956 !path->connected(path->source, path->sink))
962 if (dapm_widget_power_check(path->sink))
966 dapm_clear_walk(w->dapm);
971 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
976 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
977 struct snd_soc_dapm_widget *b,
985 sort = dapm_down_seq;
987 if (sort[a->id] != sort[b->id])
988 return sort[a->id] - sort[b->id];
989 if (a->subseq != b->subseq) {
991 return a->subseq - b->subseq;
993 return b->subseq - a->subseq;
995 if (a->reg != b->reg)
996 return a->reg - b->reg;
997 if (a->dapm != b->dapm)
998 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1003 /* Insert a widget in order into a DAPM power sequence. */
1004 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1005 struct list_head *list,
1008 struct snd_soc_dapm_widget *w;
1010 list_for_each_entry(w, list, power_list)
1011 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1012 list_add_tail(&new_widget->power_list, &w->power_list);
1016 list_add_tail(&new_widget->power_list, list);
1019 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1020 struct snd_soc_dapm_widget *w, int event)
1022 struct snd_soc_card *card = dapm->card;
1023 const char *ev_name;
1027 case SND_SOC_DAPM_PRE_PMU:
1028 ev_name = "PRE_PMU";
1031 case SND_SOC_DAPM_POST_PMU:
1032 ev_name = "POST_PMU";
1035 case SND_SOC_DAPM_PRE_PMD:
1036 ev_name = "PRE_PMD";
1039 case SND_SOC_DAPM_POST_PMD:
1040 ev_name = "POST_PMD";
1048 if (w->power != power)
1051 if (w->event && (w->event_flags & event)) {
1052 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1054 trace_snd_soc_dapm_widget_event_start(w, event);
1055 ret = w->event(w, NULL, event);
1056 trace_snd_soc_dapm_widget_event_done(w, event);
1058 pr_err("%s: %s event failed: %d\n",
1059 ev_name, w->name, ret);
1063 /* Apply the coalesced changes from a DAPM sequence */
1064 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1065 struct list_head *pending)
1067 struct snd_soc_card *card = dapm->card;
1068 struct snd_soc_dapm_widget *w;
1070 unsigned int value = 0;
1071 unsigned int mask = 0;
1072 unsigned int cur_mask;
1074 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1077 list_for_each_entry(w, pending, power_list) {
1078 cur_mask = 1 << w->shift;
1079 BUG_ON(reg != w->reg);
1090 pop_dbg(dapm->dev, card->pop_time,
1091 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1092 w->name, reg, value, mask);
1094 /* Check for events */
1095 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1096 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1100 /* Any widget will do, they should all be updating the
1103 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1106 pop_dbg(dapm->dev, card->pop_time,
1107 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1108 value, mask, reg, card->pop_time);
1109 pop_wait(card->pop_time);
1110 soc_widget_update_bits(w, reg, mask, value);
1113 list_for_each_entry(w, pending, power_list) {
1114 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1115 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1119 /* Apply a DAPM power sequence.
1121 * We walk over a pre-sorted list of widgets to apply power to. In
1122 * order to minimise the number of writes to the device required
1123 * multiple widgets will be updated in a single write where possible.
1124 * Currently anything that requires more than a single write is not
1127 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1128 struct list_head *list, int event, bool power_up)
1130 struct snd_soc_dapm_widget *w, *n;
1133 int cur_subseq = -1;
1134 int cur_reg = SND_SOC_NOPM;
1135 struct snd_soc_dapm_context *cur_dapm = NULL;
1142 sort = dapm_down_seq;
1144 list_for_each_entry_safe(w, n, list, power_list) {
1147 /* Do we need to apply any queued changes? */
1148 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1149 w->dapm != cur_dapm || w->subseq != cur_subseq) {
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 INIT_LIST_HEAD(&pending);
1163 cur_subseq = INT_MIN;
1164 cur_reg = SND_SOC_NOPM;
1169 case snd_soc_dapm_pre:
1171 list_for_each_entry_safe_continue(w, n, list,
1174 if (event == SND_SOC_DAPM_STREAM_START)
1176 NULL, SND_SOC_DAPM_PRE_PMU);
1177 else if (event == SND_SOC_DAPM_STREAM_STOP)
1179 NULL, SND_SOC_DAPM_PRE_PMD);
1182 case snd_soc_dapm_post:
1184 list_for_each_entry_safe_continue(w, n, list,
1187 if (event == SND_SOC_DAPM_STREAM_START)
1189 NULL, SND_SOC_DAPM_POST_PMU);
1190 else if (event == SND_SOC_DAPM_STREAM_STOP)
1192 NULL, SND_SOC_DAPM_POST_PMD);
1196 /* Queue it up for application */
1197 cur_sort = sort[w->id];
1198 cur_subseq = w->subseq;
1201 list_move(&w->power_list, &pending);
1206 dev_err(w->dapm->dev,
1207 "Failed to apply widget power: %d\n", ret);
1210 if (!list_empty(&pending))
1211 dapm_seq_run_coalesced(cur_dapm, &pending);
1213 if (cur_dapm && cur_dapm->seq_notifier) {
1214 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1215 if (sort[i] == cur_sort)
1216 cur_dapm->seq_notifier(cur_dapm,
1221 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1223 struct snd_soc_dapm_update *update = dapm->update;
1224 struct snd_soc_dapm_widget *w;
1233 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1234 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1236 pr_err("%s DAPM pre-event failed: %d\n",
1240 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1243 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1246 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1247 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1249 pr_err("%s DAPM post-event failed: %d\n",
1254 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1255 * they're changing state.
1257 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1259 struct snd_soc_dapm_context *d = data;
1262 /* If we're off and we're not supposed to be go into STANDBY */
1263 if (d->bias_level == SND_SOC_BIAS_OFF &&
1264 d->target_bias_level != SND_SOC_BIAS_OFF) {
1266 pm_runtime_get_sync(d->dev);
1268 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1271 "Failed to turn on bias: %d\n", ret);
1274 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1275 if (d->bias_level != d->target_bias_level) {
1276 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1279 "Failed to prepare bias: %d\n", ret);
1283 /* Async callback run prior to DAPM sequences - brings to their final
1286 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1288 struct snd_soc_dapm_context *d = data;
1291 /* If we just powered the last thing off drop to standby bias */
1292 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1293 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1294 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1295 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1297 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1301 /* If we're in standby and can support bias off then do that */
1302 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1303 d->target_bias_level == SND_SOC_BIAS_OFF) {
1304 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1306 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1309 pm_runtime_put(d->dev);
1312 /* If we just powered up then move to active bias */
1313 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1314 d->target_bias_level == SND_SOC_BIAS_ON) {
1315 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1317 dev_err(d->dev, "Failed to apply active bias: %d\n",
1322 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1323 bool power, bool connect)
1325 /* If a connection is being made or broken then that update
1326 * will have marked the peer dirty, otherwise the widgets are
1327 * not connected and this update has no impact. */
1331 /* If the peer is already in the state we're moving to then we
1332 * won't have an impact on it. */
1333 if (power != peer->power)
1334 dapm_mark_dirty(peer, "peer state change");
1337 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1338 struct list_head *up_list,
1339 struct list_head *down_list)
1341 struct snd_soc_dapm_path *path;
1343 if (w->power == power)
1346 trace_snd_soc_dapm_widget_power(w, power);
1348 /* If we changed our power state perhaps our neigbours changed
1351 list_for_each_entry(path, &w->sources, list_sink) {
1353 dapm_widget_set_peer_power(path->source, power,
1358 case snd_soc_dapm_supply:
1359 case snd_soc_dapm_regulator_supply:
1360 /* Supplies can't affect their outputs, only their inputs */
1363 list_for_each_entry(path, &w->sinks, list_source) {
1365 dapm_widget_set_peer_power(path->sink, power,
1373 dapm_seq_insert(w, up_list, true);
1375 dapm_seq_insert(w, down_list, false);
1380 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1381 struct list_head *up_list,
1382 struct list_head *down_list)
1387 case snd_soc_dapm_pre:
1388 dapm_seq_insert(w, down_list, false);
1390 case snd_soc_dapm_post:
1391 dapm_seq_insert(w, up_list, true);
1395 power = dapm_widget_power_check(w);
1397 dapm_widget_set_power(w, power, up_list, down_list);
1403 * Scan each dapm widget for complete audio path.
1404 * A complete path is a route that has valid endpoints i.e.:-
1406 * o DAC to output pin.
1407 * o Input Pin to ADC.
1408 * o Input pin to Output pin (bypass, sidetone)
1409 * o DAC to ADC (loopback).
1411 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1413 struct snd_soc_card *card = dapm->card;
1414 struct snd_soc_dapm_widget *w;
1415 struct snd_soc_dapm_context *d;
1417 LIST_HEAD(down_list);
1418 LIST_HEAD(async_domain);
1419 enum snd_soc_bias_level bias;
1421 trace_snd_soc_dapm_start(card);
1423 list_for_each_entry(d, &card->dapm_list, list) {
1424 if (d->n_widgets || d->codec == NULL) {
1425 if (d->idle_bias_off)
1426 d->target_bias_level = SND_SOC_BIAS_OFF;
1428 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1434 /* Check which widgets we need to power and store them in
1435 * lists indicating if they should be powered up or down. We
1436 * only check widgets that have been flagged as dirty but note
1437 * that new widgets may be added to the dirty list while we
1440 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1441 dapm_power_one_widget(w, &up_list, &down_list);
1444 list_for_each_entry(w, &card->widgets, list) {
1445 list_del_init(&w->dirty);
1450 /* Supplies and micbiases only bring the
1451 * context up to STANDBY as unless something
1452 * else is active and passing audio they
1453 * generally don't require full power. Signal
1454 * generators are virtual pins and have no
1455 * power impact themselves.
1458 case snd_soc_dapm_siggen:
1460 case snd_soc_dapm_supply:
1461 case snd_soc_dapm_regulator_supply:
1462 case snd_soc_dapm_micbias:
1463 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1464 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1467 d->target_bias_level = SND_SOC_BIAS_ON;
1474 /* If there are no DAPM widgets then try to figure out power from the
1477 if (!dapm->n_widgets) {
1479 case SND_SOC_DAPM_STREAM_START:
1480 case SND_SOC_DAPM_STREAM_RESUME:
1481 dapm->target_bias_level = SND_SOC_BIAS_ON;
1483 case SND_SOC_DAPM_STREAM_STOP:
1484 if (dapm->codec && dapm->codec->active)
1485 dapm->target_bias_level = SND_SOC_BIAS_ON;
1487 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1489 case SND_SOC_DAPM_STREAM_SUSPEND:
1490 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1492 case SND_SOC_DAPM_STREAM_NOP:
1493 dapm->target_bias_level = dapm->bias_level;
1500 /* Force all contexts in the card to the same bias state if
1501 * they're not ground referenced.
1503 bias = SND_SOC_BIAS_OFF;
1504 list_for_each_entry(d, &card->dapm_list, list)
1505 if (d->target_bias_level > bias)
1506 bias = d->target_bias_level;
1507 list_for_each_entry(d, &card->dapm_list, list)
1508 if (!d->idle_bias_off)
1509 d->target_bias_level = bias;
1511 trace_snd_soc_dapm_walk_done(card);
1513 /* Run all the bias changes in parallel */
1514 list_for_each_entry(d, &dapm->card->dapm_list, list)
1515 async_schedule_domain(dapm_pre_sequence_async, d,
1517 async_synchronize_full_domain(&async_domain);
1519 /* Power down widgets first; try to avoid amplifying pops. */
1520 dapm_seq_run(dapm, &down_list, event, false);
1522 dapm_widget_update(dapm);
1525 dapm_seq_run(dapm, &up_list, event, true);
1527 /* Run all the bias changes in parallel */
1528 list_for_each_entry(d, &dapm->card->dapm_list, list)
1529 async_schedule_domain(dapm_post_sequence_async, d,
1531 async_synchronize_full_domain(&async_domain);
1533 /* do we need to notify any clients that DAPM event is complete */
1534 list_for_each_entry(d, &card->dapm_list, list) {
1535 if (d->stream_event)
1536 d->stream_event(d, event);
1539 pop_dbg(dapm->dev, card->pop_time,
1540 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1541 pop_wait(card->pop_time);
1543 trace_snd_soc_dapm_done(card);
1548 #ifdef CONFIG_DEBUG_FS
1549 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1551 file->private_data = inode->i_private;
1555 static ssize_t dapm_widget_power_read_file(struct file *file,
1556 char __user *user_buf,
1557 size_t count, loff_t *ppos)
1559 struct snd_soc_dapm_widget *w = file->private_data;
1563 struct snd_soc_dapm_path *p = NULL;
1565 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1569 in = is_connected_input_ep(w);
1570 dapm_clear_walk(w->dapm);
1571 out = is_connected_output_ep(w);
1572 dapm_clear_walk(w->dapm);
1574 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1575 w->name, w->power ? "On" : "Off",
1576 w->force ? " (forced)" : "", in, out);
1579 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1580 " - R%d(0x%x) bit %d",
1581 w->reg, w->reg, w->shift);
1583 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1586 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1588 w->active ? "active" : "inactive");
1590 list_for_each_entry(p, &w->sources, list_sink) {
1591 if (p->connected && !p->connected(w, p->sink))
1595 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1596 " in \"%s\" \"%s\"\n",
1597 p->name ? p->name : "static",
1600 list_for_each_entry(p, &w->sinks, list_source) {
1601 if (p->connected && !p->connected(w, p->sink))
1605 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1606 " out \"%s\" \"%s\"\n",
1607 p->name ? p->name : "static",
1611 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1617 static const struct file_operations dapm_widget_power_fops = {
1618 .open = dapm_widget_power_open_file,
1619 .read = dapm_widget_power_read_file,
1620 .llseek = default_llseek,
1623 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1625 file->private_data = inode->i_private;
1629 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1630 size_t count, loff_t *ppos)
1632 struct snd_soc_dapm_context *dapm = file->private_data;
1635 switch (dapm->bias_level) {
1636 case SND_SOC_BIAS_ON:
1639 case SND_SOC_BIAS_PREPARE:
1640 level = "Prepare\n";
1642 case SND_SOC_BIAS_STANDBY:
1643 level = "Standby\n";
1645 case SND_SOC_BIAS_OFF:
1650 level = "Unknown\n";
1654 return simple_read_from_buffer(user_buf, count, ppos, level,
1658 static const struct file_operations dapm_bias_fops = {
1659 .open = dapm_bias_open_file,
1660 .read = dapm_bias_read_file,
1661 .llseek = default_llseek,
1664 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1665 struct dentry *parent)
1669 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1671 if (!dapm->debugfs_dapm) {
1673 "Failed to create DAPM debugfs directory\n");
1677 d = debugfs_create_file("bias_level", 0444,
1678 dapm->debugfs_dapm, dapm,
1682 "ASoC: Failed to create bias level debugfs file\n");
1685 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1687 struct snd_soc_dapm_context *dapm = w->dapm;
1690 if (!dapm->debugfs_dapm || !w->name)
1693 d = debugfs_create_file(w->name, 0444,
1694 dapm->debugfs_dapm, w,
1695 &dapm_widget_power_fops);
1697 dev_warn(w->dapm->dev,
1698 "ASoC: Failed to create %s debugfs file\n",
1702 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1704 debugfs_remove_recursive(dapm->debugfs_dapm);
1708 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1709 struct dentry *parent)
1713 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1717 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1723 /* test and update the power status of a mux widget */
1724 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1725 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1727 struct snd_soc_dapm_path *path;
1730 if (widget->id != snd_soc_dapm_mux &&
1731 widget->id != snd_soc_dapm_virt_mux &&
1732 widget->id != snd_soc_dapm_value_mux)
1735 /* find dapm widget path assoc with kcontrol */
1736 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1737 if (path->kcontrol != kcontrol)
1740 if (!path->name || !e->texts[mux])
1744 /* we now need to match the string in the enum to the path */
1745 if (!(strcmp(path->name, e->texts[mux]))) {
1746 path->connect = 1; /* new connection */
1747 dapm_mark_dirty(path->source, "mux connection");
1750 dapm_mark_dirty(path->source,
1751 "mux disconnection");
1752 path->connect = 0; /* old connection must be powered down */
1757 dapm_mark_dirty(widget, "mux change");
1758 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1763 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1765 /* test and update the power status of a mixer or switch widget */
1766 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1767 struct snd_kcontrol *kcontrol, int connect)
1769 struct snd_soc_dapm_path *path;
1772 if (widget->id != snd_soc_dapm_mixer &&
1773 widget->id != snd_soc_dapm_mixer_named_ctl &&
1774 widget->id != snd_soc_dapm_switch)
1777 /* find dapm widget path assoc with kcontrol */
1778 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1779 if (path->kcontrol != kcontrol)
1782 /* found, now check type */
1784 path->connect = connect;
1785 dapm_mark_dirty(path->source, "mixer connection");
1789 dapm_mark_dirty(widget, "mixer update");
1790 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1795 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
1797 /* show dapm widget status in sys fs */
1798 static ssize_t dapm_widget_show(struct device *dev,
1799 struct device_attribute *attr, char *buf)
1801 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
1802 struct snd_soc_codec *codec =rtd->codec;
1803 struct snd_soc_dapm_widget *w;
1805 char *state = "not set";
1807 list_for_each_entry(w, &codec->card->widgets, list) {
1808 if (w->dapm != &codec->dapm)
1811 /* only display widgets that burnm power */
1813 case snd_soc_dapm_hp:
1814 case snd_soc_dapm_mic:
1815 case snd_soc_dapm_spk:
1816 case snd_soc_dapm_line:
1817 case snd_soc_dapm_micbias:
1818 case snd_soc_dapm_dac:
1819 case snd_soc_dapm_adc:
1820 case snd_soc_dapm_pga:
1821 case snd_soc_dapm_out_drv:
1822 case snd_soc_dapm_mixer:
1823 case snd_soc_dapm_mixer_named_ctl:
1824 case snd_soc_dapm_supply:
1825 case snd_soc_dapm_regulator_supply:
1827 count += sprintf(buf + count, "%s: %s\n",
1828 w->name, w->power ? "On":"Off");
1835 switch (codec->dapm.bias_level) {
1836 case SND_SOC_BIAS_ON:
1839 case SND_SOC_BIAS_PREPARE:
1842 case SND_SOC_BIAS_STANDBY:
1845 case SND_SOC_BIAS_OFF:
1849 count += sprintf(buf + count, "PM State: %s\n", state);
1854 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1856 int snd_soc_dapm_sys_add(struct device *dev)
1858 return device_create_file(dev, &dev_attr_dapm_widget);
1861 static void snd_soc_dapm_sys_remove(struct device *dev)
1863 device_remove_file(dev, &dev_attr_dapm_widget);
1866 /* free all dapm widgets and resources */
1867 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1869 struct snd_soc_dapm_widget *w, *next_w;
1870 struct snd_soc_dapm_path *p, *next_p;
1872 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1873 if (w->dapm != dapm)
1877 * remove source and sink paths associated to this widget.
1878 * While removing the path, remove reference to it from both
1879 * source and sink widgets so that path is removed only once.
1881 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1882 list_del(&p->list_sink);
1883 list_del(&p->list_source);
1885 kfree(p->long_name);
1888 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1889 list_del(&p->list_sink);
1890 list_del(&p->list_source);
1892 kfree(p->long_name);
1895 kfree(w->kcontrols);
1901 static struct snd_soc_dapm_widget *dapm_find_widget(
1902 struct snd_soc_dapm_context *dapm, const char *pin,
1903 bool search_other_contexts)
1905 struct snd_soc_dapm_widget *w;
1906 struct snd_soc_dapm_widget *fallback = NULL;
1908 list_for_each_entry(w, &dapm->card->widgets, list) {
1909 if (!strcmp(w->name, pin)) {
1910 if (w->dapm == dapm)
1917 if (search_other_contexts)
1923 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1924 const char *pin, int status)
1926 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1929 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1933 if (w->connected != status)
1934 dapm_mark_dirty(w, "pin configuration");
1936 w->connected = status;
1944 * snd_soc_dapm_sync - scan and power dapm paths
1945 * @dapm: DAPM context
1947 * Walks all dapm audio paths and powers widgets according to their
1948 * stream or path usage.
1950 * Returns 0 for success.
1952 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1955 * Suppress early reports (eg, jacks syncing their state) to avoid
1956 * silly DAPM runs during card startup.
1958 if (!dapm->card || !dapm->card->instantiated)
1961 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1963 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1965 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1966 const struct snd_soc_dapm_route *route)
1968 struct snd_soc_dapm_path *path;
1969 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1970 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1972 const char *control = route->control;
1974 char prefixed_sink[80];
1975 char prefixed_source[80];
1978 if (dapm->codec && dapm->codec->name_prefix) {
1979 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1980 dapm->codec->name_prefix, route->sink);
1981 sink = prefixed_sink;
1982 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1983 dapm->codec->name_prefix, route->source);
1984 source = prefixed_source;
1987 source = route->source;
1991 * find src and dest widgets over all widgets but favor a widget from
1992 * current DAPM context
1994 list_for_each_entry(w, &dapm->card->widgets, list) {
1995 if (!wsink && !(strcmp(w->name, sink))) {
1997 if (w->dapm == dapm)
2001 if (!wsource && !(strcmp(w->name, source))) {
2003 if (w->dapm == dapm)
2007 /* use widget from another DAPM context if not found from this */
2013 if (wsource == NULL || wsink == NULL)
2016 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2020 path->source = wsource;
2022 path->connected = route->connected;
2023 INIT_LIST_HEAD(&path->list);
2024 INIT_LIST_HEAD(&path->list_source);
2025 INIT_LIST_HEAD(&path->list_sink);
2027 /* check for external widgets */
2028 if (wsink->id == snd_soc_dapm_input) {
2029 if (wsource->id == snd_soc_dapm_micbias ||
2030 wsource->id == snd_soc_dapm_mic ||
2031 wsource->id == snd_soc_dapm_line ||
2032 wsource->id == snd_soc_dapm_output)
2035 if (wsource->id == snd_soc_dapm_output) {
2036 if (wsink->id == snd_soc_dapm_spk ||
2037 wsink->id == snd_soc_dapm_hp ||
2038 wsink->id == snd_soc_dapm_line ||
2039 wsink->id == snd_soc_dapm_input)
2043 /* connect static paths */
2044 if (control == NULL) {
2045 list_add(&path->list, &dapm->card->paths);
2046 list_add(&path->list_sink, &wsink->sources);
2047 list_add(&path->list_source, &wsource->sinks);
2052 /* connect dynamic paths */
2053 switch (wsink->id) {
2054 case snd_soc_dapm_adc:
2055 case snd_soc_dapm_dac:
2056 case snd_soc_dapm_pga:
2057 case snd_soc_dapm_out_drv:
2058 case snd_soc_dapm_input:
2059 case snd_soc_dapm_output:
2060 case snd_soc_dapm_siggen:
2061 case snd_soc_dapm_micbias:
2062 case snd_soc_dapm_vmid:
2063 case snd_soc_dapm_pre:
2064 case snd_soc_dapm_post:
2065 case snd_soc_dapm_supply:
2066 case snd_soc_dapm_regulator_supply:
2067 case snd_soc_dapm_aif_in:
2068 case snd_soc_dapm_aif_out:
2069 case snd_soc_dapm_dai:
2070 list_add(&path->list, &dapm->card->paths);
2071 list_add(&path->list_sink, &wsink->sources);
2072 list_add(&path->list_source, &wsource->sinks);
2075 case snd_soc_dapm_mux:
2076 case snd_soc_dapm_virt_mux:
2077 case snd_soc_dapm_value_mux:
2078 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2079 &wsink->kcontrol_news[0]);
2083 case snd_soc_dapm_switch:
2084 case snd_soc_dapm_mixer:
2085 case snd_soc_dapm_mixer_named_ctl:
2086 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2090 case snd_soc_dapm_hp:
2091 case snd_soc_dapm_mic:
2092 case snd_soc_dapm_line:
2093 case snd_soc_dapm_spk:
2094 list_add(&path->list, &dapm->card->paths);
2095 list_add(&path->list_sink, &wsink->sources);
2096 list_add(&path->list_source, &wsource->sinks);
2103 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2104 source, control, sink);
2110 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2111 * @dapm: DAPM context
2112 * @route: audio routes
2113 * @num: number of routes
2115 * Connects 2 dapm widgets together via a named audio path. The sink is
2116 * the widget receiving the audio signal, whilst the source is the sender
2117 * of the audio signal.
2119 * Returns 0 for success else error. On error all resources can be freed
2120 * with a call to snd_soc_card_free().
2122 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2123 const struct snd_soc_dapm_route *route, int num)
2127 for (i = 0; i < num; i++) {
2128 ret = snd_soc_dapm_add_route(dapm, route);
2130 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2131 route->source, route->sink);
2139 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2141 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2142 const struct snd_soc_dapm_route *route)
2144 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2147 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2150 struct snd_soc_dapm_path *path;
2154 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2160 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2165 if (route->control || route->connected)
2166 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2167 route->source, route->sink);
2169 list_for_each_entry(path, &source->sinks, list_source) {
2170 if (path->sink == sink) {
2177 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2178 route->source, route->sink);
2180 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2181 count, route->source, route->sink);
2187 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2188 * @dapm: DAPM context
2189 * @route: audio routes
2190 * @num: number of routes
2192 * Mark existing routes matching those specified in the passed array
2193 * as being weak, meaning that they are ignored for the purpose of
2194 * power decisions. The main intended use case is for sidetone paths
2195 * which couple audio between other independent paths if they are both
2196 * active in order to make the combination work better at the user
2197 * level but which aren't intended to be "used".
2199 * Note that CODEC drivers should not use this as sidetone type paths
2200 * can frequently also be used as bypass paths.
2202 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2203 const struct snd_soc_dapm_route *route, int num)
2208 for (i = 0; i < num; i++) {
2209 err = snd_soc_dapm_weak_route(dapm, route);
2217 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2220 * snd_soc_dapm_new_widgets - add new dapm widgets
2221 * @dapm: DAPM context
2223 * Checks the codec for any new dapm widgets and creates them if found.
2225 * Returns 0 for success.
2227 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2229 struct snd_soc_dapm_widget *w;
2232 list_for_each_entry(w, &dapm->card->widgets, list)
2237 if (w->num_kcontrols) {
2238 w->kcontrols = kzalloc(w->num_kcontrols *
2239 sizeof(struct snd_kcontrol *),
2246 case snd_soc_dapm_switch:
2247 case snd_soc_dapm_mixer:
2248 case snd_soc_dapm_mixer_named_ctl:
2251 case snd_soc_dapm_mux:
2252 case snd_soc_dapm_virt_mux:
2253 case snd_soc_dapm_value_mux:
2256 case snd_soc_dapm_pga:
2257 case snd_soc_dapm_out_drv:
2264 /* Read the initial power state from the device */
2266 val = soc_widget_read(w, w->reg);
2267 val &= 1 << w->shift;
2277 dapm_mark_dirty(w, "new widget");
2278 dapm_debugfs_add_widget(w);
2281 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2284 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2287 * snd_soc_dapm_get_volsw - dapm mixer get callback
2288 * @kcontrol: mixer control
2289 * @ucontrol: control element information
2291 * Callback to get the value of a dapm mixer control.
2293 * Returns 0 for success.
2295 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2296 struct snd_ctl_elem_value *ucontrol)
2298 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2299 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2300 struct soc_mixer_control *mc =
2301 (struct soc_mixer_control *)kcontrol->private_value;
2302 unsigned int reg = mc->reg;
2303 unsigned int shift = mc->shift;
2304 unsigned int rshift = mc->rshift;
2306 unsigned int invert = mc->invert;
2307 unsigned int mask = (1 << fls(max)) - 1;
2309 ucontrol->value.integer.value[0] =
2310 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2311 if (shift != rshift)
2312 ucontrol->value.integer.value[1] =
2313 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2315 ucontrol->value.integer.value[0] =
2316 max - ucontrol->value.integer.value[0];
2317 if (shift != rshift)
2318 ucontrol->value.integer.value[1] =
2319 max - ucontrol->value.integer.value[1];
2324 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2327 * snd_soc_dapm_put_volsw - dapm mixer set callback
2328 * @kcontrol: mixer control
2329 * @ucontrol: control element information
2331 * Callback to set the value of a dapm mixer control.
2333 * Returns 0 for success.
2335 int snd_soc_dapm_put_volsw(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 snd_soc_codec *codec = widget->codec;
2341 struct soc_mixer_control *mc =
2342 (struct soc_mixer_control *)kcontrol->private_value;
2343 unsigned int reg = mc->reg;
2344 unsigned int shift = mc->shift;
2346 unsigned int mask = (1 << fls(max)) - 1;
2347 unsigned int invert = mc->invert;
2349 int connect, change;
2350 struct snd_soc_dapm_update update;
2353 val = (ucontrol->value.integer.value[0] & mask);
2357 mask = mask << shift;
2361 /* new connection */
2362 connect = invert ? 0 : 1;
2364 /* old connection must be powered down */
2365 connect = invert ? 1 : 0;
2367 mutex_lock(&codec->mutex);
2369 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2371 for (wi = 0; wi < wlist->num_widgets; wi++) {
2372 widget = wlist->widgets[wi];
2374 widget->value = val;
2376 update.kcontrol = kcontrol;
2377 update.widget = widget;
2381 widget->dapm->update = &update;
2383 snd_soc_dapm_mixer_update_power(widget, kcontrol, connect);
2385 widget->dapm->update = NULL;
2389 mutex_unlock(&codec->mutex);
2392 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2395 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2396 * @kcontrol: mixer control
2397 * @ucontrol: control element information
2399 * Callback to get the value of a dapm enumerated double mixer control.
2401 * Returns 0 for success.
2403 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_value *ucontrol)
2406 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2407 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2408 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2409 unsigned int val, bitmask;
2411 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2413 val = snd_soc_read(widget->codec, e->reg);
2414 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2415 if (e->shift_l != e->shift_r)
2416 ucontrol->value.enumerated.item[1] =
2417 (val >> e->shift_r) & (bitmask - 1);
2421 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2424 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2425 * @kcontrol: mixer control
2426 * @ucontrol: control element information
2428 * Callback to set the value of a dapm enumerated double mixer control.
2430 * Returns 0 for success.
2432 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_value *ucontrol)
2435 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2436 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2437 struct snd_soc_codec *codec = widget->codec;
2438 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2439 unsigned int val, mux, change;
2440 unsigned int mask, bitmask;
2441 struct snd_soc_dapm_update update;
2444 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2446 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2448 mux = ucontrol->value.enumerated.item[0];
2449 val = mux << e->shift_l;
2450 mask = (bitmask - 1) << e->shift_l;
2451 if (e->shift_l != e->shift_r) {
2452 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2454 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2455 mask |= (bitmask - 1) << e->shift_r;
2458 mutex_lock(&codec->mutex);
2460 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2462 for (wi = 0; wi < wlist->num_widgets; wi++) {
2463 widget = wlist->widgets[wi];
2465 widget->value = val;
2467 update.kcontrol = kcontrol;
2468 update.widget = widget;
2469 update.reg = e->reg;
2472 widget->dapm->update = &update;
2474 snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2476 widget->dapm->update = NULL;
2480 mutex_unlock(&codec->mutex);
2483 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2486 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2487 * @kcontrol: mixer control
2488 * @ucontrol: control element information
2490 * Returns 0 for success.
2492 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2493 struct snd_ctl_elem_value *ucontrol)
2495 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2496 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2498 ucontrol->value.enumerated.item[0] = widget->value;
2502 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2505 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2506 * @kcontrol: mixer control
2507 * @ucontrol: control element information
2509 * Returns 0 for success.
2511 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2512 struct snd_ctl_elem_value *ucontrol)
2514 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2515 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2516 struct snd_soc_codec *codec = widget->codec;
2517 struct soc_enum *e =
2518 (struct soc_enum *)kcontrol->private_value;
2523 if (ucontrol->value.enumerated.item[0] >= e->max)
2526 mutex_lock(&codec->mutex);
2528 change = widget->value != ucontrol->value.enumerated.item[0];
2530 for (wi = 0; wi < wlist->num_widgets; wi++) {
2531 widget = wlist->widgets[wi];
2533 widget->value = ucontrol->value.enumerated.item[0];
2535 snd_soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
2539 mutex_unlock(&codec->mutex);
2542 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2545 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2547 * @kcontrol: mixer control
2548 * @ucontrol: control element information
2550 * Callback to get the value of a dapm semi enumerated double mixer control.
2552 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2553 * used for handling bitfield coded enumeration for example.
2555 * Returns 0 for success.
2557 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
2560 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2561 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2562 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2563 unsigned int reg_val, val, mux;
2565 reg_val = snd_soc_read(widget->codec, e->reg);
2566 val = (reg_val >> e->shift_l) & e->mask;
2567 for (mux = 0; mux < e->max; mux++) {
2568 if (val == e->values[mux])
2571 ucontrol->value.enumerated.item[0] = mux;
2572 if (e->shift_l != e->shift_r) {
2573 val = (reg_val >> e->shift_r) & e->mask;
2574 for (mux = 0; mux < e->max; mux++) {
2575 if (val == e->values[mux])
2578 ucontrol->value.enumerated.item[1] = mux;
2583 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2586 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2588 * @kcontrol: mixer control
2589 * @ucontrol: control element information
2591 * Callback to set the value of a dapm semi enumerated double mixer control.
2593 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2594 * used for handling bitfield coded enumeration for example.
2596 * Returns 0 for success.
2598 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2601 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2602 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2603 struct snd_soc_codec *codec = widget->codec;
2604 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2605 unsigned int val, mux, change;
2607 struct snd_soc_dapm_update update;
2610 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2612 mux = ucontrol->value.enumerated.item[0];
2613 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2614 mask = e->mask << e->shift_l;
2615 if (e->shift_l != e->shift_r) {
2616 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2618 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2619 mask |= e->mask << e->shift_r;
2622 mutex_lock(&codec->mutex);
2624 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2626 for (wi = 0; wi < wlist->num_widgets; wi++) {
2627 widget = wlist->widgets[wi];
2629 widget->value = val;
2631 update.kcontrol = kcontrol;
2632 update.widget = widget;
2633 update.reg = e->reg;
2636 widget->dapm->update = &update;
2638 snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2640 widget->dapm->update = NULL;
2644 mutex_unlock(&codec->mutex);
2647 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2650 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2652 * @kcontrol: mixer control
2653 * @uinfo: control element information
2655 * Callback to provide information about a pin switch control.
2657 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2658 struct snd_ctl_elem_info *uinfo)
2660 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2662 uinfo->value.integer.min = 0;
2663 uinfo->value.integer.max = 1;
2667 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2670 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2672 * @kcontrol: mixer control
2675 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2676 struct snd_ctl_elem_value *ucontrol)
2678 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2679 const char *pin = (const char *)kcontrol->private_value;
2681 mutex_lock(&card->mutex);
2683 ucontrol->value.integer.value[0] =
2684 snd_soc_dapm_get_pin_status(&card->dapm, pin);
2686 mutex_unlock(&card->mutex);
2690 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2693 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2695 * @kcontrol: mixer control
2698 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2699 struct snd_ctl_elem_value *ucontrol)
2701 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2702 const char *pin = (const char *)kcontrol->private_value;
2704 mutex_lock(&card->mutex);
2706 if (ucontrol->value.integer.value[0])
2707 snd_soc_dapm_enable_pin(&card->dapm, pin);
2709 snd_soc_dapm_disable_pin(&card->dapm, pin);
2711 snd_soc_dapm_sync(&card->dapm);
2713 mutex_unlock(&card->mutex);
2717 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2719 static struct snd_soc_dapm_widget *
2720 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2721 const struct snd_soc_dapm_widget *widget)
2723 struct snd_soc_dapm_widget *w;
2727 if ((w = dapm_cnew_widget(widget)) == NULL)
2731 case snd_soc_dapm_regulator_supply:
2732 w->priv = devm_regulator_get(dapm->dev, w->name);
2733 if (IS_ERR(w->priv)) {
2734 ret = PTR_ERR(w->priv);
2735 dev_err(dapm->dev, "Failed to request %s: %d\n",
2744 name_len = strlen(widget->name) + 1;
2745 if (dapm->codec && dapm->codec->name_prefix)
2746 name_len += 1 + strlen(dapm->codec->name_prefix);
2747 w->name = kmalloc(name_len, GFP_KERNEL);
2748 if (w->name == NULL) {
2752 if (dapm->codec && dapm->codec->name_prefix)
2753 snprintf((char *)w->name, name_len, "%s %s",
2754 dapm->codec->name_prefix, widget->name);
2756 snprintf((char *)w->name, name_len, "%s", widget->name);
2759 case snd_soc_dapm_switch:
2760 case snd_soc_dapm_mixer:
2761 case snd_soc_dapm_mixer_named_ctl:
2762 w->power_check = dapm_generic_check_power;
2764 case snd_soc_dapm_mux:
2765 case snd_soc_dapm_virt_mux:
2766 case snd_soc_dapm_value_mux:
2767 w->power_check = dapm_generic_check_power;
2769 case snd_soc_dapm_adc:
2770 case snd_soc_dapm_aif_out:
2771 w->power_check = dapm_adc_check_power;
2773 case snd_soc_dapm_dac:
2774 case snd_soc_dapm_aif_in:
2775 w->power_check = dapm_dac_check_power;
2777 case snd_soc_dapm_pga:
2778 case snd_soc_dapm_out_drv:
2779 case snd_soc_dapm_input:
2780 case snd_soc_dapm_output:
2781 case snd_soc_dapm_micbias:
2782 case snd_soc_dapm_spk:
2783 case snd_soc_dapm_hp:
2784 case snd_soc_dapm_mic:
2785 case snd_soc_dapm_line:
2786 w->power_check = dapm_generic_check_power;
2788 case snd_soc_dapm_supply:
2789 case snd_soc_dapm_regulator_supply:
2790 w->power_check = dapm_supply_check_power;
2792 case snd_soc_dapm_dai:
2793 w->power_check = dapm_dai_check_power;
2796 w->power_check = dapm_always_on_check_power;
2802 w->codec = dapm->codec;
2803 w->platform = dapm->platform;
2804 INIT_LIST_HEAD(&w->sources);
2805 INIT_LIST_HEAD(&w->sinks);
2806 INIT_LIST_HEAD(&w->list);
2807 INIT_LIST_HEAD(&w->dirty);
2808 list_add(&w->list, &dapm->card->widgets);
2810 /* machine layer set ups unconnected pins and insertions */
2816 * snd_soc_dapm_new_controls - create new dapm controls
2817 * @dapm: DAPM context
2818 * @widget: widget array
2819 * @num: number of widgets
2821 * Creates new DAPM controls based upon the templates.
2823 * Returns 0 for success else error.
2825 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2826 const struct snd_soc_dapm_widget *widget,
2829 struct snd_soc_dapm_widget *w;
2832 for (i = 0; i < num; i++) {
2833 w = snd_soc_dapm_new_control(dapm, widget);
2836 "ASoC: Failed to create DAPM control %s\n",
2844 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2846 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
2847 struct snd_soc_dai *dai)
2849 struct snd_soc_dapm_widget template;
2850 struct snd_soc_dapm_widget *w;
2852 WARN_ON(dapm->dev != dai->dev);
2854 memset(&template, 0, sizeof(template));
2855 template.reg = SND_SOC_NOPM;
2857 if (dai->driver->playback.stream_name) {
2858 template.id = snd_soc_dapm_dai;
2859 template.name = dai->driver->playback.stream_name;
2860 template.sname = dai->driver->playback.stream_name;
2862 dev_dbg(dai->dev, "adding %s widget\n",
2865 w = snd_soc_dapm_new_control(dapm, &template);
2867 dev_err(dapm->dev, "Failed to create %s widget\n",
2868 dai->driver->playback.stream_name);
2872 dai->playback_widget = w;
2875 if (dai->driver->capture.stream_name) {
2876 template.id = snd_soc_dapm_dai;
2877 template.name = dai->driver->capture.stream_name;
2878 template.sname = dai->driver->capture.stream_name;
2880 dev_dbg(dai->dev, "adding %s widget\n",
2883 w = snd_soc_dapm_new_control(dapm, &template);
2885 dev_err(dapm->dev, "Failed to create %s widget\n",
2886 dai->driver->capture.stream_name);
2890 dai->capture_widget = w;
2896 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
2898 struct snd_soc_dapm_widget *dai_w, *w;
2899 struct snd_soc_dai *dai;
2900 struct snd_soc_dapm_route r;
2902 memset(&r, 0, sizeof(r));
2904 /* For each DAI widget... */
2905 list_for_each_entry(dai_w, &card->widgets, list) {
2906 if (dai_w->id != snd_soc_dapm_dai)
2911 /* ...find all widgets with the same stream and link them */
2912 list_for_each_entry(w, &card->widgets, list) {
2913 if (w->dapm != dai_w->dapm)
2916 if (w->id == snd_soc_dapm_dai)
2922 if (dai->driver->playback.stream_name &&
2924 dai->driver->playback.stream_name)) {
2925 r.source = dai->playback_widget->name;
2927 dev_dbg(dai->dev, "%s -> %s\n",
2930 snd_soc_dapm_add_route(w->dapm, &r);
2933 if (dai->driver->capture.stream_name &&
2935 dai->driver->capture.stream_name)) {
2937 r.sink = dai->capture_widget->name;
2938 dev_dbg(dai->dev, "%s -> %s\n",
2941 snd_soc_dapm_add_route(w->dapm, &r);
2949 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2950 int stream, struct snd_soc_dai *dai,
2953 struct snd_soc_dapm_widget *w;
2955 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2956 w = dai->playback_widget;
2958 w = dai->capture_widget;
2963 dapm_mark_dirty(w, "stream event");
2966 case SND_SOC_DAPM_STREAM_START:
2969 case SND_SOC_DAPM_STREAM_STOP:
2972 case SND_SOC_DAPM_STREAM_SUSPEND:
2973 case SND_SOC_DAPM_STREAM_RESUME:
2974 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2975 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2979 dapm_power_widgets(dapm, event);
2983 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2984 * @rtd: PCM runtime data
2985 * @stream: stream name
2986 * @event: stream event
2988 * Sends a stream event to the dapm core. The core then makes any
2989 * necessary widget power changes.
2991 * Returns 0 for success else error.
2993 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
2994 struct snd_soc_dai *dai, int event)
2996 struct snd_soc_codec *codec = rtd->codec;
2998 mutex_lock(&codec->mutex);
2999 soc_dapm_stream_event(&codec->dapm, stream, dai, event);
3000 mutex_unlock(&codec->mutex);
3005 * snd_soc_dapm_enable_pin - enable pin.
3006 * @dapm: DAPM context
3009 * Enables input/output pin and its parents or children widgets iff there is
3010 * a valid audio route and active audio stream.
3011 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3012 * do any widget power switching.
3014 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3016 return snd_soc_dapm_set_pin(dapm, pin, 1);
3018 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3021 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3022 * @dapm: DAPM context
3025 * Enables input/output pin regardless of any other state. This is
3026 * intended for use with microphone bias supplies used in microphone
3029 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3030 * do any widget power switching.
3032 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3035 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3038 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3042 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3045 dapm_mark_dirty(w, "force enable");
3049 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3052 * snd_soc_dapm_disable_pin - disable pin.
3053 * @dapm: DAPM context
3056 * Disables input/output pin and its parents or children widgets.
3057 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3058 * do any widget power switching.
3060 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3063 return snd_soc_dapm_set_pin(dapm, pin, 0);
3065 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3068 * snd_soc_dapm_nc_pin - permanently disable pin.
3069 * @dapm: DAPM context
3072 * Marks the specified pin as being not connected, disabling it along
3073 * any parent or child widgets. At present this is identical to
3074 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3075 * additional things such as disabling controls which only affect
3076 * paths through the pin.
3078 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3079 * do any widget power switching.
3081 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3083 return snd_soc_dapm_set_pin(dapm, pin, 0);
3085 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3088 * snd_soc_dapm_get_pin_status - get audio pin status
3089 * @dapm: DAPM context
3090 * @pin: audio signal pin endpoint (or start point)
3092 * Get audio pin status - connected or disconnected.
3094 * Returns 1 for connected otherwise 0.
3096 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3099 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3102 return w->connected;
3106 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3109 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3110 * @dapm: DAPM context
3111 * @pin: audio signal pin endpoint (or start point)
3113 * Mark the given endpoint or pin as ignoring suspend. When the
3114 * system is disabled a path between two endpoints flagged as ignoring
3115 * suspend will not be disabled. The path must already be enabled via
3116 * normal means at suspend time, it will not be turned on if it was not
3119 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3122 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3125 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3129 w->ignore_suspend = 1;
3133 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3135 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3136 struct snd_soc_dapm_widget *w)
3138 struct snd_soc_dapm_path *p;
3140 list_for_each_entry(p, &card->paths, list) {
3141 if ((p->source == w) || (p->sink == w)) {
3143 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3144 p->source->name, p->source->id, p->source->dapm,
3145 p->sink->name, p->sink->id, p->sink->dapm);
3147 /* Connected to something other than the codec */
3148 if (p->source->dapm != p->sink->dapm)
3151 * Loopback connection from codec external pin to
3152 * codec external pin
3154 if (p->sink->id == snd_soc_dapm_input) {
3155 switch (p->source->id) {
3156 case snd_soc_dapm_output:
3157 case snd_soc_dapm_micbias:
3170 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3171 * @codec: The codec whose pins should be processed
3173 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3174 * which are unused. Pins are used if they are connected externally to the
3175 * codec, whether that be to some other device, or a loop-back connection to
3178 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3180 struct snd_soc_card *card = codec->card;
3181 struct snd_soc_dapm_context *dapm = &codec->dapm;
3182 struct snd_soc_dapm_widget *w;
3184 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
3185 &card->dapm, &codec->dapm);
3187 list_for_each_entry(w, &card->widgets, list) {
3188 if (w->dapm != dapm)
3191 case snd_soc_dapm_input:
3192 case snd_soc_dapm_output:
3193 case snd_soc_dapm_micbias:
3194 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
3196 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3198 "... Not in map; disabling\n");
3199 snd_soc_dapm_nc_pin(dapm, w->name);
3209 * snd_soc_dapm_free - free dapm resources
3210 * @dapm: DAPM context
3212 * Free all dapm widgets and resources.
3214 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3216 snd_soc_dapm_sys_remove(dapm->dev);
3217 dapm_debugfs_cleanup(dapm);
3218 dapm_free_widgets(dapm);
3219 list_del(&dapm->list);
3221 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3223 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3225 struct snd_soc_dapm_widget *w;
3226 LIST_HEAD(down_list);
3229 list_for_each_entry(w, &dapm->card->widgets, list) {
3230 if (w->dapm != dapm)
3233 dapm_seq_insert(w, &down_list, false);
3239 /* If there were no widgets to power down we're already in
3243 if (dapm->bias_level == SND_SOC_BIAS_ON)
3244 snd_soc_dapm_set_bias_level(dapm,
3245 SND_SOC_BIAS_PREPARE);
3246 dapm_seq_run(dapm, &down_list, 0, false);
3247 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3248 snd_soc_dapm_set_bias_level(dapm,
3249 SND_SOC_BIAS_STANDBY);
3254 * snd_soc_dapm_shutdown - callback for system shutdown
3256 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3258 struct snd_soc_codec *codec;
3260 list_for_each_entry(codec, &card->codec_dev_list, list) {
3261 soc_dapm_shutdown_codec(&codec->dapm);
3262 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3263 snd_soc_dapm_set_bias_level(&codec->dapm,
3268 /* Module information */
3269 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3270 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3271 MODULE_LICENSE("GPL");