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/delay.h>
37 #include <linux/bitops.h>
38 #include <linux/platform_device.h>
39 #include <linux/jiffies.h>
40 #include <linux/debugfs.h>
41 #include <sound/core.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc-dapm.h>
45 #include <sound/initval.h>
47 /* dapm power sequences - make this per codec in the future */
48 static int dapm_up_seq[] = {
49 [snd_soc_dapm_pre] = 0,
50 [snd_soc_dapm_supply] = 1,
51 [snd_soc_dapm_micbias] = 2,
52 [snd_soc_dapm_aif_in] = 3,
53 [snd_soc_dapm_aif_out] = 3,
54 [snd_soc_dapm_mic] = 4,
55 [snd_soc_dapm_mux] = 5,
56 [snd_soc_dapm_value_mux] = 5,
57 [snd_soc_dapm_dac] = 6,
58 [snd_soc_dapm_mixer] = 7,
59 [snd_soc_dapm_mixer_named_ctl] = 7,
60 [snd_soc_dapm_pga] = 8,
61 [snd_soc_dapm_adc] = 9,
62 [snd_soc_dapm_hp] = 10,
63 [snd_soc_dapm_spk] = 10,
64 [snd_soc_dapm_post] = 11,
67 static int dapm_down_seq[] = {
68 [snd_soc_dapm_pre] = 0,
69 [snd_soc_dapm_adc] = 1,
70 [snd_soc_dapm_hp] = 2,
71 [snd_soc_dapm_spk] = 2,
72 [snd_soc_dapm_pga] = 4,
73 [snd_soc_dapm_mixer_named_ctl] = 5,
74 [snd_soc_dapm_mixer] = 5,
75 [snd_soc_dapm_dac] = 6,
76 [snd_soc_dapm_mic] = 7,
77 [snd_soc_dapm_micbias] = 8,
78 [snd_soc_dapm_mux] = 9,
79 [snd_soc_dapm_value_mux] = 9,
80 [snd_soc_dapm_aif_in] = 10,
81 [snd_soc_dapm_aif_out] = 10,
82 [snd_soc_dapm_supply] = 11,
83 [snd_soc_dapm_post] = 12,
86 static void pop_wait(u32 pop_time)
89 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
92 static void pop_dbg(u32 pop_time, const char *fmt, ...)
105 /* create a new dapm widget */
106 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
107 const struct snd_soc_dapm_widget *_widget)
109 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
113 * snd_soc_dapm_set_bias_level - set the bias level for the system
114 * @socdev: audio device
115 * @level: level to configure
117 * Configure the bias (power) levels for the SoC audio device.
119 * Returns 0 for success else error.
121 static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
122 enum snd_soc_bias_level level)
124 struct snd_soc_card *card = socdev->card;
125 struct snd_soc_codec *codec = socdev->card->codec;
129 case SND_SOC_BIAS_ON:
130 dev_dbg(socdev->dev, "Setting full bias\n");
132 case SND_SOC_BIAS_PREPARE:
133 dev_dbg(socdev->dev, "Setting bias prepare\n");
135 case SND_SOC_BIAS_STANDBY:
136 dev_dbg(socdev->dev, "Setting standby bias\n");
138 case SND_SOC_BIAS_OFF:
139 dev_dbg(socdev->dev, "Setting bias off\n");
142 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
146 if (card->set_bias_level)
147 ret = card->set_bias_level(card, level);
149 if (codec->set_bias_level)
150 ret = codec->set_bias_level(codec, level);
152 codec->bias_level = level;
158 /* set up initial codec paths */
159 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
160 struct snd_soc_dapm_path *p, int i)
163 case snd_soc_dapm_switch:
164 case snd_soc_dapm_mixer:
165 case snd_soc_dapm_mixer_named_ctl: {
167 struct soc_mixer_control *mc = (struct soc_mixer_control *)
168 w->kcontrols[i].private_value;
169 unsigned int reg = mc->reg;
170 unsigned int shift = mc->shift;
172 unsigned int mask = (1 << fls(max)) - 1;
173 unsigned int invert = mc->invert;
175 val = snd_soc_read(w->codec, reg);
176 val = (val >> shift) & mask;
178 if ((invert && !val) || (!invert && val))
184 case snd_soc_dapm_mux: {
185 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
186 int val, item, bitmask;
188 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
190 val = snd_soc_read(w->codec, e->reg);
191 item = (val >> e->shift_l) & (bitmask - 1);
194 for (i = 0; i < e->max; i++) {
195 if (!(strcmp(p->name, e->texts[i])) && item == i)
200 case snd_soc_dapm_value_mux: {
201 struct soc_enum *e = (struct soc_enum *)
202 w->kcontrols[i].private_value;
205 val = snd_soc_read(w->codec, e->reg);
206 val = (val >> e->shift_l) & e->mask;
207 for (item = 0; item < e->max; item++) {
208 if (val == e->values[item])
213 for (i = 0; i < e->max; i++) {
214 if (!(strcmp(p->name, e->texts[i])) && item == i)
219 /* does not effect routing - always connected */
220 case snd_soc_dapm_pga:
221 case snd_soc_dapm_output:
222 case snd_soc_dapm_adc:
223 case snd_soc_dapm_input:
224 case snd_soc_dapm_dac:
225 case snd_soc_dapm_micbias:
226 case snd_soc_dapm_vmid:
227 case snd_soc_dapm_supply:
228 case snd_soc_dapm_aif_in:
229 case snd_soc_dapm_aif_out:
232 /* does effect routing - dynamically connected */
233 case snd_soc_dapm_hp:
234 case snd_soc_dapm_mic:
235 case snd_soc_dapm_spk:
236 case snd_soc_dapm_line:
237 case snd_soc_dapm_pre:
238 case snd_soc_dapm_post:
244 /* connect mux widget to its interconnecting audio paths */
245 static int dapm_connect_mux(struct snd_soc_codec *codec,
246 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
247 struct snd_soc_dapm_path *path, const char *control_name,
248 const struct snd_kcontrol_new *kcontrol)
250 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
253 for (i = 0; i < e->max; i++) {
254 if (!(strcmp(control_name, e->texts[i]))) {
255 list_add(&path->list, &codec->dapm_paths);
256 list_add(&path->list_sink, &dest->sources);
257 list_add(&path->list_source, &src->sinks);
258 path->name = (char*)e->texts[i];
259 dapm_set_path_status(dest, path, 0);
267 /* connect mixer widget to its interconnecting audio paths */
268 static int dapm_connect_mixer(struct snd_soc_codec *codec,
269 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
270 struct snd_soc_dapm_path *path, const char *control_name)
274 /* search for mixer kcontrol */
275 for (i = 0; i < dest->num_kcontrols; i++) {
276 if (!strcmp(control_name, dest->kcontrols[i].name)) {
277 list_add(&path->list, &codec->dapm_paths);
278 list_add(&path->list_sink, &dest->sources);
279 list_add(&path->list_source, &src->sinks);
280 path->name = dest->kcontrols[i].name;
281 dapm_set_path_status(dest, path, i);
288 /* update dapm codec register bits */
289 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
292 unsigned int old, new;
293 struct snd_soc_codec *codec = widget->codec;
295 /* check for valid widgets */
296 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
297 widget->id == snd_soc_dapm_output ||
298 widget->id == snd_soc_dapm_hp ||
299 widget->id == snd_soc_dapm_mic ||
300 widget->id == snd_soc_dapm_line ||
301 widget->id == snd_soc_dapm_spk)
304 power = widget->power;
306 power = (power ? 0:1);
308 old = snd_soc_read(codec, widget->reg);
309 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
313 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
314 widget->name, widget->power ? "on" : "off",
316 pop_wait(codec->pop_time);
317 snd_soc_write(codec, widget->reg, new);
319 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
324 /* create new dapm mixer control */
325 static int dapm_new_mixer(struct snd_soc_codec *codec,
326 struct snd_soc_dapm_widget *w)
330 struct snd_soc_dapm_path *path;
333 for (i = 0; i < w->num_kcontrols; i++) {
336 list_for_each_entry(path, &w->sources, list_sink) {
338 /* mixer/mux paths name must match control name */
339 if (path->name != (char*)w->kcontrols[i].name)
342 /* add dapm control with long name.
343 * for dapm_mixer this is the concatenation of the
344 * mixer and kcontrol name.
345 * for dapm_mixer_named_ctl this is simply the
348 name_len = strlen(w->kcontrols[i].name) + 1;
349 if (w->id != snd_soc_dapm_mixer_named_ctl)
350 name_len += 1 + strlen(w->name);
352 path->long_name = kmalloc(name_len, GFP_KERNEL);
354 if (path->long_name == NULL)
359 snprintf(path->long_name, name_len, "%s %s",
360 w->name, w->kcontrols[i].name);
362 case snd_soc_dapm_mixer_named_ctl:
363 snprintf(path->long_name, name_len, "%s",
364 w->kcontrols[i].name);
368 path->long_name[name_len - 1] = '\0';
370 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
372 ret = snd_ctl_add(codec->card, path->kcontrol);
374 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
377 kfree(path->long_name);
378 path->long_name = NULL;
386 /* create new dapm mux control */
387 static int dapm_new_mux(struct snd_soc_codec *codec,
388 struct snd_soc_dapm_widget *w)
390 struct snd_soc_dapm_path *path = NULL;
391 struct snd_kcontrol *kcontrol;
394 if (!w->num_kcontrols) {
395 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
399 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
400 ret = snd_ctl_add(codec->card, kcontrol);
404 list_for_each_entry(path, &w->sources, list_sink)
405 path->kcontrol = kcontrol;
410 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
414 /* create new dapm volume control */
415 static int dapm_new_pga(struct snd_soc_codec *codec,
416 struct snd_soc_dapm_widget *w)
418 if (w->num_kcontrols)
419 pr_err("asoc: PGA controls not supported: '%s'\n", w->name);
424 /* reset 'walked' bit for each dapm path */
425 static inline void dapm_clear_walk(struct snd_soc_codec *codec)
427 struct snd_soc_dapm_path *p;
429 list_for_each_entry(p, &codec->dapm_paths, list)
434 * Recursively check for a completed path to an active or physically connected
435 * output widget. Returns number of complete paths.
437 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
439 struct snd_soc_dapm_path *path;
442 if (widget->id == snd_soc_dapm_supply)
445 switch (widget->id) {
446 case snd_soc_dapm_adc:
447 case snd_soc_dapm_aif_out:
454 if (widget->connected) {
455 /* connected pin ? */
456 if (widget->id == snd_soc_dapm_output && !widget->ext)
459 /* connected jack or spk ? */
460 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
461 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
465 list_for_each_entry(path, &widget->sinks, list_source) {
469 if (path->sink && path->connect) {
471 con += is_connected_output_ep(path->sink);
479 * Recursively check for a completed path to an active or physically connected
480 * input widget. Returns number of complete paths.
482 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
484 struct snd_soc_dapm_path *path;
487 if (widget->id == snd_soc_dapm_supply)
490 /* active stream ? */
491 switch (widget->id) {
492 case snd_soc_dapm_dac:
493 case snd_soc_dapm_aif_in:
500 if (widget->connected) {
501 /* connected pin ? */
502 if (widget->id == snd_soc_dapm_input && !widget->ext)
505 /* connected VMID/Bias for lower pops */
506 if (widget->id == snd_soc_dapm_vmid)
509 /* connected jack ? */
510 if (widget->id == snd_soc_dapm_mic ||
511 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
515 list_for_each_entry(path, &widget->sources, list_sink) {
519 if (path->source && path->connect) {
521 con += is_connected_input_ep(path->source);
529 * Handler for generic register modifier widget.
531 int dapm_reg_event(struct snd_soc_dapm_widget *w,
532 struct snd_kcontrol *kcontrol, int event)
536 if (SND_SOC_DAPM_EVENT_ON(event))
541 snd_soc_update_bits(w->codec, -(w->reg + 1),
542 w->mask << w->shift, val << w->shift);
546 EXPORT_SYMBOL_GPL(dapm_reg_event);
548 /* Standard power change method, used to apply power changes to most
551 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
555 /* call any power change event handlers */
557 pr_debug("power %s event for %s flags %x\n",
558 w->power ? "on" : "off",
559 w->name, w->event_flags);
561 /* power up pre event */
562 if (w->power && w->event &&
563 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
564 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
569 /* power down pre event */
570 if (!w->power && w->event &&
571 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
572 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
579 /* power up post event */
580 if (w->power && w->event &&
581 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
583 NULL, SND_SOC_DAPM_POST_PMU);
588 /* power down post event */
589 if (!w->power && w->event &&
590 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
591 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
599 /* Generic check to see if a widget should be powered.
601 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
605 in = is_connected_input_ep(w);
606 dapm_clear_walk(w->codec);
607 out = is_connected_output_ep(w);
608 dapm_clear_walk(w->codec);
609 return out != 0 && in != 0;
612 /* Check to see if an ADC has power */
613 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
618 in = is_connected_input_ep(w);
619 dapm_clear_walk(w->codec);
622 return dapm_generic_check_power(w);
626 /* Check to see if a DAC has power */
627 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
632 out = is_connected_output_ep(w);
633 dapm_clear_walk(w->codec);
636 return dapm_generic_check_power(w);
640 /* Check to see if a power supply is needed */
641 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
643 struct snd_soc_dapm_path *path;
646 /* Check if one of our outputs is connected */
647 list_for_each_entry(path, &w->sinks, list_source) {
648 if (path->connected &&
649 !path->connected(path->source, path->sink))
652 if (path->sink && path->sink->power_check &&
653 path->sink->power_check(path->sink)) {
659 dapm_clear_walk(w->codec);
664 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
665 struct snd_soc_dapm_widget *b,
668 if (a->codec != b->codec)
669 return (unsigned long)a - (unsigned long)b;
670 if (sort[a->id] != sort[b->id])
671 return sort[a->id] - sort[b->id];
672 if (a->reg != b->reg)
673 return a->reg - b->reg;
678 /* Insert a widget in order into a DAPM power sequence. */
679 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
680 struct list_head *list,
683 struct snd_soc_dapm_widget *w;
685 list_for_each_entry(w, list, power_list)
686 if (dapm_seq_compare(new_widget, w, sort) < 0) {
687 list_add_tail(&new_widget->power_list, &w->power_list);
691 list_add_tail(&new_widget->power_list, list);
694 /* Apply the coalesced changes from a DAPM sequence */
695 static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
696 struct list_head *pending)
698 struct snd_soc_dapm_widget *w;
700 unsigned int value = 0;
701 unsigned int mask = 0;
702 unsigned int cur_mask;
704 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
707 list_for_each_entry(w, pending, power_list) {
708 cur_mask = 1 << w->shift;
709 BUG_ON(reg != w->reg);
720 pop_dbg(codec->pop_time,
721 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
722 w->name, reg, value, mask);
724 /* power up pre event */
725 if (w->power && w->event &&
726 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
727 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
729 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
731 pr_err("%s: pre event failed: %d\n",
735 /* power down pre event */
736 if (!w->power && w->event &&
737 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
738 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
740 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
742 pr_err("%s: pre event failed: %d\n",
748 pop_dbg(codec->pop_time,
749 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
750 value, mask, reg, codec->pop_time);
751 pop_wait(codec->pop_time);
752 snd_soc_update_bits(codec, reg, mask, value);
755 list_for_each_entry(w, pending, power_list) {
756 /* power up post event */
757 if (w->power && w->event &&
758 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
759 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
762 NULL, SND_SOC_DAPM_POST_PMU);
764 pr_err("%s: post event failed: %d\n",
768 /* power down post event */
769 if (!w->power && w->event &&
770 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
771 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
773 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
775 pr_err("%s: post event failed: %d\n",
781 /* Apply a DAPM power sequence.
783 * We walk over a pre-sorted list of widgets to apply power to. In
784 * order to minimise the number of writes to the device required
785 * multiple widgets will be updated in a single write where possible.
786 * Currently anything that requires more than a single write is not
789 static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
790 int event, int sort[])
792 struct snd_soc_dapm_widget *w, *n;
795 int cur_reg = SND_SOC_NOPM;
798 list_for_each_entry_safe(w, n, list, power_list) {
801 /* Do we need to apply any queued changes? */
802 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
803 if (!list_empty(&pending))
804 dapm_seq_run_coalesced(codec, &pending);
806 INIT_LIST_HEAD(&pending);
808 cur_reg = SND_SOC_NOPM;
812 case snd_soc_dapm_pre:
814 list_for_each_entry_safe_continue(w, n, list,
817 if (event == SND_SOC_DAPM_STREAM_START)
819 NULL, SND_SOC_DAPM_PRE_PMU);
820 else if (event == SND_SOC_DAPM_STREAM_STOP)
822 NULL, SND_SOC_DAPM_PRE_PMD);
825 case snd_soc_dapm_post:
827 list_for_each_entry_safe_continue(w, n, list,
830 if (event == SND_SOC_DAPM_STREAM_START)
832 NULL, SND_SOC_DAPM_POST_PMU);
833 else if (event == SND_SOC_DAPM_STREAM_STOP)
835 NULL, SND_SOC_DAPM_POST_PMD);
838 case snd_soc_dapm_input:
839 case snd_soc_dapm_output:
840 case snd_soc_dapm_hp:
841 case snd_soc_dapm_mic:
842 case snd_soc_dapm_line:
843 case snd_soc_dapm_spk:
844 /* No register support currently */
845 ret = dapm_generic_apply_power(w);
849 /* Queue it up for application */
850 cur_sort = sort[w->id];
852 list_move(&w->power_list, &pending);
857 pr_err("Failed to apply widget power: %d\n",
861 if (!list_empty(&pending))
862 dapm_seq_run_coalesced(codec, &pending);
866 * Scan each dapm widget for complete audio path.
867 * A complete path is a route that has valid endpoints i.e.:-
869 * o DAC to output pin.
870 * o Input Pin to ADC.
871 * o Input pin to Output pin (bypass, sidetone)
872 * o DAC to ADC (loopback).
874 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
876 struct snd_soc_device *socdev = codec->socdev;
877 struct snd_soc_dapm_widget *w;
879 LIST_HEAD(down_list);
884 /* Check which widgets we need to power and store them in
885 * lists indicating if they should be powered up or down.
887 list_for_each_entry(w, &codec->dapm_widgets, list) {
889 case snd_soc_dapm_pre:
890 dapm_seq_insert(w, &down_list, dapm_down_seq);
892 case snd_soc_dapm_post:
893 dapm_seq_insert(w, &up_list, dapm_up_seq);
900 /* If we're suspending then pull down all the
903 case SND_SOC_DAPM_STREAM_SUSPEND:
909 power = w->power_check(w);
917 if (w->power == power)
921 dapm_seq_insert(w, &up_list, dapm_up_seq);
923 dapm_seq_insert(w, &down_list, dapm_down_seq);
930 /* If there are no DAPM widgets then try to figure out power from the
933 if (list_empty(&codec->dapm_widgets)) {
935 case SND_SOC_DAPM_STREAM_START:
936 case SND_SOC_DAPM_STREAM_RESUME:
939 case SND_SOC_DAPM_STREAM_SUSPEND:
942 case SND_SOC_DAPM_STREAM_NOP:
943 switch (codec->bias_level) {
944 case SND_SOC_BIAS_STANDBY:
945 case SND_SOC_BIAS_OFF:
958 if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
959 ret = snd_soc_dapm_set_bias_level(socdev,
960 SND_SOC_BIAS_STANDBY);
962 pr_err("Failed to turn on bias: %d\n", ret);
965 /* If we're changing to all on or all off then prepare */
966 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
967 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
968 ret = snd_soc_dapm_set_bias_level(socdev,
969 SND_SOC_BIAS_PREPARE);
971 pr_err("Failed to prepare bias: %d\n", ret);
974 /* Power down widgets first; try to avoid amplifying pops. */
975 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
978 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
980 /* If we just powered the last thing off drop to standby bias */
981 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
982 ret = snd_soc_dapm_set_bias_level(socdev,
983 SND_SOC_BIAS_STANDBY);
985 pr_err("Failed to apply standby bias: %d\n", ret);
988 /* If we're in standby and can support bias off then do that */
989 if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
990 codec->idle_bias_off) {
991 ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
993 pr_err("Failed to turn off bias: %d\n", ret);
996 /* If we just powered up then move to active bias */
997 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
998 ret = snd_soc_dapm_set_bias_level(socdev,
1001 pr_err("Failed to apply active bias: %d\n", ret);
1004 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1006 pop_wait(codec->pop_time);
1011 #ifdef CONFIG_DEBUG_FS
1012 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1014 file->private_data = inode->i_private;
1018 static ssize_t dapm_widget_power_read_file(struct file *file,
1019 char __user *user_buf,
1020 size_t count, loff_t *ppos)
1022 struct snd_soc_dapm_widget *w = file->private_data;
1026 struct snd_soc_dapm_path *p = NULL;
1028 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1032 in = is_connected_input_ep(w);
1033 dapm_clear_walk(w->codec);
1034 out = is_connected_output_ep(w);
1035 dapm_clear_walk(w->codec);
1037 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1038 w->name, w->power ? "On" : "Off", in, out);
1041 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1042 " - R%d(0x%x) bit %d",
1043 w->reg, w->reg, w->shift);
1045 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1048 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1050 w->active ? "active" : "inactive");
1052 list_for_each_entry(p, &w->sources, list_sink) {
1053 if (p->connected && !p->connected(w, p->sink))
1057 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1059 p->name ? p->name : "static",
1062 list_for_each_entry(p, &w->sinks, list_source) {
1063 if (p->connected && !p->connected(w, p->sink))
1067 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1069 p->name ? p->name : "static",
1073 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1079 static const struct file_operations dapm_widget_power_fops = {
1080 .open = dapm_widget_power_open_file,
1081 .read = dapm_widget_power_read_file,
1084 void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1086 struct snd_soc_dapm_widget *w;
1089 if (!codec->debugfs_dapm)
1092 list_for_each_entry(w, &codec->dapm_widgets, list) {
1096 d = debugfs_create_file(w->name, 0444,
1097 codec->debugfs_dapm, w,
1098 &dapm_widget_power_fops);
1101 "ASoC: Failed to create %s debugfs file\n",
1106 void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1111 /* test and update the power status of a mux widget */
1112 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1113 struct snd_kcontrol *kcontrol, int change,
1114 int mux, struct soc_enum *e)
1116 struct snd_soc_dapm_path *path;
1119 if (widget->id != snd_soc_dapm_mux &&
1120 widget->id != snd_soc_dapm_value_mux)
1126 /* find dapm widget path assoc with kcontrol */
1127 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1128 if (path->kcontrol != kcontrol)
1131 if (!path->name || !e->texts[mux])
1135 /* we now need to match the string in the enum to the path */
1136 if (!(strcmp(path->name, e->texts[mux])))
1137 path->connect = 1; /* new connection */
1139 path->connect = 0; /* old connection must be powered down */
1143 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1148 /* test and update the power status of a mixer or switch widget */
1149 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1150 struct snd_kcontrol *kcontrol, int connect)
1152 struct snd_soc_dapm_path *path;
1155 if (widget->id != snd_soc_dapm_mixer &&
1156 widget->id != snd_soc_dapm_mixer_named_ctl &&
1157 widget->id != snd_soc_dapm_switch)
1160 /* find dapm widget path assoc with kcontrol */
1161 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1162 if (path->kcontrol != kcontrol)
1165 /* found, now check type */
1167 path->connect = connect;
1172 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1177 /* show dapm widget status in sys fs */
1178 static ssize_t dapm_widget_show(struct device *dev,
1179 struct device_attribute *attr, char *buf)
1181 struct snd_soc_device *devdata = dev_get_drvdata(dev);
1182 struct snd_soc_codec *codec = devdata->card->codec;
1183 struct snd_soc_dapm_widget *w;
1185 char *state = "not set";
1187 list_for_each_entry(w, &codec->dapm_widgets, list) {
1189 /* only display widgets that burnm power */
1191 case snd_soc_dapm_hp:
1192 case snd_soc_dapm_mic:
1193 case snd_soc_dapm_spk:
1194 case snd_soc_dapm_line:
1195 case snd_soc_dapm_micbias:
1196 case snd_soc_dapm_dac:
1197 case snd_soc_dapm_adc:
1198 case snd_soc_dapm_pga:
1199 case snd_soc_dapm_mixer:
1200 case snd_soc_dapm_mixer_named_ctl:
1201 case snd_soc_dapm_supply:
1203 count += sprintf(buf + count, "%s: %s\n",
1204 w->name, w->power ? "On":"Off");
1211 switch (codec->bias_level) {
1212 case SND_SOC_BIAS_ON:
1215 case SND_SOC_BIAS_PREPARE:
1218 case SND_SOC_BIAS_STANDBY:
1221 case SND_SOC_BIAS_OFF:
1225 count += sprintf(buf + count, "PM State: %s\n", state);
1230 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1232 int snd_soc_dapm_sys_add(struct device *dev)
1234 return device_create_file(dev, &dev_attr_dapm_widget);
1237 static void snd_soc_dapm_sys_remove(struct device *dev)
1239 device_remove_file(dev, &dev_attr_dapm_widget);
1242 /* free all dapm widgets and resources */
1243 static void dapm_free_widgets(struct snd_soc_codec *codec)
1245 struct snd_soc_dapm_widget *w, *next_w;
1246 struct snd_soc_dapm_path *p, *next_p;
1248 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1253 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1255 kfree(p->long_name);
1260 static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
1261 const char *pin, int status)
1263 struct snd_soc_dapm_widget *w;
1265 list_for_each_entry(w, &codec->dapm_widgets, list) {
1266 if (!strcmp(w->name, pin)) {
1267 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
1268 w->connected = status;
1269 /* Allow disabling of forced pins */
1276 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
1281 * snd_soc_dapm_sync - scan and power dapm paths
1282 * @codec: audio codec
1284 * Walks all dapm audio paths and powers widgets according to their
1285 * stream or path usage.
1287 * Returns 0 for success.
1289 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
1291 return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1293 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1295 static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1296 const struct snd_soc_dapm_route *route)
1298 struct snd_soc_dapm_path *path;
1299 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1300 const char *sink = route->sink;
1301 const char *control = route->control;
1302 const char *source = route->source;
1305 /* find src and dest widgets */
1306 list_for_each_entry(w, &codec->dapm_widgets, list) {
1308 if (!wsink && !(strcmp(w->name, sink))) {
1312 if (!wsource && !(strcmp(w->name, source))) {
1317 if (wsource == NULL || wsink == NULL)
1320 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1324 path->source = wsource;
1326 path->connected = route->connected;
1327 INIT_LIST_HEAD(&path->list);
1328 INIT_LIST_HEAD(&path->list_source);
1329 INIT_LIST_HEAD(&path->list_sink);
1331 /* check for external widgets */
1332 if (wsink->id == snd_soc_dapm_input) {
1333 if (wsource->id == snd_soc_dapm_micbias ||
1334 wsource->id == snd_soc_dapm_mic ||
1335 wsource->id == snd_soc_dapm_line ||
1336 wsource->id == snd_soc_dapm_output)
1339 if (wsource->id == snd_soc_dapm_output) {
1340 if (wsink->id == snd_soc_dapm_spk ||
1341 wsink->id == snd_soc_dapm_hp ||
1342 wsink->id == snd_soc_dapm_line ||
1343 wsink->id == snd_soc_dapm_input)
1347 /* connect static paths */
1348 if (control == NULL) {
1349 list_add(&path->list, &codec->dapm_paths);
1350 list_add(&path->list_sink, &wsink->sources);
1351 list_add(&path->list_source, &wsource->sinks);
1356 /* connect dynamic paths */
1358 case snd_soc_dapm_adc:
1359 case snd_soc_dapm_dac:
1360 case snd_soc_dapm_pga:
1361 case snd_soc_dapm_input:
1362 case snd_soc_dapm_output:
1363 case snd_soc_dapm_micbias:
1364 case snd_soc_dapm_vmid:
1365 case snd_soc_dapm_pre:
1366 case snd_soc_dapm_post:
1367 case snd_soc_dapm_supply:
1368 case snd_soc_dapm_aif_in:
1369 case snd_soc_dapm_aif_out:
1370 list_add(&path->list, &codec->dapm_paths);
1371 list_add(&path->list_sink, &wsink->sources);
1372 list_add(&path->list_source, &wsource->sinks);
1375 case snd_soc_dapm_mux:
1376 case snd_soc_dapm_value_mux:
1377 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1378 &wsink->kcontrols[0]);
1382 case snd_soc_dapm_switch:
1383 case snd_soc_dapm_mixer:
1384 case snd_soc_dapm_mixer_named_ctl:
1385 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1389 case snd_soc_dapm_hp:
1390 case snd_soc_dapm_mic:
1391 case snd_soc_dapm_line:
1392 case snd_soc_dapm_spk:
1393 list_add(&path->list, &codec->dapm_paths);
1394 list_add(&path->list_sink, &wsink->sources);
1395 list_add(&path->list_source, &wsource->sinks);
1402 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1409 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1411 * @route: audio routes
1412 * @num: number of routes
1414 * Connects 2 dapm widgets together via a named audio path. The sink is
1415 * the widget receiving the audio signal, whilst the source is the sender
1416 * of the audio signal.
1418 * Returns 0 for success else error. On error all resources can be freed
1419 * with a call to snd_soc_card_free().
1421 int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1422 const struct snd_soc_dapm_route *route, int num)
1426 for (i = 0; i < num; i++) {
1427 ret = snd_soc_dapm_add_route(codec, route);
1429 printk(KERN_ERR "Failed to add route %s->%s\n",
1439 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1442 * snd_soc_dapm_new_widgets - add new dapm widgets
1443 * @codec: audio codec
1445 * Checks the codec for any new dapm widgets and creates them if found.
1447 * Returns 0 for success.
1449 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1451 struct snd_soc_dapm_widget *w;
1453 list_for_each_entry(w, &codec->dapm_widgets, list)
1459 case snd_soc_dapm_switch:
1460 case snd_soc_dapm_mixer:
1461 case snd_soc_dapm_mixer_named_ctl:
1462 w->power_check = dapm_generic_check_power;
1463 dapm_new_mixer(codec, w);
1465 case snd_soc_dapm_mux:
1466 case snd_soc_dapm_value_mux:
1467 w->power_check = dapm_generic_check_power;
1468 dapm_new_mux(codec, w);
1470 case snd_soc_dapm_adc:
1471 case snd_soc_dapm_aif_out:
1472 w->power_check = dapm_adc_check_power;
1474 case snd_soc_dapm_dac:
1475 case snd_soc_dapm_aif_in:
1476 w->power_check = dapm_dac_check_power;
1478 case snd_soc_dapm_pga:
1479 w->power_check = dapm_generic_check_power;
1480 dapm_new_pga(codec, w);
1482 case snd_soc_dapm_input:
1483 case snd_soc_dapm_output:
1484 case snd_soc_dapm_micbias:
1485 case snd_soc_dapm_spk:
1486 case snd_soc_dapm_hp:
1487 case snd_soc_dapm_mic:
1488 case snd_soc_dapm_line:
1489 w->power_check = dapm_generic_check_power;
1491 case snd_soc_dapm_supply:
1492 w->power_check = dapm_supply_check_power;
1493 case snd_soc_dapm_vmid:
1494 case snd_soc_dapm_pre:
1495 case snd_soc_dapm_post:
1501 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1504 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1507 * snd_soc_dapm_get_volsw - dapm mixer get callback
1508 * @kcontrol: mixer control
1509 * @ucontrol: control element information
1511 * Callback to get the value of a dapm mixer control.
1513 * Returns 0 for success.
1515 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1516 struct snd_ctl_elem_value *ucontrol)
1518 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1519 struct soc_mixer_control *mc =
1520 (struct soc_mixer_control *)kcontrol->private_value;
1521 unsigned int reg = mc->reg;
1522 unsigned int shift = mc->shift;
1523 unsigned int rshift = mc->rshift;
1525 unsigned int invert = mc->invert;
1526 unsigned int mask = (1 << fls(max)) - 1;
1528 ucontrol->value.integer.value[0] =
1529 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1530 if (shift != rshift)
1531 ucontrol->value.integer.value[1] =
1532 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1534 ucontrol->value.integer.value[0] =
1535 max - ucontrol->value.integer.value[0];
1536 if (shift != rshift)
1537 ucontrol->value.integer.value[1] =
1538 max - ucontrol->value.integer.value[1];
1543 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1546 * snd_soc_dapm_put_volsw - dapm mixer set callback
1547 * @kcontrol: mixer control
1548 * @ucontrol: control element information
1550 * Callback to set the value of a dapm mixer control.
1552 * Returns 0 for success.
1554 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1555 struct snd_ctl_elem_value *ucontrol)
1557 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1558 struct soc_mixer_control *mc =
1559 (struct soc_mixer_control *)kcontrol->private_value;
1560 unsigned int reg = mc->reg;
1561 unsigned int shift = mc->shift;
1562 unsigned int rshift = mc->rshift;
1564 unsigned int mask = (1 << fls(max)) - 1;
1565 unsigned int invert = mc->invert;
1566 unsigned int val, val2, val_mask;
1570 val = (ucontrol->value.integer.value[0] & mask);
1574 val_mask = mask << shift;
1576 if (shift != rshift) {
1577 val2 = (ucontrol->value.integer.value[1] & mask);
1580 val_mask |= mask << rshift;
1581 val |= val2 << rshift;
1584 mutex_lock(&widget->codec->mutex);
1585 widget->value = val;
1587 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1589 /* new connection */
1590 connect = invert ? 0:1;
1592 /* old connection must be powered down */
1593 connect = invert ? 1:0;
1595 dapm_mixer_update_power(widget, kcontrol, connect);
1598 if (widget->event) {
1599 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1600 ret = widget->event(widget, kcontrol,
1601 SND_SOC_DAPM_PRE_REG);
1607 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1608 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1609 ret = widget->event(widget, kcontrol,
1610 SND_SOC_DAPM_POST_REG);
1612 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1615 mutex_unlock(&widget->codec->mutex);
1618 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1621 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1622 * @kcontrol: mixer control
1623 * @ucontrol: control element information
1625 * Callback to get the value of a dapm enumerated double mixer control.
1627 * Returns 0 for success.
1629 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1630 struct snd_ctl_elem_value *ucontrol)
1632 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1633 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1634 unsigned int val, bitmask;
1636 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1638 val = snd_soc_read(widget->codec, e->reg);
1639 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1640 if (e->shift_l != e->shift_r)
1641 ucontrol->value.enumerated.item[1] =
1642 (val >> e->shift_r) & (bitmask - 1);
1646 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1649 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1650 * @kcontrol: mixer control
1651 * @ucontrol: control element information
1653 * Callback to set the value of a dapm enumerated double mixer control.
1655 * Returns 0 for success.
1657 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1658 struct snd_ctl_elem_value *ucontrol)
1660 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1661 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1662 unsigned int val, mux, change;
1663 unsigned int mask, bitmask;
1666 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1668 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1670 mux = ucontrol->value.enumerated.item[0];
1671 val = mux << e->shift_l;
1672 mask = (bitmask - 1) << e->shift_l;
1673 if (e->shift_l != e->shift_r) {
1674 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1676 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1677 mask |= (bitmask - 1) << e->shift_r;
1680 mutex_lock(&widget->codec->mutex);
1681 widget->value = val;
1682 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1683 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1685 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1686 ret = widget->event(widget,
1687 kcontrol, SND_SOC_DAPM_PRE_REG);
1692 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1694 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1695 ret = widget->event(widget,
1696 kcontrol, SND_SOC_DAPM_POST_REG);
1699 mutex_unlock(&widget->codec->mutex);
1702 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1705 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1706 * @kcontrol: mixer control
1707 * @ucontrol: control element information
1709 * Returns 0 for success.
1711 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1712 struct snd_ctl_elem_value *ucontrol)
1714 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1716 ucontrol->value.enumerated.item[0] = widget->value;
1720 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1723 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1724 * @kcontrol: mixer control
1725 * @ucontrol: control element information
1727 * Returns 0 for success.
1729 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1730 struct snd_ctl_elem_value *ucontrol)
1732 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1733 struct soc_enum *e =
1734 (struct soc_enum *)kcontrol->private_value;
1738 if (ucontrol->value.enumerated.item[0] >= e->max)
1741 mutex_lock(&widget->codec->mutex);
1743 change = widget->value != ucontrol->value.enumerated.item[0];
1744 widget->value = ucontrol->value.enumerated.item[0];
1745 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1747 mutex_unlock(&widget->codec->mutex);
1750 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1753 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1755 * @kcontrol: mixer control
1756 * @ucontrol: control element information
1758 * Callback to get the value of a dapm semi enumerated double mixer control.
1760 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1761 * used for handling bitfield coded enumeration for example.
1763 * Returns 0 for success.
1765 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_value *ucontrol)
1768 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1769 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1770 unsigned int reg_val, val, mux;
1772 reg_val = snd_soc_read(widget->codec, e->reg);
1773 val = (reg_val >> e->shift_l) & e->mask;
1774 for (mux = 0; mux < e->max; mux++) {
1775 if (val == e->values[mux])
1778 ucontrol->value.enumerated.item[0] = mux;
1779 if (e->shift_l != e->shift_r) {
1780 val = (reg_val >> e->shift_r) & e->mask;
1781 for (mux = 0; mux < e->max; mux++) {
1782 if (val == e->values[mux])
1785 ucontrol->value.enumerated.item[1] = mux;
1790 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1793 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1795 * @kcontrol: mixer control
1796 * @ucontrol: control element information
1798 * Callback to set the value of a dapm semi enumerated double mixer control.
1800 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1801 * used for handling bitfield coded enumeration for example.
1803 * Returns 0 for success.
1805 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1806 struct snd_ctl_elem_value *ucontrol)
1808 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1809 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1810 unsigned int val, mux, change;
1814 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1816 mux = ucontrol->value.enumerated.item[0];
1817 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1818 mask = e->mask << e->shift_l;
1819 if (e->shift_l != e->shift_r) {
1820 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1822 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1823 mask |= e->mask << e->shift_r;
1826 mutex_lock(&widget->codec->mutex);
1827 widget->value = val;
1828 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1829 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1831 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1832 ret = widget->event(widget,
1833 kcontrol, SND_SOC_DAPM_PRE_REG);
1838 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1840 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1841 ret = widget->event(widget,
1842 kcontrol, SND_SOC_DAPM_POST_REG);
1845 mutex_unlock(&widget->codec->mutex);
1848 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1851 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1853 * @kcontrol: mixer control
1854 * @uinfo: control element information
1856 * Callback to provide information about a pin switch control.
1858 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1859 struct snd_ctl_elem_info *uinfo)
1861 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1863 uinfo->value.integer.min = 0;
1864 uinfo->value.integer.max = 1;
1868 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1871 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1873 * @kcontrol: mixer control
1876 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1877 struct snd_ctl_elem_value *ucontrol)
1879 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1880 const char *pin = (const char *)kcontrol->private_value;
1882 mutex_lock(&codec->mutex);
1884 ucontrol->value.integer.value[0] =
1885 snd_soc_dapm_get_pin_status(codec, pin);
1887 mutex_unlock(&codec->mutex);
1891 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1894 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1896 * @kcontrol: mixer control
1899 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1900 struct snd_ctl_elem_value *ucontrol)
1902 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1903 const char *pin = (const char *)kcontrol->private_value;
1905 mutex_lock(&codec->mutex);
1907 if (ucontrol->value.integer.value[0])
1908 snd_soc_dapm_enable_pin(codec, pin);
1910 snd_soc_dapm_disable_pin(codec, pin);
1912 snd_soc_dapm_sync(codec);
1914 mutex_unlock(&codec->mutex);
1918 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1921 * snd_soc_dapm_new_control - create new dapm control
1922 * @codec: audio codec
1923 * @widget: widget template
1925 * Creates a new dapm control based upon the template.
1927 * Returns 0 for success else error.
1929 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1930 const struct snd_soc_dapm_widget *widget)
1932 struct snd_soc_dapm_widget *w;
1934 if ((w = dapm_cnew_widget(widget)) == NULL)
1938 INIT_LIST_HEAD(&w->sources);
1939 INIT_LIST_HEAD(&w->sinks);
1940 INIT_LIST_HEAD(&w->list);
1941 list_add(&w->list, &codec->dapm_widgets);
1943 /* machine layer set ups unconnected pins and insertions */
1947 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1950 * snd_soc_dapm_new_controls - create new dapm controls
1951 * @codec: audio codec
1952 * @widget: widget array
1953 * @num: number of widgets
1955 * Creates new DAPM controls based upon the templates.
1957 * Returns 0 for success else error.
1959 int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1960 const struct snd_soc_dapm_widget *widget,
1965 for (i = 0; i < num; i++) {
1966 ret = snd_soc_dapm_new_control(codec, widget);
1969 "ASoC: Failed to create DAPM control %s: %d\n",
1977 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1981 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1982 * @codec: audio codec
1983 * @stream: stream name
1984 * @event: stream event
1986 * Sends a stream event to the dapm core. The core then makes any
1987 * necessary widget power changes.
1989 * Returns 0 for success else error.
1991 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1992 char *stream, int event)
1994 struct snd_soc_dapm_widget *w;
1999 mutex_lock(&codec->mutex);
2000 list_for_each_entry(w, &codec->dapm_widgets, list)
2004 pr_debug("widget %s\n %s stream %s event %d\n",
2005 w->name, w->sname, stream, event);
2006 if (strstr(w->sname, stream)) {
2008 case SND_SOC_DAPM_STREAM_START:
2011 case SND_SOC_DAPM_STREAM_STOP:
2014 case SND_SOC_DAPM_STREAM_SUSPEND:
2015 case SND_SOC_DAPM_STREAM_RESUME:
2016 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2017 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2023 dapm_power_widgets(codec, event);
2024 mutex_unlock(&codec->mutex);
2027 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2030 * snd_soc_dapm_enable_pin - enable pin.
2034 * Enables input/output pin and its parents or children widgets iff there is
2035 * a valid audio route and active audio stream.
2036 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2037 * do any widget power switching.
2039 int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
2041 return snd_soc_dapm_set_pin(codec, pin, 1);
2043 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2046 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2050 * Enables input/output pin regardless of any other state. This is
2051 * intended for use with microphone bias supplies used in microphone
2054 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2055 * do any widget power switching.
2057 int snd_soc_dapm_force_enable_pin(struct snd_soc_codec *codec, const char *pin)
2059 struct snd_soc_dapm_widget *w;
2061 list_for_each_entry(w, &codec->dapm_widgets, list) {
2062 if (!strcmp(w->name, pin)) {
2063 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
2070 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
2073 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2076 * snd_soc_dapm_disable_pin - disable pin.
2080 * Disables input/output pin and its parents or children widgets.
2081 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2082 * do any widget power switching.
2084 int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
2086 return snd_soc_dapm_set_pin(codec, pin, 0);
2088 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2091 * snd_soc_dapm_nc_pin - permanently disable pin.
2095 * Marks the specified pin as being not connected, disabling it along
2096 * any parent or child widgets. At present this is identical to
2097 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2098 * additional things such as disabling controls which only affect
2099 * paths through the pin.
2101 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2102 * do any widget power switching.
2104 int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
2106 return snd_soc_dapm_set_pin(codec, pin, 0);
2108 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2111 * snd_soc_dapm_get_pin_status - get audio pin status
2112 * @codec: audio codec
2113 * @pin: audio signal pin endpoint (or start point)
2115 * Get audio pin status - connected or disconnected.
2117 * Returns 1 for connected otherwise 0.
2119 int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
2121 struct snd_soc_dapm_widget *w;
2123 list_for_each_entry(w, &codec->dapm_widgets, list) {
2124 if (!strcmp(w->name, pin))
2125 return w->connected;
2130 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2133 * snd_soc_dapm_free - free dapm resources
2134 * @socdev: SoC device
2136 * Free all dapm widgets and resources.
2138 void snd_soc_dapm_free(struct snd_soc_device *socdev)
2140 struct snd_soc_codec *codec = socdev->card->codec;
2142 snd_soc_dapm_sys_remove(socdev->dev);
2143 dapm_free_widgets(codec);
2145 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2148 * snd_soc_dapm_shutdown - callback for system shutdown
2150 void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2152 struct snd_soc_codec *codec = socdev->card->codec;
2153 struct snd_soc_dapm_widget *w;
2154 LIST_HEAD(down_list);
2157 list_for_each_entry(w, &codec->dapm_widgets, list) {
2159 dapm_seq_insert(w, &down_list, dapm_down_seq);
2165 /* If there were no widgets to power down we're already in
2169 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2170 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2171 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2174 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2177 /* Module information */
2178 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2179 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2180 MODULE_LICENSE("GPL");