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 /* dapm power sequences - make this per codec in the future */
52 static int dapm_up_seq[] = {
53 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
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_post] = 11,
73 static int dapm_down_seq[] = {
74 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_adc] = 1,
76 [snd_soc_dapm_hp] = 2,
77 [snd_soc_dapm_spk] = 2,
78 [snd_soc_dapm_out_drv] = 2,
79 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
81 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
86 [snd_soc_dapm_virt_mux] = 9,
87 [snd_soc_dapm_value_mux] = 9,
88 [snd_soc_dapm_aif_in] = 10,
89 [snd_soc_dapm_aif_out] = 10,
90 [snd_soc_dapm_supply] = 11,
91 [snd_soc_dapm_post] = 12,
94 static void pop_wait(u32 pop_time)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
100 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
113 vsnprintf(buf, PAGE_SIZE, fmt, args);
114 dev_info(dev, "%s", buf);
120 /* create a new dapm widget */
121 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
122 const struct snd_soc_dapm_widget *_widget)
124 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
128 * snd_soc_dapm_set_bias_level - set the bias level for the system
129 * @dapm: DAPM context
130 * @level: level to configure
132 * Configure the bias (power) levels for the SoC audio device.
134 * Returns 0 for success else error.
136 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
137 enum snd_soc_bias_level level)
139 struct snd_soc_card *card = dapm->card;
143 case SND_SOC_BIAS_ON:
144 dev_dbg(dapm->dev, "Setting full bias\n");
146 case SND_SOC_BIAS_PREPARE:
147 dev_dbg(dapm->dev, "Setting bias prepare\n");
149 case SND_SOC_BIAS_STANDBY:
150 dev_dbg(dapm->dev, "Setting standby bias\n");
152 case SND_SOC_BIAS_OFF:
153 dev_dbg(dapm->dev, "Setting bias off\n");
156 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
160 trace_snd_soc_bias_level_start(card, level);
162 if (card && card->set_bias_level)
163 ret = card->set_bias_level(card, level);
165 if (dapm->codec && dapm->codec->driver->set_bias_level)
166 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
168 dapm->bias_level = level;
171 if (card && card->set_bias_level_post)
172 ret = card->set_bias_level_post(card, level);
175 trace_snd_soc_bias_level_done(card, level);
180 /* set up initial codec paths */
181 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
182 struct snd_soc_dapm_path *p, int i)
185 case snd_soc_dapm_switch:
186 case snd_soc_dapm_mixer:
187 case snd_soc_dapm_mixer_named_ctl: {
189 struct soc_mixer_control *mc = (struct soc_mixer_control *)
190 w->kcontrols[i].private_value;
191 unsigned int reg = mc->reg;
192 unsigned int shift = mc->shift;
194 unsigned int mask = (1 << fls(max)) - 1;
195 unsigned int invert = mc->invert;
197 val = snd_soc_read(w->codec, reg);
198 val = (val >> shift) & mask;
200 if ((invert && !val) || (!invert && val))
206 case snd_soc_dapm_mux: {
207 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
208 int val, item, bitmask;
210 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
212 val = snd_soc_read(w->codec, e->reg);
213 item = (val >> e->shift_l) & (bitmask - 1);
216 for (i = 0; i < e->max; i++) {
217 if (!(strcmp(p->name, e->texts[i])) && item == i)
222 case snd_soc_dapm_virt_mux: {
223 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
226 /* since a virtual mux has no backing registers to
227 * decide which path to connect, it will try to match
228 * with the first enumeration. This is to ensure
229 * that the default mux choice (the first) will be
230 * correctly powered up during initialization.
232 if (!strcmp(p->name, e->texts[0]))
236 case snd_soc_dapm_value_mux: {
237 struct soc_enum *e = (struct soc_enum *)
238 w->kcontrols[i].private_value;
241 val = snd_soc_read(w->codec, e->reg);
242 val = (val >> e->shift_l) & e->mask;
243 for (item = 0; item < e->max; item++) {
244 if (val == e->values[item])
249 for (i = 0; i < e->max; i++) {
250 if (!(strcmp(p->name, e->texts[i])) && item == i)
255 /* does not effect routing - always connected */
256 case snd_soc_dapm_pga:
257 case snd_soc_dapm_out_drv:
258 case snd_soc_dapm_output:
259 case snd_soc_dapm_adc:
260 case snd_soc_dapm_input:
261 case snd_soc_dapm_dac:
262 case snd_soc_dapm_micbias:
263 case snd_soc_dapm_vmid:
264 case snd_soc_dapm_supply:
265 case snd_soc_dapm_aif_in:
266 case snd_soc_dapm_aif_out:
269 /* does effect routing - dynamically connected */
270 case snd_soc_dapm_hp:
271 case snd_soc_dapm_mic:
272 case snd_soc_dapm_spk:
273 case snd_soc_dapm_line:
274 case snd_soc_dapm_pre:
275 case snd_soc_dapm_post:
281 /* connect mux widget to its interconnecting audio paths */
282 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
283 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
284 struct snd_soc_dapm_path *path, const char *control_name,
285 const struct snd_kcontrol_new *kcontrol)
287 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
290 for (i = 0; i < e->max; i++) {
291 if (!(strcmp(control_name, e->texts[i]))) {
292 list_add(&path->list, &dapm->card->paths);
293 list_add(&path->list_sink, &dest->sources);
294 list_add(&path->list_source, &src->sinks);
295 path->name = (char*)e->texts[i];
296 dapm_set_path_status(dest, path, 0);
304 /* connect mixer widget to its interconnecting audio paths */
305 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
306 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
307 struct snd_soc_dapm_path *path, const char *control_name)
311 /* search for mixer kcontrol */
312 for (i = 0; i < dest->num_kcontrols; i++) {
313 if (!strcmp(control_name, dest->kcontrols[i].name)) {
314 list_add(&path->list, &dapm->card->paths);
315 list_add(&path->list_sink, &dest->sources);
316 list_add(&path->list_source, &src->sinks);
317 path->name = dest->kcontrols[i].name;
318 dapm_set_path_status(dest, path, i);
325 /* update dapm codec register bits */
326 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
329 unsigned int old, new;
330 struct snd_soc_codec *codec = widget->codec;
331 struct snd_soc_dapm_context *dapm = widget->dapm;
332 struct snd_soc_card *card = dapm->card;
334 /* check for valid widgets */
335 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
336 widget->id == snd_soc_dapm_output ||
337 widget->id == snd_soc_dapm_hp ||
338 widget->id == snd_soc_dapm_mic ||
339 widget->id == snd_soc_dapm_line ||
340 widget->id == snd_soc_dapm_spk)
343 power = widget->power;
345 power = (power ? 0:1);
347 old = snd_soc_read(codec, widget->reg);
348 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
352 pop_dbg(dapm->dev, card->pop_time,
353 "pop test %s : %s in %d ms\n",
354 widget->name, widget->power ? "on" : "off",
356 pop_wait(card->pop_time);
357 snd_soc_write(codec, widget->reg, new);
359 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
364 /* create new dapm mixer control */
365 static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
366 struct snd_soc_dapm_widget *w)
369 size_t name_len, prefix_len;
370 struct snd_soc_dapm_path *path;
371 struct snd_card *card = dapm->card->snd_card;
375 prefix = dapm->codec->name_prefix;
380 prefix_len = strlen(prefix) + 1;
385 for (i = 0; i < w->num_kcontrols; i++) {
388 list_for_each_entry(path, &w->sources, list_sink) {
390 /* mixer/mux paths name must match control name */
391 if (path->name != (char*)w->kcontrols[i].name)
394 /* add dapm control with long name.
395 * for dapm_mixer this is the concatenation of the
396 * mixer and kcontrol name.
397 * for dapm_mixer_named_ctl this is simply the
400 name_len = strlen(w->kcontrols[i].name) + 1;
401 if (w->id != snd_soc_dapm_mixer_named_ctl)
402 name_len += 1 + strlen(w->name);
404 path->long_name = kmalloc(name_len, GFP_KERNEL);
406 if (path->long_name == NULL)
411 /* The control will get a prefix from
412 * the control creation process but
413 * we're also using the same prefix
414 * for widgets so cut the prefix off
415 * the front of the widget name.
417 snprintf(path->long_name, name_len, "%s %s",
418 w->name + prefix_len,
419 w->kcontrols[i].name);
421 case snd_soc_dapm_mixer_named_ctl:
422 snprintf(path->long_name, name_len, "%s",
423 w->kcontrols[i].name);
427 path->long_name[name_len - 1] = '\0';
429 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
430 path->long_name, prefix);
431 ret = snd_ctl_add(card, path->kcontrol);
434 "asoc: failed to add dapm kcontrol %s: %d\n",
435 path->long_name, ret);
436 kfree(path->long_name);
437 path->long_name = NULL;
445 /* create new dapm mux control */
446 static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
447 struct snd_soc_dapm_widget *w)
449 struct snd_soc_dapm_path *path = NULL;
450 struct snd_kcontrol *kcontrol;
451 struct snd_card *card = dapm->card->snd_card;
456 if (!w->num_kcontrols) {
457 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
462 prefix = dapm->codec->name_prefix;
467 prefix_len = strlen(prefix) + 1;
471 /* The control will get a prefix from the control creation
472 * process but we're also using the same prefix for widgets so
473 * cut the prefix off the front of the widget name.
475 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name + prefix_len,
477 ret = snd_ctl_add(card, kcontrol);
482 list_for_each_entry(path, &w->sources, list_sink)
483 path->kcontrol = kcontrol;
488 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
492 /* create new dapm volume control */
493 static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
494 struct snd_soc_dapm_widget *w)
496 if (w->num_kcontrols)
497 dev_err(w->dapm->dev,
498 "asoc: PGA controls not supported: '%s'\n", w->name);
503 /* reset 'walked' bit for each dapm path */
504 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
506 struct snd_soc_dapm_path *p;
508 list_for_each_entry(p, &dapm->card->paths, list)
512 /* We implement power down on suspend by checking the power state of
513 * the ALSA card - when we are suspending the ALSA state for the card
516 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
518 int level = snd_power_get_state(widget->dapm->card->snd_card);
521 case SNDRV_CTL_POWER_D3hot:
522 case SNDRV_CTL_POWER_D3cold:
523 if (widget->ignore_suspend)
524 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
526 return widget->ignore_suspend;
533 * Recursively check for a completed path to an active or physically connected
534 * output widget. Returns number of complete paths.
536 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
538 struct snd_soc_dapm_path *path;
541 if (widget->id == snd_soc_dapm_supply)
544 switch (widget->id) {
545 case snd_soc_dapm_adc:
546 case snd_soc_dapm_aif_out:
548 return snd_soc_dapm_suspend_check(widget);
553 if (widget->connected) {
554 /* connected pin ? */
555 if (widget->id == snd_soc_dapm_output && !widget->ext)
556 return snd_soc_dapm_suspend_check(widget);
558 /* connected jack or spk ? */
559 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
560 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
561 return snd_soc_dapm_suspend_check(widget);
564 list_for_each_entry(path, &widget->sinks, list_source) {
568 if (path->sink && path->connect) {
570 con += is_connected_output_ep(path->sink);
578 * Recursively check for a completed path to an active or physically connected
579 * input widget. Returns number of complete paths.
581 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
583 struct snd_soc_dapm_path *path;
586 if (widget->id == snd_soc_dapm_supply)
589 /* active stream ? */
590 switch (widget->id) {
591 case snd_soc_dapm_dac:
592 case snd_soc_dapm_aif_in:
594 return snd_soc_dapm_suspend_check(widget);
599 if (widget->connected) {
600 /* connected pin ? */
601 if (widget->id == snd_soc_dapm_input && !widget->ext)
602 return snd_soc_dapm_suspend_check(widget);
604 /* connected VMID/Bias for lower pops */
605 if (widget->id == snd_soc_dapm_vmid)
606 return snd_soc_dapm_suspend_check(widget);
608 /* connected jack ? */
609 if (widget->id == snd_soc_dapm_mic ||
610 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
611 return snd_soc_dapm_suspend_check(widget);
614 list_for_each_entry(path, &widget->sources, list_sink) {
618 if (path->source && path->connect) {
620 con += is_connected_input_ep(path->source);
628 * Handler for generic register modifier widget.
630 int dapm_reg_event(struct snd_soc_dapm_widget *w,
631 struct snd_kcontrol *kcontrol, int event)
635 if (SND_SOC_DAPM_EVENT_ON(event))
640 snd_soc_update_bits(w->codec, -(w->reg + 1),
641 w->mask << w->shift, val << w->shift);
645 EXPORT_SYMBOL_GPL(dapm_reg_event);
647 /* Standard power change method, used to apply power changes to most
650 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
654 /* call any power change event handlers */
656 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
657 w->power ? "on" : "off",
658 w->name, w->event_flags);
660 /* power up pre event */
661 if (w->power && w->event &&
662 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
663 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
668 /* power down pre event */
669 if (!w->power && w->event &&
670 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
671 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
678 /* power up post event */
679 if (w->power && w->event &&
680 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
682 NULL, SND_SOC_DAPM_POST_PMU);
687 /* power down post event */
688 if (!w->power && w->event &&
689 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
690 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
698 /* Generic check to see if a widget should be powered.
700 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
704 in = is_connected_input_ep(w);
705 dapm_clear_walk(w->dapm);
706 out = is_connected_output_ep(w);
707 dapm_clear_walk(w->dapm);
708 return out != 0 && in != 0;
711 /* Check to see if an ADC has power */
712 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
717 in = is_connected_input_ep(w);
718 dapm_clear_walk(w->dapm);
721 return dapm_generic_check_power(w);
725 /* Check to see if a DAC has power */
726 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
731 out = is_connected_output_ep(w);
732 dapm_clear_walk(w->dapm);
735 return dapm_generic_check_power(w);
739 /* Check to see if a power supply is needed */
740 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
742 struct snd_soc_dapm_path *path;
745 /* Check if one of our outputs is connected */
746 list_for_each_entry(path, &w->sinks, list_source) {
747 if (path->connected &&
748 !path->connected(path->source, path->sink))
754 if (path->sink->force) {
759 if (path->sink->power_check &&
760 path->sink->power_check(path->sink)) {
766 dapm_clear_walk(w->dapm);
771 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
772 struct snd_soc_dapm_widget *b,
780 sort = dapm_down_seq;
782 if (sort[a->id] != sort[b->id])
783 return sort[a->id] - sort[b->id];
784 if (a->subseq != b->subseq) {
786 return a->subseq - b->subseq;
788 return b->subseq - a->subseq;
790 if (a->reg != b->reg)
791 return a->reg - b->reg;
792 if (a->dapm != b->dapm)
793 return (unsigned long)a->dapm - (unsigned long)b->dapm;
798 /* Insert a widget in order into a DAPM power sequence. */
799 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
800 struct list_head *list,
803 struct snd_soc_dapm_widget *w;
805 list_for_each_entry(w, list, power_list)
806 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
807 list_add_tail(&new_widget->power_list, &w->power_list);
811 list_add_tail(&new_widget->power_list, list);
814 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
815 struct snd_soc_dapm_widget *w, int event)
817 struct snd_soc_card *card = dapm->card;
822 case SND_SOC_DAPM_PRE_PMU:
826 case SND_SOC_DAPM_POST_PMU:
827 ev_name = "POST_PMU";
830 case SND_SOC_DAPM_PRE_PMD:
834 case SND_SOC_DAPM_POST_PMD:
835 ev_name = "POST_PMD";
843 if (w->power != power)
846 if (w->event && (w->event_flags & event)) {
847 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
849 trace_snd_soc_dapm_widget_event_start(w, event);
850 ret = w->event(w, NULL, event);
851 trace_snd_soc_dapm_widget_event_done(w, event);
853 pr_err("%s: %s event failed: %d\n",
854 ev_name, w->name, ret);
858 /* Apply the coalesced changes from a DAPM sequence */
859 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
860 struct list_head *pending)
862 struct snd_soc_card *card = dapm->card;
863 struct snd_soc_dapm_widget *w;
865 unsigned int value = 0;
866 unsigned int mask = 0;
867 unsigned int cur_mask;
869 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
872 list_for_each_entry(w, pending, power_list) {
873 cur_mask = 1 << w->shift;
874 BUG_ON(reg != w->reg);
885 pop_dbg(dapm->dev, card->pop_time,
886 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
887 w->name, reg, value, mask);
889 /* Check for events */
890 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
891 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
895 pop_dbg(dapm->dev, card->pop_time,
896 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
897 value, mask, reg, card->pop_time);
898 pop_wait(card->pop_time);
899 snd_soc_update_bits(dapm->codec, reg, mask, value);
902 list_for_each_entry(w, pending, power_list) {
903 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
904 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
908 /* Apply a DAPM power sequence.
910 * We walk over a pre-sorted list of widgets to apply power to. In
911 * order to minimise the number of writes to the device required
912 * multiple widgets will be updated in a single write where possible.
913 * Currently anything that requires more than a single write is not
916 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
917 struct list_head *list, int event, bool power_up)
919 struct snd_soc_dapm_widget *w, *n;
923 int cur_reg = SND_SOC_NOPM;
924 struct snd_soc_dapm_context *cur_dapm = NULL;
931 sort = dapm_down_seq;
933 list_for_each_entry_safe(w, n, list, power_list) {
936 /* Do we need to apply any queued changes? */
937 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
938 w->dapm != cur_dapm || w->subseq != cur_subseq) {
939 if (!list_empty(&pending))
940 dapm_seq_run_coalesced(cur_dapm, &pending);
942 if (cur_dapm && cur_dapm->seq_notifier) {
943 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
944 if (sort[i] == cur_sort)
945 cur_dapm->seq_notifier(cur_dapm,
950 INIT_LIST_HEAD(&pending);
953 cur_reg = SND_SOC_NOPM;
958 case snd_soc_dapm_pre:
960 list_for_each_entry_safe_continue(w, n, list,
963 if (event == SND_SOC_DAPM_STREAM_START)
965 NULL, SND_SOC_DAPM_PRE_PMU);
966 else if (event == SND_SOC_DAPM_STREAM_STOP)
968 NULL, SND_SOC_DAPM_PRE_PMD);
971 case snd_soc_dapm_post:
973 list_for_each_entry_safe_continue(w, n, list,
976 if (event == SND_SOC_DAPM_STREAM_START)
978 NULL, SND_SOC_DAPM_POST_PMU);
979 else if (event == SND_SOC_DAPM_STREAM_STOP)
981 NULL, SND_SOC_DAPM_POST_PMD);
984 case snd_soc_dapm_input:
985 case snd_soc_dapm_output:
986 case snd_soc_dapm_hp:
987 case snd_soc_dapm_mic:
988 case snd_soc_dapm_line:
989 case snd_soc_dapm_spk:
990 /* No register support currently */
991 ret = dapm_generic_apply_power(w);
995 /* Queue it up for application */
996 cur_sort = sort[w->id];
997 cur_subseq = w->subseq;
1000 list_move(&w->power_list, &pending);
1005 dev_err(w->dapm->dev,
1006 "Failed to apply widget power: %d\n", ret);
1009 if (!list_empty(&pending))
1010 dapm_seq_run_coalesced(cur_dapm, &pending);
1012 if (cur_dapm && cur_dapm->seq_notifier) {
1013 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1014 if (sort[i] == cur_sort)
1015 cur_dapm->seq_notifier(cur_dapm,
1020 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1022 struct snd_soc_dapm_update *update = dapm->update;
1023 struct snd_soc_dapm_widget *w;
1032 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1033 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1035 pr_err("%s DAPM pre-event failed: %d\n",
1039 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1042 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1045 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1046 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1048 pr_err("%s DAPM post-event failed: %d\n",
1053 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1054 * they're changing state.
1056 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1058 struct snd_soc_dapm_context *d = data;
1061 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1062 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1065 "Failed to turn on bias: %d\n", ret);
1068 /* If we're changing to all on or all off then prepare */
1069 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1070 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1071 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1074 "Failed to prepare bias: %d\n", ret);
1078 /* Async callback run prior to DAPM sequences - brings to their final
1081 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1083 struct snd_soc_dapm_context *d = data;
1086 /* If we just powered the last thing off drop to standby bias */
1087 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1088 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1090 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1094 /* If we're in standby and can support bias off then do that */
1095 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1096 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1098 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1101 /* If we just powered up then move to active bias */
1102 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1103 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1105 dev_err(d->dev, "Failed to apply active bias: %d\n",
1111 * Scan each dapm widget for complete audio path.
1112 * A complete path is a route that has valid endpoints i.e.:-
1114 * o DAC to output pin.
1115 * o Input Pin to ADC.
1116 * o Input pin to Output pin (bypass, sidetone)
1117 * o DAC to ADC (loopback).
1119 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1121 struct snd_soc_card *card = dapm->card;
1122 struct snd_soc_dapm_widget *w;
1123 struct snd_soc_dapm_context *d;
1125 LIST_HEAD(down_list);
1126 LIST_HEAD(async_domain);
1129 trace_snd_soc_dapm_start(card);
1131 list_for_each_entry(d, &card->dapm_list, list)
1135 /* Check which widgets we need to power and store them in
1136 * lists indicating if they should be powered up or down.
1138 list_for_each_entry(w, &card->widgets, list) {
1140 case snd_soc_dapm_pre:
1141 dapm_seq_insert(w, &down_list, false);
1143 case snd_soc_dapm_post:
1144 dapm_seq_insert(w, &up_list, true);
1148 if (!w->power_check)
1152 power = w->power_check(w);
1156 w->dapm->dev_power = 1;
1158 if (w->power == power)
1161 trace_snd_soc_dapm_widget_power(w, power);
1164 dapm_seq_insert(w, &up_list, true);
1166 dapm_seq_insert(w, &down_list, false);
1173 /* If there are no DAPM widgets then try to figure out power from the
1176 if (!dapm->n_widgets) {
1178 case SND_SOC_DAPM_STREAM_START:
1179 case SND_SOC_DAPM_STREAM_RESUME:
1180 dapm->dev_power = 1;
1182 case SND_SOC_DAPM_STREAM_STOP:
1183 dapm->dev_power = !!dapm->codec->active;
1185 case SND_SOC_DAPM_STREAM_SUSPEND:
1186 dapm->dev_power = 0;
1188 case SND_SOC_DAPM_STREAM_NOP:
1189 switch (dapm->bias_level) {
1190 case SND_SOC_BIAS_STANDBY:
1191 case SND_SOC_BIAS_OFF:
1192 dapm->dev_power = 0;
1195 dapm->dev_power = 1;
1204 /* Run all the bias changes in parallel */
1205 list_for_each_entry(d, &dapm->card->dapm_list, list)
1206 async_schedule_domain(dapm_pre_sequence_async, d,
1208 async_synchronize_full_domain(&async_domain);
1210 /* Power down widgets first; try to avoid amplifying pops. */
1211 dapm_seq_run(dapm, &down_list, event, false);
1213 dapm_widget_update(dapm);
1216 dapm_seq_run(dapm, &up_list, event, true);
1218 /* Run all the bias changes in parallel */
1219 list_for_each_entry(d, &dapm->card->dapm_list, list)
1220 async_schedule_domain(dapm_post_sequence_async, d,
1222 async_synchronize_full_domain(&async_domain);
1224 pop_dbg(dapm->dev, card->pop_time,
1225 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1226 pop_wait(card->pop_time);
1228 trace_snd_soc_dapm_done(card);
1233 #ifdef CONFIG_DEBUG_FS
1234 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1236 file->private_data = inode->i_private;
1240 static ssize_t dapm_widget_power_read_file(struct file *file,
1241 char __user *user_buf,
1242 size_t count, loff_t *ppos)
1244 struct snd_soc_dapm_widget *w = file->private_data;
1248 struct snd_soc_dapm_path *p = NULL;
1250 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1254 in = is_connected_input_ep(w);
1255 dapm_clear_walk(w->dapm);
1256 out = is_connected_output_ep(w);
1257 dapm_clear_walk(w->dapm);
1259 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1260 w->name, w->power ? "On" : "Off", in, out);
1263 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1264 " - R%d(0x%x) bit %d",
1265 w->reg, w->reg, w->shift);
1267 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1270 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1272 w->active ? "active" : "inactive");
1274 list_for_each_entry(p, &w->sources, list_sink) {
1275 if (p->connected && !p->connected(w, p->sink))
1279 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1280 " in \"%s\" \"%s\"\n",
1281 p->name ? p->name : "static",
1284 list_for_each_entry(p, &w->sinks, list_source) {
1285 if (p->connected && !p->connected(w, p->sink))
1289 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1290 " out \"%s\" \"%s\"\n",
1291 p->name ? p->name : "static",
1295 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1301 static const struct file_operations dapm_widget_power_fops = {
1302 .open = dapm_widget_power_open_file,
1303 .read = dapm_widget_power_read_file,
1304 .llseek = default_llseek,
1307 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1309 file->private_data = inode->i_private;
1313 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1314 size_t count, loff_t *ppos)
1316 struct snd_soc_dapm_context *dapm = file->private_data;
1319 switch (dapm->bias_level) {
1320 case SND_SOC_BIAS_ON:
1323 case SND_SOC_BIAS_PREPARE:
1324 level = "Prepare\n";
1326 case SND_SOC_BIAS_STANDBY:
1327 level = "Standby\n";
1329 case SND_SOC_BIAS_OFF:
1334 level = "Unknown\n";
1338 return simple_read_from_buffer(user_buf, count, ppos, level,
1342 static const struct file_operations dapm_bias_fops = {
1343 .open = dapm_bias_open_file,
1344 .read = dapm_bias_read_file,
1345 .llseek = default_llseek,
1348 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1350 struct snd_soc_dapm_widget *w;
1353 if (!dapm->debugfs_dapm)
1356 d = debugfs_create_file("bias_level", 0444,
1357 dapm->debugfs_dapm, dapm,
1361 "ASoC: Failed to create bias level debugfs file\n");
1363 list_for_each_entry(w, &dapm->card->widgets, list) {
1364 if (!w->name || w->dapm != dapm)
1367 d = debugfs_create_file(w->name, 0444,
1368 dapm->debugfs_dapm, w,
1369 &dapm_widget_power_fops);
1371 dev_warn(w->dapm->dev,
1372 "ASoC: Failed to create %s debugfs file\n",
1377 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1382 /* test and update the power status of a mux widget */
1383 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1384 struct snd_kcontrol *kcontrol, int change,
1385 int mux, struct soc_enum *e)
1387 struct snd_soc_dapm_path *path;
1390 if (widget->id != snd_soc_dapm_mux &&
1391 widget->id != snd_soc_dapm_virt_mux &&
1392 widget->id != snd_soc_dapm_value_mux)
1398 /* find dapm widget path assoc with kcontrol */
1399 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1400 if (path->kcontrol != kcontrol)
1403 if (!path->name || !e->texts[mux])
1407 /* we now need to match the string in the enum to the path */
1408 if (!(strcmp(path->name, e->texts[mux])))
1409 path->connect = 1; /* new connection */
1411 path->connect = 0; /* old connection must be powered down */
1415 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1420 /* test and update the power status of a mixer or switch widget */
1421 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1422 struct snd_kcontrol *kcontrol, int connect)
1424 struct snd_soc_dapm_path *path;
1427 if (widget->id != snd_soc_dapm_mixer &&
1428 widget->id != snd_soc_dapm_mixer_named_ctl &&
1429 widget->id != snd_soc_dapm_switch)
1432 /* find dapm widget path assoc with kcontrol */
1433 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1434 if (path->kcontrol != kcontrol)
1437 /* found, now check type */
1439 path->connect = connect;
1444 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1449 /* show dapm widget status in sys fs */
1450 static ssize_t dapm_widget_show(struct device *dev,
1451 struct device_attribute *attr, char *buf)
1453 struct snd_soc_pcm_runtime *rtd =
1454 container_of(dev, struct snd_soc_pcm_runtime, dev);
1455 struct snd_soc_codec *codec =rtd->codec;
1456 struct snd_soc_dapm_widget *w;
1458 char *state = "not set";
1460 list_for_each_entry(w, &codec->card->widgets, list) {
1461 if (w->dapm != &codec->dapm)
1464 /* only display widgets that burnm power */
1466 case snd_soc_dapm_hp:
1467 case snd_soc_dapm_mic:
1468 case snd_soc_dapm_spk:
1469 case snd_soc_dapm_line:
1470 case snd_soc_dapm_micbias:
1471 case snd_soc_dapm_dac:
1472 case snd_soc_dapm_adc:
1473 case snd_soc_dapm_pga:
1474 case snd_soc_dapm_out_drv:
1475 case snd_soc_dapm_mixer:
1476 case snd_soc_dapm_mixer_named_ctl:
1477 case snd_soc_dapm_supply:
1479 count += sprintf(buf + count, "%s: %s\n",
1480 w->name, w->power ? "On":"Off");
1487 switch (codec->dapm.bias_level) {
1488 case SND_SOC_BIAS_ON:
1491 case SND_SOC_BIAS_PREPARE:
1494 case SND_SOC_BIAS_STANDBY:
1497 case SND_SOC_BIAS_OFF:
1501 count += sprintf(buf + count, "PM State: %s\n", state);
1506 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1508 int snd_soc_dapm_sys_add(struct device *dev)
1510 return device_create_file(dev, &dev_attr_dapm_widget);
1513 static void snd_soc_dapm_sys_remove(struct device *dev)
1515 device_remove_file(dev, &dev_attr_dapm_widget);
1518 /* free all dapm widgets and resources */
1519 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1521 struct snd_soc_dapm_widget *w, *next_w;
1522 struct snd_soc_dapm_path *p, *next_p;
1524 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1525 if (w->dapm != dapm)
1529 * remove source and sink paths associated to this widget.
1530 * While removing the path, remove reference to it from both
1531 * source and sink widgets so that path is removed only once.
1533 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1534 list_del(&p->list_sink);
1535 list_del(&p->list_source);
1537 kfree(p->long_name);
1540 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1541 list_del(&p->list_sink);
1542 list_del(&p->list_source);
1544 kfree(p->long_name);
1552 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1553 const char *pin, int status)
1555 struct snd_soc_dapm_widget *w;
1557 list_for_each_entry(w, &dapm->card->widgets, list) {
1558 if (w->dapm != dapm)
1560 if (!strcmp(w->name, pin)) {
1561 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1563 w->connected = status;
1564 /* Allow disabling of forced pins */
1571 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1576 * snd_soc_dapm_sync - scan and power dapm paths
1577 * @dapm: DAPM context
1579 * Walks all dapm audio paths and powers widgets according to their
1580 * stream or path usage.
1582 * Returns 0 for success.
1584 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1586 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1588 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1590 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1591 const struct snd_soc_dapm_route *route)
1593 struct snd_soc_dapm_path *path;
1594 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1595 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1597 const char *control = route->control;
1599 char prefixed_sink[80];
1600 char prefixed_source[80];
1603 if (dapm->codec && dapm->codec->name_prefix) {
1604 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1605 dapm->codec->name_prefix, route->sink);
1606 sink = prefixed_sink;
1607 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1608 dapm->codec->name_prefix, route->source);
1609 source = prefixed_source;
1612 source = route->source;
1616 * find src and dest widgets over all widgets but favor a widget from
1617 * current DAPM context
1619 list_for_each_entry(w, &dapm->card->widgets, list) {
1620 if (!wsink && !(strcmp(w->name, sink))) {
1622 if (w->dapm == dapm)
1626 if (!wsource && !(strcmp(w->name, source))) {
1628 if (w->dapm == dapm)
1632 /* use widget from another DAPM context if not found from this */
1638 if (wsource == NULL || wsink == NULL)
1641 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1645 path->source = wsource;
1647 path->connected = route->connected;
1648 INIT_LIST_HEAD(&path->list);
1649 INIT_LIST_HEAD(&path->list_source);
1650 INIT_LIST_HEAD(&path->list_sink);
1652 /* check for external widgets */
1653 if (wsink->id == snd_soc_dapm_input) {
1654 if (wsource->id == snd_soc_dapm_micbias ||
1655 wsource->id == snd_soc_dapm_mic ||
1656 wsource->id == snd_soc_dapm_line ||
1657 wsource->id == snd_soc_dapm_output)
1660 if (wsource->id == snd_soc_dapm_output) {
1661 if (wsink->id == snd_soc_dapm_spk ||
1662 wsink->id == snd_soc_dapm_hp ||
1663 wsink->id == snd_soc_dapm_line ||
1664 wsink->id == snd_soc_dapm_input)
1668 /* connect static paths */
1669 if (control == NULL) {
1670 list_add(&path->list, &dapm->card->paths);
1671 list_add(&path->list_sink, &wsink->sources);
1672 list_add(&path->list_source, &wsource->sinks);
1677 /* connect dynamic paths */
1679 case snd_soc_dapm_adc:
1680 case snd_soc_dapm_dac:
1681 case snd_soc_dapm_pga:
1682 case snd_soc_dapm_out_drv:
1683 case snd_soc_dapm_input:
1684 case snd_soc_dapm_output:
1685 case snd_soc_dapm_micbias:
1686 case snd_soc_dapm_vmid:
1687 case snd_soc_dapm_pre:
1688 case snd_soc_dapm_post:
1689 case snd_soc_dapm_supply:
1690 case snd_soc_dapm_aif_in:
1691 case snd_soc_dapm_aif_out:
1692 list_add(&path->list, &dapm->card->paths);
1693 list_add(&path->list_sink, &wsink->sources);
1694 list_add(&path->list_source, &wsource->sinks);
1697 case snd_soc_dapm_mux:
1698 case snd_soc_dapm_virt_mux:
1699 case snd_soc_dapm_value_mux:
1700 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1701 &wsink->kcontrols[0]);
1705 case snd_soc_dapm_switch:
1706 case snd_soc_dapm_mixer:
1707 case snd_soc_dapm_mixer_named_ctl:
1708 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1712 case snd_soc_dapm_hp:
1713 case snd_soc_dapm_mic:
1714 case snd_soc_dapm_line:
1715 case snd_soc_dapm_spk:
1716 list_add(&path->list, &dapm->card->paths);
1717 list_add(&path->list_sink, &wsink->sources);
1718 list_add(&path->list_source, &wsource->sinks);
1725 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1726 source, control, sink);
1732 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1733 * @dapm: DAPM context
1734 * @route: audio routes
1735 * @num: number of routes
1737 * Connects 2 dapm widgets together via a named audio path. The sink is
1738 * the widget receiving the audio signal, whilst the source is the sender
1739 * of the audio signal.
1741 * Returns 0 for success else error. On error all resources can be freed
1742 * with a call to snd_soc_card_free().
1744 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1745 const struct snd_soc_dapm_route *route, int num)
1749 for (i = 0; i < num; i++) {
1750 ret = snd_soc_dapm_add_route(dapm, route);
1752 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1753 route->source, route->sink);
1761 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1764 * snd_soc_dapm_new_widgets - add new dapm widgets
1765 * @dapm: DAPM context
1767 * Checks the codec for any new dapm widgets and creates them if found.
1769 * Returns 0 for success.
1771 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1773 struct snd_soc_dapm_widget *w;
1776 list_for_each_entry(w, &dapm->card->widgets, list)
1782 case snd_soc_dapm_switch:
1783 case snd_soc_dapm_mixer:
1784 case snd_soc_dapm_mixer_named_ctl:
1785 w->power_check = dapm_generic_check_power;
1786 dapm_new_mixer(dapm, w);
1788 case snd_soc_dapm_mux:
1789 case snd_soc_dapm_virt_mux:
1790 case snd_soc_dapm_value_mux:
1791 w->power_check = dapm_generic_check_power;
1792 dapm_new_mux(dapm, w);
1794 case snd_soc_dapm_adc:
1795 case snd_soc_dapm_aif_out:
1796 w->power_check = dapm_adc_check_power;
1798 case snd_soc_dapm_dac:
1799 case snd_soc_dapm_aif_in:
1800 w->power_check = dapm_dac_check_power;
1802 case snd_soc_dapm_pga:
1803 case snd_soc_dapm_out_drv:
1804 w->power_check = dapm_generic_check_power;
1805 dapm_new_pga(dapm, w);
1807 case snd_soc_dapm_input:
1808 case snd_soc_dapm_output:
1809 case snd_soc_dapm_micbias:
1810 case snd_soc_dapm_spk:
1811 case snd_soc_dapm_hp:
1812 case snd_soc_dapm_mic:
1813 case snd_soc_dapm_line:
1814 w->power_check = dapm_generic_check_power;
1816 case snd_soc_dapm_supply:
1817 w->power_check = dapm_supply_check_power;
1818 case snd_soc_dapm_vmid:
1819 case snd_soc_dapm_pre:
1820 case snd_soc_dapm_post:
1824 /* Read the initial power state from the device */
1826 val = snd_soc_read(w->codec, w->reg);
1827 val &= 1 << w->shift;
1838 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1841 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1844 * snd_soc_dapm_get_volsw - dapm mixer get callback
1845 * @kcontrol: mixer control
1846 * @ucontrol: control element information
1848 * Callback to get the value of a dapm mixer control.
1850 * Returns 0 for success.
1852 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1853 struct snd_ctl_elem_value *ucontrol)
1855 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1856 struct soc_mixer_control *mc =
1857 (struct soc_mixer_control *)kcontrol->private_value;
1858 unsigned int reg = mc->reg;
1859 unsigned int shift = mc->shift;
1860 unsigned int rshift = mc->rshift;
1862 unsigned int invert = mc->invert;
1863 unsigned int mask = (1 << fls(max)) - 1;
1865 ucontrol->value.integer.value[0] =
1866 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1867 if (shift != rshift)
1868 ucontrol->value.integer.value[1] =
1869 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1871 ucontrol->value.integer.value[0] =
1872 max - ucontrol->value.integer.value[0];
1873 if (shift != rshift)
1874 ucontrol->value.integer.value[1] =
1875 max - ucontrol->value.integer.value[1];
1880 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1883 * snd_soc_dapm_put_volsw - dapm mixer set callback
1884 * @kcontrol: mixer control
1885 * @ucontrol: control element information
1887 * Callback to set the value of a dapm mixer control.
1889 * Returns 0 for success.
1891 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1892 struct snd_ctl_elem_value *ucontrol)
1894 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1895 struct soc_mixer_control *mc =
1896 (struct soc_mixer_control *)kcontrol->private_value;
1897 unsigned int reg = mc->reg;
1898 unsigned int shift = mc->shift;
1900 unsigned int mask = (1 << fls(max)) - 1;
1901 unsigned int invert = mc->invert;
1903 int connect, change;
1904 struct snd_soc_dapm_update update;
1906 val = (ucontrol->value.integer.value[0] & mask);
1910 mask = mask << shift;
1913 mutex_lock(&widget->codec->mutex);
1914 widget->value = val;
1916 change = snd_soc_test_bits(widget->codec, reg, mask, val);
1919 /* new connection */
1920 connect = invert ? 0:1;
1922 /* old connection must be powered down */
1923 connect = invert ? 1:0;
1925 update.kcontrol = kcontrol;
1926 update.widget = widget;
1930 widget->dapm->update = &update;
1932 dapm_mixer_update_power(widget, kcontrol, connect);
1934 widget->dapm->update = NULL;
1937 mutex_unlock(&widget->codec->mutex);
1940 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1943 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1944 * @kcontrol: mixer control
1945 * @ucontrol: control element information
1947 * Callback to get the value of a dapm enumerated double mixer control.
1949 * Returns 0 for success.
1951 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1952 struct snd_ctl_elem_value *ucontrol)
1954 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1955 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1956 unsigned int val, bitmask;
1958 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1960 val = snd_soc_read(widget->codec, e->reg);
1961 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1962 if (e->shift_l != e->shift_r)
1963 ucontrol->value.enumerated.item[1] =
1964 (val >> e->shift_r) & (bitmask - 1);
1968 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1971 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1972 * @kcontrol: mixer control
1973 * @ucontrol: control element information
1975 * Callback to set the value of a dapm enumerated double mixer control.
1977 * Returns 0 for success.
1979 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1982 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1983 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1984 unsigned int val, mux, change;
1985 unsigned int mask, bitmask;
1986 struct snd_soc_dapm_update update;
1988 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1990 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1992 mux = ucontrol->value.enumerated.item[0];
1993 val = mux << e->shift_l;
1994 mask = (bitmask - 1) << e->shift_l;
1995 if (e->shift_l != e->shift_r) {
1996 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1998 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1999 mask |= (bitmask - 1) << e->shift_r;
2002 mutex_lock(&widget->codec->mutex);
2003 widget->value = val;
2004 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2006 update.kcontrol = kcontrol;
2007 update.widget = widget;
2008 update.reg = e->reg;
2011 widget->dapm->update = &update;
2013 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2015 widget->dapm->update = NULL;
2017 mutex_unlock(&widget->codec->mutex);
2020 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2023 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2024 * @kcontrol: mixer control
2025 * @ucontrol: control element information
2027 * Returns 0 for success.
2029 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2030 struct snd_ctl_elem_value *ucontrol)
2032 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2034 ucontrol->value.enumerated.item[0] = widget->value;
2038 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2041 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2042 * @kcontrol: mixer control
2043 * @ucontrol: control element information
2045 * Returns 0 for success.
2047 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2048 struct snd_ctl_elem_value *ucontrol)
2050 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2051 struct soc_enum *e =
2052 (struct soc_enum *)kcontrol->private_value;
2056 if (ucontrol->value.enumerated.item[0] >= e->max)
2059 mutex_lock(&widget->codec->mutex);
2061 change = widget->value != ucontrol->value.enumerated.item[0];
2062 widget->value = ucontrol->value.enumerated.item[0];
2063 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
2065 mutex_unlock(&widget->codec->mutex);
2068 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2071 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2073 * @kcontrol: mixer control
2074 * @ucontrol: control element information
2076 * Callback to get the value of a dapm semi enumerated double mixer control.
2078 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2079 * used for handling bitfield coded enumeration for example.
2081 * Returns 0 for success.
2083 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2084 struct snd_ctl_elem_value *ucontrol)
2086 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2087 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2088 unsigned int reg_val, val, mux;
2090 reg_val = snd_soc_read(widget->codec, e->reg);
2091 val = (reg_val >> e->shift_l) & e->mask;
2092 for (mux = 0; mux < e->max; mux++) {
2093 if (val == e->values[mux])
2096 ucontrol->value.enumerated.item[0] = mux;
2097 if (e->shift_l != e->shift_r) {
2098 val = (reg_val >> e->shift_r) & e->mask;
2099 for (mux = 0; mux < e->max; mux++) {
2100 if (val == e->values[mux])
2103 ucontrol->value.enumerated.item[1] = mux;
2108 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2111 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2113 * @kcontrol: mixer control
2114 * @ucontrol: control element information
2116 * Callback to set the value of a dapm semi enumerated double mixer control.
2118 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2119 * used for handling bitfield coded enumeration for example.
2121 * Returns 0 for success.
2123 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2124 struct snd_ctl_elem_value *ucontrol)
2126 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2127 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2128 unsigned int val, mux, change;
2130 struct snd_soc_dapm_update update;
2132 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2134 mux = ucontrol->value.enumerated.item[0];
2135 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2136 mask = e->mask << e->shift_l;
2137 if (e->shift_l != e->shift_r) {
2138 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2140 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2141 mask |= e->mask << e->shift_r;
2144 mutex_lock(&widget->codec->mutex);
2145 widget->value = val;
2146 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2148 update.kcontrol = kcontrol;
2149 update.widget = widget;
2150 update.reg = e->reg;
2153 widget->dapm->update = &update;
2155 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2157 widget->dapm->update = NULL;
2159 mutex_unlock(&widget->codec->mutex);
2162 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2165 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2167 * @kcontrol: mixer control
2168 * @uinfo: control element information
2170 * Callback to provide information about a pin switch control.
2172 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2173 struct snd_ctl_elem_info *uinfo)
2175 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2177 uinfo->value.integer.min = 0;
2178 uinfo->value.integer.max = 1;
2182 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2185 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2187 * @kcontrol: mixer control
2190 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2191 struct snd_ctl_elem_value *ucontrol)
2193 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2194 const char *pin = (const char *)kcontrol->private_value;
2196 mutex_lock(&codec->mutex);
2198 ucontrol->value.integer.value[0] =
2199 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2201 mutex_unlock(&codec->mutex);
2205 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2208 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2210 * @kcontrol: mixer control
2213 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_value *ucontrol)
2216 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2217 const char *pin = (const char *)kcontrol->private_value;
2219 mutex_lock(&codec->mutex);
2221 if (ucontrol->value.integer.value[0])
2222 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2224 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2226 snd_soc_dapm_sync(&codec->dapm);
2228 mutex_unlock(&codec->mutex);
2232 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2235 * snd_soc_dapm_new_control - create new dapm control
2236 * @dapm: DAPM context
2237 * @widget: widget template
2239 * Creates a new dapm control based upon the template.
2241 * Returns 0 for success else error.
2243 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2244 const struct snd_soc_dapm_widget *widget)
2246 struct snd_soc_dapm_widget *w;
2249 if ((w = dapm_cnew_widget(widget)) == NULL)
2252 name_len = strlen(widget->name) + 1;
2253 if (dapm->codec && dapm->codec->name_prefix)
2254 name_len += 1 + strlen(dapm->codec->name_prefix);
2255 w->name = kmalloc(name_len, GFP_KERNEL);
2256 if (w->name == NULL) {
2260 if (dapm->codec && dapm->codec->name_prefix)
2261 snprintf(w->name, name_len, "%s %s",
2262 dapm->codec->name_prefix, widget->name);
2264 snprintf(w->name, name_len, "%s", widget->name);
2268 w->codec = dapm->codec;
2269 INIT_LIST_HEAD(&w->sources);
2270 INIT_LIST_HEAD(&w->sinks);
2271 INIT_LIST_HEAD(&w->list);
2272 list_add(&w->list, &dapm->card->widgets);
2274 /* machine layer set ups unconnected pins and insertions */
2278 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2281 * snd_soc_dapm_new_controls - create new dapm controls
2282 * @dapm: DAPM context
2283 * @widget: widget array
2284 * @num: number of widgets
2286 * Creates new DAPM controls based upon the templates.
2288 * Returns 0 for success else error.
2290 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2291 const struct snd_soc_dapm_widget *widget,
2296 for (i = 0; i < num; i++) {
2297 ret = snd_soc_dapm_new_control(dapm, widget);
2300 "ASoC: Failed to create DAPM control %s: %d\n",
2308 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2310 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2311 const char *stream, int event)
2313 struct snd_soc_dapm_widget *w;
2315 list_for_each_entry(w, &dapm->card->widgets, list)
2317 if (!w->sname || w->dapm != dapm)
2319 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2320 w->name, w->sname, stream, event);
2321 if (strstr(w->sname, stream)) {
2323 case SND_SOC_DAPM_STREAM_START:
2326 case SND_SOC_DAPM_STREAM_STOP:
2329 case SND_SOC_DAPM_STREAM_SUSPEND:
2330 case SND_SOC_DAPM_STREAM_RESUME:
2331 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2332 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2338 dapm_power_widgets(dapm, event);
2342 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2343 * @rtd: PCM runtime data
2344 * @stream: stream name
2345 * @event: stream event
2347 * Sends a stream event to the dapm core. The core then makes any
2348 * necessary widget power changes.
2350 * Returns 0 for success else error.
2352 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2353 const char *stream, int event)
2355 struct snd_soc_codec *codec = rtd->codec;
2360 mutex_lock(&codec->mutex);
2361 soc_dapm_stream_event(&codec->dapm, stream, event);
2362 mutex_unlock(&codec->mutex);
2367 * snd_soc_dapm_enable_pin - enable pin.
2368 * @dapm: DAPM context
2371 * Enables input/output pin and its parents or children widgets iff there is
2372 * a valid audio route and active audio stream.
2373 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2374 * do any widget power switching.
2376 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2378 return snd_soc_dapm_set_pin(dapm, pin, 1);
2380 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2383 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2384 * @dapm: DAPM context
2387 * Enables input/output pin regardless of any other state. This is
2388 * intended for use with microphone bias supplies used in microphone
2391 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2392 * do any widget power switching.
2394 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2397 struct snd_soc_dapm_widget *w;
2399 list_for_each_entry(w, &dapm->card->widgets, list) {
2400 if (w->dapm != dapm)
2402 if (!strcmp(w->name, pin)) {
2403 dev_dbg(w->dapm->dev,
2404 "dapm: force enable pin %s\n", pin);
2411 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2414 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2417 * snd_soc_dapm_disable_pin - disable pin.
2418 * @dapm: DAPM context
2421 * Disables input/output pin and its parents or children widgets.
2422 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2423 * do any widget power switching.
2425 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2428 return snd_soc_dapm_set_pin(dapm, pin, 0);
2430 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2433 * snd_soc_dapm_nc_pin - permanently disable pin.
2434 * @dapm: DAPM context
2437 * Marks the specified pin as being not connected, disabling it along
2438 * any parent or child widgets. At present this is identical to
2439 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2440 * additional things such as disabling controls which only affect
2441 * paths through the pin.
2443 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2444 * do any widget power switching.
2446 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2448 return snd_soc_dapm_set_pin(dapm, pin, 0);
2450 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2453 * snd_soc_dapm_get_pin_status - get audio pin status
2454 * @dapm: DAPM context
2455 * @pin: audio signal pin endpoint (or start point)
2457 * Get audio pin status - connected or disconnected.
2459 * Returns 1 for connected otherwise 0.
2461 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2464 struct snd_soc_dapm_widget *w;
2466 list_for_each_entry(w, &dapm->card->widgets, list) {
2467 if (w->dapm != dapm)
2469 if (!strcmp(w->name, pin))
2470 return w->connected;
2475 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2478 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2479 * @dapm: DAPM context
2480 * @pin: audio signal pin endpoint (or start point)
2482 * Mark the given endpoint or pin as ignoring suspend. When the
2483 * system is disabled a path between two endpoints flagged as ignoring
2484 * suspend will not be disabled. The path must already be enabled via
2485 * normal means at suspend time, it will not be turned on if it was not
2488 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2491 struct snd_soc_dapm_widget *w;
2493 list_for_each_entry(w, &dapm->card->widgets, list) {
2494 if (w->dapm != dapm)
2496 if (!strcmp(w->name, pin)) {
2497 w->ignore_suspend = 1;
2502 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2505 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2508 * snd_soc_dapm_free - free dapm resources
2511 * Free all dapm widgets and resources.
2513 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2515 snd_soc_dapm_sys_remove(dapm->dev);
2516 dapm_free_widgets(dapm);
2517 list_del(&dapm->list);
2519 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2521 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2523 struct snd_soc_dapm_widget *w;
2524 LIST_HEAD(down_list);
2527 list_for_each_entry(w, &dapm->card->widgets, list) {
2528 if (w->dapm != dapm)
2531 dapm_seq_insert(w, &down_list, false);
2537 /* If there were no widgets to power down we're already in
2541 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2542 dapm_seq_run(dapm, &down_list, 0, false);
2543 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2548 * snd_soc_dapm_shutdown - callback for system shutdown
2550 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2552 struct snd_soc_codec *codec;
2554 list_for_each_entry(codec, &card->codec_dev_list, list) {
2555 soc_dapm_shutdown_codec(&codec->dapm);
2556 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
2560 /* Module information */
2561 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2562 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2563 MODULE_LICENSE("GPL");