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->kcontrol_news[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 *)
208 w->kcontrol_news[i].private_value;
209 int val, item, bitmask;
211 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
213 val = snd_soc_read(w->codec, e->reg);
214 item = (val >> e->shift_l) & (bitmask - 1);
217 for (i = 0; i < e->max; i++) {
218 if (!(strcmp(p->name, e->texts[i])) && item == i)
223 case snd_soc_dapm_virt_mux: {
224 struct soc_enum *e = (struct soc_enum *)
225 w->kcontrol_news[i].private_value;
228 /* since a virtual mux has no backing registers to
229 * decide which path to connect, it will try to match
230 * with the first enumeration. This is to ensure
231 * that the default mux choice (the first) will be
232 * correctly powered up during initialization.
234 if (!strcmp(p->name, e->texts[0]))
238 case snd_soc_dapm_value_mux: {
239 struct soc_enum *e = (struct soc_enum *)
240 w->kcontrol_news[i].private_value;
243 val = snd_soc_read(w->codec, e->reg);
244 val = (val >> e->shift_l) & e->mask;
245 for (item = 0; item < e->max; item++) {
246 if (val == e->values[item])
251 for (i = 0; i < e->max; i++) {
252 if (!(strcmp(p->name, e->texts[i])) && item == i)
257 /* does not effect routing - always connected */
258 case snd_soc_dapm_pga:
259 case snd_soc_dapm_out_drv:
260 case snd_soc_dapm_output:
261 case snd_soc_dapm_adc:
262 case snd_soc_dapm_input:
263 case snd_soc_dapm_dac:
264 case snd_soc_dapm_micbias:
265 case snd_soc_dapm_vmid:
266 case snd_soc_dapm_supply:
267 case snd_soc_dapm_aif_in:
268 case snd_soc_dapm_aif_out:
271 /* does effect routing - dynamically connected */
272 case snd_soc_dapm_hp:
273 case snd_soc_dapm_mic:
274 case snd_soc_dapm_spk:
275 case snd_soc_dapm_line:
276 case snd_soc_dapm_pre:
277 case snd_soc_dapm_post:
283 /* connect mux widget to its interconnecting audio paths */
284 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
285 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
286 struct snd_soc_dapm_path *path, const char *control_name,
287 const struct snd_kcontrol_new *kcontrol)
289 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
292 for (i = 0; i < e->max; i++) {
293 if (!(strcmp(control_name, e->texts[i]))) {
294 list_add(&path->list, &dapm->card->paths);
295 list_add(&path->list_sink, &dest->sources);
296 list_add(&path->list_source, &src->sinks);
297 path->name = (char*)e->texts[i];
298 dapm_set_path_status(dest, path, 0);
306 /* connect mixer widget to its interconnecting audio paths */
307 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
308 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
309 struct snd_soc_dapm_path *path, const char *control_name)
313 /* search for mixer kcontrol */
314 for (i = 0; i < dest->num_kcontrols; i++) {
315 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
316 list_add(&path->list, &dapm->card->paths);
317 list_add(&path->list_sink, &dest->sources);
318 list_add(&path->list_source, &src->sinks);
319 path->name = dest->kcontrol_news[i].name;
320 dapm_set_path_status(dest, path, i);
327 /* create new dapm mixer control */
328 static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
329 struct snd_soc_dapm_widget *w)
332 size_t name_len, prefix_len;
333 struct snd_soc_dapm_path *path;
334 struct snd_card *card = dapm->card->snd_card;
336 struct snd_soc_dapm_widget_list *wlist;
340 prefix = dapm->codec->name_prefix;
345 prefix_len = strlen(prefix) + 1;
350 for (i = 0; i < w->num_kcontrols; i++) {
353 list_for_each_entry(path, &w->sources, list_sink) {
355 /* mixer/mux paths name must match control name */
356 if (path->name != (char *)w->kcontrol_news[i].name)
359 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
360 sizeof(struct snd_soc_dapm_widget *),
361 wlist = kzalloc(wlistsize, GFP_KERNEL);
364 "asoc: can't allocate widget list for %s\n",
368 wlist->num_widgets = 1;
369 wlist->widgets[0] = w;
371 /* add dapm control with long name.
372 * for dapm_mixer this is the concatenation of the
373 * mixer and kcontrol name.
374 * for dapm_mixer_named_ctl this is simply the
377 name_len = strlen(w->kcontrol_news[i].name) + 1;
378 if (w->id != snd_soc_dapm_mixer_named_ctl)
379 name_len += 1 + strlen(w->name);
381 path->long_name = kmalloc(name_len, GFP_KERNEL);
383 if (path->long_name == NULL) {
390 /* The control will get a prefix from
391 * the control creation process but
392 * we're also using the same prefix
393 * for widgets so cut the prefix off
394 * the front of the widget name.
396 snprintf(path->long_name, name_len, "%s %s",
397 w->name + prefix_len,
398 w->kcontrol_news[i].name);
400 case snd_soc_dapm_mixer_named_ctl:
401 snprintf(path->long_name, name_len, "%s",
402 w->kcontrol_news[i].name);
406 path->long_name[name_len - 1] = '\0';
408 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
409 wlist, path->long_name,
411 ret = snd_ctl_add(card, path->kcontrol);
414 "asoc: failed to add dapm kcontrol %s: %d\n",
415 path->long_name, ret);
417 kfree(path->long_name);
418 path->long_name = NULL;
421 w->kcontrols[i] = path->kcontrol;
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->card->snd_card;
437 struct snd_soc_dapm_widget_list *wlist;
440 if (!w->num_kcontrols) {
441 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
445 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
446 sizeof(struct snd_soc_dapm_widget *),
447 wlist = kzalloc(wlistsize, GFP_KERNEL);
450 "asoc: can't allocate widget list for %s\n", w->name);
453 wlist->num_widgets = 1;
454 wlist->widgets[0] = w;
457 prefix = dapm->codec->name_prefix;
462 prefix_len = strlen(prefix) + 1;
466 /* The control will get a prefix from the control creation
467 * process but we're also using the same prefix for widgets so
468 * cut the prefix off the front of the widget name.
470 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
471 w->name + prefix_len, prefix);
472 ret = snd_ctl_add(card, kcontrol);
477 w->kcontrols[0] = kcontrol;
479 list_for_each_entry(path, &w->sources, list_sink)
480 path->kcontrol = kcontrol;
485 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
490 /* create new dapm volume control */
491 static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
492 struct snd_soc_dapm_widget *w)
494 if (w->num_kcontrols)
495 dev_err(w->dapm->dev,
496 "asoc: PGA controls not supported: '%s'\n", w->name);
501 /* reset 'walked' bit for each dapm path */
502 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
504 struct snd_soc_dapm_path *p;
506 list_for_each_entry(p, &dapm->card->paths, list)
510 /* We implement power down on suspend by checking the power state of
511 * the ALSA card - when we are suspending the ALSA state for the card
514 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
516 int level = snd_power_get_state(widget->dapm->card->snd_card);
519 case SNDRV_CTL_POWER_D3hot:
520 case SNDRV_CTL_POWER_D3cold:
521 if (widget->ignore_suspend)
522 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
524 return widget->ignore_suspend;
531 * Recursively check for a completed path to an active or physically connected
532 * output widget. Returns number of complete paths.
534 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
536 struct snd_soc_dapm_path *path;
539 if (widget->id == snd_soc_dapm_supply)
542 switch (widget->id) {
543 case snd_soc_dapm_adc:
544 case snd_soc_dapm_aif_out:
546 return snd_soc_dapm_suspend_check(widget);
551 if (widget->connected) {
552 /* connected pin ? */
553 if (widget->id == snd_soc_dapm_output && !widget->ext)
554 return snd_soc_dapm_suspend_check(widget);
556 /* connected jack or spk ? */
557 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
558 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
559 return snd_soc_dapm_suspend_check(widget);
562 list_for_each_entry(path, &widget->sinks, list_source) {
566 if (path->sink && path->connect) {
568 con += is_connected_output_ep(path->sink);
576 * Recursively check for a completed path to an active or physically connected
577 * input widget. Returns number of complete paths.
579 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
581 struct snd_soc_dapm_path *path;
584 if (widget->id == snd_soc_dapm_supply)
587 /* active stream ? */
588 switch (widget->id) {
589 case snd_soc_dapm_dac:
590 case snd_soc_dapm_aif_in:
592 return snd_soc_dapm_suspend_check(widget);
597 if (widget->connected) {
598 /* connected pin ? */
599 if (widget->id == snd_soc_dapm_input && !widget->ext)
600 return snd_soc_dapm_suspend_check(widget);
602 /* connected VMID/Bias for lower pops */
603 if (widget->id == snd_soc_dapm_vmid)
604 return snd_soc_dapm_suspend_check(widget);
606 /* connected jack ? */
607 if (widget->id == snd_soc_dapm_mic ||
608 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
609 return snd_soc_dapm_suspend_check(widget);
612 list_for_each_entry(path, &widget->sources, list_sink) {
616 if (path->source && path->connect) {
618 con += is_connected_input_ep(path->source);
626 * Handler for generic register modifier widget.
628 int dapm_reg_event(struct snd_soc_dapm_widget *w,
629 struct snd_kcontrol *kcontrol, int event)
633 if (SND_SOC_DAPM_EVENT_ON(event))
638 snd_soc_update_bits(w->codec, -(w->reg + 1),
639 w->mask << w->shift, val << w->shift);
643 EXPORT_SYMBOL_GPL(dapm_reg_event);
645 /* Generic check to see if a widget should be powered.
647 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
651 in = is_connected_input_ep(w);
652 dapm_clear_walk(w->dapm);
653 out = is_connected_output_ep(w);
654 dapm_clear_walk(w->dapm);
655 return out != 0 && in != 0;
658 /* Check to see if an ADC has power */
659 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
664 in = is_connected_input_ep(w);
665 dapm_clear_walk(w->dapm);
668 return dapm_generic_check_power(w);
672 /* Check to see if a DAC has power */
673 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
678 out = is_connected_output_ep(w);
679 dapm_clear_walk(w->dapm);
682 return dapm_generic_check_power(w);
686 /* Check to see if a power supply is needed */
687 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
689 struct snd_soc_dapm_path *path;
692 /* Check if one of our outputs is connected */
693 list_for_each_entry(path, &w->sinks, list_source) {
694 if (path->connected &&
695 !path->connected(path->source, path->sink))
701 if (path->sink->force) {
706 if (path->sink->power_check &&
707 path->sink->power_check(path->sink)) {
713 dapm_clear_walk(w->dapm);
718 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
719 struct snd_soc_dapm_widget *b,
727 sort = dapm_down_seq;
729 if (sort[a->id] != sort[b->id])
730 return sort[a->id] - sort[b->id];
731 if (a->subseq != b->subseq) {
733 return a->subseq - b->subseq;
735 return b->subseq - a->subseq;
737 if (a->reg != b->reg)
738 return a->reg - b->reg;
739 if (a->dapm != b->dapm)
740 return (unsigned long)a->dapm - (unsigned long)b->dapm;
745 /* Insert a widget in order into a DAPM power sequence. */
746 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
747 struct list_head *list,
750 struct snd_soc_dapm_widget *w;
752 list_for_each_entry(w, list, power_list)
753 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
754 list_add_tail(&new_widget->power_list, &w->power_list);
758 list_add_tail(&new_widget->power_list, list);
761 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
762 struct snd_soc_dapm_widget *w, int event)
764 struct snd_soc_card *card = dapm->card;
769 case SND_SOC_DAPM_PRE_PMU:
773 case SND_SOC_DAPM_POST_PMU:
774 ev_name = "POST_PMU";
777 case SND_SOC_DAPM_PRE_PMD:
781 case SND_SOC_DAPM_POST_PMD:
782 ev_name = "POST_PMD";
790 if (w->power != power)
793 if (w->event && (w->event_flags & event)) {
794 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
796 trace_snd_soc_dapm_widget_event_start(w, event);
797 ret = w->event(w, NULL, event);
798 trace_snd_soc_dapm_widget_event_done(w, event);
800 pr_err("%s: %s event failed: %d\n",
801 ev_name, w->name, ret);
805 /* Apply the coalesced changes from a DAPM sequence */
806 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
807 struct list_head *pending)
809 struct snd_soc_card *card = dapm->card;
810 struct snd_soc_dapm_widget *w;
812 unsigned int value = 0;
813 unsigned int mask = 0;
814 unsigned int cur_mask;
816 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
819 list_for_each_entry(w, pending, power_list) {
820 cur_mask = 1 << w->shift;
821 BUG_ON(reg != w->reg);
832 pop_dbg(dapm->dev, card->pop_time,
833 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
834 w->name, reg, value, mask);
836 /* Check for events */
837 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
838 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
842 pop_dbg(dapm->dev, card->pop_time,
843 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
844 value, mask, reg, card->pop_time);
845 pop_wait(card->pop_time);
846 snd_soc_update_bits(dapm->codec, reg, mask, value);
849 list_for_each_entry(w, pending, power_list) {
850 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
851 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
855 /* Apply a DAPM power sequence.
857 * We walk over a pre-sorted list of widgets to apply power to. In
858 * order to minimise the number of writes to the device required
859 * multiple widgets will be updated in a single write where possible.
860 * Currently anything that requires more than a single write is not
863 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
864 struct list_head *list, int event, bool power_up)
866 struct snd_soc_dapm_widget *w, *n;
870 int cur_reg = SND_SOC_NOPM;
871 struct snd_soc_dapm_context *cur_dapm = NULL;
878 sort = dapm_down_seq;
880 list_for_each_entry_safe(w, n, list, power_list) {
883 /* Do we need to apply any queued changes? */
884 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
885 w->dapm != cur_dapm || w->subseq != cur_subseq) {
886 if (!list_empty(&pending))
887 dapm_seq_run_coalesced(cur_dapm, &pending);
889 if (cur_dapm && cur_dapm->seq_notifier) {
890 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
891 if (sort[i] == cur_sort)
892 cur_dapm->seq_notifier(cur_dapm,
897 INIT_LIST_HEAD(&pending);
900 cur_reg = SND_SOC_NOPM;
905 case snd_soc_dapm_pre:
907 list_for_each_entry_safe_continue(w, n, list,
910 if (event == SND_SOC_DAPM_STREAM_START)
912 NULL, SND_SOC_DAPM_PRE_PMU);
913 else if (event == SND_SOC_DAPM_STREAM_STOP)
915 NULL, SND_SOC_DAPM_PRE_PMD);
918 case snd_soc_dapm_post:
920 list_for_each_entry_safe_continue(w, n, list,
923 if (event == SND_SOC_DAPM_STREAM_START)
925 NULL, SND_SOC_DAPM_POST_PMU);
926 else if (event == SND_SOC_DAPM_STREAM_STOP)
928 NULL, SND_SOC_DAPM_POST_PMD);
932 /* Queue it up for application */
933 cur_sort = sort[w->id];
934 cur_subseq = w->subseq;
937 list_move(&w->power_list, &pending);
942 dev_err(w->dapm->dev,
943 "Failed to apply widget power: %d\n", ret);
946 if (!list_empty(&pending))
947 dapm_seq_run_coalesced(cur_dapm, &pending);
949 if (cur_dapm && cur_dapm->seq_notifier) {
950 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
951 if (sort[i] == cur_sort)
952 cur_dapm->seq_notifier(cur_dapm,
957 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
959 struct snd_soc_dapm_update *update = dapm->update;
960 struct snd_soc_dapm_widget *w;
969 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
970 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
972 pr_err("%s DAPM pre-event failed: %d\n",
976 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
979 pr_err("%s DAPM update failed: %d\n", w->name, ret);
982 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
983 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
985 pr_err("%s DAPM post-event failed: %d\n",
990 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
991 * they're changing state.
993 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
995 struct snd_soc_dapm_context *d = data;
998 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
999 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1002 "Failed to turn on bias: %d\n", ret);
1005 /* If we're changing to all on or all off then prepare */
1006 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1007 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1008 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1011 "Failed to prepare bias: %d\n", ret);
1015 /* Async callback run prior to DAPM sequences - brings to their final
1018 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1020 struct snd_soc_dapm_context *d = data;
1023 /* If we just powered the last thing off drop to standby bias */
1024 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1025 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1027 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1031 /* If we're in standby and can support bias off then do that */
1032 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1033 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1035 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1038 /* If we just powered up then move to active bias */
1039 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1040 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1042 dev_err(d->dev, "Failed to apply active bias: %d\n",
1048 * Scan each dapm widget for complete audio path.
1049 * A complete path is a route that has valid endpoints i.e.:-
1051 * o DAC to output pin.
1052 * o Input Pin to ADC.
1053 * o Input pin to Output pin (bypass, sidetone)
1054 * o DAC to ADC (loopback).
1056 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1058 struct snd_soc_card *card = dapm->card;
1059 struct snd_soc_dapm_widget *w;
1060 struct snd_soc_dapm_context *d;
1062 LIST_HEAD(down_list);
1063 LIST_HEAD(async_domain);
1066 trace_snd_soc_dapm_start(card);
1068 list_for_each_entry(d, &card->dapm_list, list)
1072 /* Check which widgets we need to power and store them in
1073 * lists indicating if they should be powered up or down.
1075 list_for_each_entry(w, &card->widgets, list) {
1077 case snd_soc_dapm_pre:
1078 dapm_seq_insert(w, &down_list, false);
1080 case snd_soc_dapm_post:
1081 dapm_seq_insert(w, &up_list, true);
1085 if (!w->power_check)
1089 power = w->power_check(w);
1093 w->dapm->dev_power = 1;
1095 if (w->power == power)
1098 trace_snd_soc_dapm_widget_power(w, power);
1101 dapm_seq_insert(w, &up_list, true);
1103 dapm_seq_insert(w, &down_list, false);
1110 /* If there are no DAPM widgets then try to figure out power from the
1113 if (!dapm->n_widgets) {
1115 case SND_SOC_DAPM_STREAM_START:
1116 case SND_SOC_DAPM_STREAM_RESUME:
1117 dapm->dev_power = 1;
1119 case SND_SOC_DAPM_STREAM_STOP:
1120 dapm->dev_power = !!dapm->codec->active;
1122 case SND_SOC_DAPM_STREAM_SUSPEND:
1123 dapm->dev_power = 0;
1125 case SND_SOC_DAPM_STREAM_NOP:
1126 switch (dapm->bias_level) {
1127 case SND_SOC_BIAS_STANDBY:
1128 case SND_SOC_BIAS_OFF:
1129 dapm->dev_power = 0;
1132 dapm->dev_power = 1;
1141 /* Force all contexts in the card to the same bias state */
1143 list_for_each_entry(d, &card->dapm_list, list)
1146 list_for_each_entry(d, &card->dapm_list, list)
1147 d->dev_power = power;
1150 /* Run all the bias changes in parallel */
1151 list_for_each_entry(d, &dapm->card->dapm_list, list)
1152 async_schedule_domain(dapm_pre_sequence_async, d,
1154 async_synchronize_full_domain(&async_domain);
1156 /* Power down widgets first; try to avoid amplifying pops. */
1157 dapm_seq_run(dapm, &down_list, event, false);
1159 dapm_widget_update(dapm);
1162 dapm_seq_run(dapm, &up_list, event, true);
1164 /* Run all the bias changes in parallel */
1165 list_for_each_entry(d, &dapm->card->dapm_list, list)
1166 async_schedule_domain(dapm_post_sequence_async, d,
1168 async_synchronize_full_domain(&async_domain);
1170 pop_dbg(dapm->dev, card->pop_time,
1171 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1172 pop_wait(card->pop_time);
1174 trace_snd_soc_dapm_done(card);
1179 #ifdef CONFIG_DEBUG_FS
1180 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1182 file->private_data = inode->i_private;
1186 static ssize_t dapm_widget_power_read_file(struct file *file,
1187 char __user *user_buf,
1188 size_t count, loff_t *ppos)
1190 struct snd_soc_dapm_widget *w = file->private_data;
1194 struct snd_soc_dapm_path *p = NULL;
1196 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1200 in = is_connected_input_ep(w);
1201 dapm_clear_walk(w->dapm);
1202 out = is_connected_output_ep(w);
1203 dapm_clear_walk(w->dapm);
1205 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1206 w->name, w->power ? "On" : "Off", in, out);
1209 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1210 " - R%d(0x%x) bit %d",
1211 w->reg, w->reg, w->shift);
1213 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1216 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1218 w->active ? "active" : "inactive");
1220 list_for_each_entry(p, &w->sources, list_sink) {
1221 if (p->connected && !p->connected(w, p->sink))
1225 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1226 " in \"%s\" \"%s\"\n",
1227 p->name ? p->name : "static",
1230 list_for_each_entry(p, &w->sinks, list_source) {
1231 if (p->connected && !p->connected(w, p->sink))
1235 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1236 " out \"%s\" \"%s\"\n",
1237 p->name ? p->name : "static",
1241 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1247 static const struct file_operations dapm_widget_power_fops = {
1248 .open = dapm_widget_power_open_file,
1249 .read = dapm_widget_power_read_file,
1250 .llseek = default_llseek,
1253 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1255 file->private_data = inode->i_private;
1259 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1260 size_t count, loff_t *ppos)
1262 struct snd_soc_dapm_context *dapm = file->private_data;
1265 switch (dapm->bias_level) {
1266 case SND_SOC_BIAS_ON:
1269 case SND_SOC_BIAS_PREPARE:
1270 level = "Prepare\n";
1272 case SND_SOC_BIAS_STANDBY:
1273 level = "Standby\n";
1275 case SND_SOC_BIAS_OFF:
1280 level = "Unknown\n";
1284 return simple_read_from_buffer(user_buf, count, ppos, level,
1288 static const struct file_operations dapm_bias_fops = {
1289 .open = dapm_bias_open_file,
1290 .read = dapm_bias_read_file,
1291 .llseek = default_llseek,
1294 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1295 struct dentry *parent)
1299 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1301 if (!dapm->debugfs_dapm) {
1303 "Failed to create DAPM debugfs directory\n");
1307 d = debugfs_create_file("bias_level", 0444,
1308 dapm->debugfs_dapm, dapm,
1312 "ASoC: Failed to create bias level debugfs file\n");
1315 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1317 struct snd_soc_dapm_context *dapm = w->dapm;
1320 if (!dapm->debugfs_dapm || !w->name)
1323 d = debugfs_create_file(w->name, 0444,
1324 dapm->debugfs_dapm, w,
1325 &dapm_widget_power_fops);
1327 dev_warn(w->dapm->dev,
1328 "ASoC: Failed to create %s debugfs file\n",
1332 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1334 debugfs_remove_recursive(dapm->debugfs_dapm);
1338 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1339 struct dentry *parent)
1343 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1347 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1353 /* test and update the power status of a mux widget */
1354 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1355 struct snd_kcontrol *kcontrol, int change,
1356 int mux, struct soc_enum *e)
1358 struct snd_soc_dapm_path *path;
1361 if (widget->id != snd_soc_dapm_mux &&
1362 widget->id != snd_soc_dapm_virt_mux &&
1363 widget->id != snd_soc_dapm_value_mux)
1369 /* find dapm widget path assoc with kcontrol */
1370 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1371 if (path->kcontrol != kcontrol)
1374 if (!path->name || !e->texts[mux])
1378 /* we now need to match the string in the enum to the path */
1379 if (!(strcmp(path->name, e->texts[mux])))
1380 path->connect = 1; /* new connection */
1382 path->connect = 0; /* old connection must be powered down */
1386 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1391 /* test and update the power status of a mixer or switch widget */
1392 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1393 struct snd_kcontrol *kcontrol, int connect)
1395 struct snd_soc_dapm_path *path;
1398 if (widget->id != snd_soc_dapm_mixer &&
1399 widget->id != snd_soc_dapm_mixer_named_ctl &&
1400 widget->id != snd_soc_dapm_switch)
1403 /* find dapm widget path assoc with kcontrol */
1404 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1405 if (path->kcontrol != kcontrol)
1408 /* found, now check type */
1410 path->connect = connect;
1415 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1420 /* show dapm widget status in sys fs */
1421 static ssize_t dapm_widget_show(struct device *dev,
1422 struct device_attribute *attr, char *buf)
1424 struct snd_soc_pcm_runtime *rtd =
1425 container_of(dev, struct snd_soc_pcm_runtime, dev);
1426 struct snd_soc_codec *codec =rtd->codec;
1427 struct snd_soc_dapm_widget *w;
1429 char *state = "not set";
1431 list_for_each_entry(w, &codec->card->widgets, list) {
1432 if (w->dapm != &codec->dapm)
1435 /* only display widgets that burnm power */
1437 case snd_soc_dapm_hp:
1438 case snd_soc_dapm_mic:
1439 case snd_soc_dapm_spk:
1440 case snd_soc_dapm_line:
1441 case snd_soc_dapm_micbias:
1442 case snd_soc_dapm_dac:
1443 case snd_soc_dapm_adc:
1444 case snd_soc_dapm_pga:
1445 case snd_soc_dapm_out_drv:
1446 case snd_soc_dapm_mixer:
1447 case snd_soc_dapm_mixer_named_ctl:
1448 case snd_soc_dapm_supply:
1450 count += sprintf(buf + count, "%s: %s\n",
1451 w->name, w->power ? "On":"Off");
1458 switch (codec->dapm.bias_level) {
1459 case SND_SOC_BIAS_ON:
1462 case SND_SOC_BIAS_PREPARE:
1465 case SND_SOC_BIAS_STANDBY:
1468 case SND_SOC_BIAS_OFF:
1472 count += sprintf(buf + count, "PM State: %s\n", state);
1477 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1479 int snd_soc_dapm_sys_add(struct device *dev)
1481 return device_create_file(dev, &dev_attr_dapm_widget);
1484 static void snd_soc_dapm_sys_remove(struct device *dev)
1486 device_remove_file(dev, &dev_attr_dapm_widget);
1489 /* free all dapm widgets and resources */
1490 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1492 struct snd_soc_dapm_widget *w, *next_w;
1493 struct snd_soc_dapm_path *p, *next_p;
1495 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1496 if (w->dapm != dapm)
1500 * remove source and sink paths associated to this widget.
1501 * While removing the path, remove reference to it from both
1502 * source and sink widgets so that path is removed only once.
1504 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1505 list_del(&p->list_sink);
1506 list_del(&p->list_source);
1508 kfree(p->long_name);
1511 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1512 list_del(&p->list_sink);
1513 list_del(&p->list_source);
1515 kfree(p->long_name);
1518 kfree(w->kcontrols);
1524 static struct snd_soc_dapm_widget *dapm_find_widget(
1525 struct snd_soc_dapm_context *dapm, const char *pin,
1526 bool search_other_contexts)
1528 struct snd_soc_dapm_widget *w;
1529 struct snd_soc_dapm_widget *fallback = NULL;
1531 list_for_each_entry(w, &dapm->card->widgets, list) {
1532 if (!strcmp(w->name, pin)) {
1533 if (w->dapm == dapm)
1540 if (search_other_contexts)
1546 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1547 const char *pin, int status)
1549 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1552 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1556 w->connected = status;
1564 * snd_soc_dapm_sync - scan and power dapm paths
1565 * @dapm: DAPM context
1567 * Walks all dapm audio paths and powers widgets according to their
1568 * stream or path usage.
1570 * Returns 0 for success.
1572 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1574 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1576 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1578 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1579 const struct snd_soc_dapm_route *route)
1581 struct snd_soc_dapm_path *path;
1582 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1583 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1585 const char *control = route->control;
1587 char prefixed_sink[80];
1588 char prefixed_source[80];
1591 if (dapm->codec && dapm->codec->name_prefix) {
1592 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1593 dapm->codec->name_prefix, route->sink);
1594 sink = prefixed_sink;
1595 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1596 dapm->codec->name_prefix, route->source);
1597 source = prefixed_source;
1600 source = route->source;
1604 * find src and dest widgets over all widgets but favor a widget from
1605 * current DAPM context
1607 list_for_each_entry(w, &dapm->card->widgets, list) {
1608 if (!wsink && !(strcmp(w->name, sink))) {
1610 if (w->dapm == dapm)
1614 if (!wsource && !(strcmp(w->name, source))) {
1616 if (w->dapm == dapm)
1620 /* use widget from another DAPM context if not found from this */
1626 if (wsource == NULL || wsink == NULL)
1629 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1633 path->source = wsource;
1635 path->connected = route->connected;
1636 INIT_LIST_HEAD(&path->list);
1637 INIT_LIST_HEAD(&path->list_source);
1638 INIT_LIST_HEAD(&path->list_sink);
1640 /* check for external widgets */
1641 if (wsink->id == snd_soc_dapm_input) {
1642 if (wsource->id == snd_soc_dapm_micbias ||
1643 wsource->id == snd_soc_dapm_mic ||
1644 wsource->id == snd_soc_dapm_line ||
1645 wsource->id == snd_soc_dapm_output)
1648 if (wsource->id == snd_soc_dapm_output) {
1649 if (wsink->id == snd_soc_dapm_spk ||
1650 wsink->id == snd_soc_dapm_hp ||
1651 wsink->id == snd_soc_dapm_line ||
1652 wsink->id == snd_soc_dapm_input)
1656 /* connect static paths */
1657 if (control == NULL) {
1658 list_add(&path->list, &dapm->card->paths);
1659 list_add(&path->list_sink, &wsink->sources);
1660 list_add(&path->list_source, &wsource->sinks);
1665 /* connect dynamic paths */
1666 switch (wsink->id) {
1667 case snd_soc_dapm_adc:
1668 case snd_soc_dapm_dac:
1669 case snd_soc_dapm_pga:
1670 case snd_soc_dapm_out_drv:
1671 case snd_soc_dapm_input:
1672 case snd_soc_dapm_output:
1673 case snd_soc_dapm_micbias:
1674 case snd_soc_dapm_vmid:
1675 case snd_soc_dapm_pre:
1676 case snd_soc_dapm_post:
1677 case snd_soc_dapm_supply:
1678 case snd_soc_dapm_aif_in:
1679 case snd_soc_dapm_aif_out:
1680 list_add(&path->list, &dapm->card->paths);
1681 list_add(&path->list_sink, &wsink->sources);
1682 list_add(&path->list_source, &wsource->sinks);
1685 case snd_soc_dapm_mux:
1686 case snd_soc_dapm_virt_mux:
1687 case snd_soc_dapm_value_mux:
1688 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1689 &wsink->kcontrol_news[0]);
1693 case snd_soc_dapm_switch:
1694 case snd_soc_dapm_mixer:
1695 case snd_soc_dapm_mixer_named_ctl:
1696 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1700 case snd_soc_dapm_hp:
1701 case snd_soc_dapm_mic:
1702 case snd_soc_dapm_line:
1703 case snd_soc_dapm_spk:
1704 list_add(&path->list, &dapm->card->paths);
1705 list_add(&path->list_sink, &wsink->sources);
1706 list_add(&path->list_source, &wsource->sinks);
1713 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1714 source, control, sink);
1720 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1721 * @dapm: DAPM context
1722 * @route: audio routes
1723 * @num: number of routes
1725 * Connects 2 dapm widgets together via a named audio path. The sink is
1726 * the widget receiving the audio signal, whilst the source is the sender
1727 * of the audio signal.
1729 * Returns 0 for success else error. On error all resources can be freed
1730 * with a call to snd_soc_card_free().
1732 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1733 const struct snd_soc_dapm_route *route, int num)
1737 for (i = 0; i < num; i++) {
1738 ret = snd_soc_dapm_add_route(dapm, route);
1740 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1741 route->source, route->sink);
1749 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1752 * snd_soc_dapm_new_widgets - add new dapm widgets
1753 * @dapm: DAPM context
1755 * Checks the codec for any new dapm widgets and creates them if found.
1757 * Returns 0 for success.
1759 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1761 struct snd_soc_dapm_widget *w;
1764 list_for_each_entry(w, &dapm->card->widgets, list)
1769 if (w->num_kcontrols) {
1770 w->kcontrols = kzalloc(w->num_kcontrols *
1771 sizeof(struct snd_kcontrol *),
1778 case snd_soc_dapm_switch:
1779 case snd_soc_dapm_mixer:
1780 case snd_soc_dapm_mixer_named_ctl:
1781 w->power_check = dapm_generic_check_power;
1782 dapm_new_mixer(dapm, w);
1784 case snd_soc_dapm_mux:
1785 case snd_soc_dapm_virt_mux:
1786 case snd_soc_dapm_value_mux:
1787 w->power_check = dapm_generic_check_power;
1788 dapm_new_mux(dapm, w);
1790 case snd_soc_dapm_adc:
1791 case snd_soc_dapm_aif_out:
1792 w->power_check = dapm_adc_check_power;
1794 case snd_soc_dapm_dac:
1795 case snd_soc_dapm_aif_in:
1796 w->power_check = dapm_dac_check_power;
1798 case snd_soc_dapm_pga:
1799 case snd_soc_dapm_out_drv:
1800 w->power_check = dapm_generic_check_power;
1801 dapm_new_pga(dapm, w);
1803 case snd_soc_dapm_input:
1804 case snd_soc_dapm_output:
1805 case snd_soc_dapm_micbias:
1806 case snd_soc_dapm_spk:
1807 case snd_soc_dapm_hp:
1808 case snd_soc_dapm_mic:
1809 case snd_soc_dapm_line:
1810 w->power_check = dapm_generic_check_power;
1812 case snd_soc_dapm_supply:
1813 w->power_check = dapm_supply_check_power;
1814 case snd_soc_dapm_vmid:
1815 case snd_soc_dapm_pre:
1816 case snd_soc_dapm_post:
1820 /* Read the initial power state from the device */
1822 val = snd_soc_read(w->codec, w->reg);
1823 val &= 1 << w->shift;
1833 dapm_debugfs_add_widget(w);
1836 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1839 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1842 * snd_soc_dapm_get_volsw - dapm mixer get callback
1843 * @kcontrol: mixer control
1844 * @ucontrol: control element information
1846 * Callback to get the value of a dapm mixer control.
1848 * Returns 0 for success.
1850 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1851 struct snd_ctl_elem_value *ucontrol)
1853 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1854 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
1855 struct soc_mixer_control *mc =
1856 (struct soc_mixer_control *)kcontrol->private_value;
1857 unsigned int reg = mc->reg;
1858 unsigned int shift = mc->shift;
1859 unsigned int rshift = mc->rshift;
1861 unsigned int invert = mc->invert;
1862 unsigned int mask = (1 << fls(max)) - 1;
1864 ucontrol->value.integer.value[0] =
1865 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1866 if (shift != rshift)
1867 ucontrol->value.integer.value[1] =
1868 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1870 ucontrol->value.integer.value[0] =
1871 max - ucontrol->value.integer.value[0];
1872 if (shift != rshift)
1873 ucontrol->value.integer.value[1] =
1874 max - ucontrol->value.integer.value[1];
1879 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1882 * snd_soc_dapm_put_volsw - dapm mixer set callback
1883 * @kcontrol: mixer control
1884 * @ucontrol: control element information
1886 * Callback to set the value of a dapm mixer control.
1888 * Returns 0 for success.
1890 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1891 struct snd_ctl_elem_value *ucontrol)
1893 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1894 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
1895 struct snd_soc_codec *codec = widget->codec;
1896 struct soc_mixer_control *mc =
1897 (struct soc_mixer_control *)kcontrol->private_value;
1898 unsigned int reg = mc->reg;
1899 unsigned int shift = mc->shift;
1901 unsigned int mask = (1 << fls(max)) - 1;
1902 unsigned int invert = mc->invert;
1904 int connect, change;
1905 struct snd_soc_dapm_update update;
1908 val = (ucontrol->value.integer.value[0] & mask);
1912 mask = mask << shift;
1916 /* new connection */
1917 connect = invert ? 0 : 1;
1919 /* old connection must be powered down */
1920 connect = invert ? 1 : 0;
1922 mutex_lock(&codec->mutex);
1924 change = snd_soc_test_bits(widget->codec, reg, mask, val);
1926 for (wi = 0; wi < wlist->num_widgets; wi++) {
1927 widget = wlist->widgets[wi];
1929 widget->value = val;
1931 update.kcontrol = kcontrol;
1932 update.widget = widget;
1936 widget->dapm->update = &update;
1938 dapm_mixer_update_power(widget, kcontrol, connect);
1940 widget->dapm->update = NULL;
1944 mutex_unlock(&codec->mutex);
1947 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1950 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1951 * @kcontrol: mixer control
1952 * @ucontrol: control element information
1954 * Callback to get the value of a dapm enumerated double mixer control.
1956 * Returns 0 for success.
1958 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1959 struct snd_ctl_elem_value *ucontrol)
1961 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1962 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
1963 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1964 unsigned int val, bitmask;
1966 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1968 val = snd_soc_read(widget->codec, e->reg);
1969 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1970 if (e->shift_l != e->shift_r)
1971 ucontrol->value.enumerated.item[1] =
1972 (val >> e->shift_r) & (bitmask - 1);
1976 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1979 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1980 * @kcontrol: mixer control
1981 * @ucontrol: control element information
1983 * Callback to set the value of a dapm enumerated double mixer control.
1985 * Returns 0 for success.
1987 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1988 struct snd_ctl_elem_value *ucontrol)
1990 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1991 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
1992 struct snd_soc_codec *codec = widget->codec;
1993 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1994 unsigned int val, mux, change;
1995 unsigned int mask, bitmask;
1996 struct snd_soc_dapm_update update;
1999 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2001 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2003 mux = ucontrol->value.enumerated.item[0];
2004 val = mux << e->shift_l;
2005 mask = (bitmask - 1) << e->shift_l;
2006 if (e->shift_l != e->shift_r) {
2007 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2009 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2010 mask |= (bitmask - 1) << e->shift_r;
2013 mutex_lock(&codec->mutex);
2015 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2017 for (wi = 0; wi < wlist->num_widgets; wi++) {
2018 widget = wlist->widgets[wi];
2020 widget->value = val;
2022 update.kcontrol = kcontrol;
2023 update.widget = widget;
2024 update.reg = e->reg;
2027 widget->dapm->update = &update;
2029 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2031 widget->dapm->update = NULL;
2035 mutex_unlock(&codec->mutex);
2038 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2041 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2042 * @kcontrol: mixer control
2043 * @ucontrol: control element information
2045 * Returns 0 for success.
2047 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2048 struct snd_ctl_elem_value *ucontrol)
2050 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2051 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2053 ucontrol->value.enumerated.item[0] = widget->value;
2057 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2060 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2061 * @kcontrol: mixer control
2062 * @ucontrol: control element information
2064 * Returns 0 for success.
2066 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_value *ucontrol)
2069 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2070 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2071 struct snd_soc_codec *codec = widget->codec;
2072 struct soc_enum *e =
2073 (struct soc_enum *)kcontrol->private_value;
2078 if (ucontrol->value.enumerated.item[0] >= e->max)
2081 mutex_lock(&codec->mutex);
2083 change = widget->value != ucontrol->value.enumerated.item[0];
2085 for (wi = 0; wi < wlist->num_widgets; wi++) {
2086 widget = wlist->widgets[wi];
2088 widget->value = ucontrol->value.enumerated.item[0];
2090 dapm_mux_update_power(widget, kcontrol, change,
2095 mutex_unlock(&codec->mutex);
2098 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2101 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2103 * @kcontrol: mixer control
2104 * @ucontrol: control element information
2106 * Callback to get the value of a dapm semi enumerated double mixer control.
2108 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2109 * used for handling bitfield coded enumeration for example.
2111 * Returns 0 for success.
2113 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2114 struct snd_ctl_elem_value *ucontrol)
2116 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2117 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2118 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2119 unsigned int reg_val, val, mux;
2121 reg_val = snd_soc_read(widget->codec, e->reg);
2122 val = (reg_val >> e->shift_l) & e->mask;
2123 for (mux = 0; mux < e->max; mux++) {
2124 if (val == e->values[mux])
2127 ucontrol->value.enumerated.item[0] = mux;
2128 if (e->shift_l != e->shift_r) {
2129 val = (reg_val >> e->shift_r) & e->mask;
2130 for (mux = 0; mux < e->max; mux++) {
2131 if (val == e->values[mux])
2134 ucontrol->value.enumerated.item[1] = mux;
2139 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2142 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2144 * @kcontrol: mixer control
2145 * @ucontrol: control element information
2147 * Callback to set the value of a dapm semi enumerated double mixer control.
2149 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2150 * used for handling bitfield coded enumeration for example.
2152 * Returns 0 for success.
2154 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
2157 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2158 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2159 struct snd_soc_codec *codec = widget->codec;
2160 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2161 unsigned int val, mux, change;
2163 struct snd_soc_dapm_update update;
2166 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2168 mux = ucontrol->value.enumerated.item[0];
2169 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2170 mask = e->mask << e->shift_l;
2171 if (e->shift_l != e->shift_r) {
2172 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2174 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2175 mask |= e->mask << e->shift_r;
2178 mutex_lock(&codec->mutex);
2180 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2182 for (wi = 0; wi < wlist->num_widgets; wi++) {
2183 widget = wlist->widgets[wi];
2185 widget->value = val;
2187 update.kcontrol = kcontrol;
2188 update.widget = widget;
2189 update.reg = e->reg;
2192 widget->dapm->update = &update;
2194 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2196 widget->dapm->update = NULL;
2200 mutex_unlock(&codec->mutex);
2203 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2206 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2208 * @kcontrol: mixer control
2209 * @uinfo: control element information
2211 * Callback to provide information about a pin switch control.
2213 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_info *uinfo)
2216 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2218 uinfo->value.integer.min = 0;
2219 uinfo->value.integer.max = 1;
2223 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2226 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2228 * @kcontrol: mixer control
2231 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2232 struct snd_ctl_elem_value *ucontrol)
2234 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2235 const char *pin = (const char *)kcontrol->private_value;
2237 mutex_lock(&codec->mutex);
2239 ucontrol->value.integer.value[0] =
2240 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2242 mutex_unlock(&codec->mutex);
2246 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2249 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2251 * @kcontrol: mixer control
2254 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2255 struct snd_ctl_elem_value *ucontrol)
2257 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2258 const char *pin = (const char *)kcontrol->private_value;
2260 mutex_lock(&codec->mutex);
2262 if (ucontrol->value.integer.value[0])
2263 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2265 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2267 snd_soc_dapm_sync(&codec->dapm);
2269 mutex_unlock(&codec->mutex);
2273 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2276 * snd_soc_dapm_new_control - create new dapm control
2277 * @dapm: DAPM context
2278 * @widget: widget template
2280 * Creates a new dapm control based upon the template.
2282 * Returns 0 for success else error.
2284 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2285 const struct snd_soc_dapm_widget *widget)
2287 struct snd_soc_dapm_widget *w;
2290 if ((w = dapm_cnew_widget(widget)) == NULL)
2293 name_len = strlen(widget->name) + 1;
2294 if (dapm->codec && dapm->codec->name_prefix)
2295 name_len += 1 + strlen(dapm->codec->name_prefix);
2296 w->name = kmalloc(name_len, GFP_KERNEL);
2297 if (w->name == NULL) {
2301 if (dapm->codec && dapm->codec->name_prefix)
2302 snprintf(w->name, name_len, "%s %s",
2303 dapm->codec->name_prefix, widget->name);
2305 snprintf(w->name, name_len, "%s", widget->name);
2309 w->codec = dapm->codec;
2310 INIT_LIST_HEAD(&w->sources);
2311 INIT_LIST_HEAD(&w->sinks);
2312 INIT_LIST_HEAD(&w->list);
2313 list_add(&w->list, &dapm->card->widgets);
2315 /* machine layer set ups unconnected pins and insertions */
2319 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2322 * snd_soc_dapm_new_controls - create new dapm controls
2323 * @dapm: DAPM context
2324 * @widget: widget array
2325 * @num: number of widgets
2327 * Creates new DAPM controls based upon the templates.
2329 * Returns 0 for success else error.
2331 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2332 const struct snd_soc_dapm_widget *widget,
2337 for (i = 0; i < num; i++) {
2338 ret = snd_soc_dapm_new_control(dapm, widget);
2341 "ASoC: Failed to create DAPM control %s: %d\n",
2349 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2351 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2352 const char *stream, int event)
2354 struct snd_soc_dapm_widget *w;
2356 list_for_each_entry(w, &dapm->card->widgets, list)
2358 if (!w->sname || w->dapm != dapm)
2360 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2361 w->name, w->sname, stream, event);
2362 if (strstr(w->sname, stream)) {
2364 case SND_SOC_DAPM_STREAM_START:
2367 case SND_SOC_DAPM_STREAM_STOP:
2370 case SND_SOC_DAPM_STREAM_SUSPEND:
2371 case SND_SOC_DAPM_STREAM_RESUME:
2372 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2373 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2379 dapm_power_widgets(dapm, event);
2383 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2384 * @rtd: PCM runtime data
2385 * @stream: stream name
2386 * @event: stream event
2388 * Sends a stream event to the dapm core. The core then makes any
2389 * necessary widget power changes.
2391 * Returns 0 for success else error.
2393 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2394 const char *stream, int event)
2396 struct snd_soc_codec *codec = rtd->codec;
2401 mutex_lock(&codec->mutex);
2402 soc_dapm_stream_event(&codec->dapm, stream, event);
2403 mutex_unlock(&codec->mutex);
2408 * snd_soc_dapm_enable_pin - enable pin.
2409 * @dapm: DAPM context
2412 * Enables input/output pin and its parents or children widgets iff there is
2413 * a valid audio route and active audio stream.
2414 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2415 * do any widget power switching.
2417 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2419 return snd_soc_dapm_set_pin(dapm, pin, 1);
2421 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2424 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2425 * @dapm: DAPM context
2428 * Enables input/output pin regardless of any other state. This is
2429 * intended for use with microphone bias supplies used in microphone
2432 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2433 * do any widget power switching.
2435 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2438 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2441 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2445 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2451 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2454 * snd_soc_dapm_disable_pin - disable pin.
2455 * @dapm: DAPM context
2458 * Disables input/output pin and its parents or children widgets.
2459 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2460 * do any widget power switching.
2462 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2465 return snd_soc_dapm_set_pin(dapm, pin, 0);
2467 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2470 * snd_soc_dapm_nc_pin - permanently disable pin.
2471 * @dapm: DAPM context
2474 * Marks the specified pin as being not connected, disabling it along
2475 * any parent or child widgets. At present this is identical to
2476 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2477 * additional things such as disabling controls which only affect
2478 * paths through the pin.
2480 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2481 * do any widget power switching.
2483 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2485 return snd_soc_dapm_set_pin(dapm, pin, 0);
2487 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2490 * snd_soc_dapm_get_pin_status - get audio pin status
2491 * @dapm: DAPM context
2492 * @pin: audio signal pin endpoint (or start point)
2494 * Get audio pin status - connected or disconnected.
2496 * Returns 1 for connected otherwise 0.
2498 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2501 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2504 return w->connected;
2508 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2511 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2512 * @dapm: DAPM context
2513 * @pin: audio signal pin endpoint (or start point)
2515 * Mark the given endpoint or pin as ignoring suspend. When the
2516 * system is disabled a path between two endpoints flagged as ignoring
2517 * suspend will not be disabled. The path must already be enabled via
2518 * normal means at suspend time, it will not be turned on if it was not
2521 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2524 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2527 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2531 w->ignore_suspend = 1;
2535 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2538 * snd_soc_dapm_free - free dapm resources
2541 * Free all dapm widgets and resources.
2543 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2545 snd_soc_dapm_sys_remove(dapm->dev);
2546 dapm_debugfs_cleanup(dapm);
2547 dapm_free_widgets(dapm);
2548 list_del(&dapm->list);
2550 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2552 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2554 struct snd_soc_dapm_widget *w;
2555 LIST_HEAD(down_list);
2558 list_for_each_entry(w, &dapm->card->widgets, list) {
2559 if (w->dapm != dapm)
2562 dapm_seq_insert(w, &down_list, false);
2568 /* If there were no widgets to power down we're already in
2572 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2573 dapm_seq_run(dapm, &down_list, 0, false);
2574 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2579 * snd_soc_dapm_shutdown - callback for system shutdown
2581 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2583 struct snd_soc_codec *codec;
2585 list_for_each_entry(codec, &card->codec_dev_list, list) {
2586 soc_dapm_shutdown_codec(&codec->dapm);
2587 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
2591 /* Module information */
2592 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2593 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2594 MODULE_LICENSE("GPL");