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 <linux/slab.h>
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc.h>
46 #include <sound/soc-dapm.h>
47 #include <sound/initval.h>
49 #include <trace/events/asoc.h>
51 /* dapm power sequences - make this per codec in the future */
52 static int dapm_up_seq[] = {
53 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
56 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
60 [snd_soc_dapm_value_mux] = 5,
61 [snd_soc_dapm_dac] = 6,
62 [snd_soc_dapm_mixer] = 7,
63 [snd_soc_dapm_mixer_named_ctl] = 7,
64 [snd_soc_dapm_pga] = 8,
65 [snd_soc_dapm_adc] = 9,
66 [snd_soc_dapm_hp] = 10,
67 [snd_soc_dapm_spk] = 10,
68 [snd_soc_dapm_post] = 11,
71 static int dapm_down_seq[] = {
72 [snd_soc_dapm_pre] = 0,
73 [snd_soc_dapm_adc] = 1,
74 [snd_soc_dapm_hp] = 2,
75 [snd_soc_dapm_spk] = 2,
76 [snd_soc_dapm_pga] = 4,
77 [snd_soc_dapm_mixer_named_ctl] = 5,
78 [snd_soc_dapm_mixer] = 5,
79 [snd_soc_dapm_dac] = 6,
80 [snd_soc_dapm_mic] = 7,
81 [snd_soc_dapm_micbias] = 8,
82 [snd_soc_dapm_mux] = 9,
83 [snd_soc_dapm_value_mux] = 9,
84 [snd_soc_dapm_aif_in] = 10,
85 [snd_soc_dapm_aif_out] = 10,
86 [snd_soc_dapm_supply] = 11,
87 [snd_soc_dapm_post] = 12,
90 static void pop_wait(u32 pop_time)
93 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
96 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
104 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
109 vsnprintf(buf, PAGE_SIZE, fmt, args);
116 /* create a new dapm widget */
117 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
118 const struct snd_soc_dapm_widget *_widget)
120 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
124 * snd_soc_dapm_set_bias_level - set the bias level for the system
125 * @card: audio device
126 * @level: level to configure
128 * Configure the bias (power) levels for the SoC audio device.
130 * Returns 0 for success else error.
132 static int snd_soc_dapm_set_bias_level(struct snd_soc_card *card,
133 struct snd_soc_dapm_context *dapm,
134 enum snd_soc_bias_level level)
139 case SND_SOC_BIAS_ON:
140 dev_dbg(dapm->dev, "Setting full bias\n");
142 case SND_SOC_BIAS_PREPARE:
143 dev_dbg(dapm->dev, "Setting bias prepare\n");
145 case SND_SOC_BIAS_STANDBY:
146 dev_dbg(dapm->dev, "Setting standby bias\n");
148 case SND_SOC_BIAS_OFF:
149 dev_dbg(dapm->dev, "Setting bias off\n");
152 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
156 trace_snd_soc_bias_level_start(card, level);
158 if (card && card->set_bias_level)
159 ret = card->set_bias_level(card, level);
161 if (dapm->codec && dapm->codec->driver->set_bias_level)
162 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
164 dapm->bias_level = level;
167 trace_snd_soc_bias_level_done(card, level);
172 /* set up initial codec paths */
173 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
174 struct snd_soc_dapm_path *p, int i)
177 case snd_soc_dapm_switch:
178 case snd_soc_dapm_mixer:
179 case snd_soc_dapm_mixer_named_ctl: {
181 struct soc_mixer_control *mc = (struct soc_mixer_control *)
182 w->kcontrols[i].private_value;
183 unsigned int reg = mc->reg;
184 unsigned int shift = mc->shift;
186 unsigned int mask = (1 << fls(max)) - 1;
187 unsigned int invert = mc->invert;
189 val = snd_soc_read(w->codec, reg);
190 val = (val >> shift) & mask;
192 if ((invert && !val) || (!invert && val))
198 case snd_soc_dapm_mux: {
199 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
200 int val, item, bitmask;
202 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
204 val = snd_soc_read(w->codec, e->reg);
205 item = (val >> e->shift_l) & (bitmask - 1);
208 for (i = 0; i < e->max; i++) {
209 if (!(strcmp(p->name, e->texts[i])) && item == i)
214 case snd_soc_dapm_value_mux: {
215 struct soc_enum *e = (struct soc_enum *)
216 w->kcontrols[i].private_value;
219 val = snd_soc_read(w->codec, e->reg);
220 val = (val >> e->shift_l) & e->mask;
221 for (item = 0; item < e->max; item++) {
222 if (val == e->values[item])
227 for (i = 0; i < e->max; i++) {
228 if (!(strcmp(p->name, e->texts[i])) && item == i)
233 /* does not effect routing - always connected */
234 case snd_soc_dapm_pga:
235 case snd_soc_dapm_output:
236 case snd_soc_dapm_adc:
237 case snd_soc_dapm_input:
238 case snd_soc_dapm_dac:
239 case snd_soc_dapm_micbias:
240 case snd_soc_dapm_vmid:
241 case snd_soc_dapm_supply:
242 case snd_soc_dapm_aif_in:
243 case snd_soc_dapm_aif_out:
246 /* does effect routing - dynamically connected */
247 case snd_soc_dapm_hp:
248 case snd_soc_dapm_mic:
249 case snd_soc_dapm_spk:
250 case snd_soc_dapm_line:
251 case snd_soc_dapm_pre:
252 case snd_soc_dapm_post:
258 /* connect mux widget to its interconnecting audio paths */
259 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
260 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
261 struct snd_soc_dapm_path *path, const char *control_name,
262 const struct snd_kcontrol_new *kcontrol)
264 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
267 for (i = 0; i < e->max; i++) {
268 if (!(strcmp(control_name, e->texts[i]))) {
269 list_add(&path->list, &dapm->paths);
270 list_add(&path->list_sink, &dest->sources);
271 list_add(&path->list_source, &src->sinks);
272 path->name = (char*)e->texts[i];
273 dapm_set_path_status(dest, path, 0);
281 /* connect mixer widget to its interconnecting audio paths */
282 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
283 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
284 struct snd_soc_dapm_path *path, const char *control_name)
288 /* search for mixer kcontrol */
289 for (i = 0; i < dest->num_kcontrols; i++) {
290 if (!strcmp(control_name, dest->kcontrols[i].name)) {
291 list_add(&path->list, &dapm->paths);
292 list_add(&path->list_sink, &dest->sources);
293 list_add(&path->list_source, &src->sinks);
294 path->name = dest->kcontrols[i].name;
295 dapm_set_path_status(dest, path, i);
302 /* update dapm codec register bits */
303 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
306 unsigned int old, new;
307 struct snd_soc_codec *codec = widget->codec;
308 struct snd_soc_dapm_context *dapm = widget->dapm;
309 struct snd_soc_card *card = dapm->card;
311 /* check for valid widgets */
312 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
313 widget->id == snd_soc_dapm_output ||
314 widget->id == snd_soc_dapm_hp ||
315 widget->id == snd_soc_dapm_mic ||
316 widget->id == snd_soc_dapm_line ||
317 widget->id == snd_soc_dapm_spk)
320 power = widget->power;
322 power = (power ? 0:1);
324 old = snd_soc_read(codec, widget->reg);
325 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
329 pop_dbg(dapm->dev, card->pop_time,
330 "pop test %s : %s in %d ms\n",
331 widget->name, widget->power ? "on" : "off",
333 pop_wait(card->pop_time);
334 snd_soc_write(codec, widget->reg, new);
336 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
341 /* create new dapm mixer control */
342 static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
343 struct snd_soc_dapm_widget *w)
347 struct snd_soc_dapm_path *path;
348 struct snd_card *card = dapm->codec->card->snd_card;
351 for (i = 0; i < w->num_kcontrols; i++) {
354 list_for_each_entry(path, &w->sources, list_sink) {
356 /* mixer/mux paths name must match control name */
357 if (path->name != (char*)w->kcontrols[i].name)
360 /* add dapm control with long name.
361 * for dapm_mixer this is the concatenation of the
362 * mixer and kcontrol name.
363 * for dapm_mixer_named_ctl this is simply the
366 name_len = strlen(w->kcontrols[i].name) + 1;
367 if (w->id != snd_soc_dapm_mixer_named_ctl)
368 name_len += 1 + strlen(w->name);
370 path->long_name = kmalloc(name_len, GFP_KERNEL);
372 if (path->long_name == NULL)
377 snprintf(path->long_name, name_len, "%s %s",
378 w->name, w->kcontrols[i].name);
380 case snd_soc_dapm_mixer_named_ctl:
381 snprintf(path->long_name, name_len, "%s",
382 w->kcontrols[i].name);
386 path->long_name[name_len - 1] = '\0';
388 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
390 ret = snd_ctl_add(card, path->kcontrol);
393 "asoc: failed to add dapm kcontrol %s: %d\n",
394 path->long_name, ret);
395 kfree(path->long_name);
396 path->long_name = NULL;
404 /* create new dapm mux control */
405 static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
406 struct snd_soc_dapm_widget *w)
408 struct snd_soc_dapm_path *path = NULL;
409 struct snd_kcontrol *kcontrol;
410 struct snd_card *card = dapm->codec->card->snd_card;
413 if (!w->num_kcontrols) {
414 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
418 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
419 ret = snd_ctl_add(card, kcontrol);
424 list_for_each_entry(path, &w->sources, list_sink)
425 path->kcontrol = kcontrol;
430 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
434 /* create new dapm volume control */
435 static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
436 struct snd_soc_dapm_widget *w)
438 if (w->num_kcontrols)
439 dev_err(w->dapm->dev,
440 "asoc: PGA controls not supported: '%s'\n", w->name);
445 /* reset 'walked' bit for each dapm path */
446 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
448 struct snd_soc_dapm_path *p;
450 list_for_each_entry(p, &dapm->paths, list)
454 /* We implement power down on suspend by checking the power state of
455 * the ALSA card - when we are suspending the ALSA state for the card
458 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
460 int level = snd_power_get_state(widget->dapm->codec->card->snd_card);
463 case SNDRV_CTL_POWER_D3hot:
464 case SNDRV_CTL_POWER_D3cold:
465 if (widget->ignore_suspend)
466 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
468 return widget->ignore_suspend;
475 * Recursively check for a completed path to an active or physically connected
476 * output widget. Returns number of complete paths.
478 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
480 struct snd_soc_dapm_path *path;
483 if (widget->id == snd_soc_dapm_supply)
486 switch (widget->id) {
487 case snd_soc_dapm_adc:
488 case snd_soc_dapm_aif_out:
490 return snd_soc_dapm_suspend_check(widget);
495 if (widget->connected) {
496 /* connected pin ? */
497 if (widget->id == snd_soc_dapm_output && !widget->ext)
498 return snd_soc_dapm_suspend_check(widget);
500 /* connected jack or spk ? */
501 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
502 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
503 return snd_soc_dapm_suspend_check(widget);
506 list_for_each_entry(path, &widget->sinks, list_source) {
510 if (path->sink && path->connect) {
512 con += is_connected_output_ep(path->sink);
520 * Recursively check for a completed path to an active or physically connected
521 * input widget. Returns number of complete paths.
523 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
525 struct snd_soc_dapm_path *path;
528 if (widget->id == snd_soc_dapm_supply)
531 /* active stream ? */
532 switch (widget->id) {
533 case snd_soc_dapm_dac:
534 case snd_soc_dapm_aif_in:
536 return snd_soc_dapm_suspend_check(widget);
541 if (widget->connected) {
542 /* connected pin ? */
543 if (widget->id == snd_soc_dapm_input && !widget->ext)
544 return snd_soc_dapm_suspend_check(widget);
546 /* connected VMID/Bias for lower pops */
547 if (widget->id == snd_soc_dapm_vmid)
548 return snd_soc_dapm_suspend_check(widget);
550 /* connected jack ? */
551 if (widget->id == snd_soc_dapm_mic ||
552 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
553 return snd_soc_dapm_suspend_check(widget);
556 list_for_each_entry(path, &widget->sources, list_sink) {
560 if (path->source && path->connect) {
562 con += is_connected_input_ep(path->source);
570 * Handler for generic register modifier widget.
572 int dapm_reg_event(struct snd_soc_dapm_widget *w,
573 struct snd_kcontrol *kcontrol, int event)
577 if (SND_SOC_DAPM_EVENT_ON(event))
582 snd_soc_update_bits(w->codec, -(w->reg + 1),
583 w->mask << w->shift, val << w->shift);
587 EXPORT_SYMBOL_GPL(dapm_reg_event);
589 /* Standard power change method, used to apply power changes to most
592 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
596 /* call any power change event handlers */
598 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
599 w->power ? "on" : "off",
600 w->name, w->event_flags);
602 /* power up pre event */
603 if (w->power && w->event &&
604 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
605 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
610 /* power down pre event */
611 if (!w->power && w->event &&
612 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
613 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
620 /* power up post event */
621 if (w->power && w->event &&
622 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
624 NULL, SND_SOC_DAPM_POST_PMU);
629 /* power down post event */
630 if (!w->power && w->event &&
631 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
632 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
640 /* Generic check to see if a widget should be powered.
642 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
646 in = is_connected_input_ep(w);
647 dapm_clear_walk(w->dapm);
648 out = is_connected_output_ep(w);
649 dapm_clear_walk(w->dapm);
650 return out != 0 && in != 0;
653 /* Check to see if an ADC has power */
654 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
659 in = is_connected_input_ep(w);
660 dapm_clear_walk(w->dapm);
663 return dapm_generic_check_power(w);
667 /* Check to see if a DAC has power */
668 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
673 out = is_connected_output_ep(w);
674 dapm_clear_walk(w->dapm);
677 return dapm_generic_check_power(w);
681 /* Check to see if a power supply is needed */
682 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
684 struct snd_soc_dapm_path *path;
687 /* Check if one of our outputs is connected */
688 list_for_each_entry(path, &w->sinks, list_source) {
689 if (path->connected &&
690 !path->connected(path->source, path->sink))
693 if (path->sink && path->sink->power_check &&
694 path->sink->power_check(path->sink)) {
700 dapm_clear_walk(w->dapm);
705 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
706 struct snd_soc_dapm_widget *b,
709 if (sort[a->id] != sort[b->id])
710 return sort[a->id] - sort[b->id];
711 if (a->reg != b->reg)
712 return a->reg - b->reg;
713 if (a->codec != b->codec)
714 return (unsigned long)a->codec - (unsigned long)b->codec;
719 /* Insert a widget in order into a DAPM power sequence. */
720 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
721 struct list_head *list,
724 struct snd_soc_dapm_widget *w;
726 list_for_each_entry(w, list, power_list)
727 if (dapm_seq_compare(new_widget, w, sort) < 0) {
728 list_add_tail(&new_widget->power_list, &w->power_list);
732 list_add_tail(&new_widget->power_list, list);
735 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
736 struct snd_soc_dapm_widget *w, int event)
738 struct snd_soc_card *card = dapm->card;
743 case SND_SOC_DAPM_PRE_PMU:
747 case SND_SOC_DAPM_POST_PMU:
748 ev_name = "POST_PMU";
751 case SND_SOC_DAPM_PRE_PMD:
755 case SND_SOC_DAPM_POST_PMD:
756 ev_name = "POST_PMD";
764 if (w->power != power)
767 if (w->event && (w->event_flags & event)) {
768 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
770 trace_snd_soc_dapm_widget_event_start(w, event);
771 ret = w->event(w, NULL, event);
772 trace_snd_soc_dapm_widget_event_done(w, event);
774 pr_err("%s: %s event failed: %d\n",
775 ev_name, w->name, ret);
779 /* Apply the coalesced changes from a DAPM sequence */
780 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
781 struct list_head *pending)
783 struct snd_soc_card *card = dapm->card;
784 struct snd_soc_dapm_widget *w;
786 unsigned int value = 0;
787 unsigned int mask = 0;
788 unsigned int cur_mask;
790 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
793 list_for_each_entry(w, pending, power_list) {
794 cur_mask = 1 << w->shift;
795 BUG_ON(reg != w->reg);
806 pop_dbg(dapm->dev, card->pop_time,
807 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
808 w->name, reg, value, mask);
810 /* Check for events */
811 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
812 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
816 pop_dbg(dapm->dev, card->pop_time,
817 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
818 value, mask, reg, card->pop_time);
819 pop_wait(card->pop_time);
820 snd_soc_update_bits(dapm->codec, reg, mask, value);
823 list_for_each_entry(w, pending, power_list) {
824 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
825 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
829 /* Apply a DAPM power sequence.
831 * We walk over a pre-sorted list of widgets to apply power to. In
832 * order to minimise the number of writes to the device required
833 * multiple widgets will be updated in a single write where possible.
834 * Currently anything that requires more than a single write is not
837 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
838 struct list_head *list, int event, int sort[])
840 struct snd_soc_dapm_widget *w, *n;
843 int cur_reg = SND_SOC_NOPM;
846 list_for_each_entry_safe(w, n, list, power_list) {
849 /* Do we need to apply any queued changes? */
850 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
851 if (!list_empty(&pending))
852 dapm_seq_run_coalesced(dapm, &pending);
854 INIT_LIST_HEAD(&pending);
856 cur_reg = SND_SOC_NOPM;
860 case snd_soc_dapm_pre:
862 list_for_each_entry_safe_continue(w, n, list,
865 if (event == SND_SOC_DAPM_STREAM_START)
867 NULL, SND_SOC_DAPM_PRE_PMU);
868 else if (event == SND_SOC_DAPM_STREAM_STOP)
870 NULL, SND_SOC_DAPM_PRE_PMD);
873 case snd_soc_dapm_post:
875 list_for_each_entry_safe_continue(w, n, list,
878 if (event == SND_SOC_DAPM_STREAM_START)
880 NULL, SND_SOC_DAPM_POST_PMU);
881 else if (event == SND_SOC_DAPM_STREAM_STOP)
883 NULL, SND_SOC_DAPM_POST_PMD);
886 case snd_soc_dapm_input:
887 case snd_soc_dapm_output:
888 case snd_soc_dapm_hp:
889 case snd_soc_dapm_mic:
890 case snd_soc_dapm_line:
891 case snd_soc_dapm_spk:
892 /* No register support currently */
893 ret = dapm_generic_apply_power(w);
897 /* Queue it up for application */
898 cur_sort = sort[w->id];
900 list_move(&w->power_list, &pending);
905 dev_err(w->dapm->dev,
906 "Failed to apply widget power: %d\n", ret);
909 if (!list_empty(&pending))
910 dapm_seq_run_coalesced(dapm, &pending);
914 * Scan each dapm widget for complete audio path.
915 * A complete path is a route that has valid endpoints i.e.:-
917 * o DAC to output pin.
918 * o Input Pin to ADC.
919 * o Input pin to Output pin (bypass, sidetone)
920 * o DAC to ADC (loopback).
922 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
924 struct snd_soc_card *card = dapm->codec->card;
925 struct snd_soc_dapm_widget *w;
927 LIST_HEAD(down_list);
932 trace_snd_soc_dapm_start(card);
934 /* Check which widgets we need to power and store them in
935 * lists indicating if they should be powered up or down.
937 list_for_each_entry(w, &dapm->widgets, list) {
939 case snd_soc_dapm_pre:
940 dapm_seq_insert(w, &down_list, dapm_down_seq);
942 case snd_soc_dapm_post:
943 dapm_seq_insert(w, &up_list, dapm_up_seq);
951 power = w->power_check(w);
957 if (w->power == power)
960 trace_snd_soc_dapm_widget_power(w, power);
963 dapm_seq_insert(w, &up_list, dapm_up_seq);
965 dapm_seq_insert(w, &down_list, dapm_down_seq);
972 /* If there are no DAPM widgets then try to figure out power from the
975 if (list_empty(&dapm->widgets)) {
977 case SND_SOC_DAPM_STREAM_START:
978 case SND_SOC_DAPM_STREAM_RESUME:
981 case SND_SOC_DAPM_STREAM_SUSPEND:
984 case SND_SOC_DAPM_STREAM_NOP:
985 switch (dapm->bias_level) {
986 case SND_SOC_BIAS_STANDBY:
987 case SND_SOC_BIAS_OFF:
1000 if (sys_power && dapm->bias_level == SND_SOC_BIAS_OFF) {
1001 ret = snd_soc_dapm_set_bias_level(card, dapm,
1002 SND_SOC_BIAS_STANDBY);
1005 "Failed to turn on bias: %d\n", ret);
1008 /* If we're changing to all on or all off then prepare */
1009 if ((sys_power && dapm->bias_level == SND_SOC_BIAS_STANDBY) ||
1010 (!sys_power && dapm->bias_level == SND_SOC_BIAS_ON)) {
1011 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_PREPARE);
1014 "Failed to prepare bias: %d\n", ret);
1017 /* Power down widgets first; try to avoid amplifying pops. */
1018 dapm_seq_run(dapm, &down_list, event, dapm_down_seq);
1021 dapm_seq_run(dapm, &up_list, event, dapm_up_seq);
1023 /* If we just powered the last thing off drop to standby bias */
1024 if (dapm->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
1025 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_STANDBY);
1028 "Failed to apply standby bias: %d\n", ret);
1031 /* If we're in standby and can support bias off then do that */
1032 if (dapm->bias_level == SND_SOC_BIAS_STANDBY &&
1033 dapm->idle_bias_off) {
1034 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_OFF);
1037 "Failed to turn off bias: %d\n", ret);
1040 /* If we just powered up then move to active bias */
1041 if (dapm->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1042 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_ON);
1045 "Failed to apply active bias: %d\n", ret);
1048 pop_dbg(dapm->dev, card->pop_time,
1049 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1050 pop_wait(card->pop_time);
1052 trace_snd_soc_dapm_done(card);
1057 #ifdef CONFIG_DEBUG_FS
1058 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1060 file->private_data = inode->i_private;
1064 static ssize_t dapm_widget_power_read_file(struct file *file,
1065 char __user *user_buf,
1066 size_t count, loff_t *ppos)
1068 struct snd_soc_dapm_widget *w = file->private_data;
1072 struct snd_soc_dapm_path *p = NULL;
1074 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1078 in = is_connected_input_ep(w);
1079 dapm_clear_walk(w->dapm);
1080 out = is_connected_output_ep(w);
1081 dapm_clear_walk(w->dapm);
1083 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1084 w->name, w->power ? "On" : "Off", in, out);
1087 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1088 " - R%d(0x%x) bit %d",
1089 w->reg, w->reg, w->shift);
1091 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1094 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1096 w->active ? "active" : "inactive");
1098 list_for_each_entry(p, &w->sources, list_sink) {
1099 if (p->connected && !p->connected(w, p->sink))
1103 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1105 p->name ? p->name : "static",
1108 list_for_each_entry(p, &w->sinks, list_source) {
1109 if (p->connected && !p->connected(w, p->sink))
1113 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1115 p->name ? p->name : "static",
1119 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1125 static const struct file_operations dapm_widget_power_fops = {
1126 .open = dapm_widget_power_open_file,
1127 .read = dapm_widget_power_read_file,
1128 .llseek = default_llseek,
1131 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1133 struct snd_soc_dapm_widget *w;
1136 if (!dapm->debugfs_dapm)
1139 list_for_each_entry(w, &dapm->widgets, list) {
1143 d = debugfs_create_file(w->name, 0444,
1144 dapm->debugfs_dapm, w,
1145 &dapm_widget_power_fops);
1147 dev_warn(w->dapm->dev,
1148 "ASoC: Failed to create %s debugfs file\n",
1153 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1158 /* test and update the power status of a mux widget */
1159 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1160 struct snd_kcontrol *kcontrol, int change,
1161 int mux, struct soc_enum *e)
1163 struct snd_soc_dapm_path *path;
1166 if (widget->id != snd_soc_dapm_mux &&
1167 widget->id != snd_soc_dapm_value_mux)
1173 /* find dapm widget path assoc with kcontrol */
1174 list_for_each_entry(path, &widget->dapm->paths, list) {
1175 if (path->kcontrol != kcontrol)
1178 if (!path->name || !e->texts[mux])
1182 /* we now need to match the string in the enum to the path */
1183 if (!(strcmp(path->name, e->texts[mux])))
1184 path->connect = 1; /* new connection */
1186 path->connect = 0; /* old connection must be powered down */
1190 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1195 /* test and update the power status of a mixer or switch widget */
1196 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1197 struct snd_kcontrol *kcontrol, int connect)
1199 struct snd_soc_dapm_path *path;
1202 if (widget->id != snd_soc_dapm_mixer &&
1203 widget->id != snd_soc_dapm_mixer_named_ctl &&
1204 widget->id != snd_soc_dapm_switch)
1207 /* find dapm widget path assoc with kcontrol */
1208 list_for_each_entry(path, &widget->dapm->paths, list) {
1209 if (path->kcontrol != kcontrol)
1212 /* found, now check type */
1214 path->connect = connect;
1219 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1224 /* show dapm widget status in sys fs */
1225 static ssize_t dapm_widget_show(struct device *dev,
1226 struct device_attribute *attr, char *buf)
1228 struct snd_soc_pcm_runtime *rtd =
1229 container_of(dev, struct snd_soc_pcm_runtime, dev);
1230 struct snd_soc_codec *codec =rtd->codec;
1231 struct snd_soc_dapm_widget *w;
1233 char *state = "not set";
1235 list_for_each_entry(w, &codec->dapm.widgets, list) {
1237 /* only display widgets that burnm power */
1239 case snd_soc_dapm_hp:
1240 case snd_soc_dapm_mic:
1241 case snd_soc_dapm_spk:
1242 case snd_soc_dapm_line:
1243 case snd_soc_dapm_micbias:
1244 case snd_soc_dapm_dac:
1245 case snd_soc_dapm_adc:
1246 case snd_soc_dapm_pga:
1247 case snd_soc_dapm_mixer:
1248 case snd_soc_dapm_mixer_named_ctl:
1249 case snd_soc_dapm_supply:
1251 count += sprintf(buf + count, "%s: %s\n",
1252 w->name, w->power ? "On":"Off");
1259 switch (codec->dapm.bias_level) {
1260 case SND_SOC_BIAS_ON:
1263 case SND_SOC_BIAS_PREPARE:
1266 case SND_SOC_BIAS_STANDBY:
1269 case SND_SOC_BIAS_OFF:
1273 count += sprintf(buf + count, "PM State: %s\n", state);
1278 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1280 int snd_soc_dapm_sys_add(struct device *dev)
1282 return device_create_file(dev, &dev_attr_dapm_widget);
1285 static void snd_soc_dapm_sys_remove(struct device *dev)
1287 device_remove_file(dev, &dev_attr_dapm_widget);
1290 /* free all dapm widgets and resources */
1291 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1293 struct snd_soc_dapm_widget *w, *next_w;
1294 struct snd_soc_dapm_path *p, *next_p;
1296 list_for_each_entry_safe(w, next_w, &dapm->widgets, list) {
1301 list_for_each_entry_safe(p, next_p, &dapm->paths, list) {
1303 kfree(p->long_name);
1308 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1309 const char *pin, int status)
1311 struct snd_soc_dapm_widget *w;
1313 list_for_each_entry(w, &dapm->widgets, list) {
1314 if (!strcmp(w->name, pin)) {
1315 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1317 w->connected = status;
1318 /* Allow disabling of forced pins */
1325 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1330 * snd_soc_dapm_sync - scan and power dapm paths
1331 * @dapm: DAPM context
1333 * Walks all dapm audio paths and powers widgets according to their
1334 * stream or path usage.
1336 * Returns 0 for success.
1338 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1340 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1342 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1344 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1345 const struct snd_soc_dapm_route *route)
1347 struct snd_soc_dapm_path *path;
1348 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1349 const char *sink = route->sink;
1350 const char *control = route->control;
1351 const char *source = route->source;
1354 /* find src and dest widgets */
1355 list_for_each_entry(w, &dapm->widgets, list) {
1357 if (!wsink && !(strcmp(w->name, sink))) {
1361 if (!wsource && !(strcmp(w->name, source))) {
1366 if (wsource == NULL || wsink == NULL)
1369 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1373 path->source = wsource;
1375 path->connected = route->connected;
1376 INIT_LIST_HEAD(&path->list);
1377 INIT_LIST_HEAD(&path->list_source);
1378 INIT_LIST_HEAD(&path->list_sink);
1380 /* check for external widgets */
1381 if (wsink->id == snd_soc_dapm_input) {
1382 if (wsource->id == snd_soc_dapm_micbias ||
1383 wsource->id == snd_soc_dapm_mic ||
1384 wsource->id == snd_soc_dapm_line ||
1385 wsource->id == snd_soc_dapm_output)
1388 if (wsource->id == snd_soc_dapm_output) {
1389 if (wsink->id == snd_soc_dapm_spk ||
1390 wsink->id == snd_soc_dapm_hp ||
1391 wsink->id == snd_soc_dapm_line ||
1392 wsink->id == snd_soc_dapm_input)
1396 /* connect static paths */
1397 if (control == NULL) {
1398 list_add(&path->list, &dapm->paths);
1399 list_add(&path->list_sink, &wsink->sources);
1400 list_add(&path->list_source, &wsource->sinks);
1405 /* connect dynamic paths */
1407 case snd_soc_dapm_adc:
1408 case snd_soc_dapm_dac:
1409 case snd_soc_dapm_pga:
1410 case snd_soc_dapm_input:
1411 case snd_soc_dapm_output:
1412 case snd_soc_dapm_micbias:
1413 case snd_soc_dapm_vmid:
1414 case snd_soc_dapm_pre:
1415 case snd_soc_dapm_post:
1416 case snd_soc_dapm_supply:
1417 case snd_soc_dapm_aif_in:
1418 case snd_soc_dapm_aif_out:
1419 list_add(&path->list, &dapm->paths);
1420 list_add(&path->list_sink, &wsink->sources);
1421 list_add(&path->list_source, &wsource->sinks);
1424 case snd_soc_dapm_mux:
1425 case snd_soc_dapm_value_mux:
1426 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1427 &wsink->kcontrols[0]);
1431 case snd_soc_dapm_switch:
1432 case snd_soc_dapm_mixer:
1433 case snd_soc_dapm_mixer_named_ctl:
1434 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1438 case snd_soc_dapm_hp:
1439 case snd_soc_dapm_mic:
1440 case snd_soc_dapm_line:
1441 case snd_soc_dapm_spk:
1442 list_add(&path->list, &dapm->paths);
1443 list_add(&path->list_sink, &wsink->sources);
1444 list_add(&path->list_source, &wsource->sinks);
1451 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1452 source, control, sink);
1458 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1459 * @dapm: DAPM context
1460 * @route: audio routes
1461 * @num: number of routes
1463 * Connects 2 dapm widgets together via a named audio path. The sink is
1464 * the widget receiving the audio signal, whilst the source is the sender
1465 * of the audio signal.
1467 * Returns 0 for success else error. On error all resources can be freed
1468 * with a call to snd_soc_card_free().
1470 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1471 const struct snd_soc_dapm_route *route, int num)
1475 for (i = 0; i < num; i++) {
1476 ret = snd_soc_dapm_add_route(dapm, route);
1478 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1479 route->source, route->sink);
1487 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1490 * snd_soc_dapm_new_widgets - add new dapm widgets
1491 * @dapm: DAPM context
1493 * Checks the codec for any new dapm widgets and creates them if found.
1495 * Returns 0 for success.
1497 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1499 struct snd_soc_dapm_widget *w;
1501 list_for_each_entry(w, &dapm->widgets, list)
1507 case snd_soc_dapm_switch:
1508 case snd_soc_dapm_mixer:
1509 case snd_soc_dapm_mixer_named_ctl:
1510 w->power_check = dapm_generic_check_power;
1511 dapm_new_mixer(dapm, w);
1513 case snd_soc_dapm_mux:
1514 case snd_soc_dapm_value_mux:
1515 w->power_check = dapm_generic_check_power;
1516 dapm_new_mux(dapm, w);
1518 case snd_soc_dapm_adc:
1519 case snd_soc_dapm_aif_out:
1520 w->power_check = dapm_adc_check_power;
1522 case snd_soc_dapm_dac:
1523 case snd_soc_dapm_aif_in:
1524 w->power_check = dapm_dac_check_power;
1526 case snd_soc_dapm_pga:
1527 w->power_check = dapm_generic_check_power;
1528 dapm_new_pga(dapm, w);
1530 case snd_soc_dapm_input:
1531 case snd_soc_dapm_output:
1532 case snd_soc_dapm_micbias:
1533 case snd_soc_dapm_spk:
1534 case snd_soc_dapm_hp:
1535 case snd_soc_dapm_mic:
1536 case snd_soc_dapm_line:
1537 w->power_check = dapm_generic_check_power;
1539 case snd_soc_dapm_supply:
1540 w->power_check = dapm_supply_check_power;
1541 case snd_soc_dapm_vmid:
1542 case snd_soc_dapm_pre:
1543 case snd_soc_dapm_post:
1549 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1552 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1555 * snd_soc_dapm_get_volsw - dapm mixer get callback
1556 * @kcontrol: mixer control
1557 * @ucontrol: control element information
1559 * Callback to get the value of a dapm mixer control.
1561 * Returns 0 for success.
1563 int snd_soc_dapm_get_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 invert = mc->invert;
1574 unsigned int mask = (1 << fls(max)) - 1;
1576 ucontrol->value.integer.value[0] =
1577 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1578 if (shift != rshift)
1579 ucontrol->value.integer.value[1] =
1580 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1582 ucontrol->value.integer.value[0] =
1583 max - ucontrol->value.integer.value[0];
1584 if (shift != rshift)
1585 ucontrol->value.integer.value[1] =
1586 max - ucontrol->value.integer.value[1];
1591 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1594 * snd_soc_dapm_put_volsw - dapm mixer set callback
1595 * @kcontrol: mixer control
1596 * @ucontrol: control element information
1598 * Callback to set the value of a dapm mixer control.
1600 * Returns 0 for success.
1602 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1603 struct snd_ctl_elem_value *ucontrol)
1605 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1606 struct soc_mixer_control *mc =
1607 (struct soc_mixer_control *)kcontrol->private_value;
1608 unsigned int reg = mc->reg;
1609 unsigned int shift = mc->shift;
1610 unsigned int rshift = mc->rshift;
1612 unsigned int mask = (1 << fls(max)) - 1;
1613 unsigned int invert = mc->invert;
1614 unsigned int val, val2, val_mask;
1618 val = (ucontrol->value.integer.value[0] & mask);
1622 val_mask = mask << shift;
1624 if (shift != rshift) {
1625 val2 = (ucontrol->value.integer.value[1] & mask);
1628 val_mask |= mask << rshift;
1629 val |= val2 << rshift;
1632 mutex_lock(&widget->codec->mutex);
1633 widget->value = val;
1635 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1637 /* new connection */
1638 connect = invert ? 0:1;
1640 /* old connection must be powered down */
1641 connect = invert ? 1:0;
1643 dapm_mixer_update_power(widget, kcontrol, connect);
1646 if (widget->event) {
1647 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1648 ret = widget->event(widget, kcontrol,
1649 SND_SOC_DAPM_PRE_REG);
1655 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1656 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1657 ret = widget->event(widget, kcontrol,
1658 SND_SOC_DAPM_POST_REG);
1660 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1663 mutex_unlock(&widget->codec->mutex);
1666 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1669 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1670 * @kcontrol: mixer control
1671 * @ucontrol: control element information
1673 * Callback to get the value of a dapm enumerated double mixer control.
1675 * Returns 0 for success.
1677 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1678 struct snd_ctl_elem_value *ucontrol)
1680 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1681 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1682 unsigned int val, bitmask;
1684 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1686 val = snd_soc_read(widget->codec, e->reg);
1687 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1688 if (e->shift_l != e->shift_r)
1689 ucontrol->value.enumerated.item[1] =
1690 (val >> e->shift_r) & (bitmask - 1);
1694 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1697 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1698 * @kcontrol: mixer control
1699 * @ucontrol: control element information
1701 * Callback to set the value of a dapm enumerated double mixer control.
1703 * Returns 0 for success.
1705 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1706 struct snd_ctl_elem_value *ucontrol)
1708 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1709 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1710 unsigned int val, mux, change;
1711 unsigned int mask, bitmask;
1714 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1716 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1718 mux = ucontrol->value.enumerated.item[0];
1719 val = mux << e->shift_l;
1720 mask = (bitmask - 1) << e->shift_l;
1721 if (e->shift_l != e->shift_r) {
1722 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1724 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1725 mask |= (bitmask - 1) << e->shift_r;
1728 mutex_lock(&widget->codec->mutex);
1729 widget->value = val;
1730 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1731 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1733 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1734 ret = widget->event(widget,
1735 kcontrol, SND_SOC_DAPM_PRE_REG);
1740 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1742 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1743 ret = widget->event(widget,
1744 kcontrol, SND_SOC_DAPM_POST_REG);
1747 mutex_unlock(&widget->codec->mutex);
1750 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1753 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1754 * @kcontrol: mixer control
1755 * @ucontrol: control element information
1757 * Returns 0 for success.
1759 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1760 struct snd_ctl_elem_value *ucontrol)
1762 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1764 ucontrol->value.enumerated.item[0] = widget->value;
1768 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1771 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1772 * @kcontrol: mixer control
1773 * @ucontrol: control element information
1775 * Returns 0 for success.
1777 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1778 struct snd_ctl_elem_value *ucontrol)
1780 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1781 struct soc_enum *e =
1782 (struct soc_enum *)kcontrol->private_value;
1786 if (ucontrol->value.enumerated.item[0] >= e->max)
1789 mutex_lock(&widget->codec->mutex);
1791 change = widget->value != ucontrol->value.enumerated.item[0];
1792 widget->value = ucontrol->value.enumerated.item[0];
1793 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1795 mutex_unlock(&widget->codec->mutex);
1798 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1801 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1803 * @kcontrol: mixer control
1804 * @ucontrol: control element information
1806 * Callback to get the value of a dapm semi enumerated double mixer control.
1808 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1809 * used for handling bitfield coded enumeration for example.
1811 * Returns 0 for success.
1813 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1814 struct snd_ctl_elem_value *ucontrol)
1816 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1817 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1818 unsigned int reg_val, val, mux;
1820 reg_val = snd_soc_read(widget->codec, e->reg);
1821 val = (reg_val >> e->shift_l) & e->mask;
1822 for (mux = 0; mux < e->max; mux++) {
1823 if (val == e->values[mux])
1826 ucontrol->value.enumerated.item[0] = mux;
1827 if (e->shift_l != e->shift_r) {
1828 val = (reg_val >> e->shift_r) & e->mask;
1829 for (mux = 0; mux < e->max; mux++) {
1830 if (val == e->values[mux])
1833 ucontrol->value.enumerated.item[1] = mux;
1838 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1841 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1843 * @kcontrol: mixer control
1844 * @ucontrol: control element information
1846 * Callback to set the value of a dapm semi enumerated double mixer control.
1848 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1849 * used for handling bitfield coded enumeration for example.
1851 * Returns 0 for success.
1853 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1854 struct snd_ctl_elem_value *ucontrol)
1856 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1857 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1858 unsigned int val, mux, change;
1862 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1864 mux = ucontrol->value.enumerated.item[0];
1865 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1866 mask = e->mask << e->shift_l;
1867 if (e->shift_l != e->shift_r) {
1868 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1870 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1871 mask |= e->mask << e->shift_r;
1874 mutex_lock(&widget->codec->mutex);
1875 widget->value = val;
1876 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1877 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1879 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1880 ret = widget->event(widget,
1881 kcontrol, SND_SOC_DAPM_PRE_REG);
1886 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1888 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1889 ret = widget->event(widget,
1890 kcontrol, SND_SOC_DAPM_POST_REG);
1893 mutex_unlock(&widget->codec->mutex);
1896 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1899 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1901 * @kcontrol: mixer control
1902 * @uinfo: control element information
1904 * Callback to provide information about a pin switch control.
1906 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1907 struct snd_ctl_elem_info *uinfo)
1909 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1911 uinfo->value.integer.min = 0;
1912 uinfo->value.integer.max = 1;
1916 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1919 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1921 * @kcontrol: mixer control
1924 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1925 struct snd_ctl_elem_value *ucontrol)
1927 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1928 const char *pin = (const char *)kcontrol->private_value;
1930 mutex_lock(&codec->mutex);
1932 ucontrol->value.integer.value[0] =
1933 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
1935 mutex_unlock(&codec->mutex);
1939 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1942 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1944 * @kcontrol: mixer control
1947 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1948 struct snd_ctl_elem_value *ucontrol)
1950 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1951 const char *pin = (const char *)kcontrol->private_value;
1953 mutex_lock(&codec->mutex);
1955 if (ucontrol->value.integer.value[0])
1956 snd_soc_dapm_enable_pin(&codec->dapm, pin);
1958 snd_soc_dapm_disable_pin(&codec->dapm, pin);
1960 snd_soc_dapm_sync(&codec->dapm);
1962 mutex_unlock(&codec->mutex);
1966 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1969 * snd_soc_dapm_new_control - create new dapm control
1970 * @dapm: DAPM context
1971 * @widget: widget template
1973 * Creates a new dapm control based upon the template.
1975 * Returns 0 for success else error.
1977 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
1978 const struct snd_soc_dapm_widget *widget)
1980 struct snd_soc_dapm_widget *w;
1982 if ((w = dapm_cnew_widget(widget)) == NULL)
1986 w->codec = dapm->codec;
1987 INIT_LIST_HEAD(&w->sources);
1988 INIT_LIST_HEAD(&w->sinks);
1989 INIT_LIST_HEAD(&w->list);
1990 list_add(&w->list, &dapm->widgets);
1992 /* machine layer set ups unconnected pins and insertions */
1996 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1999 * snd_soc_dapm_new_controls - create new dapm controls
2000 * @dapm: DAPM context
2001 * @widget: widget array
2002 * @num: number of widgets
2004 * Creates new DAPM controls based upon the templates.
2006 * Returns 0 for success else error.
2008 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2009 const struct snd_soc_dapm_widget *widget,
2014 for (i = 0; i < num; i++) {
2015 ret = snd_soc_dapm_new_control(dapm, widget);
2018 "ASoC: Failed to create DAPM control %s: %d\n",
2026 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2028 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2029 const char *stream, int event)
2031 struct snd_soc_dapm_widget *w;
2033 list_for_each_entry(w, &dapm->widgets, list)
2037 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2038 w->name, w->sname, stream, event);
2039 if (strstr(w->sname, stream)) {
2041 case SND_SOC_DAPM_STREAM_START:
2044 case SND_SOC_DAPM_STREAM_STOP:
2047 case SND_SOC_DAPM_STREAM_SUSPEND:
2048 case SND_SOC_DAPM_STREAM_RESUME:
2049 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2050 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2056 dapm_power_widgets(dapm, event);
2060 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2061 * @rtd: PCM runtime data
2062 * @stream: stream name
2063 * @event: stream event
2065 * Sends a stream event to the dapm core. The core then makes any
2066 * necessary widget power changes.
2068 * Returns 0 for success else error.
2070 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2071 const char *stream, int event)
2073 struct snd_soc_codec *codec = rtd->codec;
2078 mutex_lock(&codec->mutex);
2079 soc_dapm_stream_event(&codec->dapm, stream, event);
2080 mutex_unlock(&codec->mutex);
2083 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2086 * snd_soc_dapm_enable_pin - enable pin.
2087 * @dapm: DAPM context
2090 * Enables input/output pin and its parents or children widgets iff there is
2091 * a valid audio route and active audio stream.
2092 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2093 * do any widget power switching.
2095 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2097 return snd_soc_dapm_set_pin(dapm, pin, 1);
2099 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2102 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2103 * @dapm: DAPM context
2106 * Enables input/output pin regardless of any other state. This is
2107 * intended for use with microphone bias supplies used in microphone
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_force_enable_pin(struct snd_soc_dapm_context *dapm,
2116 struct snd_soc_dapm_widget *w;
2118 list_for_each_entry(w, &dapm->widgets, list) {
2119 if (!strcmp(w->name, pin)) {
2120 dev_dbg(w->dapm->dev,
2121 "dapm: force enable pin %s\n", pin);
2128 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2131 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2134 * snd_soc_dapm_disable_pin - disable pin.
2135 * @dapm: DAPM context
2138 * Disables input/output pin and its parents or children widgets.
2139 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2140 * do any widget power switching.
2142 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2145 return snd_soc_dapm_set_pin(dapm, pin, 0);
2147 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2150 * snd_soc_dapm_nc_pin - permanently disable pin.
2151 * @dapm: DAPM context
2154 * Marks the specified pin as being not connected, disabling it along
2155 * any parent or child widgets. At present this is identical to
2156 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2157 * additional things such as disabling controls which only affect
2158 * paths through the pin.
2160 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2161 * do any widget power switching.
2163 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2165 return snd_soc_dapm_set_pin(dapm, pin, 0);
2167 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2170 * snd_soc_dapm_get_pin_status - get audio pin status
2171 * @dapm: DAPM context
2172 * @pin: audio signal pin endpoint (or start point)
2174 * Get audio pin status - connected or disconnected.
2176 * Returns 1 for connected otherwise 0.
2178 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2181 struct snd_soc_dapm_widget *w;
2183 list_for_each_entry(w, &dapm->widgets, list) {
2184 if (!strcmp(w->name, pin))
2185 return w->connected;
2190 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2193 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2194 * @dapm: DAPM context
2195 * @pin: audio signal pin endpoint (or start point)
2197 * Mark the given endpoint or pin as ignoring suspend. When the
2198 * system is disabled a path between two endpoints flagged as ignoring
2199 * suspend will not be disabled. The path must already be enabled via
2200 * normal means at suspend time, it will not be turned on if it was not
2203 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2206 struct snd_soc_dapm_widget *w;
2208 list_for_each_entry(w, &dapm->widgets, list) {
2209 if (!strcmp(w->name, pin)) {
2210 w->ignore_suspend = 1;
2215 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2218 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2221 * snd_soc_dapm_free - free dapm resources
2224 * Free all dapm widgets and resources.
2226 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2228 snd_soc_dapm_sys_remove(dapm->dev);
2229 dapm_free_widgets(dapm);
2231 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2233 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2235 struct snd_soc_dapm_widget *w;
2236 LIST_HEAD(down_list);
2239 list_for_each_entry(w, &dapm->widgets, list) {
2241 dapm_seq_insert(w, &down_list, dapm_down_seq);
2247 /* If there were no widgets to power down we're already in
2251 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
2252 dapm_seq_run(dapm, &down_list, 0, dapm_down_seq);
2253 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
2258 * snd_soc_dapm_shutdown - callback for system shutdown
2260 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2262 struct snd_soc_codec *codec;
2264 list_for_each_entry(codec, &card->codec_dev_list, list) {
2265 soc_dapm_shutdown_codec(&codec->dapm);
2266 snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
2270 /* Module information */
2271 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2272 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2273 MODULE_LICENSE("GPL");