2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
25 * o DAPM power change sequencing - allow for configurable per
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/async.h>
36 #include <linux/delay.h>
38 #include <linux/bitops.h>
39 #include <linux/platform_device.h>
40 #include <linux/jiffies.h>
41 #include <linux/debugfs.h>
42 #include <linux/slab.h>
43 #include <sound/core.h>
44 #include <sound/pcm.h>
45 #include <sound/pcm_params.h>
46 #include <sound/soc.h>
47 #include <sound/initval.h>
49 #include <trace/events/asoc.h>
51 /* dapm power sequences - make this per codec in the future */
52 static int dapm_up_seq[] = {
53 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
56 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
60 [snd_soc_dapm_virt_mux] = 5,
61 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
67 [snd_soc_dapm_out_drv] = 10,
68 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
70 [snd_soc_dapm_post] = 11,
73 static int dapm_down_seq[] = {
74 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_adc] = 1,
76 [snd_soc_dapm_hp] = 2,
77 [snd_soc_dapm_spk] = 2,
78 [snd_soc_dapm_out_drv] = 2,
79 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
81 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
86 [snd_soc_dapm_virt_mux] = 9,
87 [snd_soc_dapm_value_mux] = 9,
88 [snd_soc_dapm_aif_in] = 10,
89 [snd_soc_dapm_aif_out] = 10,
90 [snd_soc_dapm_supply] = 11,
91 [snd_soc_dapm_post] = 12,
94 static void pop_wait(u32 pop_time)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
100 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
113 vsnprintf(buf, PAGE_SIZE, fmt, args);
114 dev_info(dev, "%s", buf);
120 /* create a new dapm widget */
121 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
122 const struct snd_soc_dapm_widget *_widget)
124 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
128 * snd_soc_dapm_set_bias_level - set the bias level for the system
129 * @dapm: DAPM context
130 * @level: level to configure
132 * Configure the bias (power) levels for the SoC audio device.
134 * Returns 0 for success else error.
136 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
137 enum snd_soc_bias_level level)
139 struct snd_soc_card *card = dapm->card;
143 case SND_SOC_BIAS_ON:
144 dev_dbg(dapm->dev, "Setting full bias\n");
146 case SND_SOC_BIAS_PREPARE:
147 dev_dbg(dapm->dev, "Setting bias prepare\n");
149 case SND_SOC_BIAS_STANDBY:
150 dev_dbg(dapm->dev, "Setting standby bias\n");
152 case SND_SOC_BIAS_OFF:
153 dev_dbg(dapm->dev, "Setting bias off\n");
156 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
160 trace_snd_soc_bias_level_start(card, level);
162 if (card && card->set_bias_level)
163 ret = card->set_bias_level(card, level);
165 if (dapm->codec && dapm->codec->driver->set_bias_level)
166 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
168 dapm->bias_level = level;
171 if (card && card->set_bias_level_post)
172 ret = card->set_bias_level_post(card, level);
175 trace_snd_soc_bias_level_done(card, level);
180 /* set up initial codec paths */
181 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
182 struct snd_soc_dapm_path *p, int i)
185 case snd_soc_dapm_switch:
186 case snd_soc_dapm_mixer:
187 case snd_soc_dapm_mixer_named_ctl: {
189 struct soc_mixer_control *mc = (struct soc_mixer_control *)
190 w->kcontrols[i].private_value;
191 unsigned int reg = mc->reg;
192 unsigned int shift = mc->shift;
194 unsigned int mask = (1 << fls(max)) - 1;
195 unsigned int invert = mc->invert;
197 val = snd_soc_read(w->codec, reg);
198 val = (val >> shift) & mask;
200 if ((invert && !val) || (!invert && val))
206 case snd_soc_dapm_mux: {
207 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
208 int val, item, bitmask;
210 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
212 val = snd_soc_read(w->codec, e->reg);
213 item = (val >> e->shift_l) & (bitmask - 1);
216 for (i = 0; i < e->max; i++) {
217 if (!(strcmp(p->name, e->texts[i])) && item == i)
222 case snd_soc_dapm_virt_mux: {
223 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
226 /* since a virtual mux has no backing registers to
227 * decide which path to connect, it will try to match
228 * with the first enumeration. This is to ensure
229 * that the default mux choice (the first) will be
230 * correctly powered up during initialization.
232 if (!strcmp(p->name, e->texts[0]))
236 case snd_soc_dapm_value_mux: {
237 struct soc_enum *e = (struct soc_enum *)
238 w->kcontrols[i].private_value;
241 val = snd_soc_read(w->codec, e->reg);
242 val = (val >> e->shift_l) & e->mask;
243 for (item = 0; item < e->max; item++) {
244 if (val == e->values[item])
249 for (i = 0; i < e->max; i++) {
250 if (!(strcmp(p->name, e->texts[i])) && item == i)
255 /* does not effect routing - always connected */
256 case snd_soc_dapm_pga:
257 case snd_soc_dapm_out_drv:
258 case snd_soc_dapm_output:
259 case snd_soc_dapm_adc:
260 case snd_soc_dapm_input:
261 case snd_soc_dapm_dac:
262 case snd_soc_dapm_micbias:
263 case snd_soc_dapm_vmid:
264 case snd_soc_dapm_supply:
265 case snd_soc_dapm_aif_in:
266 case snd_soc_dapm_aif_out:
269 /* does effect routing - dynamically connected */
270 case snd_soc_dapm_hp:
271 case snd_soc_dapm_mic:
272 case snd_soc_dapm_spk:
273 case snd_soc_dapm_line:
274 case snd_soc_dapm_pre:
275 case snd_soc_dapm_post:
281 /* connect mux widget to its interconnecting audio paths */
282 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
283 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
284 struct snd_soc_dapm_path *path, const char *control_name,
285 const struct snd_kcontrol_new *kcontrol)
287 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
290 for (i = 0; i < e->max; i++) {
291 if (!(strcmp(control_name, e->texts[i]))) {
292 list_add(&path->list, &dapm->card->paths);
293 list_add(&path->list_sink, &dest->sources);
294 list_add(&path->list_source, &src->sinks);
295 path->name = (char*)e->texts[i];
296 dapm_set_path_status(dest, path, 0);
304 /* connect mixer widget to its interconnecting audio paths */
305 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
306 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
307 struct snd_soc_dapm_path *path, const char *control_name)
311 /* search for mixer kcontrol */
312 for (i = 0; i < dest->num_kcontrols; i++) {
313 if (!strcmp(control_name, dest->kcontrols[i].name)) {
314 list_add(&path->list, &dapm->card->paths);
315 list_add(&path->list_sink, &dest->sources);
316 list_add(&path->list_source, &src->sinks);
317 path->name = dest->kcontrols[i].name;
318 dapm_set_path_status(dest, path, i);
325 /* update dapm codec register bits */
326 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
329 unsigned int old, new;
330 struct snd_soc_codec *codec = widget->codec;
331 struct snd_soc_dapm_context *dapm = widget->dapm;
332 struct snd_soc_card *card = dapm->card;
334 /* check for valid widgets */
335 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
336 widget->id == snd_soc_dapm_output ||
337 widget->id == snd_soc_dapm_hp ||
338 widget->id == snd_soc_dapm_mic ||
339 widget->id == snd_soc_dapm_line ||
340 widget->id == snd_soc_dapm_spk)
343 power = widget->power;
345 power = (power ? 0:1);
347 old = snd_soc_read(codec, widget->reg);
348 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
352 pop_dbg(dapm->dev, card->pop_time,
353 "pop test %s : %s in %d ms\n",
354 widget->name, widget->power ? "on" : "off",
356 pop_wait(card->pop_time);
357 snd_soc_write(codec, widget->reg, new);
359 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
364 /* create new dapm mixer control */
365 static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
366 struct snd_soc_dapm_widget *w)
370 struct snd_soc_dapm_path *path;
371 struct snd_card *card = dapm->codec->card->snd_card;
374 for (i = 0; i < w->num_kcontrols; i++) {
377 list_for_each_entry(path, &w->sources, list_sink) {
379 /* mixer/mux paths name must match control name */
380 if (path->name != (char*)w->kcontrols[i].name)
383 /* add dapm control with long name.
384 * for dapm_mixer this is the concatenation of the
385 * mixer and kcontrol name.
386 * for dapm_mixer_named_ctl this is simply the
389 name_len = strlen(w->kcontrols[i].name) + 1;
390 if (w->id != snd_soc_dapm_mixer_named_ctl)
391 name_len += 1 + strlen(w->name);
393 path->long_name = kmalloc(name_len, GFP_KERNEL);
395 if (path->long_name == NULL)
400 snprintf(path->long_name, name_len, "%s %s",
401 w->name, w->kcontrols[i].name);
403 case snd_soc_dapm_mixer_named_ctl:
404 snprintf(path->long_name, name_len, "%s",
405 w->kcontrols[i].name);
409 path->long_name[name_len - 1] = '\0';
411 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
413 ret = snd_ctl_add(card, path->kcontrol);
416 "asoc: failed to add dapm kcontrol %s: %d\n",
417 path->long_name, ret);
418 kfree(path->long_name);
419 path->long_name = NULL;
427 /* create new dapm mux control */
428 static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
429 struct snd_soc_dapm_widget *w)
431 struct snd_soc_dapm_path *path = NULL;
432 struct snd_kcontrol *kcontrol;
433 struct snd_card *card = dapm->codec->card->snd_card;
436 if (!w->num_kcontrols) {
437 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
441 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
442 ret = snd_ctl_add(card, kcontrol);
447 list_for_each_entry(path, &w->sources, list_sink)
448 path->kcontrol = kcontrol;
453 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
457 /* create new dapm volume control */
458 static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
459 struct snd_soc_dapm_widget *w)
461 if (w->num_kcontrols)
462 dev_err(w->dapm->dev,
463 "asoc: PGA controls not supported: '%s'\n", w->name);
468 /* reset 'walked' bit for each dapm path */
469 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
471 struct snd_soc_dapm_path *p;
473 list_for_each_entry(p, &dapm->card->paths, list)
477 /* We implement power down on suspend by checking the power state of
478 * the ALSA card - when we are suspending the ALSA state for the card
481 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
483 int level = snd_power_get_state(widget->dapm->codec->card->snd_card);
486 case SNDRV_CTL_POWER_D3hot:
487 case SNDRV_CTL_POWER_D3cold:
488 if (widget->ignore_suspend)
489 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
491 return widget->ignore_suspend;
498 * Recursively check for a completed path to an active or physically connected
499 * output widget. Returns number of complete paths.
501 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
503 struct snd_soc_dapm_path *path;
506 if (widget->id == snd_soc_dapm_supply)
509 switch (widget->id) {
510 case snd_soc_dapm_adc:
511 case snd_soc_dapm_aif_out:
513 return snd_soc_dapm_suspend_check(widget);
518 if (widget->connected) {
519 /* connected pin ? */
520 if (widget->id == snd_soc_dapm_output && !widget->ext)
521 return snd_soc_dapm_suspend_check(widget);
523 /* connected jack or spk ? */
524 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
525 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
526 return snd_soc_dapm_suspend_check(widget);
529 list_for_each_entry(path, &widget->sinks, list_source) {
533 if (path->sink && path->connect) {
535 con += is_connected_output_ep(path->sink);
543 * Recursively check for a completed path to an active or physically connected
544 * input widget. Returns number of complete paths.
546 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
548 struct snd_soc_dapm_path *path;
551 if (widget->id == snd_soc_dapm_supply)
554 /* active stream ? */
555 switch (widget->id) {
556 case snd_soc_dapm_dac:
557 case snd_soc_dapm_aif_in:
559 return snd_soc_dapm_suspend_check(widget);
564 if (widget->connected) {
565 /* connected pin ? */
566 if (widget->id == snd_soc_dapm_input && !widget->ext)
567 return snd_soc_dapm_suspend_check(widget);
569 /* connected VMID/Bias for lower pops */
570 if (widget->id == snd_soc_dapm_vmid)
571 return snd_soc_dapm_suspend_check(widget);
573 /* connected jack ? */
574 if (widget->id == snd_soc_dapm_mic ||
575 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
576 return snd_soc_dapm_suspend_check(widget);
579 list_for_each_entry(path, &widget->sources, list_sink) {
583 if (path->source && path->connect) {
585 con += is_connected_input_ep(path->source);
593 * Handler for generic register modifier widget.
595 int dapm_reg_event(struct snd_soc_dapm_widget *w,
596 struct snd_kcontrol *kcontrol, int event)
600 if (SND_SOC_DAPM_EVENT_ON(event))
605 snd_soc_update_bits(w->codec, -(w->reg + 1),
606 w->mask << w->shift, val << w->shift);
610 EXPORT_SYMBOL_GPL(dapm_reg_event);
612 /* Standard power change method, used to apply power changes to most
615 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
619 /* call any power change event handlers */
621 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
622 w->power ? "on" : "off",
623 w->name, w->event_flags);
625 /* power up pre event */
626 if (w->power && w->event &&
627 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
628 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
633 /* power down pre event */
634 if (!w->power && w->event &&
635 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
636 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
643 /* power up post event */
644 if (w->power && w->event &&
645 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
647 NULL, SND_SOC_DAPM_POST_PMU);
652 /* power down post event */
653 if (!w->power && w->event &&
654 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
655 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
663 /* Generic check to see if a widget should be powered.
665 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
669 in = is_connected_input_ep(w);
670 dapm_clear_walk(w->dapm);
671 out = is_connected_output_ep(w);
672 dapm_clear_walk(w->dapm);
673 return out != 0 && in != 0;
676 /* Check to see if an ADC has power */
677 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
682 in = is_connected_input_ep(w);
683 dapm_clear_walk(w->dapm);
686 return dapm_generic_check_power(w);
690 /* Check to see if a DAC has power */
691 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
696 out = is_connected_output_ep(w);
697 dapm_clear_walk(w->dapm);
700 return dapm_generic_check_power(w);
704 /* Check to see if a power supply is needed */
705 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
707 struct snd_soc_dapm_path *path;
710 /* Check if one of our outputs is connected */
711 list_for_each_entry(path, &w->sinks, list_source) {
712 if (path->connected &&
713 !path->connected(path->source, path->sink))
719 if (path->sink->force) {
724 if (path->sink->power_check &&
725 path->sink->power_check(path->sink)) {
731 dapm_clear_walk(w->dapm);
736 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
737 struct snd_soc_dapm_widget *b,
745 sort = dapm_down_seq;
747 if (sort[a->id] != sort[b->id])
748 return sort[a->id] - sort[b->id];
749 if (a->subseq != b->subseq) {
751 return a->subseq - b->subseq;
753 return b->subseq - a->subseq;
755 if (a->reg != b->reg)
756 return a->reg - b->reg;
757 if (a->dapm != b->dapm)
758 return (unsigned long)a->dapm - (unsigned long)b->dapm;
763 /* Insert a widget in order into a DAPM power sequence. */
764 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
765 struct list_head *list,
768 struct snd_soc_dapm_widget *w;
770 list_for_each_entry(w, list, power_list)
771 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
772 list_add_tail(&new_widget->power_list, &w->power_list);
776 list_add_tail(&new_widget->power_list, list);
779 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
780 struct snd_soc_dapm_widget *w, int event)
782 struct snd_soc_card *card = dapm->card;
787 case SND_SOC_DAPM_PRE_PMU:
791 case SND_SOC_DAPM_POST_PMU:
792 ev_name = "POST_PMU";
795 case SND_SOC_DAPM_PRE_PMD:
799 case SND_SOC_DAPM_POST_PMD:
800 ev_name = "POST_PMD";
808 if (w->power != power)
811 if (w->event && (w->event_flags & event)) {
812 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
814 trace_snd_soc_dapm_widget_event_start(w, event);
815 ret = w->event(w, NULL, event);
816 trace_snd_soc_dapm_widget_event_done(w, event);
818 pr_err("%s: %s event failed: %d\n",
819 ev_name, w->name, ret);
823 /* Apply the coalesced changes from a DAPM sequence */
824 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
825 struct list_head *pending)
827 struct snd_soc_card *card = dapm->card;
828 struct snd_soc_dapm_widget *w;
830 unsigned int value = 0;
831 unsigned int mask = 0;
832 unsigned int cur_mask;
834 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
837 list_for_each_entry(w, pending, power_list) {
838 cur_mask = 1 << w->shift;
839 BUG_ON(reg != w->reg);
850 pop_dbg(dapm->dev, card->pop_time,
851 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
852 w->name, reg, value, mask);
854 /* Check for events */
855 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
856 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
860 pop_dbg(dapm->dev, card->pop_time,
861 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
862 value, mask, reg, card->pop_time);
863 pop_wait(card->pop_time);
864 snd_soc_update_bits(dapm->codec, reg, mask, value);
867 list_for_each_entry(w, pending, power_list) {
868 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
869 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
873 /* Apply a DAPM power sequence.
875 * We walk over a pre-sorted list of widgets to apply power to. In
876 * order to minimise the number of writes to the device required
877 * multiple widgets will be updated in a single write where possible.
878 * Currently anything that requires more than a single write is not
881 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
882 struct list_head *list, int event, bool power_up)
884 struct snd_soc_dapm_widget *w, *n;
888 int cur_reg = SND_SOC_NOPM;
889 struct snd_soc_dapm_context *cur_dapm = NULL;
896 sort = dapm_down_seq;
898 list_for_each_entry_safe(w, n, list, power_list) {
901 /* Do we need to apply any queued changes? */
902 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
903 w->dapm != cur_dapm || w->subseq != cur_subseq) {
904 if (!list_empty(&pending))
905 dapm_seq_run_coalesced(cur_dapm, &pending);
907 if (cur_dapm && cur_dapm->seq_notifier) {
908 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
909 if (sort[i] == cur_sort)
910 cur_dapm->seq_notifier(cur_dapm,
915 INIT_LIST_HEAD(&pending);
918 cur_reg = SND_SOC_NOPM;
923 case snd_soc_dapm_pre:
925 list_for_each_entry_safe_continue(w, n, list,
928 if (event == SND_SOC_DAPM_STREAM_START)
930 NULL, SND_SOC_DAPM_PRE_PMU);
931 else if (event == SND_SOC_DAPM_STREAM_STOP)
933 NULL, SND_SOC_DAPM_PRE_PMD);
936 case snd_soc_dapm_post:
938 list_for_each_entry_safe_continue(w, n, list,
941 if (event == SND_SOC_DAPM_STREAM_START)
943 NULL, SND_SOC_DAPM_POST_PMU);
944 else if (event == SND_SOC_DAPM_STREAM_STOP)
946 NULL, SND_SOC_DAPM_POST_PMD);
949 case snd_soc_dapm_input:
950 case snd_soc_dapm_output:
951 case snd_soc_dapm_hp:
952 case snd_soc_dapm_mic:
953 case snd_soc_dapm_line:
954 case snd_soc_dapm_spk:
955 /* No register support currently */
956 ret = dapm_generic_apply_power(w);
960 /* Queue it up for application */
961 cur_sort = sort[w->id];
962 cur_subseq = w->subseq;
965 list_move(&w->power_list, &pending);
970 dev_err(w->dapm->dev,
971 "Failed to apply widget power: %d\n", ret);
974 if (!list_empty(&pending))
975 dapm_seq_run_coalesced(dapm, &pending);
977 if (cur_dapm && cur_dapm->seq_notifier) {
978 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
979 if (sort[i] == cur_sort)
980 cur_dapm->seq_notifier(cur_dapm,
985 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
987 struct snd_soc_dapm_update *update = dapm->update;
988 struct snd_soc_dapm_widget *w;
997 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
998 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1000 pr_err("%s DAPM pre-event failed: %d\n",
1004 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1007 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1010 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1011 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1013 pr_err("%s DAPM post-event failed: %d\n",
1018 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1019 * they're changing state.
1021 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1023 struct snd_soc_dapm_context *d = data;
1026 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1027 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1030 "Failed to turn on bias: %d\n", ret);
1033 /* If we're changing to all on or all off then prepare */
1034 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1035 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1036 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1039 "Failed to prepare bias: %d\n", ret);
1043 /* Async callback run prior to DAPM sequences - brings to their final
1046 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1048 struct snd_soc_dapm_context *d = data;
1051 /* If we just powered the last thing off drop to standby bias */
1052 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1053 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1055 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1059 /* If we're in standby and can support bias off then do that */
1060 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1061 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1063 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1066 /* If we just powered up then move to active bias */
1067 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1068 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1070 dev_err(d->dev, "Failed to apply active bias: %d\n",
1076 * Scan each dapm widget for complete audio path.
1077 * A complete path is a route that has valid endpoints i.e.:-
1079 * o DAC to output pin.
1080 * o Input Pin to ADC.
1081 * o Input pin to Output pin (bypass, sidetone)
1082 * o DAC to ADC (loopback).
1084 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1086 struct snd_soc_card *card = dapm->codec->card;
1087 struct snd_soc_dapm_widget *w;
1088 struct snd_soc_dapm_context *d;
1090 LIST_HEAD(down_list);
1091 LIST_HEAD(async_domain);
1094 trace_snd_soc_dapm_start(card);
1096 list_for_each_entry(d, &card->dapm_list, list)
1100 /* Check which widgets we need to power and store them in
1101 * lists indicating if they should be powered up or down.
1103 list_for_each_entry(w, &card->widgets, list) {
1105 case snd_soc_dapm_pre:
1106 dapm_seq_insert(w, &down_list, false);
1108 case snd_soc_dapm_post:
1109 dapm_seq_insert(w, &up_list, true);
1113 if (!w->power_check)
1117 power = w->power_check(w);
1121 w->dapm->dev_power = 1;
1123 if (w->power == power)
1126 trace_snd_soc_dapm_widget_power(w, power);
1129 dapm_seq_insert(w, &up_list, true);
1131 dapm_seq_insert(w, &down_list, false);
1138 /* If there are no DAPM widgets then try to figure out power from the
1141 if (!dapm->n_widgets) {
1143 case SND_SOC_DAPM_STREAM_START:
1144 case SND_SOC_DAPM_STREAM_RESUME:
1145 dapm->dev_power = 1;
1147 case SND_SOC_DAPM_STREAM_STOP:
1148 dapm->dev_power = !!dapm->codec->active;
1150 case SND_SOC_DAPM_STREAM_SUSPEND:
1151 dapm->dev_power = 0;
1153 case SND_SOC_DAPM_STREAM_NOP:
1154 switch (dapm->bias_level) {
1155 case SND_SOC_BIAS_STANDBY:
1156 case SND_SOC_BIAS_OFF:
1157 dapm->dev_power = 0;
1160 dapm->dev_power = 1;
1169 /* Run all the bias changes in parallel */
1170 list_for_each_entry(d, &dapm->card->dapm_list, list)
1171 async_schedule_domain(dapm_pre_sequence_async, d,
1173 async_synchronize_full_domain(&async_domain);
1175 /* Power down widgets first; try to avoid amplifying pops. */
1176 dapm_seq_run(dapm, &down_list, event, false);
1178 dapm_widget_update(dapm);
1181 dapm_seq_run(dapm, &up_list, event, true);
1183 /* Run all the bias changes in parallel */
1184 list_for_each_entry(d, &dapm->card->dapm_list, list)
1185 async_schedule_domain(dapm_post_sequence_async, d,
1187 async_synchronize_full_domain(&async_domain);
1189 pop_dbg(dapm->dev, card->pop_time,
1190 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1191 pop_wait(card->pop_time);
1193 trace_snd_soc_dapm_done(card);
1198 #ifdef CONFIG_DEBUG_FS
1199 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1201 file->private_data = inode->i_private;
1205 static ssize_t dapm_widget_power_read_file(struct file *file,
1206 char __user *user_buf,
1207 size_t count, loff_t *ppos)
1209 struct snd_soc_dapm_widget *w = file->private_data;
1213 struct snd_soc_dapm_path *p = NULL;
1215 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1219 in = is_connected_input_ep(w);
1220 dapm_clear_walk(w->dapm);
1221 out = is_connected_output_ep(w);
1222 dapm_clear_walk(w->dapm);
1224 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1225 w->name, w->power ? "On" : "Off", in, out);
1228 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1229 " - R%d(0x%x) bit %d",
1230 w->reg, w->reg, w->shift);
1232 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1235 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1237 w->active ? "active" : "inactive");
1239 list_for_each_entry(p, &w->sources, list_sink) {
1240 if (p->connected && !p->connected(w, p->sink))
1244 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1245 " in \"%s\" \"%s\"\n",
1246 p->name ? p->name : "static",
1249 list_for_each_entry(p, &w->sinks, list_source) {
1250 if (p->connected && !p->connected(w, p->sink))
1254 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1255 " out \"%s\" \"%s\"\n",
1256 p->name ? p->name : "static",
1260 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1266 static const struct file_operations dapm_widget_power_fops = {
1267 .open = dapm_widget_power_open_file,
1268 .read = dapm_widget_power_read_file,
1269 .llseek = default_llseek,
1272 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1274 struct snd_soc_dapm_widget *w;
1277 if (!dapm->debugfs_dapm)
1280 list_for_each_entry(w, &dapm->card->widgets, list) {
1281 if (!w->name || w->dapm != dapm)
1284 d = debugfs_create_file(w->name, 0444,
1285 dapm->debugfs_dapm, w,
1286 &dapm_widget_power_fops);
1288 dev_warn(w->dapm->dev,
1289 "ASoC: Failed to create %s debugfs file\n",
1294 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1299 /* test and update the power status of a mux widget */
1300 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1301 struct snd_kcontrol *kcontrol, int change,
1302 int mux, struct soc_enum *e)
1304 struct snd_soc_dapm_path *path;
1307 if (widget->id != snd_soc_dapm_mux &&
1308 widget->id != snd_soc_dapm_virt_mux &&
1309 widget->id != snd_soc_dapm_value_mux)
1315 /* find dapm widget path assoc with kcontrol */
1316 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1317 if (path->kcontrol != kcontrol)
1320 if (!path->name || !e->texts[mux])
1324 /* we now need to match the string in the enum to the path */
1325 if (!(strcmp(path->name, e->texts[mux])))
1326 path->connect = 1; /* new connection */
1328 path->connect = 0; /* old connection must be powered down */
1332 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1337 /* test and update the power status of a mixer or switch widget */
1338 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1339 struct snd_kcontrol *kcontrol, int connect)
1341 struct snd_soc_dapm_path *path;
1344 if (widget->id != snd_soc_dapm_mixer &&
1345 widget->id != snd_soc_dapm_mixer_named_ctl &&
1346 widget->id != snd_soc_dapm_switch)
1349 /* find dapm widget path assoc with kcontrol */
1350 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1351 if (path->kcontrol != kcontrol)
1354 /* found, now check type */
1356 path->connect = connect;
1361 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1366 /* show dapm widget status in sys fs */
1367 static ssize_t dapm_widget_show(struct device *dev,
1368 struct device_attribute *attr, char *buf)
1370 struct snd_soc_pcm_runtime *rtd =
1371 container_of(dev, struct snd_soc_pcm_runtime, dev);
1372 struct snd_soc_codec *codec =rtd->codec;
1373 struct snd_soc_dapm_widget *w;
1375 char *state = "not set";
1377 list_for_each_entry(w, &codec->card->widgets, list) {
1378 if (w->dapm != &codec->dapm)
1381 /* only display widgets that burnm power */
1383 case snd_soc_dapm_hp:
1384 case snd_soc_dapm_mic:
1385 case snd_soc_dapm_spk:
1386 case snd_soc_dapm_line:
1387 case snd_soc_dapm_micbias:
1388 case snd_soc_dapm_dac:
1389 case snd_soc_dapm_adc:
1390 case snd_soc_dapm_pga:
1391 case snd_soc_dapm_out_drv:
1392 case snd_soc_dapm_mixer:
1393 case snd_soc_dapm_mixer_named_ctl:
1394 case snd_soc_dapm_supply:
1396 count += sprintf(buf + count, "%s: %s\n",
1397 w->name, w->power ? "On":"Off");
1404 switch (codec->dapm.bias_level) {
1405 case SND_SOC_BIAS_ON:
1408 case SND_SOC_BIAS_PREPARE:
1411 case SND_SOC_BIAS_STANDBY:
1414 case SND_SOC_BIAS_OFF:
1418 count += sprintf(buf + count, "PM State: %s\n", state);
1423 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1425 int snd_soc_dapm_sys_add(struct device *dev)
1427 return device_create_file(dev, &dev_attr_dapm_widget);
1430 static void snd_soc_dapm_sys_remove(struct device *dev)
1432 device_remove_file(dev, &dev_attr_dapm_widget);
1435 /* free all dapm widgets and resources */
1436 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1438 struct snd_soc_dapm_widget *w, *next_w;
1439 struct snd_soc_dapm_path *p, *next_p;
1441 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1442 if (w->dapm != dapm)
1446 * remove source and sink paths associated to this widget.
1447 * While removing the path, remove reference to it from both
1448 * source and sink widgets so that path is removed only once.
1450 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1451 list_del(&p->list_sink);
1452 list_del(&p->list_source);
1454 kfree(p->long_name);
1457 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1458 list_del(&p->list_sink);
1459 list_del(&p->list_source);
1461 kfree(p->long_name);
1469 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1470 const char *pin, int status)
1472 struct snd_soc_dapm_widget *w;
1474 list_for_each_entry(w, &dapm->card->widgets, list) {
1475 if (w->dapm != dapm)
1477 if (!strcmp(w->name, pin)) {
1478 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1480 w->connected = status;
1481 /* Allow disabling of forced pins */
1488 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1493 * snd_soc_dapm_sync - scan and power dapm paths
1494 * @dapm: DAPM context
1496 * Walks all dapm audio paths and powers widgets according to their
1497 * stream or path usage.
1499 * Returns 0 for success.
1501 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1503 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1505 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1507 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1508 const struct snd_soc_dapm_route *route)
1510 struct snd_soc_dapm_path *path;
1511 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1512 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1514 const char *control = route->control;
1516 char prefixed_sink[80];
1517 char prefixed_source[80];
1520 if (dapm->codec->name_prefix) {
1521 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1522 dapm->codec->name_prefix, route->sink);
1523 sink = prefixed_sink;
1524 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1525 dapm->codec->name_prefix, route->source);
1526 source = prefixed_source;
1529 source = route->source;
1533 * find src and dest widgets over all widgets but favor a widget from
1534 * current DAPM context
1536 list_for_each_entry(w, &dapm->card->widgets, list) {
1537 if (!wsink && !(strcmp(w->name, sink))) {
1539 if (w->dapm == dapm)
1543 if (!wsource && !(strcmp(w->name, source))) {
1545 if (w->dapm == dapm)
1549 /* use widget from another DAPM context if not found from this */
1555 if (wsource == NULL || wsink == NULL)
1558 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1562 path->source = wsource;
1564 path->connected = route->connected;
1565 INIT_LIST_HEAD(&path->list);
1566 INIT_LIST_HEAD(&path->list_source);
1567 INIT_LIST_HEAD(&path->list_sink);
1569 /* check for external widgets */
1570 if (wsink->id == snd_soc_dapm_input) {
1571 if (wsource->id == snd_soc_dapm_micbias ||
1572 wsource->id == snd_soc_dapm_mic ||
1573 wsource->id == snd_soc_dapm_line ||
1574 wsource->id == snd_soc_dapm_output)
1577 if (wsource->id == snd_soc_dapm_output) {
1578 if (wsink->id == snd_soc_dapm_spk ||
1579 wsink->id == snd_soc_dapm_hp ||
1580 wsink->id == snd_soc_dapm_line ||
1581 wsink->id == snd_soc_dapm_input)
1585 /* connect static paths */
1586 if (control == NULL) {
1587 list_add(&path->list, &dapm->card->paths);
1588 list_add(&path->list_sink, &wsink->sources);
1589 list_add(&path->list_source, &wsource->sinks);
1594 /* connect dynamic paths */
1596 case snd_soc_dapm_adc:
1597 case snd_soc_dapm_dac:
1598 case snd_soc_dapm_pga:
1599 case snd_soc_dapm_out_drv:
1600 case snd_soc_dapm_input:
1601 case snd_soc_dapm_output:
1602 case snd_soc_dapm_micbias:
1603 case snd_soc_dapm_vmid:
1604 case snd_soc_dapm_pre:
1605 case snd_soc_dapm_post:
1606 case snd_soc_dapm_supply:
1607 case snd_soc_dapm_aif_in:
1608 case snd_soc_dapm_aif_out:
1609 list_add(&path->list, &dapm->card->paths);
1610 list_add(&path->list_sink, &wsink->sources);
1611 list_add(&path->list_source, &wsource->sinks);
1614 case snd_soc_dapm_mux:
1615 case snd_soc_dapm_virt_mux:
1616 case snd_soc_dapm_value_mux:
1617 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1618 &wsink->kcontrols[0]);
1622 case snd_soc_dapm_switch:
1623 case snd_soc_dapm_mixer:
1624 case snd_soc_dapm_mixer_named_ctl:
1625 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1629 case snd_soc_dapm_hp:
1630 case snd_soc_dapm_mic:
1631 case snd_soc_dapm_line:
1632 case snd_soc_dapm_spk:
1633 list_add(&path->list, &dapm->card->paths);
1634 list_add(&path->list_sink, &wsink->sources);
1635 list_add(&path->list_source, &wsource->sinks);
1642 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1643 source, control, sink);
1649 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1650 * @dapm: DAPM context
1651 * @route: audio routes
1652 * @num: number of routes
1654 * Connects 2 dapm widgets together via a named audio path. The sink is
1655 * the widget receiving the audio signal, whilst the source is the sender
1656 * of the audio signal.
1658 * Returns 0 for success else error. On error all resources can be freed
1659 * with a call to snd_soc_card_free().
1661 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1662 const struct snd_soc_dapm_route *route, int num)
1666 for (i = 0; i < num; i++) {
1667 ret = snd_soc_dapm_add_route(dapm, route);
1669 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1670 route->source, route->sink);
1678 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1681 * snd_soc_dapm_new_widgets - add new dapm widgets
1682 * @dapm: DAPM context
1684 * Checks the codec for any new dapm widgets and creates them if found.
1686 * Returns 0 for success.
1688 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1690 struct snd_soc_dapm_widget *w;
1693 list_for_each_entry(w, &dapm->card->widgets, list)
1699 case snd_soc_dapm_switch:
1700 case snd_soc_dapm_mixer:
1701 case snd_soc_dapm_mixer_named_ctl:
1702 w->power_check = dapm_generic_check_power;
1703 dapm_new_mixer(dapm, w);
1705 case snd_soc_dapm_mux:
1706 case snd_soc_dapm_virt_mux:
1707 case snd_soc_dapm_value_mux:
1708 w->power_check = dapm_generic_check_power;
1709 dapm_new_mux(dapm, w);
1711 case snd_soc_dapm_adc:
1712 case snd_soc_dapm_aif_out:
1713 w->power_check = dapm_adc_check_power;
1715 case snd_soc_dapm_dac:
1716 case snd_soc_dapm_aif_in:
1717 w->power_check = dapm_dac_check_power;
1719 case snd_soc_dapm_pga:
1720 case snd_soc_dapm_out_drv:
1721 w->power_check = dapm_generic_check_power;
1722 dapm_new_pga(dapm, w);
1724 case snd_soc_dapm_input:
1725 case snd_soc_dapm_output:
1726 case snd_soc_dapm_micbias:
1727 case snd_soc_dapm_spk:
1728 case snd_soc_dapm_hp:
1729 case snd_soc_dapm_mic:
1730 case snd_soc_dapm_line:
1731 w->power_check = dapm_generic_check_power;
1733 case snd_soc_dapm_supply:
1734 w->power_check = dapm_supply_check_power;
1735 case snd_soc_dapm_vmid:
1736 case snd_soc_dapm_pre:
1737 case snd_soc_dapm_post:
1741 /* Read the initial power state from the device */
1743 val = snd_soc_read(w->codec, w->reg);
1744 val &= 1 << w->shift;
1755 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1758 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1761 * snd_soc_dapm_get_volsw - dapm mixer get callback
1762 * @kcontrol: mixer control
1763 * @ucontrol: control element information
1765 * Callback to get the value of a dapm mixer control.
1767 * Returns 0 for success.
1769 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1770 struct snd_ctl_elem_value *ucontrol)
1772 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1773 struct soc_mixer_control *mc =
1774 (struct soc_mixer_control *)kcontrol->private_value;
1775 unsigned int reg = mc->reg;
1776 unsigned int shift = mc->shift;
1777 unsigned int rshift = mc->rshift;
1779 unsigned int invert = mc->invert;
1780 unsigned int mask = (1 << fls(max)) - 1;
1782 ucontrol->value.integer.value[0] =
1783 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1784 if (shift != rshift)
1785 ucontrol->value.integer.value[1] =
1786 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1788 ucontrol->value.integer.value[0] =
1789 max - ucontrol->value.integer.value[0];
1790 if (shift != rshift)
1791 ucontrol->value.integer.value[1] =
1792 max - ucontrol->value.integer.value[1];
1797 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1800 * snd_soc_dapm_put_volsw - dapm mixer set callback
1801 * @kcontrol: mixer control
1802 * @ucontrol: control element information
1804 * Callback to set the value of a dapm mixer control.
1806 * Returns 0 for success.
1808 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_value *ucontrol)
1811 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1812 struct soc_mixer_control *mc =
1813 (struct soc_mixer_control *)kcontrol->private_value;
1814 unsigned int reg = mc->reg;
1815 unsigned int shift = mc->shift;
1817 unsigned int mask = (1 << fls(max)) - 1;
1818 unsigned int invert = mc->invert;
1820 int connect, change;
1821 struct snd_soc_dapm_update update;
1823 val = (ucontrol->value.integer.value[0] & mask);
1827 mask = mask << shift;
1830 mutex_lock(&widget->codec->mutex);
1831 widget->value = val;
1833 change = snd_soc_test_bits(widget->codec, reg, mask, val);
1836 /* new connection */
1837 connect = invert ? 0:1;
1839 /* old connection must be powered down */
1840 connect = invert ? 1:0;
1842 update.kcontrol = kcontrol;
1843 update.widget = widget;
1847 widget->dapm->update = &update;
1849 dapm_mixer_update_power(widget, kcontrol, connect);
1851 widget->dapm->update = NULL;
1854 mutex_unlock(&widget->codec->mutex);
1857 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1860 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1861 * @kcontrol: mixer control
1862 * @ucontrol: control element information
1864 * Callback to get the value of a dapm enumerated double mixer control.
1866 * Returns 0 for success.
1868 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1869 struct snd_ctl_elem_value *ucontrol)
1871 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1872 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1873 unsigned int val, bitmask;
1875 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1877 val = snd_soc_read(widget->codec, e->reg);
1878 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1879 if (e->shift_l != e->shift_r)
1880 ucontrol->value.enumerated.item[1] =
1881 (val >> e->shift_r) & (bitmask - 1);
1885 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1888 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1889 * @kcontrol: mixer control
1890 * @ucontrol: control element information
1892 * Callback to set the value of a dapm enumerated double mixer control.
1894 * Returns 0 for success.
1896 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1897 struct snd_ctl_elem_value *ucontrol)
1899 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1900 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1901 unsigned int val, mux, change;
1902 unsigned int mask, bitmask;
1903 struct snd_soc_dapm_update update;
1905 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1907 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1909 mux = ucontrol->value.enumerated.item[0];
1910 val = mux << e->shift_l;
1911 mask = (bitmask - 1) << e->shift_l;
1912 if (e->shift_l != e->shift_r) {
1913 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1915 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1916 mask |= (bitmask - 1) << e->shift_r;
1919 mutex_lock(&widget->codec->mutex);
1920 widget->value = val;
1921 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1923 update.kcontrol = kcontrol;
1924 update.widget = widget;
1925 update.reg = e->reg;
1928 widget->dapm->update = &update;
1930 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1932 widget->dapm->update = NULL;
1934 mutex_unlock(&widget->codec->mutex);
1937 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1940 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1941 * @kcontrol: mixer control
1942 * @ucontrol: control element information
1944 * Returns 0 for success.
1946 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1947 struct snd_ctl_elem_value *ucontrol)
1949 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1951 ucontrol->value.enumerated.item[0] = widget->value;
1955 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1958 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1959 * @kcontrol: mixer control
1960 * @ucontrol: control element information
1962 * Returns 0 for success.
1964 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1965 struct snd_ctl_elem_value *ucontrol)
1967 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1968 struct soc_enum *e =
1969 (struct soc_enum *)kcontrol->private_value;
1973 if (ucontrol->value.enumerated.item[0] >= e->max)
1976 mutex_lock(&widget->codec->mutex);
1978 change = widget->value != ucontrol->value.enumerated.item[0];
1979 widget->value = ucontrol->value.enumerated.item[0];
1980 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1982 mutex_unlock(&widget->codec->mutex);
1985 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1988 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1990 * @kcontrol: mixer control
1991 * @ucontrol: control element information
1993 * Callback to get the value of a dapm semi enumerated double mixer control.
1995 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1996 * used for handling bitfield coded enumeration for example.
1998 * Returns 0 for success.
2000 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2001 struct snd_ctl_elem_value *ucontrol)
2003 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2004 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2005 unsigned int reg_val, val, mux;
2007 reg_val = snd_soc_read(widget->codec, e->reg);
2008 val = (reg_val >> e->shift_l) & e->mask;
2009 for (mux = 0; mux < e->max; mux++) {
2010 if (val == e->values[mux])
2013 ucontrol->value.enumerated.item[0] = mux;
2014 if (e->shift_l != e->shift_r) {
2015 val = (reg_val >> e->shift_r) & e->mask;
2016 for (mux = 0; mux < e->max; mux++) {
2017 if (val == e->values[mux])
2020 ucontrol->value.enumerated.item[1] = mux;
2025 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2028 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2030 * @kcontrol: mixer control
2031 * @ucontrol: control element information
2033 * Callback to set the value of a dapm semi enumerated double mixer control.
2035 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2036 * used for handling bitfield coded enumeration for example.
2038 * Returns 0 for success.
2040 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2041 struct snd_ctl_elem_value *ucontrol)
2043 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2044 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2045 unsigned int val, mux, change;
2047 struct snd_soc_dapm_update update;
2049 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2051 mux = ucontrol->value.enumerated.item[0];
2052 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2053 mask = e->mask << e->shift_l;
2054 if (e->shift_l != e->shift_r) {
2055 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2057 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2058 mask |= e->mask << e->shift_r;
2061 mutex_lock(&widget->codec->mutex);
2062 widget->value = val;
2063 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2065 update.kcontrol = kcontrol;
2066 update.widget = widget;
2067 update.reg = e->reg;
2070 widget->dapm->update = &update;
2072 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2074 widget->dapm->update = NULL;
2076 mutex_unlock(&widget->codec->mutex);
2079 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2082 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2084 * @kcontrol: mixer control
2085 * @uinfo: control element information
2087 * Callback to provide information about a pin switch control.
2089 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_info *uinfo)
2092 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2094 uinfo->value.integer.min = 0;
2095 uinfo->value.integer.max = 1;
2099 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2102 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2104 * @kcontrol: mixer control
2107 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2108 struct snd_ctl_elem_value *ucontrol)
2110 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2111 const char *pin = (const char *)kcontrol->private_value;
2113 mutex_lock(&codec->mutex);
2115 ucontrol->value.integer.value[0] =
2116 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2118 mutex_unlock(&codec->mutex);
2122 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2125 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2127 * @kcontrol: mixer control
2130 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2131 struct snd_ctl_elem_value *ucontrol)
2133 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2134 const char *pin = (const char *)kcontrol->private_value;
2136 mutex_lock(&codec->mutex);
2138 if (ucontrol->value.integer.value[0])
2139 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2141 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2143 snd_soc_dapm_sync(&codec->dapm);
2145 mutex_unlock(&codec->mutex);
2149 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2152 * snd_soc_dapm_new_control - create new dapm control
2153 * @dapm: DAPM context
2154 * @widget: widget template
2156 * Creates a new dapm control based upon the template.
2158 * Returns 0 for success else error.
2160 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2161 const struct snd_soc_dapm_widget *widget)
2163 struct snd_soc_dapm_widget *w;
2166 if ((w = dapm_cnew_widget(widget)) == NULL)
2169 name_len = strlen(widget->name) + 1;
2170 if (dapm->codec->name_prefix)
2171 name_len += 1 + strlen(dapm->codec->name_prefix);
2172 w->name = kmalloc(name_len, GFP_KERNEL);
2173 if (w->name == NULL) {
2177 if (dapm->codec->name_prefix)
2178 snprintf(w->name, name_len, "%s %s",
2179 dapm->codec->name_prefix, widget->name);
2181 snprintf(w->name, name_len, "%s", widget->name);
2185 w->codec = dapm->codec;
2186 INIT_LIST_HEAD(&w->sources);
2187 INIT_LIST_HEAD(&w->sinks);
2188 INIT_LIST_HEAD(&w->list);
2189 list_add(&w->list, &dapm->card->widgets);
2191 /* machine layer set ups unconnected pins and insertions */
2195 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2198 * snd_soc_dapm_new_controls - create new dapm controls
2199 * @dapm: DAPM context
2200 * @widget: widget array
2201 * @num: number of widgets
2203 * Creates new DAPM controls based upon the templates.
2205 * Returns 0 for success else error.
2207 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2208 const struct snd_soc_dapm_widget *widget,
2213 for (i = 0; i < num; i++) {
2214 ret = snd_soc_dapm_new_control(dapm, widget);
2217 "ASoC: Failed to create DAPM control %s: %d\n",
2225 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2227 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2228 const char *stream, int event)
2230 struct snd_soc_dapm_widget *w;
2232 list_for_each_entry(w, &dapm->card->widgets, list)
2234 if (!w->sname || w->dapm != dapm)
2236 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2237 w->name, w->sname, stream, event);
2238 if (strstr(w->sname, stream)) {
2240 case SND_SOC_DAPM_STREAM_START:
2243 case SND_SOC_DAPM_STREAM_STOP:
2246 case SND_SOC_DAPM_STREAM_SUSPEND:
2247 case SND_SOC_DAPM_STREAM_RESUME:
2248 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2249 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2255 dapm_power_widgets(dapm, event);
2259 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2260 * @rtd: PCM runtime data
2261 * @stream: stream name
2262 * @event: stream event
2264 * Sends a stream event to the dapm core. The core then makes any
2265 * necessary widget power changes.
2267 * Returns 0 for success else error.
2269 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2270 const char *stream, int event)
2272 struct snd_soc_codec *codec = rtd->codec;
2277 mutex_lock(&codec->mutex);
2278 soc_dapm_stream_event(&codec->dapm, stream, event);
2279 mutex_unlock(&codec->mutex);
2284 * snd_soc_dapm_enable_pin - enable pin.
2285 * @dapm: DAPM context
2288 * Enables input/output pin and its parents or children widgets iff there is
2289 * a valid audio route and active audio stream.
2290 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2291 * do any widget power switching.
2293 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2295 return snd_soc_dapm_set_pin(dapm, pin, 1);
2297 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2300 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2301 * @dapm: DAPM context
2304 * Enables input/output pin regardless of any other state. This is
2305 * intended for use with microphone bias supplies used in microphone
2308 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2309 * do any widget power switching.
2311 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2314 struct snd_soc_dapm_widget *w;
2316 list_for_each_entry(w, &dapm->card->widgets, list) {
2317 if (w->dapm != dapm)
2319 if (!strcmp(w->name, pin)) {
2320 dev_dbg(w->dapm->dev,
2321 "dapm: force enable pin %s\n", pin);
2328 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2331 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2334 * snd_soc_dapm_disable_pin - disable pin.
2335 * @dapm: DAPM context
2338 * Disables input/output pin and its parents or children widgets.
2339 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2340 * do any widget power switching.
2342 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2345 return snd_soc_dapm_set_pin(dapm, pin, 0);
2347 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2350 * snd_soc_dapm_nc_pin - permanently disable pin.
2351 * @dapm: DAPM context
2354 * Marks the specified pin as being not connected, disabling it along
2355 * any parent or child widgets. At present this is identical to
2356 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2357 * additional things such as disabling controls which only affect
2358 * paths through the pin.
2360 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2361 * do any widget power switching.
2363 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2365 return snd_soc_dapm_set_pin(dapm, pin, 0);
2367 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2370 * snd_soc_dapm_get_pin_status - get audio pin status
2371 * @dapm: DAPM context
2372 * @pin: audio signal pin endpoint (or start point)
2374 * Get audio pin status - connected or disconnected.
2376 * Returns 1 for connected otherwise 0.
2378 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2381 struct snd_soc_dapm_widget *w;
2383 list_for_each_entry(w, &dapm->card->widgets, list) {
2384 if (w->dapm != dapm)
2386 if (!strcmp(w->name, pin))
2387 return w->connected;
2392 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2395 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2396 * @dapm: DAPM context
2397 * @pin: audio signal pin endpoint (or start point)
2399 * Mark the given endpoint or pin as ignoring suspend. When the
2400 * system is disabled a path between two endpoints flagged as ignoring
2401 * suspend will not be disabled. The path must already be enabled via
2402 * normal means at suspend time, it will not be turned on if it was not
2405 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2408 struct snd_soc_dapm_widget *w;
2410 list_for_each_entry(w, &dapm->card->widgets, list) {
2411 if (w->dapm != dapm)
2413 if (!strcmp(w->name, pin)) {
2414 w->ignore_suspend = 1;
2419 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2422 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2425 * snd_soc_dapm_free - free dapm resources
2428 * Free all dapm widgets and resources.
2430 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2432 snd_soc_dapm_sys_remove(dapm->dev);
2433 dapm_free_widgets(dapm);
2434 list_del(&dapm->list);
2436 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2438 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2440 struct snd_soc_dapm_widget *w;
2441 LIST_HEAD(down_list);
2444 list_for_each_entry(w, &dapm->card->widgets, list) {
2445 if (w->dapm != dapm)
2448 dapm_seq_insert(w, &down_list, false);
2454 /* If there were no widgets to power down we're already in
2458 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2459 dapm_seq_run(dapm, &down_list, 0, false);
2460 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2465 * snd_soc_dapm_shutdown - callback for system shutdown
2467 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2469 struct snd_soc_codec *codec;
2471 list_for_each_entry(codec, &card->codec_dev_list, list) {
2472 soc_dapm_shutdown_codec(&codec->dapm);
2473 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
2477 /* Module information */
2478 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2479 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2480 MODULE_LICENSE("GPL");