2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
25 * o DAPM power change sequencing - allow for configurable per
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/async.h>
36 #include <linux/delay.h>
38 #include <linux/bitops.h>
39 #include <linux/platform_device.h>
40 #include <linux/jiffies.h>
41 #include <linux/debugfs.h>
42 #include <linux/slab.h>
43 #include <sound/core.h>
44 #include <sound/pcm.h>
45 #include <sound/pcm_params.h>
46 #include <sound/soc.h>
47 #include <sound/initval.h>
49 #include <trace/events/asoc.h>
51 /* dapm power sequences - make this per codec in the future */
52 static int dapm_up_seq[] = {
53 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
56 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
60 [snd_soc_dapm_virt_mux] = 5,
61 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
67 [snd_soc_dapm_out_drv] = 10,
68 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
70 [snd_soc_dapm_post] = 11,
73 static int dapm_down_seq[] = {
74 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_adc] = 1,
76 [snd_soc_dapm_hp] = 2,
77 [snd_soc_dapm_spk] = 2,
78 [snd_soc_dapm_out_drv] = 2,
79 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
81 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
86 [snd_soc_dapm_virt_mux] = 9,
87 [snd_soc_dapm_value_mux] = 9,
88 [snd_soc_dapm_aif_in] = 10,
89 [snd_soc_dapm_aif_out] = 10,
90 [snd_soc_dapm_supply] = 11,
91 [snd_soc_dapm_post] = 12,
94 static void pop_wait(u32 pop_time)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
100 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
113 vsnprintf(buf, PAGE_SIZE, fmt, args);
114 dev_info(dev, "%s", buf);
120 /* create a new dapm widget */
121 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
122 const struct snd_soc_dapm_widget *_widget)
124 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
128 * snd_soc_dapm_set_bias_level - set the bias level for the system
129 * @dapm: DAPM context
130 * @level: level to configure
132 * Configure the bias (power) levels for the SoC audio device.
134 * Returns 0 for success else error.
136 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
137 enum snd_soc_bias_level level)
139 struct snd_soc_card *card = dapm->card;
143 case SND_SOC_BIAS_ON:
144 dev_dbg(dapm->dev, "Setting full bias\n");
146 case SND_SOC_BIAS_PREPARE:
147 dev_dbg(dapm->dev, "Setting bias prepare\n");
149 case SND_SOC_BIAS_STANDBY:
150 dev_dbg(dapm->dev, "Setting standby bias\n");
152 case SND_SOC_BIAS_OFF:
153 dev_dbg(dapm->dev, "Setting bias off\n");
156 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
160 trace_snd_soc_bias_level_start(card, level);
162 if (card && card->set_bias_level)
163 ret = card->set_bias_level(card, level);
165 if (dapm->codec && dapm->codec->driver->set_bias_level)
166 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
168 dapm->bias_level = level;
171 if (card && card->set_bias_level_post)
172 ret = card->set_bias_level_post(card, level);
175 trace_snd_soc_bias_level_done(card, level);
180 /* set up initial codec paths */
181 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
182 struct snd_soc_dapm_path *p, int i)
185 case snd_soc_dapm_switch:
186 case snd_soc_dapm_mixer:
187 case snd_soc_dapm_mixer_named_ctl: {
189 struct soc_mixer_control *mc = (struct soc_mixer_control *)
190 w->kcontrols[i].private_value;
191 unsigned int reg = mc->reg;
192 unsigned int shift = mc->shift;
194 unsigned int mask = (1 << fls(max)) - 1;
195 unsigned int invert = mc->invert;
197 val = snd_soc_read(w->codec, reg);
198 val = (val >> shift) & mask;
200 if ((invert && !val) || (!invert && val))
206 case snd_soc_dapm_mux: {
207 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
208 int val, item, bitmask;
210 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
212 val = snd_soc_read(w->codec, e->reg);
213 item = (val >> e->shift_l) & (bitmask - 1);
216 for (i = 0; i < e->max; i++) {
217 if (!(strcmp(p->name, e->texts[i])) && item == i)
222 case snd_soc_dapm_virt_mux: {
223 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
226 /* since a virtual mux has no backing registers to
227 * decide which path to connect, it will try to match
228 * with the first enumeration. This is to ensure
229 * that the default mux choice (the first) will be
230 * correctly powered up during initialization.
232 if (!strcmp(p->name, e->texts[0]))
236 case snd_soc_dapm_value_mux: {
237 struct soc_enum *e = (struct soc_enum *)
238 w->kcontrols[i].private_value;
241 val = snd_soc_read(w->codec, e->reg);
242 val = (val >> e->shift_l) & e->mask;
243 for (item = 0; item < e->max; item++) {
244 if (val == e->values[item])
249 for (i = 0; i < e->max; i++) {
250 if (!(strcmp(p->name, e->texts[i])) && item == i)
255 /* does not effect routing - always connected */
256 case snd_soc_dapm_pga:
257 case snd_soc_dapm_out_drv:
258 case snd_soc_dapm_output:
259 case snd_soc_dapm_adc:
260 case snd_soc_dapm_input:
261 case snd_soc_dapm_dac:
262 case snd_soc_dapm_micbias:
263 case snd_soc_dapm_vmid:
264 case snd_soc_dapm_supply:
265 case snd_soc_dapm_aif_in:
266 case snd_soc_dapm_aif_out:
269 /* does effect routing - dynamically connected */
270 case snd_soc_dapm_hp:
271 case snd_soc_dapm_mic:
272 case snd_soc_dapm_spk:
273 case snd_soc_dapm_line:
274 case snd_soc_dapm_pre:
275 case snd_soc_dapm_post:
281 /* connect mux widget to its interconnecting audio paths */
282 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
283 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
284 struct snd_soc_dapm_path *path, const char *control_name,
285 const struct snd_kcontrol_new *kcontrol)
287 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
290 for (i = 0; i < e->max; i++) {
291 if (!(strcmp(control_name, e->texts[i]))) {
292 list_add(&path->list, &dapm->card->paths);
293 list_add(&path->list_sink, &dest->sources);
294 list_add(&path->list_source, &src->sinks);
295 path->name = (char*)e->texts[i];
296 dapm_set_path_status(dest, path, 0);
304 /* connect mixer widget to its interconnecting audio paths */
305 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
306 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
307 struct snd_soc_dapm_path *path, const char *control_name)
311 /* search for mixer kcontrol */
312 for (i = 0; i < dest->num_kcontrols; i++) {
313 if (!strcmp(control_name, dest->kcontrols[i].name)) {
314 list_add(&path->list, &dapm->card->paths);
315 list_add(&path->list_sink, &dest->sources);
316 list_add(&path->list_source, &src->sinks);
317 path->name = dest->kcontrols[i].name;
318 dapm_set_path_status(dest, path, i);
325 /* create new dapm mixer control */
326 static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
327 struct snd_soc_dapm_widget *w)
330 size_t name_len, prefix_len;
331 struct snd_soc_dapm_path *path;
332 struct snd_card *card = dapm->card->snd_card;
336 prefix = dapm->codec->name_prefix;
341 prefix_len = strlen(prefix) + 1;
346 for (i = 0; i < w->num_kcontrols; i++) {
349 list_for_each_entry(path, &w->sources, list_sink) {
351 /* mixer/mux paths name must match control name */
352 if (path->name != (char*)w->kcontrols[i].name)
355 /* add dapm control with long name.
356 * for dapm_mixer this is the concatenation of the
357 * mixer and kcontrol name.
358 * for dapm_mixer_named_ctl this is simply the
361 name_len = strlen(w->kcontrols[i].name) + 1;
362 if (w->id != snd_soc_dapm_mixer_named_ctl)
363 name_len += 1 + strlen(w->name);
365 path->long_name = kmalloc(name_len, GFP_KERNEL);
367 if (path->long_name == NULL)
372 /* The control will get a prefix from
373 * the control creation process but
374 * we're also using the same prefix
375 * for widgets so cut the prefix off
376 * the front of the widget name.
378 snprintf(path->long_name, name_len, "%s %s",
379 w->name + prefix_len,
380 w->kcontrols[i].name);
382 case snd_soc_dapm_mixer_named_ctl:
383 snprintf(path->long_name, name_len, "%s",
384 w->kcontrols[i].name);
388 path->long_name[name_len - 1] = '\0';
390 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
391 path->long_name, prefix);
392 ret = snd_ctl_add(card, path->kcontrol);
395 "asoc: failed to add dapm kcontrol %s: %d\n",
396 path->long_name, ret);
397 kfree(path->long_name);
398 path->long_name = NULL;
406 /* create new dapm mux control */
407 static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
408 struct snd_soc_dapm_widget *w)
410 struct snd_soc_dapm_path *path = NULL;
411 struct snd_kcontrol *kcontrol;
412 struct snd_card *card = dapm->card->snd_card;
417 if (!w->num_kcontrols) {
418 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
423 prefix = dapm->codec->name_prefix;
428 prefix_len = strlen(prefix) + 1;
432 /* The control will get a prefix from the control creation
433 * process but we're also using the same prefix for widgets so
434 * cut the prefix off the front of the widget name.
436 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name + prefix_len,
438 ret = snd_ctl_add(card, kcontrol);
443 list_for_each_entry(path, &w->sources, list_sink)
444 path->kcontrol = kcontrol;
449 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
453 /* create new dapm volume control */
454 static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
455 struct snd_soc_dapm_widget *w)
457 if (w->num_kcontrols)
458 dev_err(w->dapm->dev,
459 "asoc: PGA controls not supported: '%s'\n", w->name);
464 /* reset 'walked' bit for each dapm path */
465 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
467 struct snd_soc_dapm_path *p;
469 list_for_each_entry(p, &dapm->card->paths, list)
473 /* We implement power down on suspend by checking the power state of
474 * the ALSA card - when we are suspending the ALSA state for the card
477 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
479 int level = snd_power_get_state(widget->dapm->card->snd_card);
482 case SNDRV_CTL_POWER_D3hot:
483 case SNDRV_CTL_POWER_D3cold:
484 if (widget->ignore_suspend)
485 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
487 return widget->ignore_suspend;
494 * Recursively check for a completed path to an active or physically connected
495 * output widget. Returns number of complete paths.
497 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
499 struct snd_soc_dapm_path *path;
502 if (widget->id == snd_soc_dapm_supply)
505 switch (widget->id) {
506 case snd_soc_dapm_adc:
507 case snd_soc_dapm_aif_out:
509 return snd_soc_dapm_suspend_check(widget);
514 if (widget->connected) {
515 /* connected pin ? */
516 if (widget->id == snd_soc_dapm_output && !widget->ext)
517 return snd_soc_dapm_suspend_check(widget);
519 /* connected jack or spk ? */
520 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
521 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
522 return snd_soc_dapm_suspend_check(widget);
525 list_for_each_entry(path, &widget->sinks, list_source) {
529 if (path->sink && path->connect) {
531 con += is_connected_output_ep(path->sink);
539 * Recursively check for a completed path to an active or physically connected
540 * input widget. Returns number of complete paths.
542 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
544 struct snd_soc_dapm_path *path;
547 if (widget->id == snd_soc_dapm_supply)
550 /* active stream ? */
551 switch (widget->id) {
552 case snd_soc_dapm_dac:
553 case snd_soc_dapm_aif_in:
555 return snd_soc_dapm_suspend_check(widget);
560 if (widget->connected) {
561 /* connected pin ? */
562 if (widget->id == snd_soc_dapm_input && !widget->ext)
563 return snd_soc_dapm_suspend_check(widget);
565 /* connected VMID/Bias for lower pops */
566 if (widget->id == snd_soc_dapm_vmid)
567 return snd_soc_dapm_suspend_check(widget);
569 /* connected jack ? */
570 if (widget->id == snd_soc_dapm_mic ||
571 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
572 return snd_soc_dapm_suspend_check(widget);
575 list_for_each_entry(path, &widget->sources, list_sink) {
579 if (path->source && path->connect) {
581 con += is_connected_input_ep(path->source);
589 * Handler for generic register modifier widget.
591 int dapm_reg_event(struct snd_soc_dapm_widget *w,
592 struct snd_kcontrol *kcontrol, int event)
596 if (SND_SOC_DAPM_EVENT_ON(event))
601 snd_soc_update_bits(w->codec, -(w->reg + 1),
602 w->mask << w->shift, val << w->shift);
606 EXPORT_SYMBOL_GPL(dapm_reg_event);
608 /* Generic check to see if a widget should be powered.
610 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
614 in = is_connected_input_ep(w);
615 dapm_clear_walk(w->dapm);
616 out = is_connected_output_ep(w);
617 dapm_clear_walk(w->dapm);
618 return out != 0 && in != 0;
621 /* Check to see if an ADC has power */
622 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
627 in = is_connected_input_ep(w);
628 dapm_clear_walk(w->dapm);
631 return dapm_generic_check_power(w);
635 /* Check to see if a DAC has power */
636 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
641 out = is_connected_output_ep(w);
642 dapm_clear_walk(w->dapm);
645 return dapm_generic_check_power(w);
649 /* Check to see if a power supply is needed */
650 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
652 struct snd_soc_dapm_path *path;
655 /* Check if one of our outputs is connected */
656 list_for_each_entry(path, &w->sinks, list_source) {
657 if (path->connected &&
658 !path->connected(path->source, path->sink))
664 if (path->sink->force) {
669 if (path->sink->power_check &&
670 path->sink->power_check(path->sink)) {
676 dapm_clear_walk(w->dapm);
681 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
682 struct snd_soc_dapm_widget *b,
690 sort = dapm_down_seq;
692 if (sort[a->id] != sort[b->id])
693 return sort[a->id] - sort[b->id];
694 if (a->subseq != b->subseq) {
696 return a->subseq - b->subseq;
698 return b->subseq - a->subseq;
700 if (a->reg != b->reg)
701 return a->reg - b->reg;
702 if (a->dapm != b->dapm)
703 return (unsigned long)a->dapm - (unsigned long)b->dapm;
708 /* Insert a widget in order into a DAPM power sequence. */
709 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
710 struct list_head *list,
713 struct snd_soc_dapm_widget *w;
715 list_for_each_entry(w, list, power_list)
716 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
717 list_add_tail(&new_widget->power_list, &w->power_list);
721 list_add_tail(&new_widget->power_list, list);
724 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
725 struct snd_soc_dapm_widget *w, int event)
727 struct snd_soc_card *card = dapm->card;
732 case SND_SOC_DAPM_PRE_PMU:
736 case SND_SOC_DAPM_POST_PMU:
737 ev_name = "POST_PMU";
740 case SND_SOC_DAPM_PRE_PMD:
744 case SND_SOC_DAPM_POST_PMD:
745 ev_name = "POST_PMD";
753 if (w->power != power)
756 if (w->event && (w->event_flags & event)) {
757 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
759 trace_snd_soc_dapm_widget_event_start(w, event);
760 ret = w->event(w, NULL, event);
761 trace_snd_soc_dapm_widget_event_done(w, event);
763 pr_err("%s: %s event failed: %d\n",
764 ev_name, w->name, ret);
768 /* Apply the coalesced changes from a DAPM sequence */
769 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
770 struct list_head *pending)
772 struct snd_soc_card *card = dapm->card;
773 struct snd_soc_dapm_widget *w;
775 unsigned int value = 0;
776 unsigned int mask = 0;
777 unsigned int cur_mask;
779 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
782 list_for_each_entry(w, pending, power_list) {
783 cur_mask = 1 << w->shift;
784 BUG_ON(reg != w->reg);
795 pop_dbg(dapm->dev, card->pop_time,
796 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
797 w->name, reg, value, mask);
799 /* Check for events */
800 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
801 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
805 pop_dbg(dapm->dev, card->pop_time,
806 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
807 value, mask, reg, card->pop_time);
808 pop_wait(card->pop_time);
809 snd_soc_update_bits(dapm->codec, reg, mask, value);
812 list_for_each_entry(w, pending, power_list) {
813 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
814 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
818 /* Apply a DAPM power sequence.
820 * We walk over a pre-sorted list of widgets to apply power to. In
821 * order to minimise the number of writes to the device required
822 * multiple widgets will be updated in a single write where possible.
823 * Currently anything that requires more than a single write is not
826 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
827 struct list_head *list, int event, bool power_up)
829 struct snd_soc_dapm_widget *w, *n;
833 int cur_reg = SND_SOC_NOPM;
834 struct snd_soc_dapm_context *cur_dapm = NULL;
841 sort = dapm_down_seq;
843 list_for_each_entry_safe(w, n, list, power_list) {
846 /* Do we need to apply any queued changes? */
847 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
848 w->dapm != cur_dapm || w->subseq != cur_subseq) {
849 if (!list_empty(&pending))
850 dapm_seq_run_coalesced(cur_dapm, &pending);
852 if (cur_dapm && cur_dapm->seq_notifier) {
853 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
854 if (sort[i] == cur_sort)
855 cur_dapm->seq_notifier(cur_dapm,
860 INIT_LIST_HEAD(&pending);
863 cur_reg = SND_SOC_NOPM;
868 case snd_soc_dapm_pre:
870 list_for_each_entry_safe_continue(w, n, list,
873 if (event == SND_SOC_DAPM_STREAM_START)
875 NULL, SND_SOC_DAPM_PRE_PMU);
876 else if (event == SND_SOC_DAPM_STREAM_STOP)
878 NULL, SND_SOC_DAPM_PRE_PMD);
881 case snd_soc_dapm_post:
883 list_for_each_entry_safe_continue(w, n, list,
886 if (event == SND_SOC_DAPM_STREAM_START)
888 NULL, SND_SOC_DAPM_POST_PMU);
889 else if (event == SND_SOC_DAPM_STREAM_STOP)
891 NULL, SND_SOC_DAPM_POST_PMD);
895 /* Queue it up for application */
896 cur_sort = sort[w->id];
897 cur_subseq = w->subseq;
900 list_move(&w->power_list, &pending);
905 dev_err(w->dapm->dev,
906 "Failed to apply widget power: %d\n", ret);
909 if (!list_empty(&pending))
910 dapm_seq_run_coalesced(cur_dapm, &pending);
912 if (cur_dapm && cur_dapm->seq_notifier) {
913 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
914 if (sort[i] == cur_sort)
915 cur_dapm->seq_notifier(cur_dapm,
920 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
922 struct snd_soc_dapm_update *update = dapm->update;
923 struct snd_soc_dapm_widget *w;
932 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
933 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
935 pr_err("%s DAPM pre-event failed: %d\n",
939 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
942 pr_err("%s DAPM update failed: %d\n", w->name, ret);
945 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
946 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
948 pr_err("%s DAPM post-event failed: %d\n",
953 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
954 * they're changing state.
956 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
958 struct snd_soc_dapm_context *d = data;
961 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
962 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
965 "Failed to turn on bias: %d\n", ret);
968 /* If we're changing to all on or all off then prepare */
969 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
970 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
971 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
974 "Failed to prepare bias: %d\n", ret);
978 /* Async callback run prior to DAPM sequences - brings to their final
981 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
983 struct snd_soc_dapm_context *d = data;
986 /* If we just powered the last thing off drop to standby bias */
987 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
988 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
990 dev_err(d->dev, "Failed to apply standby bias: %d\n",
994 /* If we're in standby and can support bias off then do that */
995 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
996 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
998 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1001 /* If we just powered up then move to active bias */
1002 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1003 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1005 dev_err(d->dev, "Failed to apply active bias: %d\n",
1011 * Scan each dapm widget for complete audio path.
1012 * A complete path is a route that has valid endpoints i.e.:-
1014 * o DAC to output pin.
1015 * o Input Pin to ADC.
1016 * o Input pin to Output pin (bypass, sidetone)
1017 * o DAC to ADC (loopback).
1019 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1021 struct snd_soc_card *card = dapm->card;
1022 struct snd_soc_dapm_widget *w;
1023 struct snd_soc_dapm_context *d;
1025 LIST_HEAD(down_list);
1026 LIST_HEAD(async_domain);
1029 trace_snd_soc_dapm_start(card);
1031 list_for_each_entry(d, &card->dapm_list, list)
1035 /* Check which widgets we need to power and store them in
1036 * lists indicating if they should be powered up or down.
1038 list_for_each_entry(w, &card->widgets, list) {
1040 case snd_soc_dapm_pre:
1041 dapm_seq_insert(w, &down_list, false);
1043 case snd_soc_dapm_post:
1044 dapm_seq_insert(w, &up_list, true);
1048 if (!w->power_check)
1052 power = w->power_check(w);
1056 w->dapm->dev_power = 1;
1058 if (w->power == power)
1061 trace_snd_soc_dapm_widget_power(w, power);
1064 dapm_seq_insert(w, &up_list, true);
1066 dapm_seq_insert(w, &down_list, false);
1073 /* If there are no DAPM widgets then try to figure out power from the
1076 if (!dapm->n_widgets) {
1078 case SND_SOC_DAPM_STREAM_START:
1079 case SND_SOC_DAPM_STREAM_RESUME:
1080 dapm->dev_power = 1;
1082 case SND_SOC_DAPM_STREAM_STOP:
1083 dapm->dev_power = !!dapm->codec->active;
1085 case SND_SOC_DAPM_STREAM_SUSPEND:
1086 dapm->dev_power = 0;
1088 case SND_SOC_DAPM_STREAM_NOP:
1089 switch (dapm->bias_level) {
1090 case SND_SOC_BIAS_STANDBY:
1091 case SND_SOC_BIAS_OFF:
1092 dapm->dev_power = 0;
1095 dapm->dev_power = 1;
1104 /* Force all contexts in the card to the same bias state */
1106 list_for_each_entry(d, &card->dapm_list, list)
1109 list_for_each_entry(d, &card->dapm_list, list)
1110 d->dev_power = power;
1113 /* Run all the bias changes in parallel */
1114 list_for_each_entry(d, &dapm->card->dapm_list, list)
1115 async_schedule_domain(dapm_pre_sequence_async, d,
1117 async_synchronize_full_domain(&async_domain);
1119 /* Power down widgets first; try to avoid amplifying pops. */
1120 dapm_seq_run(dapm, &down_list, event, false);
1122 dapm_widget_update(dapm);
1125 dapm_seq_run(dapm, &up_list, event, true);
1127 /* Run all the bias changes in parallel */
1128 list_for_each_entry(d, &dapm->card->dapm_list, list)
1129 async_schedule_domain(dapm_post_sequence_async, d,
1131 async_synchronize_full_domain(&async_domain);
1133 pop_dbg(dapm->dev, card->pop_time,
1134 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1135 pop_wait(card->pop_time);
1137 trace_snd_soc_dapm_done(card);
1142 #ifdef CONFIG_DEBUG_FS
1143 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1145 file->private_data = inode->i_private;
1149 static ssize_t dapm_widget_power_read_file(struct file *file,
1150 char __user *user_buf,
1151 size_t count, loff_t *ppos)
1153 struct snd_soc_dapm_widget *w = file->private_data;
1157 struct snd_soc_dapm_path *p = NULL;
1159 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1163 in = is_connected_input_ep(w);
1164 dapm_clear_walk(w->dapm);
1165 out = is_connected_output_ep(w);
1166 dapm_clear_walk(w->dapm);
1168 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1169 w->name, w->power ? "On" : "Off", in, out);
1172 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1173 " - R%d(0x%x) bit %d",
1174 w->reg, w->reg, w->shift);
1176 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1179 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1181 w->active ? "active" : "inactive");
1183 list_for_each_entry(p, &w->sources, list_sink) {
1184 if (p->connected && !p->connected(w, p->sink))
1188 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1189 " in \"%s\" \"%s\"\n",
1190 p->name ? p->name : "static",
1193 list_for_each_entry(p, &w->sinks, list_source) {
1194 if (p->connected && !p->connected(w, p->sink))
1198 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1199 " out \"%s\" \"%s\"\n",
1200 p->name ? p->name : "static",
1204 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1210 static const struct file_operations dapm_widget_power_fops = {
1211 .open = dapm_widget_power_open_file,
1212 .read = dapm_widget_power_read_file,
1213 .llseek = default_llseek,
1216 static int dapm_bias_open_file(struct inode *inode, struct file *file)
1218 file->private_data = inode->i_private;
1222 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1223 size_t count, loff_t *ppos)
1225 struct snd_soc_dapm_context *dapm = file->private_data;
1228 switch (dapm->bias_level) {
1229 case SND_SOC_BIAS_ON:
1232 case SND_SOC_BIAS_PREPARE:
1233 level = "Prepare\n";
1235 case SND_SOC_BIAS_STANDBY:
1236 level = "Standby\n";
1238 case SND_SOC_BIAS_OFF:
1243 level = "Unknown\n";
1247 return simple_read_from_buffer(user_buf, count, ppos, level,
1251 static const struct file_operations dapm_bias_fops = {
1252 .open = dapm_bias_open_file,
1253 .read = dapm_bias_read_file,
1254 .llseek = default_llseek,
1257 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1258 struct dentry *parent)
1262 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1264 if (!dapm->debugfs_dapm) {
1266 "Failed to create DAPM debugfs directory\n");
1270 d = debugfs_create_file("bias_level", 0444,
1271 dapm->debugfs_dapm, dapm,
1275 "ASoC: Failed to create bias level debugfs file\n");
1278 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1280 struct snd_soc_dapm_context *dapm = w->dapm;
1283 if (!dapm->debugfs_dapm || !w->name)
1286 d = debugfs_create_file(w->name, 0444,
1287 dapm->debugfs_dapm, w,
1288 &dapm_widget_power_fops);
1290 dev_warn(w->dapm->dev,
1291 "ASoC: Failed to create %s debugfs file\n",
1296 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1297 struct dentry *parent)
1301 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1307 /* test and update the power status of a mux widget */
1308 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1309 struct snd_kcontrol *kcontrol, int change,
1310 int mux, struct soc_enum *e)
1312 struct snd_soc_dapm_path *path;
1315 if (widget->id != snd_soc_dapm_mux &&
1316 widget->id != snd_soc_dapm_virt_mux &&
1317 widget->id != snd_soc_dapm_value_mux)
1323 /* find dapm widget path assoc with kcontrol */
1324 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1325 if (path->kcontrol != kcontrol)
1328 if (!path->name || !e->texts[mux])
1332 /* we now need to match the string in the enum to the path */
1333 if (!(strcmp(path->name, e->texts[mux])))
1334 path->connect = 1; /* new connection */
1336 path->connect = 0; /* old connection must be powered down */
1340 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1345 /* test and update the power status of a mixer or switch widget */
1346 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1347 struct snd_kcontrol *kcontrol, int connect)
1349 struct snd_soc_dapm_path *path;
1352 if (widget->id != snd_soc_dapm_mixer &&
1353 widget->id != snd_soc_dapm_mixer_named_ctl &&
1354 widget->id != snd_soc_dapm_switch)
1357 /* find dapm widget path assoc with kcontrol */
1358 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1359 if (path->kcontrol != kcontrol)
1362 /* found, now check type */
1364 path->connect = connect;
1369 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1374 /* show dapm widget status in sys fs */
1375 static ssize_t dapm_widget_show(struct device *dev,
1376 struct device_attribute *attr, char *buf)
1378 struct snd_soc_pcm_runtime *rtd =
1379 container_of(dev, struct snd_soc_pcm_runtime, dev);
1380 struct snd_soc_codec *codec =rtd->codec;
1381 struct snd_soc_dapm_widget *w;
1383 char *state = "not set";
1385 list_for_each_entry(w, &codec->card->widgets, list) {
1386 if (w->dapm != &codec->dapm)
1389 /* only display widgets that burnm power */
1391 case snd_soc_dapm_hp:
1392 case snd_soc_dapm_mic:
1393 case snd_soc_dapm_spk:
1394 case snd_soc_dapm_line:
1395 case snd_soc_dapm_micbias:
1396 case snd_soc_dapm_dac:
1397 case snd_soc_dapm_adc:
1398 case snd_soc_dapm_pga:
1399 case snd_soc_dapm_out_drv:
1400 case snd_soc_dapm_mixer:
1401 case snd_soc_dapm_mixer_named_ctl:
1402 case snd_soc_dapm_supply:
1404 count += sprintf(buf + count, "%s: %s\n",
1405 w->name, w->power ? "On":"Off");
1412 switch (codec->dapm.bias_level) {
1413 case SND_SOC_BIAS_ON:
1416 case SND_SOC_BIAS_PREPARE:
1419 case SND_SOC_BIAS_STANDBY:
1422 case SND_SOC_BIAS_OFF:
1426 count += sprintf(buf + count, "PM State: %s\n", state);
1431 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1433 int snd_soc_dapm_sys_add(struct device *dev)
1435 return device_create_file(dev, &dev_attr_dapm_widget);
1438 static void snd_soc_dapm_sys_remove(struct device *dev)
1440 device_remove_file(dev, &dev_attr_dapm_widget);
1443 /* free all dapm widgets and resources */
1444 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1446 struct snd_soc_dapm_widget *w, *next_w;
1447 struct snd_soc_dapm_path *p, *next_p;
1449 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1450 if (w->dapm != dapm)
1454 * remove source and sink paths associated to this widget.
1455 * While removing the path, remove reference to it from both
1456 * source and sink widgets so that path is removed only once.
1458 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1459 list_del(&p->list_sink);
1460 list_del(&p->list_source);
1462 kfree(p->long_name);
1465 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1466 list_del(&p->list_sink);
1467 list_del(&p->list_source);
1469 kfree(p->long_name);
1477 static struct snd_soc_dapm_widget *dapm_find_widget(
1478 struct snd_soc_dapm_context *dapm, const char *pin,
1479 bool search_other_contexts)
1481 struct snd_soc_dapm_widget *w;
1482 struct snd_soc_dapm_widget *fallback = NULL;
1484 list_for_each_entry(w, &dapm->card->widgets, list) {
1485 if (!strcmp(w->name, pin)) {
1486 if (w->dapm == dapm)
1493 if (search_other_contexts)
1499 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1500 const char *pin, int status)
1502 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
1505 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1509 w->connected = status;
1517 * snd_soc_dapm_sync - scan and power dapm paths
1518 * @dapm: DAPM context
1520 * Walks all dapm audio paths and powers widgets according to their
1521 * stream or path usage.
1523 * Returns 0 for success.
1525 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1527 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1529 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1531 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1532 const struct snd_soc_dapm_route *route)
1534 struct snd_soc_dapm_path *path;
1535 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1536 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1538 const char *control = route->control;
1540 char prefixed_sink[80];
1541 char prefixed_source[80];
1544 if (dapm->codec && dapm->codec->name_prefix) {
1545 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1546 dapm->codec->name_prefix, route->sink);
1547 sink = prefixed_sink;
1548 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1549 dapm->codec->name_prefix, route->source);
1550 source = prefixed_source;
1553 source = route->source;
1557 * find src and dest widgets over all widgets but favor a widget from
1558 * current DAPM context
1560 list_for_each_entry(w, &dapm->card->widgets, list) {
1561 if (!wsink && !(strcmp(w->name, sink))) {
1563 if (w->dapm == dapm)
1567 if (!wsource && !(strcmp(w->name, source))) {
1569 if (w->dapm == dapm)
1573 /* use widget from another DAPM context if not found from this */
1579 if (wsource == NULL || wsink == NULL)
1582 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1586 path->source = wsource;
1588 path->connected = route->connected;
1589 INIT_LIST_HEAD(&path->list);
1590 INIT_LIST_HEAD(&path->list_source);
1591 INIT_LIST_HEAD(&path->list_sink);
1593 /* check for external widgets */
1594 if (wsink->id == snd_soc_dapm_input) {
1595 if (wsource->id == snd_soc_dapm_micbias ||
1596 wsource->id == snd_soc_dapm_mic ||
1597 wsource->id == snd_soc_dapm_line ||
1598 wsource->id == snd_soc_dapm_output)
1601 if (wsource->id == snd_soc_dapm_output) {
1602 if (wsink->id == snd_soc_dapm_spk ||
1603 wsink->id == snd_soc_dapm_hp ||
1604 wsink->id == snd_soc_dapm_line ||
1605 wsink->id == snd_soc_dapm_input)
1609 /* connect static paths */
1610 if (control == NULL) {
1611 list_add(&path->list, &dapm->card->paths);
1612 list_add(&path->list_sink, &wsink->sources);
1613 list_add(&path->list_source, &wsource->sinks);
1618 /* connect dynamic paths */
1619 switch (wsink->id) {
1620 case snd_soc_dapm_adc:
1621 case snd_soc_dapm_dac:
1622 case snd_soc_dapm_pga:
1623 case snd_soc_dapm_out_drv:
1624 case snd_soc_dapm_input:
1625 case snd_soc_dapm_output:
1626 case snd_soc_dapm_micbias:
1627 case snd_soc_dapm_vmid:
1628 case snd_soc_dapm_pre:
1629 case snd_soc_dapm_post:
1630 case snd_soc_dapm_supply:
1631 case snd_soc_dapm_aif_in:
1632 case snd_soc_dapm_aif_out:
1633 list_add(&path->list, &dapm->card->paths);
1634 list_add(&path->list_sink, &wsink->sources);
1635 list_add(&path->list_source, &wsource->sinks);
1638 case snd_soc_dapm_mux:
1639 case snd_soc_dapm_virt_mux:
1640 case snd_soc_dapm_value_mux:
1641 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1642 &wsink->kcontrols[0]);
1646 case snd_soc_dapm_switch:
1647 case snd_soc_dapm_mixer:
1648 case snd_soc_dapm_mixer_named_ctl:
1649 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1653 case snd_soc_dapm_hp:
1654 case snd_soc_dapm_mic:
1655 case snd_soc_dapm_line:
1656 case snd_soc_dapm_spk:
1657 list_add(&path->list, &dapm->card->paths);
1658 list_add(&path->list_sink, &wsink->sources);
1659 list_add(&path->list_source, &wsource->sinks);
1666 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1667 source, control, sink);
1673 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1674 * @dapm: DAPM context
1675 * @route: audio routes
1676 * @num: number of routes
1678 * Connects 2 dapm widgets together via a named audio path. The sink is
1679 * the widget receiving the audio signal, whilst the source is the sender
1680 * of the audio signal.
1682 * Returns 0 for success else error. On error all resources can be freed
1683 * with a call to snd_soc_card_free().
1685 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1686 const struct snd_soc_dapm_route *route, int num)
1690 for (i = 0; i < num; i++) {
1691 ret = snd_soc_dapm_add_route(dapm, route);
1693 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1694 route->source, route->sink);
1702 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1705 * snd_soc_dapm_new_widgets - add new dapm widgets
1706 * @dapm: DAPM context
1708 * Checks the codec for any new dapm widgets and creates them if found.
1710 * Returns 0 for success.
1712 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1714 struct snd_soc_dapm_widget *w;
1717 list_for_each_entry(w, &dapm->card->widgets, list)
1723 case snd_soc_dapm_switch:
1724 case snd_soc_dapm_mixer:
1725 case snd_soc_dapm_mixer_named_ctl:
1726 w->power_check = dapm_generic_check_power;
1727 dapm_new_mixer(dapm, w);
1729 case snd_soc_dapm_mux:
1730 case snd_soc_dapm_virt_mux:
1731 case snd_soc_dapm_value_mux:
1732 w->power_check = dapm_generic_check_power;
1733 dapm_new_mux(dapm, w);
1735 case snd_soc_dapm_adc:
1736 case snd_soc_dapm_aif_out:
1737 w->power_check = dapm_adc_check_power;
1739 case snd_soc_dapm_dac:
1740 case snd_soc_dapm_aif_in:
1741 w->power_check = dapm_dac_check_power;
1743 case snd_soc_dapm_pga:
1744 case snd_soc_dapm_out_drv:
1745 w->power_check = dapm_generic_check_power;
1746 dapm_new_pga(dapm, w);
1748 case snd_soc_dapm_input:
1749 case snd_soc_dapm_output:
1750 case snd_soc_dapm_micbias:
1751 case snd_soc_dapm_spk:
1752 case snd_soc_dapm_hp:
1753 case snd_soc_dapm_mic:
1754 case snd_soc_dapm_line:
1755 w->power_check = dapm_generic_check_power;
1757 case snd_soc_dapm_supply:
1758 w->power_check = dapm_supply_check_power;
1759 case snd_soc_dapm_vmid:
1760 case snd_soc_dapm_pre:
1761 case snd_soc_dapm_post:
1765 /* Read the initial power state from the device */
1767 val = snd_soc_read(w->codec, w->reg);
1768 val &= 1 << w->shift;
1778 dapm_debugfs_add_widget(w);
1781 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1784 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1787 * snd_soc_dapm_get_volsw - dapm mixer get callback
1788 * @kcontrol: mixer control
1789 * @ucontrol: control element information
1791 * Callback to get the value of a dapm mixer control.
1793 * Returns 0 for success.
1795 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1796 struct snd_ctl_elem_value *ucontrol)
1798 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1799 struct soc_mixer_control *mc =
1800 (struct soc_mixer_control *)kcontrol->private_value;
1801 unsigned int reg = mc->reg;
1802 unsigned int shift = mc->shift;
1803 unsigned int rshift = mc->rshift;
1805 unsigned int invert = mc->invert;
1806 unsigned int mask = (1 << fls(max)) - 1;
1808 ucontrol->value.integer.value[0] =
1809 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1810 if (shift != rshift)
1811 ucontrol->value.integer.value[1] =
1812 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1814 ucontrol->value.integer.value[0] =
1815 max - ucontrol->value.integer.value[0];
1816 if (shift != rshift)
1817 ucontrol->value.integer.value[1] =
1818 max - ucontrol->value.integer.value[1];
1823 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1826 * snd_soc_dapm_put_volsw - dapm mixer set callback
1827 * @kcontrol: mixer control
1828 * @ucontrol: control element information
1830 * Callback to set the value of a dapm mixer control.
1832 * Returns 0 for success.
1834 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1835 struct snd_ctl_elem_value *ucontrol)
1837 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1838 struct soc_mixer_control *mc =
1839 (struct soc_mixer_control *)kcontrol->private_value;
1840 unsigned int reg = mc->reg;
1841 unsigned int shift = mc->shift;
1843 unsigned int mask = (1 << fls(max)) - 1;
1844 unsigned int invert = mc->invert;
1846 int connect, change;
1847 struct snd_soc_dapm_update update;
1849 val = (ucontrol->value.integer.value[0] & mask);
1853 mask = mask << shift;
1856 mutex_lock(&widget->codec->mutex);
1857 widget->value = val;
1859 change = snd_soc_test_bits(widget->codec, reg, mask, val);
1862 /* new connection */
1863 connect = invert ? 0:1;
1865 /* old connection must be powered down */
1866 connect = invert ? 1:0;
1868 update.kcontrol = kcontrol;
1869 update.widget = widget;
1873 widget->dapm->update = &update;
1875 dapm_mixer_update_power(widget, kcontrol, connect);
1877 widget->dapm->update = NULL;
1880 mutex_unlock(&widget->codec->mutex);
1883 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1886 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1887 * @kcontrol: mixer control
1888 * @ucontrol: control element information
1890 * Callback to get the value of a dapm enumerated double mixer control.
1892 * Returns 0 for success.
1894 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1895 struct snd_ctl_elem_value *ucontrol)
1897 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1898 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1899 unsigned int val, bitmask;
1901 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1903 val = snd_soc_read(widget->codec, e->reg);
1904 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1905 if (e->shift_l != e->shift_r)
1906 ucontrol->value.enumerated.item[1] =
1907 (val >> e->shift_r) & (bitmask - 1);
1911 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1914 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1915 * @kcontrol: mixer control
1916 * @ucontrol: control element information
1918 * Callback to set the value of a dapm enumerated double mixer control.
1920 * Returns 0 for success.
1922 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1923 struct snd_ctl_elem_value *ucontrol)
1925 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1926 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1927 unsigned int val, mux, change;
1928 unsigned int mask, bitmask;
1929 struct snd_soc_dapm_update update;
1931 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1933 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1935 mux = ucontrol->value.enumerated.item[0];
1936 val = mux << e->shift_l;
1937 mask = (bitmask - 1) << e->shift_l;
1938 if (e->shift_l != e->shift_r) {
1939 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1941 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1942 mask |= (bitmask - 1) << e->shift_r;
1945 mutex_lock(&widget->codec->mutex);
1946 widget->value = val;
1947 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1949 update.kcontrol = kcontrol;
1950 update.widget = widget;
1951 update.reg = e->reg;
1954 widget->dapm->update = &update;
1956 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1958 widget->dapm->update = NULL;
1960 mutex_unlock(&widget->codec->mutex);
1963 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1966 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1967 * @kcontrol: mixer control
1968 * @ucontrol: control element information
1970 * Returns 0 for success.
1972 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1973 struct snd_ctl_elem_value *ucontrol)
1975 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1977 ucontrol->value.enumerated.item[0] = widget->value;
1981 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1984 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1985 * @kcontrol: mixer control
1986 * @ucontrol: control element information
1988 * Returns 0 for success.
1990 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1991 struct snd_ctl_elem_value *ucontrol)
1993 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1994 struct soc_enum *e =
1995 (struct soc_enum *)kcontrol->private_value;
1999 if (ucontrol->value.enumerated.item[0] >= e->max)
2002 mutex_lock(&widget->codec->mutex);
2004 change = widget->value != ucontrol->value.enumerated.item[0];
2005 widget->value = ucontrol->value.enumerated.item[0];
2006 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
2008 mutex_unlock(&widget->codec->mutex);
2011 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2014 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2016 * @kcontrol: mixer control
2017 * @ucontrol: control element information
2019 * Callback to get the value of a dapm semi enumerated double mixer control.
2021 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2022 * used for handling bitfield coded enumeration for example.
2024 * Returns 0 for success.
2026 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2027 struct snd_ctl_elem_value *ucontrol)
2029 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2030 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2031 unsigned int reg_val, val, mux;
2033 reg_val = snd_soc_read(widget->codec, e->reg);
2034 val = (reg_val >> e->shift_l) & e->mask;
2035 for (mux = 0; mux < e->max; mux++) {
2036 if (val == e->values[mux])
2039 ucontrol->value.enumerated.item[0] = mux;
2040 if (e->shift_l != e->shift_r) {
2041 val = (reg_val >> e->shift_r) & e->mask;
2042 for (mux = 0; mux < e->max; mux++) {
2043 if (val == e->values[mux])
2046 ucontrol->value.enumerated.item[1] = mux;
2051 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2054 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2056 * @kcontrol: mixer control
2057 * @ucontrol: control element information
2059 * Callback to set the value of a dapm semi enumerated double mixer control.
2061 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2062 * used for handling bitfield coded enumeration for example.
2064 * Returns 0 for success.
2066 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_value *ucontrol)
2069 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
2070 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2071 unsigned int val, mux, change;
2073 struct snd_soc_dapm_update update;
2075 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2077 mux = ucontrol->value.enumerated.item[0];
2078 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2079 mask = e->mask << e->shift_l;
2080 if (e->shift_l != e->shift_r) {
2081 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2083 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2084 mask |= e->mask << e->shift_r;
2087 mutex_lock(&widget->codec->mutex);
2088 widget->value = val;
2089 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2091 update.kcontrol = kcontrol;
2092 update.widget = widget;
2093 update.reg = e->reg;
2096 widget->dapm->update = &update;
2098 dapm_mux_update_power(widget, kcontrol, change, mux, e);
2100 widget->dapm->update = NULL;
2102 mutex_unlock(&widget->codec->mutex);
2105 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2108 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2110 * @kcontrol: mixer control
2111 * @uinfo: control element information
2113 * Callback to provide information about a pin switch control.
2115 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2116 struct snd_ctl_elem_info *uinfo)
2118 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2120 uinfo->value.integer.min = 0;
2121 uinfo->value.integer.max = 1;
2125 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2128 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2130 * @kcontrol: mixer control
2133 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2134 struct snd_ctl_elem_value *ucontrol)
2136 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2137 const char *pin = (const char *)kcontrol->private_value;
2139 mutex_lock(&codec->mutex);
2141 ucontrol->value.integer.value[0] =
2142 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2144 mutex_unlock(&codec->mutex);
2148 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2151 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2153 * @kcontrol: mixer control
2156 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2157 struct snd_ctl_elem_value *ucontrol)
2159 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2160 const char *pin = (const char *)kcontrol->private_value;
2162 mutex_lock(&codec->mutex);
2164 if (ucontrol->value.integer.value[0])
2165 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2167 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2169 snd_soc_dapm_sync(&codec->dapm);
2171 mutex_unlock(&codec->mutex);
2175 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2178 * snd_soc_dapm_new_control - create new dapm control
2179 * @dapm: DAPM context
2180 * @widget: widget template
2182 * Creates a new dapm control based upon the template.
2184 * Returns 0 for success else error.
2186 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2187 const struct snd_soc_dapm_widget *widget)
2189 struct snd_soc_dapm_widget *w;
2192 if ((w = dapm_cnew_widget(widget)) == NULL)
2195 name_len = strlen(widget->name) + 1;
2196 if (dapm->codec && dapm->codec->name_prefix)
2197 name_len += 1 + strlen(dapm->codec->name_prefix);
2198 w->name = kmalloc(name_len, GFP_KERNEL);
2199 if (w->name == NULL) {
2203 if (dapm->codec && dapm->codec->name_prefix)
2204 snprintf(w->name, name_len, "%s %s",
2205 dapm->codec->name_prefix, widget->name);
2207 snprintf(w->name, name_len, "%s", widget->name);
2211 w->codec = dapm->codec;
2212 INIT_LIST_HEAD(&w->sources);
2213 INIT_LIST_HEAD(&w->sinks);
2214 INIT_LIST_HEAD(&w->list);
2215 list_add(&w->list, &dapm->card->widgets);
2217 /* machine layer set ups unconnected pins and insertions */
2221 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2224 * snd_soc_dapm_new_controls - create new dapm controls
2225 * @dapm: DAPM context
2226 * @widget: widget array
2227 * @num: number of widgets
2229 * Creates new DAPM controls based upon the templates.
2231 * Returns 0 for success else error.
2233 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2234 const struct snd_soc_dapm_widget *widget,
2239 for (i = 0; i < num; i++) {
2240 ret = snd_soc_dapm_new_control(dapm, widget);
2243 "ASoC: Failed to create DAPM control %s: %d\n",
2251 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2253 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2254 const char *stream, int event)
2256 struct snd_soc_dapm_widget *w;
2258 list_for_each_entry(w, &dapm->card->widgets, list)
2260 if (!w->sname || w->dapm != dapm)
2262 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2263 w->name, w->sname, stream, event);
2264 if (strstr(w->sname, stream)) {
2266 case SND_SOC_DAPM_STREAM_START:
2269 case SND_SOC_DAPM_STREAM_STOP:
2272 case SND_SOC_DAPM_STREAM_SUSPEND:
2273 case SND_SOC_DAPM_STREAM_RESUME:
2274 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2275 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2281 dapm_power_widgets(dapm, event);
2285 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2286 * @rtd: PCM runtime data
2287 * @stream: stream name
2288 * @event: stream event
2290 * Sends a stream event to the dapm core. The core then makes any
2291 * necessary widget power changes.
2293 * Returns 0 for success else error.
2295 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2296 const char *stream, int event)
2298 struct snd_soc_codec *codec = rtd->codec;
2303 mutex_lock(&codec->mutex);
2304 soc_dapm_stream_event(&codec->dapm, stream, event);
2305 mutex_unlock(&codec->mutex);
2310 * snd_soc_dapm_enable_pin - enable pin.
2311 * @dapm: DAPM context
2314 * Enables input/output pin and its parents or children widgets iff there is
2315 * a valid audio route and active audio stream.
2316 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2317 * do any widget power switching.
2319 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2321 return snd_soc_dapm_set_pin(dapm, pin, 1);
2323 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2326 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2327 * @dapm: DAPM context
2330 * Enables input/output pin regardless of any other state. This is
2331 * intended for use with microphone bias supplies used in microphone
2334 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2335 * do any widget power switching.
2337 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2340 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2343 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2347 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2353 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2356 * snd_soc_dapm_disable_pin - disable pin.
2357 * @dapm: DAPM context
2360 * Disables input/output pin and its parents or children widgets.
2361 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2362 * do any widget power switching.
2364 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2367 return snd_soc_dapm_set_pin(dapm, pin, 0);
2369 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2372 * snd_soc_dapm_nc_pin - permanently disable pin.
2373 * @dapm: DAPM context
2376 * Marks the specified pin as being not connected, disabling it along
2377 * any parent or child widgets. At present this is identical to
2378 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2379 * additional things such as disabling controls which only affect
2380 * paths through the pin.
2382 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2383 * do any widget power switching.
2385 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2387 return snd_soc_dapm_set_pin(dapm, pin, 0);
2389 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2392 * snd_soc_dapm_get_pin_status - get audio pin status
2393 * @dapm: DAPM context
2394 * @pin: audio signal pin endpoint (or start point)
2396 * Get audio pin status - connected or disconnected.
2398 * Returns 1 for connected otherwise 0.
2400 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2403 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2406 return w->connected;
2410 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2413 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2414 * @dapm: DAPM context
2415 * @pin: audio signal pin endpoint (or start point)
2417 * Mark the given endpoint or pin as ignoring suspend. When the
2418 * system is disabled a path between two endpoints flagged as ignoring
2419 * suspend will not be disabled. The path must already be enabled via
2420 * normal means at suspend time, it will not be turned on if it was not
2423 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2426 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2429 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2433 w->ignore_suspend = 1;
2437 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2440 * snd_soc_dapm_free - free dapm resources
2443 * Free all dapm widgets and resources.
2445 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2447 snd_soc_dapm_sys_remove(dapm->dev);
2448 dapm_free_widgets(dapm);
2449 list_del(&dapm->list);
2451 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2453 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2455 struct snd_soc_dapm_widget *w;
2456 LIST_HEAD(down_list);
2459 list_for_each_entry(w, &dapm->card->widgets, list) {
2460 if (w->dapm != dapm)
2463 dapm_seq_insert(w, &down_list, false);
2469 /* If there were no widgets to power down we're already in
2473 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2474 dapm_seq_run(dapm, &down_list, 0, false);
2475 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
2480 * snd_soc_dapm_shutdown - callback for system shutdown
2482 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2484 struct snd_soc_codec *codec;
2486 list_for_each_entry(codec, &card->codec_dev_list, list) {
2487 soc_dapm_shutdown_codec(&codec->dapm);
2488 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
2492 /* Module information */
2493 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2494 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2495 MODULE_LICENSE("GPL");