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/initval.h>
48 #include <trace/events/asoc.h>
50 /* dapm power sequences - make this per codec in the future */
51 static int dapm_up_seq[] = {
52 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
54 [snd_soc_dapm_micbias] = 2,
55 [snd_soc_dapm_aif_in] = 3,
56 [snd_soc_dapm_aif_out] = 3,
57 [snd_soc_dapm_mic] = 4,
58 [snd_soc_dapm_mux] = 5,
59 [snd_soc_dapm_virt_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_out_drv] = 10,
67 [snd_soc_dapm_hp] = 10,
68 [snd_soc_dapm_spk] = 10,
69 [snd_soc_dapm_post] = 11,
72 static int dapm_down_seq[] = {
73 [snd_soc_dapm_pre] = 0,
74 [snd_soc_dapm_adc] = 1,
75 [snd_soc_dapm_hp] = 2,
76 [snd_soc_dapm_spk] = 2,
77 [snd_soc_dapm_out_drv] = 2,
78 [snd_soc_dapm_pga] = 4,
79 [snd_soc_dapm_mixer_named_ctl] = 5,
80 [snd_soc_dapm_mixer] = 5,
81 [snd_soc_dapm_dac] = 6,
82 [snd_soc_dapm_mic] = 7,
83 [snd_soc_dapm_micbias] = 8,
84 [snd_soc_dapm_mux] = 9,
85 [snd_soc_dapm_virt_mux] = 9,
86 [snd_soc_dapm_value_mux] = 9,
87 [snd_soc_dapm_aif_in] = 10,
88 [snd_soc_dapm_aif_out] = 10,
89 [snd_soc_dapm_supply] = 11,
90 [snd_soc_dapm_post] = 12,
93 static void pop_wait(u32 pop_time)
96 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
99 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
107 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
112 vsnprintf(buf, PAGE_SIZE, fmt, args);
113 dev_info(dev, "%s", buf);
119 /* create a new dapm widget */
120 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
121 const struct snd_soc_dapm_widget *_widget)
123 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
127 * snd_soc_dapm_set_bias_level - set the bias level for the system
128 * @card: audio device
129 * @level: level to configure
131 * Configure the bias (power) levels for the SoC audio device.
133 * Returns 0 for success else error.
135 static int snd_soc_dapm_set_bias_level(struct snd_soc_card *card,
136 struct snd_soc_dapm_context *dapm,
137 enum snd_soc_bias_level level)
142 case SND_SOC_BIAS_ON:
143 dev_dbg(dapm->dev, "Setting full bias\n");
145 case SND_SOC_BIAS_PREPARE:
146 dev_dbg(dapm->dev, "Setting bias prepare\n");
148 case SND_SOC_BIAS_STANDBY:
149 dev_dbg(dapm->dev, "Setting standby bias\n");
151 case SND_SOC_BIAS_OFF:
152 dev_dbg(dapm->dev, "Setting bias off\n");
155 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
159 trace_snd_soc_bias_level_start(card, level);
161 if (card && card->set_bias_level)
162 ret = card->set_bias_level(card, level);
164 if (dapm->codec && dapm->codec->driver->set_bias_level)
165 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
167 dapm->bias_level = level;
170 if (card && card->set_bias_level_post)
171 ret = card->set_bias_level_post(card, level);
174 trace_snd_soc_bias_level_done(card, level);
179 /* set up initial codec paths */
180 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
181 struct snd_soc_dapm_path *p, int i)
184 case snd_soc_dapm_switch:
185 case snd_soc_dapm_mixer:
186 case snd_soc_dapm_mixer_named_ctl: {
188 struct soc_mixer_control *mc = (struct soc_mixer_control *)
189 w->kcontrols[i].private_value;
190 unsigned int reg = mc->reg;
191 unsigned int shift = mc->shift;
193 unsigned int mask = (1 << fls(max)) - 1;
194 unsigned int invert = mc->invert;
196 val = snd_soc_read(w->codec, reg);
197 val = (val >> shift) & mask;
199 if ((invert && !val) || (!invert && val))
205 case snd_soc_dapm_mux: {
206 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
207 int val, item, bitmask;
209 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
211 val = snd_soc_read(w->codec, e->reg);
212 item = (val >> e->shift_l) & (bitmask - 1);
215 for (i = 0; i < e->max; i++) {
216 if (!(strcmp(p->name, e->texts[i])) && item == i)
221 case snd_soc_dapm_virt_mux: {
222 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
225 /* since a virtual mux has no backing registers to
226 * decide which path to connect, it will try to match
227 * with the first enumeration. This is to ensure
228 * that the default mux choice (the first) will be
229 * correctly powered up during initialization.
231 if (!strcmp(p->name, e->texts[0]))
235 case snd_soc_dapm_value_mux: {
236 struct soc_enum *e = (struct soc_enum *)
237 w->kcontrols[i].private_value;
240 val = snd_soc_read(w->codec, e->reg);
241 val = (val >> e->shift_l) & e->mask;
242 for (item = 0; item < e->max; item++) {
243 if (val == e->values[item])
248 for (i = 0; i < e->max; i++) {
249 if (!(strcmp(p->name, e->texts[i])) && item == i)
254 /* does not effect routing - always connected */
255 case snd_soc_dapm_pga:
256 case snd_soc_dapm_out_drv:
257 case snd_soc_dapm_output:
258 case snd_soc_dapm_adc:
259 case snd_soc_dapm_input:
260 case snd_soc_dapm_dac:
261 case snd_soc_dapm_micbias:
262 case snd_soc_dapm_vmid:
263 case snd_soc_dapm_supply:
264 case snd_soc_dapm_aif_in:
265 case snd_soc_dapm_aif_out:
268 /* does effect routing - dynamically connected */
269 case snd_soc_dapm_hp:
270 case snd_soc_dapm_mic:
271 case snd_soc_dapm_spk:
272 case snd_soc_dapm_line:
273 case snd_soc_dapm_pre:
274 case snd_soc_dapm_post:
280 /* connect mux widget to its interconnecting audio paths */
281 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
282 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
283 struct snd_soc_dapm_path *path, const char *control_name,
284 const struct snd_kcontrol_new *kcontrol)
286 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
289 for (i = 0; i < e->max; i++) {
290 if (!(strcmp(control_name, e->texts[i]))) {
291 list_add(&path->list, &dapm->card->paths);
292 list_add(&path->list_sink, &dest->sources);
293 list_add(&path->list_source, &src->sinks);
294 path->name = (char*)e->texts[i];
295 dapm_set_path_status(dest, path, 0);
303 /* connect mixer widget to its interconnecting audio paths */
304 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
305 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
306 struct snd_soc_dapm_path *path, const char *control_name)
310 /* search for mixer kcontrol */
311 for (i = 0; i < dest->num_kcontrols; i++) {
312 if (!strcmp(control_name, dest->kcontrols[i].name)) {
313 list_add(&path->list, &dapm->card->paths);
314 list_add(&path->list_sink, &dest->sources);
315 list_add(&path->list_source, &src->sinks);
316 path->name = dest->kcontrols[i].name;
317 dapm_set_path_status(dest, path, i);
324 /* update dapm codec register bits */
325 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
328 unsigned int old, new;
329 struct snd_soc_codec *codec = widget->codec;
330 struct snd_soc_dapm_context *dapm = widget->dapm;
331 struct snd_soc_card *card = dapm->card;
333 /* check for valid widgets */
334 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
335 widget->id == snd_soc_dapm_output ||
336 widget->id == snd_soc_dapm_hp ||
337 widget->id == snd_soc_dapm_mic ||
338 widget->id == snd_soc_dapm_line ||
339 widget->id == snd_soc_dapm_spk)
342 power = widget->power;
344 power = (power ? 0:1);
346 old = snd_soc_read(codec, widget->reg);
347 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
351 pop_dbg(dapm->dev, card->pop_time,
352 "pop test %s : %s in %d ms\n",
353 widget->name, widget->power ? "on" : "off",
355 pop_wait(card->pop_time);
356 snd_soc_write(codec, widget->reg, new);
358 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
363 /* create new dapm mixer control */
364 static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
365 struct snd_soc_dapm_widget *w)
369 struct snd_soc_dapm_path *path;
370 struct snd_card *card = dapm->codec->card->snd_card;
373 for (i = 0; i < w->num_kcontrols; i++) {
376 list_for_each_entry(path, &w->sources, list_sink) {
378 /* mixer/mux paths name must match control name */
379 if (path->name != (char*)w->kcontrols[i].name)
382 /* add dapm control with long name.
383 * for dapm_mixer this is the concatenation of the
384 * mixer and kcontrol name.
385 * for dapm_mixer_named_ctl this is simply the
388 name_len = strlen(w->kcontrols[i].name) + 1;
389 if (w->id != snd_soc_dapm_mixer_named_ctl)
390 name_len += 1 + strlen(w->name);
392 path->long_name = kmalloc(name_len, GFP_KERNEL);
394 if (path->long_name == NULL)
399 snprintf(path->long_name, name_len, "%s %s",
400 w->name, w->kcontrols[i].name);
402 case snd_soc_dapm_mixer_named_ctl:
403 snprintf(path->long_name, name_len, "%s",
404 w->kcontrols[i].name);
408 path->long_name[name_len - 1] = '\0';
410 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
412 ret = snd_ctl_add(card, path->kcontrol);
415 "asoc: failed to add dapm kcontrol %s: %d\n",
416 path->long_name, ret);
417 kfree(path->long_name);
418 path->long_name = NULL;
426 /* create new dapm mux control */
427 static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
428 struct snd_soc_dapm_widget *w)
430 struct snd_soc_dapm_path *path = NULL;
431 struct snd_kcontrol *kcontrol;
432 struct snd_card *card = dapm->codec->card->snd_card;
435 if (!w->num_kcontrols) {
436 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
440 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
441 ret = snd_ctl_add(card, kcontrol);
446 list_for_each_entry(path, &w->sources, list_sink)
447 path->kcontrol = kcontrol;
452 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
456 /* create new dapm volume control */
457 static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
458 struct snd_soc_dapm_widget *w)
460 if (w->num_kcontrols)
461 dev_err(w->dapm->dev,
462 "asoc: PGA controls not supported: '%s'\n", w->name);
467 /* reset 'walked' bit for each dapm path */
468 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
470 struct snd_soc_dapm_path *p;
472 list_for_each_entry(p, &dapm->card->paths, list)
476 /* We implement power down on suspend by checking the power state of
477 * the ALSA card - when we are suspending the ALSA state for the card
480 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
482 int level = snd_power_get_state(widget->dapm->codec->card->snd_card);
485 case SNDRV_CTL_POWER_D3hot:
486 case SNDRV_CTL_POWER_D3cold:
487 if (widget->ignore_suspend)
488 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
490 return widget->ignore_suspend;
497 * Recursively check for a completed path to an active or physically connected
498 * output widget. Returns number of complete paths.
500 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
502 struct snd_soc_dapm_path *path;
505 if (widget->id == snd_soc_dapm_supply)
508 switch (widget->id) {
509 case snd_soc_dapm_adc:
510 case snd_soc_dapm_aif_out:
512 return snd_soc_dapm_suspend_check(widget);
517 if (widget->connected) {
518 /* connected pin ? */
519 if (widget->id == snd_soc_dapm_output && !widget->ext)
520 return snd_soc_dapm_suspend_check(widget);
522 /* connected jack or spk ? */
523 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
524 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
525 return snd_soc_dapm_suspend_check(widget);
528 list_for_each_entry(path, &widget->sinks, list_source) {
532 if (path->sink && path->connect) {
534 con += is_connected_output_ep(path->sink);
542 * Recursively check for a completed path to an active or physically connected
543 * input widget. Returns number of complete paths.
545 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
547 struct snd_soc_dapm_path *path;
550 if (widget->id == snd_soc_dapm_supply)
553 /* active stream ? */
554 switch (widget->id) {
555 case snd_soc_dapm_dac:
556 case snd_soc_dapm_aif_in:
558 return snd_soc_dapm_suspend_check(widget);
563 if (widget->connected) {
564 /* connected pin ? */
565 if (widget->id == snd_soc_dapm_input && !widget->ext)
566 return snd_soc_dapm_suspend_check(widget);
568 /* connected VMID/Bias for lower pops */
569 if (widget->id == snd_soc_dapm_vmid)
570 return snd_soc_dapm_suspend_check(widget);
572 /* connected jack ? */
573 if (widget->id == snd_soc_dapm_mic ||
574 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
575 return snd_soc_dapm_suspend_check(widget);
578 list_for_each_entry(path, &widget->sources, list_sink) {
582 if (path->source && path->connect) {
584 con += is_connected_input_ep(path->source);
592 * Handler for generic register modifier widget.
594 int dapm_reg_event(struct snd_soc_dapm_widget *w,
595 struct snd_kcontrol *kcontrol, int event)
599 if (SND_SOC_DAPM_EVENT_ON(event))
604 snd_soc_update_bits(w->codec, -(w->reg + 1),
605 w->mask << w->shift, val << w->shift);
609 EXPORT_SYMBOL_GPL(dapm_reg_event);
611 /* Standard power change method, used to apply power changes to most
614 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
618 /* call any power change event handlers */
620 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
621 w->power ? "on" : "off",
622 w->name, w->event_flags);
624 /* power up pre event */
625 if (w->power && w->event &&
626 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
627 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
632 /* power down pre event */
633 if (!w->power && w->event &&
634 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
635 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
642 /* power up post event */
643 if (w->power && w->event &&
644 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
646 NULL, SND_SOC_DAPM_POST_PMU);
651 /* power down post event */
652 if (!w->power && w->event &&
653 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
654 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
662 /* Generic check to see if a widget should be powered.
664 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
668 in = is_connected_input_ep(w);
669 dapm_clear_walk(w->dapm);
670 out = is_connected_output_ep(w);
671 dapm_clear_walk(w->dapm);
672 return out != 0 && in != 0;
675 /* Check to see if an ADC has power */
676 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
681 in = is_connected_input_ep(w);
682 dapm_clear_walk(w->dapm);
685 return dapm_generic_check_power(w);
689 /* Check to see if a DAC has power */
690 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
695 out = is_connected_output_ep(w);
696 dapm_clear_walk(w->dapm);
699 return dapm_generic_check_power(w);
703 /* Check to see if a power supply is needed */
704 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
706 struct snd_soc_dapm_path *path;
709 /* Check if one of our outputs is connected */
710 list_for_each_entry(path, &w->sinks, list_source) {
711 if (path->connected &&
712 !path->connected(path->source, path->sink))
718 if (path->sink->force) {
723 if (path->sink->power_check &&
724 path->sink->power_check(path->sink)) {
730 dapm_clear_walk(w->dapm);
735 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
736 struct snd_soc_dapm_widget *b,
744 sort = dapm_down_seq;
746 if (sort[a->id] != sort[b->id])
747 return sort[a->id] - sort[b->id];
748 if (a->subseq != b->subseq) {
750 return a->subseq - b->subseq;
752 return b->subseq - a->subseq;
754 if (a->reg != b->reg)
755 return a->reg - b->reg;
756 if (a->dapm != b->dapm)
757 return (unsigned long)a->dapm - (unsigned long)b->dapm;
762 /* Insert a widget in order into a DAPM power sequence. */
763 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
764 struct list_head *list,
767 struct snd_soc_dapm_widget *w;
769 list_for_each_entry(w, list, power_list)
770 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
771 list_add_tail(&new_widget->power_list, &w->power_list);
775 list_add_tail(&new_widget->power_list, list);
778 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
779 struct snd_soc_dapm_widget *w, int event)
781 struct snd_soc_card *card = dapm->card;
786 case SND_SOC_DAPM_PRE_PMU:
790 case SND_SOC_DAPM_POST_PMU:
791 ev_name = "POST_PMU";
794 case SND_SOC_DAPM_PRE_PMD:
798 case SND_SOC_DAPM_POST_PMD:
799 ev_name = "POST_PMD";
807 if (w->power != power)
810 if (w->event && (w->event_flags & event)) {
811 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
813 trace_snd_soc_dapm_widget_event_start(w, event);
814 ret = w->event(w, NULL, event);
815 trace_snd_soc_dapm_widget_event_done(w, event);
817 pr_err("%s: %s event failed: %d\n",
818 ev_name, w->name, ret);
822 /* Apply the coalesced changes from a DAPM sequence */
823 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
824 struct list_head *pending)
826 struct snd_soc_card *card = dapm->card;
827 struct snd_soc_dapm_widget *w;
829 unsigned int value = 0;
830 unsigned int mask = 0;
831 unsigned int cur_mask;
833 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
836 list_for_each_entry(w, pending, power_list) {
837 cur_mask = 1 << w->shift;
838 BUG_ON(reg != w->reg);
849 pop_dbg(dapm->dev, card->pop_time,
850 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
851 w->name, reg, value, mask);
853 /* Check for events */
854 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
855 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
859 pop_dbg(dapm->dev, card->pop_time,
860 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
861 value, mask, reg, card->pop_time);
862 pop_wait(card->pop_time);
863 snd_soc_update_bits(dapm->codec, reg, mask, value);
866 list_for_each_entry(w, pending, power_list) {
867 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
868 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
872 /* Apply a DAPM power sequence.
874 * We walk over a pre-sorted list of widgets to apply power to. In
875 * order to minimise the number of writes to the device required
876 * multiple widgets will be updated in a single write where possible.
877 * Currently anything that requires more than a single write is not
880 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
881 struct list_head *list, int event, bool power_up)
883 struct snd_soc_dapm_widget *w, *n;
887 int cur_reg = SND_SOC_NOPM;
888 struct snd_soc_dapm_context *cur_dapm = NULL;
895 sort = dapm_down_seq;
897 list_for_each_entry_safe(w, n, list, power_list) {
900 /* Do we need to apply any queued changes? */
901 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
902 w->dapm != cur_dapm || w->subseq != cur_subseq) {
903 if (!list_empty(&pending))
904 dapm_seq_run_coalesced(cur_dapm, &pending);
906 if (cur_dapm && cur_dapm->seq_notifier) {
907 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
908 if (sort[i] == cur_sort)
909 cur_dapm->seq_notifier(cur_dapm,
914 INIT_LIST_HEAD(&pending);
917 cur_reg = SND_SOC_NOPM;
922 case snd_soc_dapm_pre:
924 list_for_each_entry_safe_continue(w, n, list,
927 if (event == SND_SOC_DAPM_STREAM_START)
929 NULL, SND_SOC_DAPM_PRE_PMU);
930 else if (event == SND_SOC_DAPM_STREAM_STOP)
932 NULL, SND_SOC_DAPM_PRE_PMD);
935 case snd_soc_dapm_post:
937 list_for_each_entry_safe_continue(w, n, list,
940 if (event == SND_SOC_DAPM_STREAM_START)
942 NULL, SND_SOC_DAPM_POST_PMU);
943 else if (event == SND_SOC_DAPM_STREAM_STOP)
945 NULL, SND_SOC_DAPM_POST_PMD);
948 case snd_soc_dapm_input:
949 case snd_soc_dapm_output:
950 case snd_soc_dapm_hp:
951 case snd_soc_dapm_mic:
952 case snd_soc_dapm_line:
953 case snd_soc_dapm_spk:
954 /* No register support currently */
955 ret = dapm_generic_apply_power(w);
959 /* Queue it up for application */
960 cur_sort = sort[w->id];
961 cur_subseq = w->subseq;
964 list_move(&w->power_list, &pending);
969 dev_err(w->dapm->dev,
970 "Failed to apply widget power: %d\n", ret);
973 if (!list_empty(&pending))
974 dapm_seq_run_coalesced(dapm, &pending);
976 if (cur_dapm && cur_dapm->seq_notifier) {
977 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
978 if (sort[i] == cur_sort)
979 cur_dapm->seq_notifier(cur_dapm,
984 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
986 struct snd_soc_dapm_update *update = dapm->update;
987 struct snd_soc_dapm_widget *w;
996 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
997 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
999 pr_err("%s DAPM pre-event failed: %d\n",
1003 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1006 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1009 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1010 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1012 pr_err("%s DAPM post-event failed: %d\n",
1020 * Scan each dapm widget for complete audio path.
1021 * A complete path is a route that has valid endpoints i.e.:-
1023 * o DAC to output pin.
1024 * o Input Pin to ADC.
1025 * o Input pin to Output pin (bypass, sidetone)
1026 * o DAC to ADC (loopback).
1028 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1030 struct snd_soc_card *card = dapm->codec->card;
1031 struct snd_soc_dapm_widget *w;
1032 struct snd_soc_dapm_context *d;
1034 LIST_HEAD(down_list);
1038 trace_snd_soc_dapm_start(card);
1040 list_for_each_entry(d, &card->dapm_list, list)
1044 /* Check which widgets we need to power and store them in
1045 * lists indicating if they should be powered up or down.
1047 list_for_each_entry(w, &card->widgets, list) {
1049 case snd_soc_dapm_pre:
1050 dapm_seq_insert(w, &down_list, false);
1052 case snd_soc_dapm_post:
1053 dapm_seq_insert(w, &up_list, true);
1057 if (!w->power_check)
1061 power = w->power_check(w);
1065 w->dapm->dev_power = 1;
1067 if (w->power == power)
1070 trace_snd_soc_dapm_widget_power(w, power);
1073 dapm_seq_insert(w, &up_list, true);
1075 dapm_seq_insert(w, &down_list, false);
1082 /* If there are no DAPM widgets then try to figure out power from the
1085 if (!dapm->n_widgets) {
1087 case SND_SOC_DAPM_STREAM_START:
1088 case SND_SOC_DAPM_STREAM_RESUME:
1089 dapm->dev_power = 1;
1091 case SND_SOC_DAPM_STREAM_STOP:
1092 dapm->dev_power = !!dapm->codec->active;
1094 case SND_SOC_DAPM_STREAM_SUSPEND:
1095 dapm->dev_power = 0;
1097 case SND_SOC_DAPM_STREAM_NOP:
1098 switch (dapm->bias_level) {
1099 case SND_SOC_BIAS_STANDBY:
1100 case SND_SOC_BIAS_OFF:
1101 dapm->dev_power = 0;
1104 dapm->dev_power = 1;
1113 list_for_each_entry(d, &dapm->card->dapm_list, list) {
1114 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1115 ret = snd_soc_dapm_set_bias_level(card, d,
1116 SND_SOC_BIAS_STANDBY);
1119 "Failed to turn on bias: %d\n", ret);
1122 /* If we're changing to all on or all off then prepare */
1123 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1124 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1125 ret = snd_soc_dapm_set_bias_level(card, d,
1126 SND_SOC_BIAS_PREPARE);
1129 "Failed to prepare bias: %d\n", ret);
1133 /* Power down widgets first; try to avoid amplifying pops. */
1134 dapm_seq_run(dapm, &down_list, event, false);
1136 dapm_widget_update(dapm);
1139 dapm_seq_run(dapm, &up_list, event, true);
1141 list_for_each_entry(d, &dapm->card->dapm_list, list) {
1142 /* If we just powered the last thing off drop to standby bias */
1143 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1144 ret = snd_soc_dapm_set_bias_level(card, d,
1145 SND_SOC_BIAS_STANDBY);
1148 "Failed to apply standby bias: %d\n",
1152 /* If we're in standby and can support bias off then do that */
1153 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1155 ret = snd_soc_dapm_set_bias_level(card, d,
1159 "Failed to turn off bias: %d\n", ret);
1162 /* If we just powered up then move to active bias */
1163 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1164 ret = snd_soc_dapm_set_bias_level(card, d,
1168 "Failed to apply active bias: %d\n",
1173 pop_dbg(dapm->dev, card->pop_time,
1174 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1175 pop_wait(card->pop_time);
1177 trace_snd_soc_dapm_done(card);
1182 #ifdef CONFIG_DEBUG_FS
1183 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1185 file->private_data = inode->i_private;
1189 static ssize_t dapm_widget_power_read_file(struct file *file,
1190 char __user *user_buf,
1191 size_t count, loff_t *ppos)
1193 struct snd_soc_dapm_widget *w = file->private_data;
1197 struct snd_soc_dapm_path *p = NULL;
1199 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1203 in = is_connected_input_ep(w);
1204 dapm_clear_walk(w->dapm);
1205 out = is_connected_output_ep(w);
1206 dapm_clear_walk(w->dapm);
1208 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1209 w->name, w->power ? "On" : "Off", in, out);
1212 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1213 " - R%d(0x%x) bit %d",
1214 w->reg, w->reg, w->shift);
1216 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1219 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1221 w->active ? "active" : "inactive");
1223 list_for_each_entry(p, &w->sources, list_sink) {
1224 if (p->connected && !p->connected(w, p->sink))
1228 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1230 p->name ? p->name : "static",
1233 list_for_each_entry(p, &w->sinks, list_source) {
1234 if (p->connected && !p->connected(w, p->sink))
1238 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1240 p->name ? p->name : "static",
1244 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1250 static const struct file_operations dapm_widget_power_fops = {
1251 .open = dapm_widget_power_open_file,
1252 .read = dapm_widget_power_read_file,
1253 .llseek = default_llseek,
1256 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1258 struct snd_soc_dapm_widget *w;
1261 if (!dapm->debugfs_dapm)
1264 list_for_each_entry(w, &dapm->card->widgets, list) {
1265 if (!w->name || w->dapm != dapm)
1268 d = debugfs_create_file(w->name, 0444,
1269 dapm->debugfs_dapm, w,
1270 &dapm_widget_power_fops);
1272 dev_warn(w->dapm->dev,
1273 "ASoC: Failed to create %s debugfs file\n",
1278 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1283 /* test and update the power status of a mux widget */
1284 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1285 struct snd_kcontrol *kcontrol, int change,
1286 int mux, struct soc_enum *e)
1288 struct snd_soc_dapm_path *path;
1291 if (widget->id != snd_soc_dapm_mux &&
1292 widget->id != snd_soc_dapm_virt_mux &&
1293 widget->id != snd_soc_dapm_value_mux)
1299 /* find dapm widget path assoc with kcontrol */
1300 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1301 if (path->kcontrol != kcontrol)
1304 if (!path->name || !e->texts[mux])
1308 /* we now need to match the string in the enum to the path */
1309 if (!(strcmp(path->name, e->texts[mux])))
1310 path->connect = 1; /* new connection */
1312 path->connect = 0; /* old connection must be powered down */
1316 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1321 /* test and update the power status of a mixer or switch widget */
1322 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1323 struct snd_kcontrol *kcontrol, int connect)
1325 struct snd_soc_dapm_path *path;
1328 if (widget->id != snd_soc_dapm_mixer &&
1329 widget->id != snd_soc_dapm_mixer_named_ctl &&
1330 widget->id != snd_soc_dapm_switch)
1333 /* find dapm widget path assoc with kcontrol */
1334 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1335 if (path->kcontrol != kcontrol)
1338 /* found, now check type */
1340 path->connect = connect;
1345 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1350 /* show dapm widget status in sys fs */
1351 static ssize_t dapm_widget_show(struct device *dev,
1352 struct device_attribute *attr, char *buf)
1354 struct snd_soc_pcm_runtime *rtd =
1355 container_of(dev, struct snd_soc_pcm_runtime, dev);
1356 struct snd_soc_codec *codec =rtd->codec;
1357 struct snd_soc_dapm_widget *w;
1359 char *state = "not set";
1361 list_for_each_entry(w, &codec->card->widgets, list) {
1362 if (w->dapm != &codec->dapm)
1365 /* only display widgets that burnm power */
1367 case snd_soc_dapm_hp:
1368 case snd_soc_dapm_mic:
1369 case snd_soc_dapm_spk:
1370 case snd_soc_dapm_line:
1371 case snd_soc_dapm_micbias:
1372 case snd_soc_dapm_dac:
1373 case snd_soc_dapm_adc:
1374 case snd_soc_dapm_pga:
1375 case snd_soc_dapm_out_drv:
1376 case snd_soc_dapm_mixer:
1377 case snd_soc_dapm_mixer_named_ctl:
1378 case snd_soc_dapm_supply:
1380 count += sprintf(buf + count, "%s: %s\n",
1381 w->name, w->power ? "On":"Off");
1388 switch (codec->dapm.bias_level) {
1389 case SND_SOC_BIAS_ON:
1392 case SND_SOC_BIAS_PREPARE:
1395 case SND_SOC_BIAS_STANDBY:
1398 case SND_SOC_BIAS_OFF:
1402 count += sprintf(buf + count, "PM State: %s\n", state);
1407 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1409 int snd_soc_dapm_sys_add(struct device *dev)
1411 return device_create_file(dev, &dev_attr_dapm_widget);
1414 static void snd_soc_dapm_sys_remove(struct device *dev)
1416 device_remove_file(dev, &dev_attr_dapm_widget);
1419 /* free all dapm widgets and resources */
1420 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1422 struct snd_soc_dapm_widget *w, *next_w;
1423 struct snd_soc_dapm_path *p, *next_p;
1425 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1426 if (w->dapm != dapm)
1430 * remove source and sink paths associated to this widget.
1431 * While removing the path, remove reference to it from both
1432 * source and sink widgets so that path is removed only once.
1434 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1435 list_del(&p->list_sink);
1436 list_del(&p->list_source);
1438 kfree(p->long_name);
1441 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1442 list_del(&p->list_sink);
1443 list_del(&p->list_source);
1445 kfree(p->long_name);
1453 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1454 const char *pin, int status)
1456 struct snd_soc_dapm_widget *w;
1458 list_for_each_entry(w, &dapm->card->widgets, list) {
1459 if (w->dapm != dapm)
1461 if (!strcmp(w->name, pin)) {
1462 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1464 w->connected = status;
1465 /* Allow disabling of forced pins */
1472 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1477 * snd_soc_dapm_sync - scan and power dapm paths
1478 * @dapm: DAPM context
1480 * Walks all dapm audio paths and powers widgets according to their
1481 * stream or path usage.
1483 * Returns 0 for success.
1485 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1487 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1489 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1491 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1492 const struct snd_soc_dapm_route *route)
1494 struct snd_soc_dapm_path *path;
1495 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1496 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1498 const char *control = route->control;
1500 char prefixed_sink[80];
1501 char prefixed_source[80];
1504 if (dapm->codec->name_prefix) {
1505 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1506 dapm->codec->name_prefix, route->sink);
1507 sink = prefixed_sink;
1508 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1509 dapm->codec->name_prefix, route->source);
1510 source = prefixed_source;
1513 source = route->source;
1517 * find src and dest widgets over all widgets but favor a widget from
1518 * current DAPM context
1520 list_for_each_entry(w, &dapm->card->widgets, list) {
1521 if (!wsink && !(strcmp(w->name, sink))) {
1523 if (w->dapm == dapm)
1527 if (!wsource && !(strcmp(w->name, source))) {
1529 if (w->dapm == dapm)
1533 /* use widget from another DAPM context if not found from this */
1539 if (wsource == NULL || wsink == NULL)
1542 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1546 path->source = wsource;
1548 path->connected = route->connected;
1549 INIT_LIST_HEAD(&path->list);
1550 INIT_LIST_HEAD(&path->list_source);
1551 INIT_LIST_HEAD(&path->list_sink);
1553 /* check for external widgets */
1554 if (wsink->id == snd_soc_dapm_input) {
1555 if (wsource->id == snd_soc_dapm_micbias ||
1556 wsource->id == snd_soc_dapm_mic ||
1557 wsource->id == snd_soc_dapm_line ||
1558 wsource->id == snd_soc_dapm_output)
1561 if (wsource->id == snd_soc_dapm_output) {
1562 if (wsink->id == snd_soc_dapm_spk ||
1563 wsink->id == snd_soc_dapm_hp ||
1564 wsink->id == snd_soc_dapm_line ||
1565 wsink->id == snd_soc_dapm_input)
1569 /* connect static paths */
1570 if (control == NULL) {
1571 list_add(&path->list, &dapm->card->paths);
1572 list_add(&path->list_sink, &wsink->sources);
1573 list_add(&path->list_source, &wsource->sinks);
1578 /* connect dynamic paths */
1580 case snd_soc_dapm_adc:
1581 case snd_soc_dapm_dac:
1582 case snd_soc_dapm_pga:
1583 case snd_soc_dapm_out_drv:
1584 case snd_soc_dapm_input:
1585 case snd_soc_dapm_output:
1586 case snd_soc_dapm_micbias:
1587 case snd_soc_dapm_vmid:
1588 case snd_soc_dapm_pre:
1589 case snd_soc_dapm_post:
1590 case snd_soc_dapm_supply:
1591 case snd_soc_dapm_aif_in:
1592 case snd_soc_dapm_aif_out:
1593 list_add(&path->list, &dapm->card->paths);
1594 list_add(&path->list_sink, &wsink->sources);
1595 list_add(&path->list_source, &wsource->sinks);
1598 case snd_soc_dapm_mux:
1599 case snd_soc_dapm_virt_mux:
1600 case snd_soc_dapm_value_mux:
1601 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1602 &wsink->kcontrols[0]);
1606 case snd_soc_dapm_switch:
1607 case snd_soc_dapm_mixer:
1608 case snd_soc_dapm_mixer_named_ctl:
1609 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1613 case snd_soc_dapm_hp:
1614 case snd_soc_dapm_mic:
1615 case snd_soc_dapm_line:
1616 case snd_soc_dapm_spk:
1617 list_add(&path->list, &dapm->card->paths);
1618 list_add(&path->list_sink, &wsink->sources);
1619 list_add(&path->list_source, &wsource->sinks);
1626 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1627 source, control, sink);
1633 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1634 * @dapm: DAPM context
1635 * @route: audio routes
1636 * @num: number of routes
1638 * Connects 2 dapm widgets together via a named audio path. The sink is
1639 * the widget receiving the audio signal, whilst the source is the sender
1640 * of the audio signal.
1642 * Returns 0 for success else error. On error all resources can be freed
1643 * with a call to snd_soc_card_free().
1645 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1646 const struct snd_soc_dapm_route *route, int num)
1650 for (i = 0; i < num; i++) {
1651 ret = snd_soc_dapm_add_route(dapm, route);
1653 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1654 route->source, route->sink);
1662 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1665 * snd_soc_dapm_new_widgets - add new dapm widgets
1666 * @dapm: DAPM context
1668 * Checks the codec for any new dapm widgets and creates them if found.
1670 * Returns 0 for success.
1672 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1674 struct snd_soc_dapm_widget *w;
1677 list_for_each_entry(w, &dapm->card->widgets, list)
1683 case snd_soc_dapm_switch:
1684 case snd_soc_dapm_mixer:
1685 case snd_soc_dapm_mixer_named_ctl:
1686 w->power_check = dapm_generic_check_power;
1687 dapm_new_mixer(dapm, w);
1689 case snd_soc_dapm_mux:
1690 case snd_soc_dapm_virt_mux:
1691 case snd_soc_dapm_value_mux:
1692 w->power_check = dapm_generic_check_power;
1693 dapm_new_mux(dapm, w);
1695 case snd_soc_dapm_adc:
1696 case snd_soc_dapm_aif_out:
1697 w->power_check = dapm_adc_check_power;
1699 case snd_soc_dapm_dac:
1700 case snd_soc_dapm_aif_in:
1701 w->power_check = dapm_dac_check_power;
1703 case snd_soc_dapm_pga:
1704 case snd_soc_dapm_out_drv:
1705 w->power_check = dapm_generic_check_power;
1706 dapm_new_pga(dapm, w);
1708 case snd_soc_dapm_input:
1709 case snd_soc_dapm_output:
1710 case snd_soc_dapm_micbias:
1711 case snd_soc_dapm_spk:
1712 case snd_soc_dapm_hp:
1713 case snd_soc_dapm_mic:
1714 case snd_soc_dapm_line:
1715 w->power_check = dapm_generic_check_power;
1717 case snd_soc_dapm_supply:
1718 w->power_check = dapm_supply_check_power;
1719 case snd_soc_dapm_vmid:
1720 case snd_soc_dapm_pre:
1721 case snd_soc_dapm_post:
1725 /* Read the initial power state from the device */
1727 val = snd_soc_read(w->codec, w->reg);
1728 val &= 1 << w->shift;
1739 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1742 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1745 * snd_soc_dapm_get_volsw - dapm mixer get callback
1746 * @kcontrol: mixer control
1747 * @ucontrol: control element information
1749 * Callback to get the value of a dapm mixer control.
1751 * Returns 0 for success.
1753 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1754 struct snd_ctl_elem_value *ucontrol)
1756 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1757 struct soc_mixer_control *mc =
1758 (struct soc_mixer_control *)kcontrol->private_value;
1759 unsigned int reg = mc->reg;
1760 unsigned int shift = mc->shift;
1761 unsigned int rshift = mc->rshift;
1763 unsigned int invert = mc->invert;
1764 unsigned int mask = (1 << fls(max)) - 1;
1766 ucontrol->value.integer.value[0] =
1767 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1768 if (shift != rshift)
1769 ucontrol->value.integer.value[1] =
1770 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1772 ucontrol->value.integer.value[0] =
1773 max - ucontrol->value.integer.value[0];
1774 if (shift != rshift)
1775 ucontrol->value.integer.value[1] =
1776 max - ucontrol->value.integer.value[1];
1781 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1784 * snd_soc_dapm_put_volsw - dapm mixer set callback
1785 * @kcontrol: mixer control
1786 * @ucontrol: control element information
1788 * Callback to set the value of a dapm mixer control.
1790 * Returns 0 for success.
1792 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1793 struct snd_ctl_elem_value *ucontrol)
1795 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1796 struct soc_mixer_control *mc =
1797 (struct soc_mixer_control *)kcontrol->private_value;
1798 unsigned int reg = mc->reg;
1799 unsigned int shift = mc->shift;
1801 unsigned int mask = (1 << fls(max)) - 1;
1802 unsigned int invert = mc->invert;
1804 int connect, change;
1805 struct snd_soc_dapm_update update;
1807 val = (ucontrol->value.integer.value[0] & mask);
1811 mask = mask << shift;
1814 mutex_lock(&widget->codec->mutex);
1815 widget->value = val;
1817 change = snd_soc_test_bits(widget->codec, reg, mask, val);
1820 /* new connection */
1821 connect = invert ? 0:1;
1823 /* old connection must be powered down */
1824 connect = invert ? 1:0;
1826 update.kcontrol = kcontrol;
1827 update.widget = widget;
1831 widget->dapm->update = &update;
1833 dapm_mixer_update_power(widget, kcontrol, connect);
1835 widget->dapm->update = NULL;
1838 mutex_unlock(&widget->codec->mutex);
1841 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1844 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1845 * @kcontrol: mixer control
1846 * @ucontrol: control element information
1848 * Callback to get the value of a dapm enumerated double mixer control.
1850 * Returns 0 for success.
1852 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1853 struct snd_ctl_elem_value *ucontrol)
1855 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1856 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1857 unsigned int val, bitmask;
1859 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1861 val = snd_soc_read(widget->codec, e->reg);
1862 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1863 if (e->shift_l != e->shift_r)
1864 ucontrol->value.enumerated.item[1] =
1865 (val >> e->shift_r) & (bitmask - 1);
1869 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1872 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1873 * @kcontrol: mixer control
1874 * @ucontrol: control element information
1876 * Callback to set the value of a dapm enumerated double mixer control.
1878 * Returns 0 for success.
1880 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1881 struct snd_ctl_elem_value *ucontrol)
1883 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1884 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1885 unsigned int val, mux, change;
1886 unsigned int mask, bitmask;
1887 struct snd_soc_dapm_update update;
1889 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1891 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1893 mux = ucontrol->value.enumerated.item[0];
1894 val = mux << e->shift_l;
1895 mask = (bitmask - 1) << e->shift_l;
1896 if (e->shift_l != e->shift_r) {
1897 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1899 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1900 mask |= (bitmask - 1) << e->shift_r;
1903 mutex_lock(&widget->codec->mutex);
1904 widget->value = val;
1905 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1907 update.kcontrol = kcontrol;
1908 update.widget = widget;
1909 update.reg = e->reg;
1912 widget->dapm->update = &update;
1914 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1916 widget->dapm->update = NULL;
1918 mutex_unlock(&widget->codec->mutex);
1921 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1924 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1925 * @kcontrol: mixer control
1926 * @ucontrol: control element information
1928 * Returns 0 for success.
1930 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1931 struct snd_ctl_elem_value *ucontrol)
1933 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1935 ucontrol->value.enumerated.item[0] = widget->value;
1939 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1942 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1943 * @kcontrol: mixer control
1944 * @ucontrol: control element information
1946 * Returns 0 for success.
1948 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1949 struct snd_ctl_elem_value *ucontrol)
1951 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1952 struct soc_enum *e =
1953 (struct soc_enum *)kcontrol->private_value;
1957 if (ucontrol->value.enumerated.item[0] >= e->max)
1960 mutex_lock(&widget->codec->mutex);
1962 change = widget->value != ucontrol->value.enumerated.item[0];
1963 widget->value = ucontrol->value.enumerated.item[0];
1964 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1966 mutex_unlock(&widget->codec->mutex);
1969 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1972 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1974 * @kcontrol: mixer control
1975 * @ucontrol: control element information
1977 * Callback to get the value of a dapm semi enumerated double mixer control.
1979 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1980 * used for handling bitfield coded enumeration for example.
1982 * Returns 0 for success.
1984 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1985 struct snd_ctl_elem_value *ucontrol)
1987 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1988 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1989 unsigned int reg_val, val, mux;
1991 reg_val = snd_soc_read(widget->codec, e->reg);
1992 val = (reg_val >> e->shift_l) & e->mask;
1993 for (mux = 0; mux < e->max; mux++) {
1994 if (val == e->values[mux])
1997 ucontrol->value.enumerated.item[0] = mux;
1998 if (e->shift_l != e->shift_r) {
1999 val = (reg_val >> e->shift_r) & e->mask;
2000 for (mux = 0; mux < e->max; mux++) {
2001 if (val == e->values[mux])
2004 ucontrol->value.enumerated.item[1] = mux;
2009 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2012 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2014 * @kcontrol: mixer control
2015 * @ucontrol: control element information
2017 * Callback to set the value of a dapm semi enumerated double mixer control.
2019 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2020 * used for handling bitfield coded enumeration for example.
2022 * Returns 0 for success.
2024 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2025 struct snd_ctl_elem_value *ucontrol)
2027 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2028 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2029 unsigned int val, mux, change;
2031 struct snd_soc_dapm_update update;
2033 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2035 mux = ucontrol->value.enumerated.item[0];
2036 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2037 mask = e->mask << e->shift_l;
2038 if (e->shift_l != e->shift_r) {
2039 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2041 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2042 mask |= e->mask << e->shift_r;
2045 mutex_lock(&widget->codec->mutex);
2046 widget->value = val;
2047 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2049 update.kcontrol = kcontrol;
2050 update.widget = widget;
2051 update.reg = e->reg;
2054 widget->dapm->update = &update;
2056 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2058 widget->dapm->update = NULL;
2060 mutex_unlock(&widget->codec->mutex);
2063 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2066 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2068 * @kcontrol: mixer control
2069 * @uinfo: control element information
2071 * Callback to provide information about a pin switch control.
2073 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2074 struct snd_ctl_elem_info *uinfo)
2076 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2078 uinfo->value.integer.min = 0;
2079 uinfo->value.integer.max = 1;
2083 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2086 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2088 * @kcontrol: mixer control
2091 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2092 struct snd_ctl_elem_value *ucontrol)
2094 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2095 const char *pin = (const char *)kcontrol->private_value;
2097 mutex_lock(&codec->mutex);
2099 ucontrol->value.integer.value[0] =
2100 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2102 mutex_unlock(&codec->mutex);
2106 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2109 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2111 * @kcontrol: mixer control
2114 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_value *ucontrol)
2117 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2118 const char *pin = (const char *)kcontrol->private_value;
2120 mutex_lock(&codec->mutex);
2122 if (ucontrol->value.integer.value[0])
2123 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2125 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2127 snd_soc_dapm_sync(&codec->dapm);
2129 mutex_unlock(&codec->mutex);
2133 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2136 * snd_soc_dapm_new_control - create new dapm control
2137 * @dapm: DAPM context
2138 * @widget: widget template
2140 * Creates a new dapm control based upon the template.
2142 * Returns 0 for success else error.
2144 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2145 const struct snd_soc_dapm_widget *widget)
2147 struct snd_soc_dapm_widget *w;
2150 if ((w = dapm_cnew_widget(widget)) == NULL)
2153 name_len = strlen(widget->name) + 1;
2154 if (dapm->codec->name_prefix)
2155 name_len += 1 + strlen(dapm->codec->name_prefix);
2156 w->name = kmalloc(name_len, GFP_KERNEL);
2157 if (w->name == NULL) {
2161 if (dapm->codec->name_prefix)
2162 snprintf(w->name, name_len, "%s %s",
2163 dapm->codec->name_prefix, widget->name);
2165 snprintf(w->name, name_len, "%s", widget->name);
2169 w->codec = dapm->codec;
2170 INIT_LIST_HEAD(&w->sources);
2171 INIT_LIST_HEAD(&w->sinks);
2172 INIT_LIST_HEAD(&w->list);
2173 list_add(&w->list, &dapm->card->widgets);
2175 /* machine layer set ups unconnected pins and insertions */
2179 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2182 * snd_soc_dapm_new_controls - create new dapm controls
2183 * @dapm: DAPM context
2184 * @widget: widget array
2185 * @num: number of widgets
2187 * Creates new DAPM controls based upon the templates.
2189 * Returns 0 for success else error.
2191 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2192 const struct snd_soc_dapm_widget *widget,
2197 for (i = 0; i < num; i++) {
2198 ret = snd_soc_dapm_new_control(dapm, widget);
2201 "ASoC: Failed to create DAPM control %s: %d\n",
2209 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2211 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2212 const char *stream, int event)
2214 struct snd_soc_dapm_widget *w;
2216 list_for_each_entry(w, &dapm->card->widgets, list)
2218 if (!w->sname || w->dapm != dapm)
2220 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2221 w->name, w->sname, stream, event);
2222 if (strstr(w->sname, stream)) {
2224 case SND_SOC_DAPM_STREAM_START:
2227 case SND_SOC_DAPM_STREAM_STOP:
2230 case SND_SOC_DAPM_STREAM_SUSPEND:
2231 case SND_SOC_DAPM_STREAM_RESUME:
2232 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2233 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2239 dapm_power_widgets(dapm, event);
2243 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2244 * @rtd: PCM runtime data
2245 * @stream: stream name
2246 * @event: stream event
2248 * Sends a stream event to the dapm core. The core then makes any
2249 * necessary widget power changes.
2251 * Returns 0 for success else error.
2253 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2254 const char *stream, int event)
2256 struct snd_soc_codec *codec = rtd->codec;
2261 mutex_lock(&codec->mutex);
2262 soc_dapm_stream_event(&codec->dapm, stream, event);
2263 mutex_unlock(&codec->mutex);
2266 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2269 * snd_soc_dapm_enable_pin - enable pin.
2270 * @dapm: DAPM context
2273 * Enables input/output pin and its parents or children widgets iff there is
2274 * a valid audio route and active audio stream.
2275 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2276 * do any widget power switching.
2278 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2280 return snd_soc_dapm_set_pin(dapm, pin, 1);
2282 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2285 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2286 * @dapm: DAPM context
2289 * Enables input/output pin regardless of any other state. This is
2290 * intended for use with microphone bias supplies used in microphone
2293 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2294 * do any widget power switching.
2296 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2299 struct snd_soc_dapm_widget *w;
2301 list_for_each_entry(w, &dapm->card->widgets, list) {
2302 if (w->dapm != dapm)
2304 if (!strcmp(w->name, pin)) {
2305 dev_dbg(w->dapm->dev,
2306 "dapm: force enable pin %s\n", pin);
2313 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2316 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2319 * snd_soc_dapm_disable_pin - disable pin.
2320 * @dapm: DAPM context
2323 * Disables input/output pin and its parents or children widgets.
2324 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2325 * do any widget power switching.
2327 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2330 return snd_soc_dapm_set_pin(dapm, pin, 0);
2332 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2335 * snd_soc_dapm_nc_pin - permanently disable pin.
2336 * @dapm: DAPM context
2339 * Marks the specified pin as being not connected, disabling it along
2340 * any parent or child widgets. At present this is identical to
2341 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2342 * additional things such as disabling controls which only affect
2343 * paths through the pin.
2345 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2346 * do any widget power switching.
2348 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2350 return snd_soc_dapm_set_pin(dapm, pin, 0);
2352 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2355 * snd_soc_dapm_get_pin_status - get audio pin status
2356 * @dapm: DAPM context
2357 * @pin: audio signal pin endpoint (or start point)
2359 * Get audio pin status - connected or disconnected.
2361 * Returns 1 for connected otherwise 0.
2363 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2366 struct snd_soc_dapm_widget *w;
2368 list_for_each_entry(w, &dapm->card->widgets, list) {
2369 if (w->dapm != dapm)
2371 if (!strcmp(w->name, pin))
2372 return w->connected;
2377 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2380 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2381 * @dapm: DAPM context
2382 * @pin: audio signal pin endpoint (or start point)
2384 * Mark the given endpoint or pin as ignoring suspend. When the
2385 * system is disabled a path between two endpoints flagged as ignoring
2386 * suspend will not be disabled. The path must already be enabled via
2387 * normal means at suspend time, it will not be turned on if it was not
2390 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2393 struct snd_soc_dapm_widget *w;
2395 list_for_each_entry(w, &dapm->card->widgets, list) {
2396 if (w->dapm != dapm)
2398 if (!strcmp(w->name, pin)) {
2399 w->ignore_suspend = 1;
2404 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2407 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2410 * snd_soc_dapm_free - free dapm resources
2413 * Free all dapm widgets and resources.
2415 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2417 snd_soc_dapm_sys_remove(dapm->dev);
2418 dapm_free_widgets(dapm);
2419 list_del(&dapm->list);
2421 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2423 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2425 struct snd_soc_dapm_widget *w;
2426 LIST_HEAD(down_list);
2429 list_for_each_entry(w, &dapm->card->widgets, list) {
2430 if (w->dapm != dapm)
2433 dapm_seq_insert(w, &down_list, false);
2439 /* If there were no widgets to power down we're already in
2443 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
2444 dapm_seq_run(dapm, &down_list, 0, false);
2445 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
2450 * snd_soc_dapm_shutdown - callback for system shutdown
2452 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2454 struct snd_soc_codec *codec;
2456 list_for_each_entry(codec, &card->codec_dev_list, list) {
2457 soc_dapm_shutdown_codec(&codec->dapm);
2458 snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
2462 /* Module information */
2463 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2464 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2465 MODULE_LICENSE("GPL");