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)
433 /* We implement power down on suspend by checking the power state of
434 * the ALSA card - when we are suspending the ALSA state for the card
437 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
439 struct snd_soc_codec *codec = widget->codec;
441 switch (snd_power_get_state(codec->card)) {
442 case SNDRV_CTL_POWER_D3hot:
443 case SNDRV_CTL_POWER_D3cold:
444 if (widget->ignore_suspend)
445 pr_debug("%s ignoring suspend\n", widget->name);
446 return widget->ignore_suspend;
453 * Recursively check for a completed path to an active or physically connected
454 * output widget. Returns number of complete paths.
456 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
458 struct snd_soc_dapm_path *path;
461 if (widget->id == snd_soc_dapm_supply)
464 switch (widget->id) {
465 case snd_soc_dapm_adc:
466 case snd_soc_dapm_aif_out:
468 return snd_soc_dapm_suspend_check(widget);
473 if (widget->connected) {
474 /* connected pin ? */
475 if (widget->id == snd_soc_dapm_output && !widget->ext)
476 return snd_soc_dapm_suspend_check(widget);
478 /* connected jack or spk ? */
479 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
480 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
481 return snd_soc_dapm_suspend_check(widget);
484 list_for_each_entry(path, &widget->sinks, list_source) {
488 if (path->sink && path->connect) {
490 con += is_connected_output_ep(path->sink);
498 * Recursively check for a completed path to an active or physically connected
499 * input widget. Returns number of complete paths.
501 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
503 struct snd_soc_dapm_path *path;
506 if (widget->id == snd_soc_dapm_supply)
509 /* active stream ? */
510 switch (widget->id) {
511 case snd_soc_dapm_dac:
512 case snd_soc_dapm_aif_in:
514 return snd_soc_dapm_suspend_check(widget);
519 if (widget->connected) {
520 /* connected pin ? */
521 if (widget->id == snd_soc_dapm_input && !widget->ext)
522 return snd_soc_dapm_suspend_check(widget);
524 /* connected VMID/Bias for lower pops */
525 if (widget->id == snd_soc_dapm_vmid)
526 return snd_soc_dapm_suspend_check(widget);
528 /* connected jack ? */
529 if (widget->id == snd_soc_dapm_mic ||
530 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
531 return snd_soc_dapm_suspend_check(widget);
534 list_for_each_entry(path, &widget->sources, list_sink) {
538 if (path->source && path->connect) {
540 con += is_connected_input_ep(path->source);
548 * Handler for generic register modifier widget.
550 int dapm_reg_event(struct snd_soc_dapm_widget *w,
551 struct snd_kcontrol *kcontrol, int event)
555 if (SND_SOC_DAPM_EVENT_ON(event))
560 snd_soc_update_bits(w->codec, -(w->reg + 1),
561 w->mask << w->shift, val << w->shift);
565 EXPORT_SYMBOL_GPL(dapm_reg_event);
567 /* Standard power change method, used to apply power changes to most
570 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
574 /* call any power change event handlers */
576 pr_debug("power %s event for %s flags %x\n",
577 w->power ? "on" : "off",
578 w->name, w->event_flags);
580 /* power up pre event */
581 if (w->power && w->event &&
582 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
583 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
588 /* power down pre event */
589 if (!w->power && w->event &&
590 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
591 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
598 /* power up post event */
599 if (w->power && w->event &&
600 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
602 NULL, SND_SOC_DAPM_POST_PMU);
607 /* power down post event */
608 if (!w->power && w->event &&
609 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
610 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
618 /* Generic check to see if a widget should be powered.
620 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
624 in = is_connected_input_ep(w);
625 dapm_clear_walk(w->codec);
626 out = is_connected_output_ep(w);
627 dapm_clear_walk(w->codec);
628 return out != 0 && in != 0;
631 /* Check to see if an ADC has power */
632 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
637 in = is_connected_input_ep(w);
638 dapm_clear_walk(w->codec);
641 return dapm_generic_check_power(w);
645 /* Check to see if a DAC has power */
646 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
651 out = is_connected_output_ep(w);
652 dapm_clear_walk(w->codec);
655 return dapm_generic_check_power(w);
659 /* Check to see if a power supply is needed */
660 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
662 struct snd_soc_dapm_path *path;
665 /* Check if one of our outputs is connected */
666 list_for_each_entry(path, &w->sinks, list_source) {
667 if (path->connected &&
668 !path->connected(path->source, path->sink))
671 if (path->sink && path->sink->power_check &&
672 path->sink->power_check(path->sink)) {
678 dapm_clear_walk(w->codec);
683 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
684 struct snd_soc_dapm_widget *b,
687 if (a->codec != b->codec)
688 return (unsigned long)a - (unsigned long)b;
689 if (sort[a->id] != sort[b->id])
690 return sort[a->id] - sort[b->id];
691 if (a->reg != b->reg)
692 return a->reg - b->reg;
697 /* Insert a widget in order into a DAPM power sequence. */
698 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
699 struct list_head *list,
702 struct snd_soc_dapm_widget *w;
704 list_for_each_entry(w, list, power_list)
705 if (dapm_seq_compare(new_widget, w, sort) < 0) {
706 list_add_tail(&new_widget->power_list, &w->power_list);
710 list_add_tail(&new_widget->power_list, list);
713 /* Apply the coalesced changes from a DAPM sequence */
714 static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
715 struct list_head *pending)
717 struct snd_soc_dapm_widget *w;
719 unsigned int value = 0;
720 unsigned int mask = 0;
721 unsigned int cur_mask;
723 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
726 list_for_each_entry(w, pending, power_list) {
727 cur_mask = 1 << w->shift;
728 BUG_ON(reg != w->reg);
739 pop_dbg(codec->pop_time,
740 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
741 w->name, reg, value, mask);
743 /* power up pre event */
744 if (w->power && w->event &&
745 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
746 pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
748 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
750 pr_err("%s: pre event failed: %d\n",
754 /* power down pre event */
755 if (!w->power && w->event &&
756 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
757 pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
759 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
761 pr_err("%s: pre event failed: %d\n",
767 pop_dbg(codec->pop_time,
768 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
769 value, mask, reg, codec->pop_time);
770 pop_wait(codec->pop_time);
771 snd_soc_update_bits(codec, reg, mask, value);
774 list_for_each_entry(w, pending, power_list) {
775 /* power up post event */
776 if (w->power && w->event &&
777 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
778 pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
781 NULL, SND_SOC_DAPM_POST_PMU);
783 pr_err("%s: post event failed: %d\n",
787 /* power down post event */
788 if (!w->power && w->event &&
789 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
790 pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
792 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
794 pr_err("%s: post event failed: %d\n",
800 /* Apply a DAPM power sequence.
802 * We walk over a pre-sorted list of widgets to apply power to. In
803 * order to minimise the number of writes to the device required
804 * multiple widgets will be updated in a single write where possible.
805 * Currently anything that requires more than a single write is not
808 static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
809 int event, int sort[])
811 struct snd_soc_dapm_widget *w, *n;
814 int cur_reg = SND_SOC_NOPM;
817 list_for_each_entry_safe(w, n, list, power_list) {
820 /* Do we need to apply any queued changes? */
821 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
822 if (!list_empty(&pending))
823 dapm_seq_run_coalesced(codec, &pending);
825 INIT_LIST_HEAD(&pending);
827 cur_reg = SND_SOC_NOPM;
831 case snd_soc_dapm_pre:
833 list_for_each_entry_safe_continue(w, n, list,
836 if (event == SND_SOC_DAPM_STREAM_START)
838 NULL, SND_SOC_DAPM_PRE_PMU);
839 else if (event == SND_SOC_DAPM_STREAM_STOP)
841 NULL, SND_SOC_DAPM_PRE_PMD);
844 case snd_soc_dapm_post:
846 list_for_each_entry_safe_continue(w, n, list,
849 if (event == SND_SOC_DAPM_STREAM_START)
851 NULL, SND_SOC_DAPM_POST_PMU);
852 else if (event == SND_SOC_DAPM_STREAM_STOP)
854 NULL, SND_SOC_DAPM_POST_PMD);
857 case snd_soc_dapm_input:
858 case snd_soc_dapm_output:
859 case snd_soc_dapm_hp:
860 case snd_soc_dapm_mic:
861 case snd_soc_dapm_line:
862 case snd_soc_dapm_spk:
863 /* No register support currently */
864 ret = dapm_generic_apply_power(w);
868 /* Queue it up for application */
869 cur_sort = sort[w->id];
871 list_move(&w->power_list, &pending);
876 pr_err("Failed to apply widget power: %d\n",
880 if (!list_empty(&pending))
881 dapm_seq_run_coalesced(codec, &pending);
885 * Scan each dapm widget for complete audio path.
886 * A complete path is a route that has valid endpoints i.e.:-
888 * o DAC to output pin.
889 * o Input Pin to ADC.
890 * o Input pin to Output pin (bypass, sidetone)
891 * o DAC to ADC (loopback).
893 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
895 struct snd_soc_device *socdev = codec->socdev;
896 struct snd_soc_dapm_widget *w;
898 LIST_HEAD(down_list);
903 /* Check which widgets we need to power and store them in
904 * lists indicating if they should be powered up or down.
906 list_for_each_entry(w, &codec->dapm_widgets, list) {
908 case snd_soc_dapm_pre:
909 dapm_seq_insert(w, &down_list, dapm_down_seq);
911 case snd_soc_dapm_post:
912 dapm_seq_insert(w, &up_list, dapm_up_seq);
920 power = w->power_check(w);
926 if (w->power == power)
930 dapm_seq_insert(w, &up_list, dapm_up_seq);
932 dapm_seq_insert(w, &down_list, dapm_down_seq);
939 /* If there are no DAPM widgets then try to figure out power from the
942 if (list_empty(&codec->dapm_widgets)) {
944 case SND_SOC_DAPM_STREAM_START:
945 case SND_SOC_DAPM_STREAM_RESUME:
948 case SND_SOC_DAPM_STREAM_SUSPEND:
951 case SND_SOC_DAPM_STREAM_NOP:
952 switch (codec->bias_level) {
953 case SND_SOC_BIAS_STANDBY:
954 case SND_SOC_BIAS_OFF:
967 if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
968 ret = snd_soc_dapm_set_bias_level(socdev,
969 SND_SOC_BIAS_STANDBY);
971 pr_err("Failed to turn on bias: %d\n", ret);
974 /* If we're changing to all on or all off then prepare */
975 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
976 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
977 ret = snd_soc_dapm_set_bias_level(socdev,
978 SND_SOC_BIAS_PREPARE);
980 pr_err("Failed to prepare bias: %d\n", ret);
983 /* Power down widgets first; try to avoid amplifying pops. */
984 dapm_seq_run(codec, &down_list, event, dapm_down_seq);
987 dapm_seq_run(codec, &up_list, event, dapm_up_seq);
989 /* If we just powered the last thing off drop to standby bias */
990 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
991 ret = snd_soc_dapm_set_bias_level(socdev,
992 SND_SOC_BIAS_STANDBY);
994 pr_err("Failed to apply standby bias: %d\n", ret);
997 /* If we're in standby and can support bias off then do that */
998 if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
999 codec->idle_bias_off) {
1000 ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
1002 pr_err("Failed to turn off bias: %d\n", ret);
1005 /* If we just powered up then move to active bias */
1006 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1007 ret = snd_soc_dapm_set_bias_level(socdev,
1010 pr_err("Failed to apply active bias: %d\n", ret);
1013 pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
1015 pop_wait(codec->pop_time);
1020 #ifdef CONFIG_DEBUG_FS
1021 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1023 file->private_data = inode->i_private;
1027 static ssize_t dapm_widget_power_read_file(struct file *file,
1028 char __user *user_buf,
1029 size_t count, loff_t *ppos)
1031 struct snd_soc_dapm_widget *w = file->private_data;
1035 struct snd_soc_dapm_path *p = NULL;
1037 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1041 in = is_connected_input_ep(w);
1042 dapm_clear_walk(w->codec);
1043 out = is_connected_output_ep(w);
1044 dapm_clear_walk(w->codec);
1046 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1047 w->name, w->power ? "On" : "Off", in, out);
1050 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1051 " - R%d(0x%x) bit %d",
1052 w->reg, w->reg, w->shift);
1054 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1057 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1059 w->active ? "active" : "inactive");
1061 list_for_each_entry(p, &w->sources, list_sink) {
1062 if (p->connected && !p->connected(w, p->sink))
1066 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1068 p->name ? p->name : "static",
1071 list_for_each_entry(p, &w->sinks, list_source) {
1072 if (p->connected && !p->connected(w, p->sink))
1076 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1078 p->name ? p->name : "static",
1082 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1088 static const struct file_operations dapm_widget_power_fops = {
1089 .open = dapm_widget_power_open_file,
1090 .read = dapm_widget_power_read_file,
1093 void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1095 struct snd_soc_dapm_widget *w;
1098 if (!codec->debugfs_dapm)
1101 list_for_each_entry(w, &codec->dapm_widgets, list) {
1105 d = debugfs_create_file(w->name, 0444,
1106 codec->debugfs_dapm, w,
1107 &dapm_widget_power_fops);
1110 "ASoC: Failed to create %s debugfs file\n",
1115 void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
1120 /* test and update the power status of a mux widget */
1121 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1122 struct snd_kcontrol *kcontrol, int change,
1123 int mux, struct soc_enum *e)
1125 struct snd_soc_dapm_path *path;
1128 if (widget->id != snd_soc_dapm_mux &&
1129 widget->id != snd_soc_dapm_value_mux)
1135 /* find dapm widget path assoc with kcontrol */
1136 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1137 if (path->kcontrol != kcontrol)
1140 if (!path->name || !e->texts[mux])
1144 /* we now need to match the string in the enum to the path */
1145 if (!(strcmp(path->name, e->texts[mux])))
1146 path->connect = 1; /* new connection */
1148 path->connect = 0; /* old connection must be powered down */
1152 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1157 /* test and update the power status of a mixer or switch widget */
1158 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1159 struct snd_kcontrol *kcontrol, int connect)
1161 struct snd_soc_dapm_path *path;
1164 if (widget->id != snd_soc_dapm_mixer &&
1165 widget->id != snd_soc_dapm_mixer_named_ctl &&
1166 widget->id != snd_soc_dapm_switch)
1169 /* find dapm widget path assoc with kcontrol */
1170 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1171 if (path->kcontrol != kcontrol)
1174 /* found, now check type */
1176 path->connect = connect;
1181 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
1186 /* show dapm widget status in sys fs */
1187 static ssize_t dapm_widget_show(struct device *dev,
1188 struct device_attribute *attr, char *buf)
1190 struct snd_soc_device *devdata = dev_get_drvdata(dev);
1191 struct snd_soc_codec *codec = devdata->card->codec;
1192 struct snd_soc_dapm_widget *w;
1194 char *state = "not set";
1196 list_for_each_entry(w, &codec->dapm_widgets, list) {
1198 /* only display widgets that burnm power */
1200 case snd_soc_dapm_hp:
1201 case snd_soc_dapm_mic:
1202 case snd_soc_dapm_spk:
1203 case snd_soc_dapm_line:
1204 case snd_soc_dapm_micbias:
1205 case snd_soc_dapm_dac:
1206 case snd_soc_dapm_adc:
1207 case snd_soc_dapm_pga:
1208 case snd_soc_dapm_mixer:
1209 case snd_soc_dapm_mixer_named_ctl:
1210 case snd_soc_dapm_supply:
1212 count += sprintf(buf + count, "%s: %s\n",
1213 w->name, w->power ? "On":"Off");
1220 switch (codec->bias_level) {
1221 case SND_SOC_BIAS_ON:
1224 case SND_SOC_BIAS_PREPARE:
1227 case SND_SOC_BIAS_STANDBY:
1230 case SND_SOC_BIAS_OFF:
1234 count += sprintf(buf + count, "PM State: %s\n", state);
1239 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1241 int snd_soc_dapm_sys_add(struct device *dev)
1243 return device_create_file(dev, &dev_attr_dapm_widget);
1246 static void snd_soc_dapm_sys_remove(struct device *dev)
1248 device_remove_file(dev, &dev_attr_dapm_widget);
1251 /* free all dapm widgets and resources */
1252 static void dapm_free_widgets(struct snd_soc_codec *codec)
1254 struct snd_soc_dapm_widget *w, *next_w;
1255 struct snd_soc_dapm_path *p, *next_p;
1257 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1262 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1264 kfree(p->long_name);
1269 static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
1270 const char *pin, int status)
1272 struct snd_soc_dapm_widget *w;
1274 list_for_each_entry(w, &codec->dapm_widgets, list) {
1275 if (!strcmp(w->name, pin)) {
1276 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
1277 w->connected = status;
1278 /* Allow disabling of forced pins */
1285 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
1290 * snd_soc_dapm_sync - scan and power dapm paths
1291 * @codec: audio codec
1293 * Walks all dapm audio paths and powers widgets according to their
1294 * stream or path usage.
1296 * Returns 0 for success.
1298 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
1300 return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1302 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1304 static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1305 const struct snd_soc_dapm_route *route)
1307 struct snd_soc_dapm_path *path;
1308 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1309 const char *sink = route->sink;
1310 const char *control = route->control;
1311 const char *source = route->source;
1314 /* find src and dest widgets */
1315 list_for_each_entry(w, &codec->dapm_widgets, list) {
1317 if (!wsink && !(strcmp(w->name, sink))) {
1321 if (!wsource && !(strcmp(w->name, source))) {
1326 if (wsource == NULL || wsink == NULL)
1329 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1333 path->source = wsource;
1335 path->connected = route->connected;
1336 INIT_LIST_HEAD(&path->list);
1337 INIT_LIST_HEAD(&path->list_source);
1338 INIT_LIST_HEAD(&path->list_sink);
1340 /* check for external widgets */
1341 if (wsink->id == snd_soc_dapm_input) {
1342 if (wsource->id == snd_soc_dapm_micbias ||
1343 wsource->id == snd_soc_dapm_mic ||
1344 wsource->id == snd_soc_dapm_line ||
1345 wsource->id == snd_soc_dapm_output)
1348 if (wsource->id == snd_soc_dapm_output) {
1349 if (wsink->id == snd_soc_dapm_spk ||
1350 wsink->id == snd_soc_dapm_hp ||
1351 wsink->id == snd_soc_dapm_line ||
1352 wsink->id == snd_soc_dapm_input)
1356 /* connect static paths */
1357 if (control == NULL) {
1358 list_add(&path->list, &codec->dapm_paths);
1359 list_add(&path->list_sink, &wsink->sources);
1360 list_add(&path->list_source, &wsource->sinks);
1365 /* connect dynamic paths */
1367 case snd_soc_dapm_adc:
1368 case snd_soc_dapm_dac:
1369 case snd_soc_dapm_pga:
1370 case snd_soc_dapm_input:
1371 case snd_soc_dapm_output:
1372 case snd_soc_dapm_micbias:
1373 case snd_soc_dapm_vmid:
1374 case snd_soc_dapm_pre:
1375 case snd_soc_dapm_post:
1376 case snd_soc_dapm_supply:
1377 case snd_soc_dapm_aif_in:
1378 case snd_soc_dapm_aif_out:
1379 list_add(&path->list, &codec->dapm_paths);
1380 list_add(&path->list_sink, &wsink->sources);
1381 list_add(&path->list_source, &wsource->sinks);
1384 case snd_soc_dapm_mux:
1385 case snd_soc_dapm_value_mux:
1386 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1387 &wsink->kcontrols[0]);
1391 case snd_soc_dapm_switch:
1392 case snd_soc_dapm_mixer:
1393 case snd_soc_dapm_mixer_named_ctl:
1394 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1398 case snd_soc_dapm_hp:
1399 case snd_soc_dapm_mic:
1400 case snd_soc_dapm_line:
1401 case snd_soc_dapm_spk:
1402 list_add(&path->list, &codec->dapm_paths);
1403 list_add(&path->list_sink, &wsink->sources);
1404 list_add(&path->list_source, &wsource->sinks);
1411 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1418 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1420 * @route: audio routes
1421 * @num: number of routes
1423 * Connects 2 dapm widgets together via a named audio path. The sink is
1424 * the widget receiving the audio signal, whilst the source is the sender
1425 * of the audio signal.
1427 * Returns 0 for success else error. On error all resources can be freed
1428 * with a call to snd_soc_card_free().
1430 int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1431 const struct snd_soc_dapm_route *route, int num)
1435 for (i = 0; i < num; i++) {
1436 ret = snd_soc_dapm_add_route(codec, route);
1438 printk(KERN_ERR "Failed to add route %s->%s\n",
1448 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1451 * snd_soc_dapm_new_widgets - add new dapm widgets
1452 * @codec: audio codec
1454 * Checks the codec for any new dapm widgets and creates them if found.
1456 * Returns 0 for success.
1458 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1460 struct snd_soc_dapm_widget *w;
1462 list_for_each_entry(w, &codec->dapm_widgets, list)
1468 case snd_soc_dapm_switch:
1469 case snd_soc_dapm_mixer:
1470 case snd_soc_dapm_mixer_named_ctl:
1471 w->power_check = dapm_generic_check_power;
1472 dapm_new_mixer(codec, w);
1474 case snd_soc_dapm_mux:
1475 case snd_soc_dapm_value_mux:
1476 w->power_check = dapm_generic_check_power;
1477 dapm_new_mux(codec, w);
1479 case snd_soc_dapm_adc:
1480 case snd_soc_dapm_aif_out:
1481 w->power_check = dapm_adc_check_power;
1483 case snd_soc_dapm_dac:
1484 case snd_soc_dapm_aif_in:
1485 w->power_check = dapm_dac_check_power;
1487 case snd_soc_dapm_pga:
1488 w->power_check = dapm_generic_check_power;
1489 dapm_new_pga(codec, w);
1491 case snd_soc_dapm_input:
1492 case snd_soc_dapm_output:
1493 case snd_soc_dapm_micbias:
1494 case snd_soc_dapm_spk:
1495 case snd_soc_dapm_hp:
1496 case snd_soc_dapm_mic:
1497 case snd_soc_dapm_line:
1498 w->power_check = dapm_generic_check_power;
1500 case snd_soc_dapm_supply:
1501 w->power_check = dapm_supply_check_power;
1502 case snd_soc_dapm_vmid:
1503 case snd_soc_dapm_pre:
1504 case snd_soc_dapm_post:
1510 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1513 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1516 * snd_soc_dapm_get_volsw - dapm mixer get callback
1517 * @kcontrol: mixer control
1518 * @ucontrol: control element information
1520 * Callback to get the value of a dapm mixer control.
1522 * Returns 0 for success.
1524 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1525 struct snd_ctl_elem_value *ucontrol)
1527 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1528 struct soc_mixer_control *mc =
1529 (struct soc_mixer_control *)kcontrol->private_value;
1530 unsigned int reg = mc->reg;
1531 unsigned int shift = mc->shift;
1532 unsigned int rshift = mc->rshift;
1534 unsigned int invert = mc->invert;
1535 unsigned int mask = (1 << fls(max)) - 1;
1537 ucontrol->value.integer.value[0] =
1538 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1539 if (shift != rshift)
1540 ucontrol->value.integer.value[1] =
1541 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1543 ucontrol->value.integer.value[0] =
1544 max - ucontrol->value.integer.value[0];
1545 if (shift != rshift)
1546 ucontrol->value.integer.value[1] =
1547 max - ucontrol->value.integer.value[1];
1552 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1555 * snd_soc_dapm_put_volsw - dapm mixer set callback
1556 * @kcontrol: mixer control
1557 * @ucontrol: control element information
1559 * Callback to set the value of a dapm mixer control.
1561 * Returns 0 for success.
1563 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1564 struct snd_ctl_elem_value *ucontrol)
1566 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1567 struct soc_mixer_control *mc =
1568 (struct soc_mixer_control *)kcontrol->private_value;
1569 unsigned int reg = mc->reg;
1570 unsigned int shift = mc->shift;
1571 unsigned int rshift = mc->rshift;
1573 unsigned int mask = (1 << fls(max)) - 1;
1574 unsigned int invert = mc->invert;
1575 unsigned int val, val2, val_mask;
1579 val = (ucontrol->value.integer.value[0] & mask);
1583 val_mask = mask << shift;
1585 if (shift != rshift) {
1586 val2 = (ucontrol->value.integer.value[1] & mask);
1589 val_mask |= mask << rshift;
1590 val |= val2 << rshift;
1593 mutex_lock(&widget->codec->mutex);
1594 widget->value = val;
1596 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1598 /* new connection */
1599 connect = invert ? 0:1;
1601 /* old connection must be powered down */
1602 connect = invert ? 1:0;
1604 dapm_mixer_update_power(widget, kcontrol, connect);
1607 if (widget->event) {
1608 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1609 ret = widget->event(widget, kcontrol,
1610 SND_SOC_DAPM_PRE_REG);
1616 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1617 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1618 ret = widget->event(widget, kcontrol,
1619 SND_SOC_DAPM_POST_REG);
1621 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1624 mutex_unlock(&widget->codec->mutex);
1627 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1630 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1631 * @kcontrol: mixer control
1632 * @ucontrol: control element information
1634 * Callback to get the value of a dapm enumerated double mixer control.
1636 * Returns 0 for success.
1638 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1639 struct snd_ctl_elem_value *ucontrol)
1641 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1642 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1643 unsigned int val, bitmask;
1645 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1647 val = snd_soc_read(widget->codec, e->reg);
1648 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1649 if (e->shift_l != e->shift_r)
1650 ucontrol->value.enumerated.item[1] =
1651 (val >> e->shift_r) & (bitmask - 1);
1655 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1658 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1659 * @kcontrol: mixer control
1660 * @ucontrol: control element information
1662 * Callback to set the value of a dapm enumerated double mixer control.
1664 * Returns 0 for success.
1666 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1667 struct snd_ctl_elem_value *ucontrol)
1669 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1670 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1671 unsigned int val, mux, change;
1672 unsigned int mask, bitmask;
1675 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1677 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1679 mux = ucontrol->value.enumerated.item[0];
1680 val = mux << e->shift_l;
1681 mask = (bitmask - 1) << e->shift_l;
1682 if (e->shift_l != e->shift_r) {
1683 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1685 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1686 mask |= (bitmask - 1) << e->shift_r;
1689 mutex_lock(&widget->codec->mutex);
1690 widget->value = val;
1691 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1692 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1694 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1695 ret = widget->event(widget,
1696 kcontrol, SND_SOC_DAPM_PRE_REG);
1701 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1703 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1704 ret = widget->event(widget,
1705 kcontrol, SND_SOC_DAPM_POST_REG);
1708 mutex_unlock(&widget->codec->mutex);
1711 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1714 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1715 * @kcontrol: mixer control
1716 * @ucontrol: control element information
1718 * Returns 0 for success.
1720 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1721 struct snd_ctl_elem_value *ucontrol)
1723 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1725 ucontrol->value.enumerated.item[0] = widget->value;
1729 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1732 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1733 * @kcontrol: mixer control
1734 * @ucontrol: control element information
1736 * Returns 0 for success.
1738 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1739 struct snd_ctl_elem_value *ucontrol)
1741 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1742 struct soc_enum *e =
1743 (struct soc_enum *)kcontrol->private_value;
1747 if (ucontrol->value.enumerated.item[0] >= e->max)
1750 mutex_lock(&widget->codec->mutex);
1752 change = widget->value != ucontrol->value.enumerated.item[0];
1753 widget->value = ucontrol->value.enumerated.item[0];
1754 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1756 mutex_unlock(&widget->codec->mutex);
1759 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1762 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1764 * @kcontrol: mixer control
1765 * @ucontrol: control element information
1767 * Callback to get the value of a dapm semi enumerated double mixer control.
1769 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1770 * used for handling bitfield coded enumeration for example.
1772 * Returns 0 for success.
1774 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1775 struct snd_ctl_elem_value *ucontrol)
1777 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1778 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1779 unsigned int reg_val, val, mux;
1781 reg_val = snd_soc_read(widget->codec, e->reg);
1782 val = (reg_val >> e->shift_l) & e->mask;
1783 for (mux = 0; mux < e->max; mux++) {
1784 if (val == e->values[mux])
1787 ucontrol->value.enumerated.item[0] = mux;
1788 if (e->shift_l != e->shift_r) {
1789 val = (reg_val >> e->shift_r) & e->mask;
1790 for (mux = 0; mux < e->max; mux++) {
1791 if (val == e->values[mux])
1794 ucontrol->value.enumerated.item[1] = mux;
1799 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1802 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1804 * @kcontrol: mixer control
1805 * @ucontrol: control element information
1807 * Callback to set the value of a dapm semi enumerated double mixer control.
1809 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1810 * used for handling bitfield coded enumeration for example.
1812 * Returns 0 for success.
1814 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1815 struct snd_ctl_elem_value *ucontrol)
1817 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1818 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1819 unsigned int val, mux, change;
1823 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1825 mux = ucontrol->value.enumerated.item[0];
1826 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1827 mask = e->mask << e->shift_l;
1828 if (e->shift_l != e->shift_r) {
1829 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1831 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1832 mask |= e->mask << e->shift_r;
1835 mutex_lock(&widget->codec->mutex);
1836 widget->value = val;
1837 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1838 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1840 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1841 ret = widget->event(widget,
1842 kcontrol, SND_SOC_DAPM_PRE_REG);
1847 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1849 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1850 ret = widget->event(widget,
1851 kcontrol, SND_SOC_DAPM_POST_REG);
1854 mutex_unlock(&widget->codec->mutex);
1857 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1860 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1862 * @kcontrol: mixer control
1863 * @uinfo: control element information
1865 * Callback to provide information about a pin switch control.
1867 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1868 struct snd_ctl_elem_info *uinfo)
1870 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1872 uinfo->value.integer.min = 0;
1873 uinfo->value.integer.max = 1;
1877 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1880 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1882 * @kcontrol: mixer control
1885 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1886 struct snd_ctl_elem_value *ucontrol)
1888 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1889 const char *pin = (const char *)kcontrol->private_value;
1891 mutex_lock(&codec->mutex);
1893 ucontrol->value.integer.value[0] =
1894 snd_soc_dapm_get_pin_status(codec, pin);
1896 mutex_unlock(&codec->mutex);
1900 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1903 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1905 * @kcontrol: mixer control
1908 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1909 struct snd_ctl_elem_value *ucontrol)
1911 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1912 const char *pin = (const char *)kcontrol->private_value;
1914 mutex_lock(&codec->mutex);
1916 if (ucontrol->value.integer.value[0])
1917 snd_soc_dapm_enable_pin(codec, pin);
1919 snd_soc_dapm_disable_pin(codec, pin);
1921 snd_soc_dapm_sync(codec);
1923 mutex_unlock(&codec->mutex);
1927 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1930 * snd_soc_dapm_new_control - create new dapm control
1931 * @codec: audio codec
1932 * @widget: widget template
1934 * Creates a new dapm control based upon the template.
1936 * Returns 0 for success else error.
1938 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1939 const struct snd_soc_dapm_widget *widget)
1941 struct snd_soc_dapm_widget *w;
1943 if ((w = dapm_cnew_widget(widget)) == NULL)
1947 INIT_LIST_HEAD(&w->sources);
1948 INIT_LIST_HEAD(&w->sinks);
1949 INIT_LIST_HEAD(&w->list);
1950 list_add(&w->list, &codec->dapm_widgets);
1952 /* machine layer set ups unconnected pins and insertions */
1956 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1959 * snd_soc_dapm_new_controls - create new dapm controls
1960 * @codec: audio codec
1961 * @widget: widget array
1962 * @num: number of widgets
1964 * Creates new DAPM controls based upon the templates.
1966 * Returns 0 for success else error.
1968 int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1969 const struct snd_soc_dapm_widget *widget,
1974 for (i = 0; i < num; i++) {
1975 ret = snd_soc_dapm_new_control(codec, widget);
1978 "ASoC: Failed to create DAPM control %s: %d\n",
1986 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1990 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1991 * @codec: audio codec
1992 * @stream: stream name
1993 * @event: stream event
1995 * Sends a stream event to the dapm core. The core then makes any
1996 * necessary widget power changes.
1998 * Returns 0 for success else error.
2000 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
2001 char *stream, int event)
2003 struct snd_soc_dapm_widget *w;
2008 mutex_lock(&codec->mutex);
2009 list_for_each_entry(w, &codec->dapm_widgets, list)
2013 pr_debug("widget %s\n %s stream %s event %d\n",
2014 w->name, w->sname, stream, event);
2015 if (strstr(w->sname, stream)) {
2017 case SND_SOC_DAPM_STREAM_START:
2020 case SND_SOC_DAPM_STREAM_STOP:
2023 case SND_SOC_DAPM_STREAM_SUSPEND:
2024 case SND_SOC_DAPM_STREAM_RESUME:
2025 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2026 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2032 dapm_power_widgets(codec, event);
2033 mutex_unlock(&codec->mutex);
2036 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2039 * snd_soc_dapm_enable_pin - enable pin.
2043 * Enables input/output pin and its parents or children widgets iff there is
2044 * a valid audio route and active audio stream.
2045 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2046 * do any widget power switching.
2048 int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
2050 return snd_soc_dapm_set_pin(codec, pin, 1);
2052 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2055 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2059 * Enables input/output pin regardless of any other state. This is
2060 * intended for use with microphone bias supplies used in microphone
2063 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2064 * do any widget power switching.
2066 int snd_soc_dapm_force_enable_pin(struct snd_soc_codec *codec, const char *pin)
2068 struct snd_soc_dapm_widget *w;
2070 list_for_each_entry(w, &codec->dapm_widgets, list) {
2071 if (!strcmp(w->name, pin)) {
2072 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
2079 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
2082 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2085 * snd_soc_dapm_disable_pin - disable pin.
2089 * Disables input/output pin and its parents or children widgets.
2090 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2091 * do any widget power switching.
2093 int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
2095 return snd_soc_dapm_set_pin(codec, pin, 0);
2097 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2100 * snd_soc_dapm_nc_pin - permanently disable pin.
2104 * Marks the specified pin as being not connected, disabling it along
2105 * any parent or child widgets. At present this is identical to
2106 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2107 * additional things such as disabling controls which only affect
2108 * paths through the pin.
2110 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2111 * do any widget power switching.
2113 int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
2115 return snd_soc_dapm_set_pin(codec, pin, 0);
2117 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2120 * snd_soc_dapm_get_pin_status - get audio pin status
2121 * @codec: audio codec
2122 * @pin: audio signal pin endpoint (or start point)
2124 * Get audio pin status - connected or disconnected.
2126 * Returns 1 for connected otherwise 0.
2128 int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
2130 struct snd_soc_dapm_widget *w;
2132 list_for_each_entry(w, &codec->dapm_widgets, list) {
2133 if (!strcmp(w->name, pin))
2134 return w->connected;
2139 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2142 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2143 * @codec: audio codec
2144 * @pin: audio signal pin endpoint (or start point)
2146 * Mark the given endpoint or pin as ignoring suspend. When the
2147 * system is disabled a path between two endpoints flagged as ignoring
2148 * suspend will not be disabled. The path must already be enabled via
2149 * normal means at suspend time, it will not be turned on if it was not
2152 int snd_soc_dapm_ignore_suspend(struct snd_soc_codec *codec, const char *pin)
2154 struct snd_soc_dapm_widget *w;
2156 list_for_each_entry(w, &codec->dapm_widgets, list) {
2157 if (!strcmp(w->name, pin)) {
2158 w->ignore_suspend = 1;
2163 pr_err("Unknown DAPM pin: %s\n", pin);
2166 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2169 * snd_soc_dapm_free - free dapm resources
2170 * @socdev: SoC device
2172 * Free all dapm widgets and resources.
2174 void snd_soc_dapm_free(struct snd_soc_device *socdev)
2176 struct snd_soc_codec *codec = socdev->card->codec;
2178 snd_soc_dapm_sys_remove(socdev->dev);
2179 dapm_free_widgets(codec);
2181 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2184 * snd_soc_dapm_shutdown - callback for system shutdown
2186 void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
2188 struct snd_soc_codec *codec = socdev->card->codec;
2189 struct snd_soc_dapm_widget *w;
2190 LIST_HEAD(down_list);
2193 list_for_each_entry(w, &codec->dapm_widgets, list) {
2195 dapm_seq_insert(w, &down_list, dapm_down_seq);
2201 /* If there were no widgets to power down we're already in
2205 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
2206 dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
2207 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
2210 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
2213 /* Module information */
2214 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2215 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2216 MODULE_LICENSE("GPL");