2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
25 * o DAPM power change sequencing - allow for configurable per
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
37 #include <linux/bitops.h>
38 #include <linux/platform_device.h>
39 #include <linux/jiffies.h>
40 #include <linux/debugfs.h>
41 #include <linux/slab.h>
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc.h>
46 #include <sound/initval.h>
48 #include <trace/events/asoc.h>
50 /* dapm power sequences - make this per codec in the future */
51 static int dapm_up_seq[] = {
52 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
54 [snd_soc_dapm_micbias] = 2,
55 [snd_soc_dapm_aif_in] = 3,
56 [snd_soc_dapm_aif_out] = 3,
57 [snd_soc_dapm_mic] = 4,
58 [snd_soc_dapm_mux] = 5,
59 [snd_soc_dapm_value_mux] = 5,
60 [snd_soc_dapm_dac] = 6,
61 [snd_soc_dapm_mixer] = 7,
62 [snd_soc_dapm_mixer_named_ctl] = 7,
63 [snd_soc_dapm_pga] = 8,
64 [snd_soc_dapm_adc] = 9,
65 [snd_soc_dapm_out_drv] = 10,
66 [snd_soc_dapm_hp] = 10,
67 [snd_soc_dapm_spk] = 10,
68 [snd_soc_dapm_post] = 11,
71 static int dapm_down_seq[] = {
72 [snd_soc_dapm_pre] = 0,
73 [snd_soc_dapm_adc] = 1,
74 [snd_soc_dapm_hp] = 2,
75 [snd_soc_dapm_spk] = 2,
76 [snd_soc_dapm_out_drv] = 2,
77 [snd_soc_dapm_pga] = 4,
78 [snd_soc_dapm_mixer_named_ctl] = 5,
79 [snd_soc_dapm_mixer] = 5,
80 [snd_soc_dapm_dac] = 6,
81 [snd_soc_dapm_mic] = 7,
82 [snd_soc_dapm_micbias] = 8,
83 [snd_soc_dapm_mux] = 9,
84 [snd_soc_dapm_value_mux] = 9,
85 [snd_soc_dapm_aif_in] = 10,
86 [snd_soc_dapm_aif_out] = 10,
87 [snd_soc_dapm_supply] = 11,
88 [snd_soc_dapm_post] = 12,
91 static void pop_wait(u32 pop_time)
94 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
97 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
105 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
110 vsnprintf(buf, PAGE_SIZE, fmt, args);
117 /* create a new dapm widget */
118 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
119 const struct snd_soc_dapm_widget *_widget)
121 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
125 * snd_soc_dapm_set_bias_level - set the bias level for the system
126 * @card: audio device
127 * @level: level to configure
129 * Configure the bias (power) levels for the SoC audio device.
131 * Returns 0 for success else error.
133 static int snd_soc_dapm_set_bias_level(struct snd_soc_card *card,
134 struct snd_soc_dapm_context *dapm,
135 enum snd_soc_bias_level level)
140 case SND_SOC_BIAS_ON:
141 dev_dbg(dapm->dev, "Setting full bias\n");
143 case SND_SOC_BIAS_PREPARE:
144 dev_dbg(dapm->dev, "Setting bias prepare\n");
146 case SND_SOC_BIAS_STANDBY:
147 dev_dbg(dapm->dev, "Setting standby bias\n");
149 case SND_SOC_BIAS_OFF:
150 dev_dbg(dapm->dev, "Setting bias off\n");
153 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
157 trace_snd_soc_bias_level_start(card, level);
159 if (card && card->set_bias_level)
160 ret = card->set_bias_level(card, level);
162 if (dapm->codec && dapm->codec->driver->set_bias_level)
163 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
165 dapm->bias_level = level;
168 if (card && card->set_bias_level_post)
169 ret = card->set_bias_level_post(card, level);
172 trace_snd_soc_bias_level_done(card, level);
177 /* set up initial codec paths */
178 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
179 struct snd_soc_dapm_path *p, int i)
182 case snd_soc_dapm_switch:
183 case snd_soc_dapm_mixer:
184 case snd_soc_dapm_mixer_named_ctl: {
186 struct soc_mixer_control *mc = (struct soc_mixer_control *)
187 w->kcontrols[i].private_value;
188 unsigned int reg = mc->reg;
189 unsigned int shift = mc->shift;
191 unsigned int mask = (1 << fls(max)) - 1;
192 unsigned int invert = mc->invert;
194 val = snd_soc_read(w->codec, reg);
195 val = (val >> shift) & mask;
197 if ((invert && !val) || (!invert && val))
203 case snd_soc_dapm_mux: {
204 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
205 int val, item, bitmask;
207 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
209 val = snd_soc_read(w->codec, e->reg);
210 item = (val >> e->shift_l) & (bitmask - 1);
213 for (i = 0; i < e->max; i++) {
214 if (!(strcmp(p->name, e->texts[i])) && item == i)
219 case snd_soc_dapm_value_mux: {
220 struct soc_enum *e = (struct soc_enum *)
221 w->kcontrols[i].private_value;
224 val = snd_soc_read(w->codec, e->reg);
225 val = (val >> e->shift_l) & e->mask;
226 for (item = 0; item < e->max; item++) {
227 if (val == e->values[item])
232 for (i = 0; i < e->max; i++) {
233 if (!(strcmp(p->name, e->texts[i])) && item == i)
238 /* does not effect routing - always connected */
239 case snd_soc_dapm_pga:
240 case snd_soc_dapm_out_drv:
241 case snd_soc_dapm_output:
242 case snd_soc_dapm_adc:
243 case snd_soc_dapm_input:
244 case snd_soc_dapm_dac:
245 case snd_soc_dapm_micbias:
246 case snd_soc_dapm_vmid:
247 case snd_soc_dapm_supply:
248 case snd_soc_dapm_aif_in:
249 case snd_soc_dapm_aif_out:
252 /* does effect routing - dynamically connected */
253 case snd_soc_dapm_hp:
254 case snd_soc_dapm_mic:
255 case snd_soc_dapm_spk:
256 case snd_soc_dapm_line:
257 case snd_soc_dapm_pre:
258 case snd_soc_dapm_post:
264 /* connect mux widget to its interconnecting audio paths */
265 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
266 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
267 struct snd_soc_dapm_path *path, const char *control_name,
268 const struct snd_kcontrol_new *kcontrol)
270 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
273 for (i = 0; i < e->max; i++) {
274 if (!(strcmp(control_name, e->texts[i]))) {
275 list_add(&path->list, &dapm->card->paths);
276 list_add(&path->list_sink, &dest->sources);
277 list_add(&path->list_source, &src->sinks);
278 path->name = (char*)e->texts[i];
279 dapm_set_path_status(dest, path, 0);
287 /* connect mixer widget to its interconnecting audio paths */
288 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
289 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
290 struct snd_soc_dapm_path *path, const char *control_name)
294 /* search for mixer kcontrol */
295 for (i = 0; i < dest->num_kcontrols; i++) {
296 if (!strcmp(control_name, dest->kcontrols[i].name)) {
297 list_add(&path->list, &dapm->card->paths);
298 list_add(&path->list_sink, &dest->sources);
299 list_add(&path->list_source, &src->sinks);
300 path->name = dest->kcontrols[i].name;
301 dapm_set_path_status(dest, path, i);
308 /* update dapm codec register bits */
309 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
312 unsigned int old, new;
313 struct snd_soc_codec *codec = widget->codec;
314 struct snd_soc_dapm_context *dapm = widget->dapm;
315 struct snd_soc_card *card = dapm->card;
317 /* check for valid widgets */
318 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
319 widget->id == snd_soc_dapm_output ||
320 widget->id == snd_soc_dapm_hp ||
321 widget->id == snd_soc_dapm_mic ||
322 widget->id == snd_soc_dapm_line ||
323 widget->id == snd_soc_dapm_spk)
326 power = widget->power;
328 power = (power ? 0:1);
330 old = snd_soc_read(codec, widget->reg);
331 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
335 pop_dbg(dapm->dev, card->pop_time,
336 "pop test %s : %s in %d ms\n",
337 widget->name, widget->power ? "on" : "off",
339 pop_wait(card->pop_time);
340 snd_soc_write(codec, widget->reg, new);
342 dev_dbg(dapm->dev, "reg %x old %x new %x change %d\n", widget->reg,
347 /* create new dapm mixer control */
348 static int dapm_new_mixer(struct snd_soc_dapm_context *dapm,
349 struct snd_soc_dapm_widget *w)
353 struct snd_soc_dapm_path *path;
354 struct snd_card *card = dapm->codec->card->snd_card;
357 for (i = 0; i < w->num_kcontrols; i++) {
360 list_for_each_entry(path, &w->sources, list_sink) {
362 /* mixer/mux paths name must match control name */
363 if (path->name != (char*)w->kcontrols[i].name)
366 /* add dapm control with long name.
367 * for dapm_mixer this is the concatenation of the
368 * mixer and kcontrol name.
369 * for dapm_mixer_named_ctl this is simply the
372 name_len = strlen(w->kcontrols[i].name) + 1;
373 if (w->id != snd_soc_dapm_mixer_named_ctl)
374 name_len += 1 + strlen(w->name);
376 path->long_name = kmalloc(name_len, GFP_KERNEL);
378 if (path->long_name == NULL)
383 snprintf(path->long_name, name_len, "%s %s",
384 w->name, w->kcontrols[i].name);
386 case snd_soc_dapm_mixer_named_ctl:
387 snprintf(path->long_name, name_len, "%s",
388 w->kcontrols[i].name);
392 path->long_name[name_len - 1] = '\0';
394 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
396 ret = snd_ctl_add(card, path->kcontrol);
399 "asoc: failed to add dapm kcontrol %s: %d\n",
400 path->long_name, ret);
401 kfree(path->long_name);
402 path->long_name = NULL;
410 /* create new dapm mux control */
411 static int dapm_new_mux(struct snd_soc_dapm_context *dapm,
412 struct snd_soc_dapm_widget *w)
414 struct snd_soc_dapm_path *path = NULL;
415 struct snd_kcontrol *kcontrol;
416 struct snd_card *card = dapm->codec->card->snd_card;
419 if (!w->num_kcontrols) {
420 dev_err(dapm->dev, "asoc: mux %s has no controls\n", w->name);
424 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
425 ret = snd_ctl_add(card, kcontrol);
430 list_for_each_entry(path, &w->sources, list_sink)
431 path->kcontrol = kcontrol;
436 dev_err(dapm->dev, "asoc: failed to add kcontrol %s\n", w->name);
440 /* create new dapm volume control */
441 static int dapm_new_pga(struct snd_soc_dapm_context *dapm,
442 struct snd_soc_dapm_widget *w)
444 if (w->num_kcontrols)
445 dev_err(w->dapm->dev,
446 "asoc: PGA controls not supported: '%s'\n", w->name);
451 /* reset 'walked' bit for each dapm path */
452 static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
454 struct snd_soc_dapm_path *p;
456 list_for_each_entry(p, &dapm->card->paths, list)
460 /* We implement power down on suspend by checking the power state of
461 * the ALSA card - when we are suspending the ALSA state for the card
464 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
466 int level = snd_power_get_state(widget->dapm->codec->card->snd_card);
469 case SNDRV_CTL_POWER_D3hot:
470 case SNDRV_CTL_POWER_D3cold:
471 if (widget->ignore_suspend)
472 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
474 return widget->ignore_suspend;
481 * Recursively check for a completed path to an active or physically connected
482 * output widget. Returns number of complete paths.
484 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
486 struct snd_soc_dapm_path *path;
489 if (widget->id == snd_soc_dapm_supply)
492 switch (widget->id) {
493 case snd_soc_dapm_adc:
494 case snd_soc_dapm_aif_out:
496 return snd_soc_dapm_suspend_check(widget);
501 if (widget->connected) {
502 /* connected pin ? */
503 if (widget->id == snd_soc_dapm_output && !widget->ext)
504 return snd_soc_dapm_suspend_check(widget);
506 /* connected jack or spk ? */
507 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
508 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
509 return snd_soc_dapm_suspend_check(widget);
512 list_for_each_entry(path, &widget->sinks, list_source) {
516 if (path->sink && path->connect) {
518 con += is_connected_output_ep(path->sink);
526 * Recursively check for a completed path to an active or physically connected
527 * input widget. Returns number of complete paths.
529 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
531 struct snd_soc_dapm_path *path;
534 if (widget->id == snd_soc_dapm_supply)
537 /* active stream ? */
538 switch (widget->id) {
539 case snd_soc_dapm_dac:
540 case snd_soc_dapm_aif_in:
542 return snd_soc_dapm_suspend_check(widget);
547 if (widget->connected) {
548 /* connected pin ? */
549 if (widget->id == snd_soc_dapm_input && !widget->ext)
550 return snd_soc_dapm_suspend_check(widget);
552 /* connected VMID/Bias for lower pops */
553 if (widget->id == snd_soc_dapm_vmid)
554 return snd_soc_dapm_suspend_check(widget);
556 /* connected jack ? */
557 if (widget->id == snd_soc_dapm_mic ||
558 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
559 return snd_soc_dapm_suspend_check(widget);
562 list_for_each_entry(path, &widget->sources, list_sink) {
566 if (path->source && path->connect) {
568 con += is_connected_input_ep(path->source);
576 * Handler for generic register modifier widget.
578 int dapm_reg_event(struct snd_soc_dapm_widget *w,
579 struct snd_kcontrol *kcontrol, int event)
583 if (SND_SOC_DAPM_EVENT_ON(event))
588 snd_soc_update_bits(w->codec, -(w->reg + 1),
589 w->mask << w->shift, val << w->shift);
593 EXPORT_SYMBOL_GPL(dapm_reg_event);
595 /* Standard power change method, used to apply power changes to most
598 static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
602 /* call any power change event handlers */
604 dev_dbg(w->dapm->dev, "power %s event for %s flags %x\n",
605 w->power ? "on" : "off",
606 w->name, w->event_flags);
608 /* power up pre event */
609 if (w->power && w->event &&
610 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
611 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
616 /* power down pre event */
617 if (!w->power && w->event &&
618 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
619 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
626 /* power up post event */
627 if (w->power && w->event &&
628 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
630 NULL, SND_SOC_DAPM_POST_PMU);
635 /* power down post event */
636 if (!w->power && w->event &&
637 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
638 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
646 /* Generic check to see if a widget should be powered.
648 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
652 in = is_connected_input_ep(w);
653 dapm_clear_walk(w->dapm);
654 out = is_connected_output_ep(w);
655 dapm_clear_walk(w->dapm);
656 return out != 0 && in != 0;
659 /* Check to see if an ADC has power */
660 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
665 in = is_connected_input_ep(w);
666 dapm_clear_walk(w->dapm);
669 return dapm_generic_check_power(w);
673 /* Check to see if a DAC has power */
674 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
679 out = is_connected_output_ep(w);
680 dapm_clear_walk(w->dapm);
683 return dapm_generic_check_power(w);
687 /* Check to see if a power supply is needed */
688 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
690 struct snd_soc_dapm_path *path;
693 /* Check if one of our outputs is connected */
694 list_for_each_entry(path, &w->sinks, list_source) {
695 if (path->connected &&
696 !path->connected(path->source, path->sink))
699 if (path->sink && path->sink->power_check &&
700 path->sink->power_check(path->sink)) {
706 dapm_clear_walk(w->dapm);
711 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
712 struct snd_soc_dapm_widget *b,
715 if (sort[a->id] != sort[b->id])
716 return sort[a->id] - sort[b->id];
717 if (a->reg != b->reg)
718 return a->reg - b->reg;
719 if (a->dapm != b->dapm)
720 return (unsigned long)a->dapm - (unsigned long)b->dapm;
725 /* Insert a widget in order into a DAPM power sequence. */
726 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
727 struct list_head *list,
730 struct snd_soc_dapm_widget *w;
732 list_for_each_entry(w, list, power_list)
733 if (dapm_seq_compare(new_widget, w, sort) < 0) {
734 list_add_tail(&new_widget->power_list, &w->power_list);
738 list_add_tail(&new_widget->power_list, list);
741 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
742 struct snd_soc_dapm_widget *w, int event)
744 struct snd_soc_card *card = dapm->card;
749 case SND_SOC_DAPM_PRE_PMU:
753 case SND_SOC_DAPM_POST_PMU:
754 ev_name = "POST_PMU";
757 case SND_SOC_DAPM_PRE_PMD:
761 case SND_SOC_DAPM_POST_PMD:
762 ev_name = "POST_PMD";
770 if (w->power != power)
773 if (w->event && (w->event_flags & event)) {
774 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
776 trace_snd_soc_dapm_widget_event_start(w, event);
777 ret = w->event(w, NULL, event);
778 trace_snd_soc_dapm_widget_event_done(w, event);
780 pr_err("%s: %s event failed: %d\n",
781 ev_name, w->name, ret);
785 /* Apply the coalesced changes from a DAPM sequence */
786 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
787 struct list_head *pending)
789 struct snd_soc_card *card = dapm->card;
790 struct snd_soc_dapm_widget *w;
792 unsigned int value = 0;
793 unsigned int mask = 0;
794 unsigned int cur_mask;
796 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
799 list_for_each_entry(w, pending, power_list) {
800 cur_mask = 1 << w->shift;
801 BUG_ON(reg != w->reg);
812 pop_dbg(dapm->dev, card->pop_time,
813 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
814 w->name, reg, value, mask);
816 /* Check for events */
817 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
818 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
822 pop_dbg(dapm->dev, card->pop_time,
823 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
824 value, mask, reg, card->pop_time);
825 pop_wait(card->pop_time);
826 snd_soc_update_bits(dapm->codec, reg, mask, value);
829 list_for_each_entry(w, pending, power_list) {
830 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
831 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
835 /* Apply a DAPM power sequence.
837 * We walk over a pre-sorted list of widgets to apply power to. In
838 * order to minimise the number of writes to the device required
839 * multiple widgets will be updated in a single write where possible.
840 * Currently anything that requires more than a single write is not
843 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
844 struct list_head *list, int event, int sort[])
846 struct snd_soc_dapm_widget *w, *n;
849 int cur_reg = SND_SOC_NOPM;
852 list_for_each_entry_safe(w, n, list, power_list) {
855 /* Do we need to apply any queued changes? */
856 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
857 if (!list_empty(&pending))
858 dapm_seq_run_coalesced(dapm, &pending);
860 INIT_LIST_HEAD(&pending);
862 cur_reg = SND_SOC_NOPM;
866 case snd_soc_dapm_pre:
868 list_for_each_entry_safe_continue(w, n, list,
871 if (event == SND_SOC_DAPM_STREAM_START)
873 NULL, SND_SOC_DAPM_PRE_PMU);
874 else if (event == SND_SOC_DAPM_STREAM_STOP)
876 NULL, SND_SOC_DAPM_PRE_PMD);
879 case snd_soc_dapm_post:
881 list_for_each_entry_safe_continue(w, n, list,
884 if (event == SND_SOC_DAPM_STREAM_START)
886 NULL, SND_SOC_DAPM_POST_PMU);
887 else if (event == SND_SOC_DAPM_STREAM_STOP)
889 NULL, SND_SOC_DAPM_POST_PMD);
892 case snd_soc_dapm_input:
893 case snd_soc_dapm_output:
894 case snd_soc_dapm_hp:
895 case snd_soc_dapm_mic:
896 case snd_soc_dapm_line:
897 case snd_soc_dapm_spk:
898 /* No register support currently */
899 ret = dapm_generic_apply_power(w);
903 /* Queue it up for application */
904 cur_sort = sort[w->id];
906 list_move(&w->power_list, &pending);
911 dev_err(w->dapm->dev,
912 "Failed to apply widget power: %d\n", ret);
915 if (!list_empty(&pending))
916 dapm_seq_run_coalesced(dapm, &pending);
920 * Scan each dapm widget for complete audio path.
921 * A complete path is a route that has valid endpoints i.e.:-
923 * o DAC to output pin.
924 * o Input Pin to ADC.
925 * o Input pin to Output pin (bypass, sidetone)
926 * o DAC to ADC (loopback).
928 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
930 struct snd_soc_card *card = dapm->codec->card;
931 struct snd_soc_dapm_widget *w;
933 LIST_HEAD(down_list);
938 trace_snd_soc_dapm_start(card);
940 /* Check which widgets we need to power and store them in
941 * lists indicating if they should be powered up or down.
943 list_for_each_entry(w, &card->widgets, list) {
947 case snd_soc_dapm_pre:
948 dapm_seq_insert(w, &down_list, dapm_down_seq);
950 case snd_soc_dapm_post:
951 dapm_seq_insert(w, &up_list, dapm_up_seq);
959 power = w->power_check(w);
965 if (w->power == power)
968 trace_snd_soc_dapm_widget_power(w, power);
971 dapm_seq_insert(w, &up_list, dapm_up_seq);
973 dapm_seq_insert(w, &down_list, dapm_down_seq);
980 /* If there are no DAPM widgets then try to figure out power from the
983 if (!dapm->n_widgets) {
985 case SND_SOC_DAPM_STREAM_START:
986 case SND_SOC_DAPM_STREAM_RESUME:
989 case SND_SOC_DAPM_STREAM_STOP:
990 sys_power = !!dapm->codec->active;
992 case SND_SOC_DAPM_STREAM_SUSPEND:
995 case SND_SOC_DAPM_STREAM_NOP:
996 switch (dapm->bias_level) {
997 case SND_SOC_BIAS_STANDBY:
998 case SND_SOC_BIAS_OFF:
1011 if (sys_power && dapm->bias_level == SND_SOC_BIAS_OFF) {
1012 ret = snd_soc_dapm_set_bias_level(card, dapm,
1013 SND_SOC_BIAS_STANDBY);
1016 "Failed to turn on bias: %d\n", ret);
1019 /* If we're changing to all on or all off then prepare */
1020 if ((sys_power && dapm->bias_level == SND_SOC_BIAS_STANDBY) ||
1021 (!sys_power && dapm->bias_level == SND_SOC_BIAS_ON)) {
1022 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_PREPARE);
1025 "Failed to prepare bias: %d\n", ret);
1028 /* Power down widgets first; try to avoid amplifying pops. */
1029 dapm_seq_run(dapm, &down_list, event, dapm_down_seq);
1032 dapm_seq_run(dapm, &up_list, event, dapm_up_seq);
1034 /* If we just powered the last thing off drop to standby bias */
1035 if (dapm->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
1036 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_STANDBY);
1039 "Failed to apply standby bias: %d\n", ret);
1042 /* If we're in standby and can support bias off then do that */
1043 if (dapm->bias_level == SND_SOC_BIAS_STANDBY &&
1044 dapm->idle_bias_off) {
1045 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_OFF);
1048 "Failed to turn off bias: %d\n", ret);
1051 /* If we just powered up then move to active bias */
1052 if (dapm->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
1053 ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_ON);
1056 "Failed to apply active bias: %d\n", ret);
1059 pop_dbg(dapm->dev, card->pop_time,
1060 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1061 pop_wait(card->pop_time);
1063 trace_snd_soc_dapm_done(card);
1068 #ifdef CONFIG_DEBUG_FS
1069 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1071 file->private_data = inode->i_private;
1075 static ssize_t dapm_widget_power_read_file(struct file *file,
1076 char __user *user_buf,
1077 size_t count, loff_t *ppos)
1079 struct snd_soc_dapm_widget *w = file->private_data;
1083 struct snd_soc_dapm_path *p = NULL;
1085 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1089 in = is_connected_input_ep(w);
1090 dapm_clear_walk(w->dapm);
1091 out = is_connected_output_ep(w);
1092 dapm_clear_walk(w->dapm);
1094 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
1095 w->name, w->power ? "On" : "Off", in, out);
1098 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1099 " - R%d(0x%x) bit %d",
1100 w->reg, w->reg, w->shift);
1102 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1105 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1107 w->active ? "active" : "inactive");
1109 list_for_each_entry(p, &w->sources, list_sink) {
1110 if (p->connected && !p->connected(w, p->sink))
1114 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1116 p->name ? p->name : "static",
1119 list_for_each_entry(p, &w->sinks, list_source) {
1120 if (p->connected && !p->connected(w, p->sink))
1124 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1126 p->name ? p->name : "static",
1130 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1136 static const struct file_operations dapm_widget_power_fops = {
1137 .open = dapm_widget_power_open_file,
1138 .read = dapm_widget_power_read_file,
1139 .llseek = default_llseek,
1142 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1144 struct snd_soc_dapm_widget *w;
1147 if (!dapm->debugfs_dapm)
1150 list_for_each_entry(w, &dapm->card->widgets, list) {
1151 if (!w->name || w->dapm != dapm)
1154 d = debugfs_create_file(w->name, 0444,
1155 dapm->debugfs_dapm, w,
1156 &dapm_widget_power_fops);
1158 dev_warn(w->dapm->dev,
1159 "ASoC: Failed to create %s debugfs file\n",
1164 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
1169 /* test and update the power status of a mux widget */
1170 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1171 struct snd_kcontrol *kcontrol, int change,
1172 int mux, struct soc_enum *e)
1174 struct snd_soc_dapm_path *path;
1177 if (widget->id != snd_soc_dapm_mux &&
1178 widget->id != snd_soc_dapm_value_mux)
1184 /* find dapm widget path assoc with kcontrol */
1185 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1186 if (path->kcontrol != kcontrol)
1189 if (!path->name || !e->texts[mux])
1193 /* we now need to match the string in the enum to the path */
1194 if (!(strcmp(path->name, e->texts[mux])))
1195 path->connect = 1; /* new connection */
1197 path->connect = 0; /* old connection must be powered down */
1201 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1206 /* test and update the power status of a mixer or switch widget */
1207 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1208 struct snd_kcontrol *kcontrol, int connect)
1210 struct snd_soc_dapm_path *path;
1213 if (widget->id != snd_soc_dapm_mixer &&
1214 widget->id != snd_soc_dapm_mixer_named_ctl &&
1215 widget->id != snd_soc_dapm_switch)
1218 /* find dapm widget path assoc with kcontrol */
1219 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1220 if (path->kcontrol != kcontrol)
1223 /* found, now check type */
1225 path->connect = connect;
1230 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1235 /* show dapm widget status in sys fs */
1236 static ssize_t dapm_widget_show(struct device *dev,
1237 struct device_attribute *attr, char *buf)
1239 struct snd_soc_pcm_runtime *rtd =
1240 container_of(dev, struct snd_soc_pcm_runtime, dev);
1241 struct snd_soc_codec *codec =rtd->codec;
1242 struct snd_soc_dapm_widget *w;
1244 char *state = "not set";
1246 list_for_each_entry(w, &codec->card->widgets, list) {
1247 if (w->dapm != &codec->dapm)
1250 /* only display widgets that burnm power */
1252 case snd_soc_dapm_hp:
1253 case snd_soc_dapm_mic:
1254 case snd_soc_dapm_spk:
1255 case snd_soc_dapm_line:
1256 case snd_soc_dapm_micbias:
1257 case snd_soc_dapm_dac:
1258 case snd_soc_dapm_adc:
1259 case snd_soc_dapm_pga:
1260 case snd_soc_dapm_out_drv:
1261 case snd_soc_dapm_mixer:
1262 case snd_soc_dapm_mixer_named_ctl:
1263 case snd_soc_dapm_supply:
1265 count += sprintf(buf + count, "%s: %s\n",
1266 w->name, w->power ? "On":"Off");
1273 switch (codec->dapm.bias_level) {
1274 case SND_SOC_BIAS_ON:
1277 case SND_SOC_BIAS_PREPARE:
1280 case SND_SOC_BIAS_STANDBY:
1283 case SND_SOC_BIAS_OFF:
1287 count += sprintf(buf + count, "PM State: %s\n", state);
1292 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1294 int snd_soc_dapm_sys_add(struct device *dev)
1296 return device_create_file(dev, &dev_attr_dapm_widget);
1299 static void snd_soc_dapm_sys_remove(struct device *dev)
1301 device_remove_file(dev, &dev_attr_dapm_widget);
1304 /* free all dapm widgets and resources */
1305 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1307 struct snd_soc_dapm_widget *w, *next_w;
1308 struct snd_soc_dapm_path *p, *next_p;
1310 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1311 if (w->dapm != dapm)
1315 * remove source and sink paths associated to this widget.
1316 * While removing the path, remove reference to it from both
1317 * source and sink widgets so that path is removed only once.
1319 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1320 list_del(&p->list_sink);
1321 list_del(&p->list_source);
1323 kfree(p->long_name);
1326 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1327 list_del(&p->list_sink);
1328 list_del(&p->list_source);
1330 kfree(p->long_name);
1338 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1339 const char *pin, int status)
1341 struct snd_soc_dapm_widget *w;
1343 list_for_each_entry(w, &dapm->card->widgets, list) {
1344 if (w->dapm != dapm)
1346 if (!strcmp(w->name, pin)) {
1347 dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
1349 w->connected = status;
1350 /* Allow disabling of forced pins */
1357 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1362 * snd_soc_dapm_sync - scan and power dapm paths
1363 * @dapm: DAPM context
1365 * Walks all dapm audio paths and powers widgets according to their
1366 * stream or path usage.
1368 * Returns 0 for success.
1370 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1372 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1374 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1376 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1377 const struct snd_soc_dapm_route *route)
1379 struct snd_soc_dapm_path *path;
1380 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1381 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1383 const char *control = route->control;
1385 char prefixed_sink[80];
1386 char prefixed_source[80];
1389 if (dapm->codec->name_prefix) {
1390 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1391 dapm->codec->name_prefix, route->sink);
1392 sink = prefixed_sink;
1393 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1394 dapm->codec->name_prefix, route->source);
1395 source = prefixed_source;
1398 source = route->source;
1402 * find src and dest widgets over all widgets but favor a widget from
1403 * current DAPM context
1405 list_for_each_entry(w, &dapm->card->widgets, list) {
1406 if (!wsink && !(strcmp(w->name, sink))) {
1408 if (w->dapm == dapm)
1412 if (!wsource && !(strcmp(w->name, source))) {
1414 if (w->dapm == dapm)
1418 /* use widget from another DAPM context if not found from this */
1424 if (wsource == NULL || wsink == NULL)
1427 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1431 path->source = wsource;
1433 path->connected = route->connected;
1434 INIT_LIST_HEAD(&path->list);
1435 INIT_LIST_HEAD(&path->list_source);
1436 INIT_LIST_HEAD(&path->list_sink);
1438 /* check for external widgets */
1439 if (wsink->id == snd_soc_dapm_input) {
1440 if (wsource->id == snd_soc_dapm_micbias ||
1441 wsource->id == snd_soc_dapm_mic ||
1442 wsource->id == snd_soc_dapm_line ||
1443 wsource->id == snd_soc_dapm_output)
1446 if (wsource->id == snd_soc_dapm_output) {
1447 if (wsink->id == snd_soc_dapm_spk ||
1448 wsink->id == snd_soc_dapm_hp ||
1449 wsink->id == snd_soc_dapm_line ||
1450 wsink->id == snd_soc_dapm_input)
1454 /* connect static paths */
1455 if (control == NULL) {
1456 list_add(&path->list, &dapm->card->paths);
1457 list_add(&path->list_sink, &wsink->sources);
1458 list_add(&path->list_source, &wsource->sinks);
1463 /* connect dynamic paths */
1465 case snd_soc_dapm_adc:
1466 case snd_soc_dapm_dac:
1467 case snd_soc_dapm_pga:
1468 case snd_soc_dapm_out_drv:
1469 case snd_soc_dapm_input:
1470 case snd_soc_dapm_output:
1471 case snd_soc_dapm_micbias:
1472 case snd_soc_dapm_vmid:
1473 case snd_soc_dapm_pre:
1474 case snd_soc_dapm_post:
1475 case snd_soc_dapm_supply:
1476 case snd_soc_dapm_aif_in:
1477 case snd_soc_dapm_aif_out:
1478 list_add(&path->list, &dapm->card->paths);
1479 list_add(&path->list_sink, &wsink->sources);
1480 list_add(&path->list_source, &wsource->sinks);
1483 case snd_soc_dapm_mux:
1484 case snd_soc_dapm_value_mux:
1485 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1486 &wsink->kcontrols[0]);
1490 case snd_soc_dapm_switch:
1491 case snd_soc_dapm_mixer:
1492 case snd_soc_dapm_mixer_named_ctl:
1493 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1497 case snd_soc_dapm_hp:
1498 case snd_soc_dapm_mic:
1499 case snd_soc_dapm_line:
1500 case snd_soc_dapm_spk:
1501 list_add(&path->list, &dapm->card->paths);
1502 list_add(&path->list_sink, &wsink->sources);
1503 list_add(&path->list_source, &wsource->sinks);
1510 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1511 source, control, sink);
1517 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1518 * @dapm: DAPM context
1519 * @route: audio routes
1520 * @num: number of routes
1522 * Connects 2 dapm widgets together via a named audio path. The sink is
1523 * the widget receiving the audio signal, whilst the source is the sender
1524 * of the audio signal.
1526 * Returns 0 for success else error. On error all resources can be freed
1527 * with a call to snd_soc_card_free().
1529 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1530 const struct snd_soc_dapm_route *route, int num)
1534 for (i = 0; i < num; i++) {
1535 ret = snd_soc_dapm_add_route(dapm, route);
1537 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1538 route->source, route->sink);
1546 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1549 * snd_soc_dapm_new_widgets - add new dapm widgets
1550 * @dapm: DAPM context
1552 * Checks the codec for any new dapm widgets and creates them if found.
1554 * Returns 0 for success.
1556 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1558 struct snd_soc_dapm_widget *w;
1560 list_for_each_entry(w, &dapm->card->widgets, list)
1566 case snd_soc_dapm_switch:
1567 case snd_soc_dapm_mixer:
1568 case snd_soc_dapm_mixer_named_ctl:
1569 w->power_check = dapm_generic_check_power;
1570 dapm_new_mixer(dapm, w);
1572 case snd_soc_dapm_mux:
1573 case snd_soc_dapm_value_mux:
1574 w->power_check = dapm_generic_check_power;
1575 dapm_new_mux(dapm, w);
1577 case snd_soc_dapm_adc:
1578 case snd_soc_dapm_aif_out:
1579 w->power_check = dapm_adc_check_power;
1581 case snd_soc_dapm_dac:
1582 case snd_soc_dapm_aif_in:
1583 w->power_check = dapm_dac_check_power;
1585 case snd_soc_dapm_pga:
1586 case snd_soc_dapm_out_drv:
1587 w->power_check = dapm_generic_check_power;
1588 dapm_new_pga(dapm, w);
1590 case snd_soc_dapm_input:
1591 case snd_soc_dapm_output:
1592 case snd_soc_dapm_micbias:
1593 case snd_soc_dapm_spk:
1594 case snd_soc_dapm_hp:
1595 case snd_soc_dapm_mic:
1596 case snd_soc_dapm_line:
1597 w->power_check = dapm_generic_check_power;
1599 case snd_soc_dapm_supply:
1600 w->power_check = dapm_supply_check_power;
1601 case snd_soc_dapm_vmid:
1602 case snd_soc_dapm_pre:
1603 case snd_soc_dapm_post:
1609 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1612 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1615 * snd_soc_dapm_get_volsw - dapm mixer get callback
1616 * @kcontrol: mixer control
1617 * @ucontrol: control element information
1619 * Callback to get the value of a dapm mixer control.
1621 * Returns 0 for success.
1623 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1624 struct snd_ctl_elem_value *ucontrol)
1626 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1627 struct soc_mixer_control *mc =
1628 (struct soc_mixer_control *)kcontrol->private_value;
1629 unsigned int reg = mc->reg;
1630 unsigned int shift = mc->shift;
1631 unsigned int rshift = mc->rshift;
1633 unsigned int invert = mc->invert;
1634 unsigned int mask = (1 << fls(max)) - 1;
1636 ucontrol->value.integer.value[0] =
1637 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1638 if (shift != rshift)
1639 ucontrol->value.integer.value[1] =
1640 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1642 ucontrol->value.integer.value[0] =
1643 max - ucontrol->value.integer.value[0];
1644 if (shift != rshift)
1645 ucontrol->value.integer.value[1] =
1646 max - ucontrol->value.integer.value[1];
1651 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1654 * snd_soc_dapm_put_volsw - dapm mixer set callback
1655 * @kcontrol: mixer control
1656 * @ucontrol: control element information
1658 * Callback to set the value of a dapm mixer control.
1660 * Returns 0 for success.
1662 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1663 struct snd_ctl_elem_value *ucontrol)
1665 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1666 struct soc_mixer_control *mc =
1667 (struct soc_mixer_control *)kcontrol->private_value;
1668 unsigned int reg = mc->reg;
1669 unsigned int shift = mc->shift;
1670 unsigned int rshift = mc->rshift;
1672 unsigned int mask = (1 << fls(max)) - 1;
1673 unsigned int invert = mc->invert;
1674 unsigned int val, val2, val_mask;
1678 val = (ucontrol->value.integer.value[0] & mask);
1682 val_mask = mask << shift;
1684 if (shift != rshift) {
1685 val2 = (ucontrol->value.integer.value[1] & mask);
1688 val_mask |= mask << rshift;
1689 val |= val2 << rshift;
1692 mutex_lock(&widget->codec->mutex);
1693 widget->value = val;
1695 if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
1697 /* new connection */
1698 connect = invert ? 0:1;
1700 /* old connection must be powered down */
1701 connect = invert ? 1:0;
1703 dapm_mixer_update_power(widget, kcontrol, connect);
1706 if (widget->event) {
1707 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1708 ret = widget->event(widget, kcontrol,
1709 SND_SOC_DAPM_PRE_REG);
1715 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1716 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1717 ret = widget->event(widget, kcontrol,
1718 SND_SOC_DAPM_POST_REG);
1720 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1723 mutex_unlock(&widget->codec->mutex);
1726 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1729 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1730 * @kcontrol: mixer control
1731 * @ucontrol: control element information
1733 * Callback to get the value of a dapm enumerated double mixer control.
1735 * Returns 0 for success.
1737 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1738 struct snd_ctl_elem_value *ucontrol)
1740 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1741 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1742 unsigned int val, bitmask;
1744 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1746 val = snd_soc_read(widget->codec, e->reg);
1747 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1748 if (e->shift_l != e->shift_r)
1749 ucontrol->value.enumerated.item[1] =
1750 (val >> e->shift_r) & (bitmask - 1);
1754 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1757 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1758 * @kcontrol: mixer control
1759 * @ucontrol: control element information
1761 * Callback to set the value of a dapm enumerated double mixer control.
1763 * Returns 0 for success.
1765 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_value *ucontrol)
1768 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1769 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1770 unsigned int val, mux, change;
1771 unsigned int mask, bitmask;
1774 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1776 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1778 mux = ucontrol->value.enumerated.item[0];
1779 val = mux << e->shift_l;
1780 mask = (bitmask - 1) << e->shift_l;
1781 if (e->shift_l != e->shift_r) {
1782 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1784 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1785 mask |= (bitmask - 1) << e->shift_r;
1788 mutex_lock(&widget->codec->mutex);
1789 widget->value = val;
1790 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1791 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1793 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1794 ret = widget->event(widget,
1795 kcontrol, SND_SOC_DAPM_PRE_REG);
1800 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1802 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1803 ret = widget->event(widget,
1804 kcontrol, SND_SOC_DAPM_POST_REG);
1807 mutex_unlock(&widget->codec->mutex);
1810 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1813 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1814 * @kcontrol: mixer control
1815 * @ucontrol: control element information
1817 * Returns 0 for success.
1819 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
1820 struct snd_ctl_elem_value *ucontrol)
1822 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1824 ucontrol->value.enumerated.item[0] = widget->value;
1828 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
1831 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1832 * @kcontrol: mixer control
1833 * @ucontrol: control element information
1835 * Returns 0 for success.
1837 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
1838 struct snd_ctl_elem_value *ucontrol)
1840 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1841 struct soc_enum *e =
1842 (struct soc_enum *)kcontrol->private_value;
1846 if (ucontrol->value.enumerated.item[0] >= e->max)
1849 mutex_lock(&widget->codec->mutex);
1851 change = widget->value != ucontrol->value.enumerated.item[0];
1852 widget->value = ucontrol->value.enumerated.item[0];
1853 dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
1855 mutex_unlock(&widget->codec->mutex);
1858 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
1861 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1863 * @kcontrol: mixer control
1864 * @ucontrol: control element information
1866 * Callback to get the value of a dapm semi enumerated double mixer control.
1868 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1869 * used for handling bitfield coded enumeration for example.
1871 * Returns 0 for success.
1873 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1874 struct snd_ctl_elem_value *ucontrol)
1876 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1877 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1878 unsigned int reg_val, val, mux;
1880 reg_val = snd_soc_read(widget->codec, e->reg);
1881 val = (reg_val >> e->shift_l) & e->mask;
1882 for (mux = 0; mux < e->max; mux++) {
1883 if (val == e->values[mux])
1886 ucontrol->value.enumerated.item[0] = mux;
1887 if (e->shift_l != e->shift_r) {
1888 val = (reg_val >> e->shift_r) & e->mask;
1889 for (mux = 0; mux < e->max; mux++) {
1890 if (val == e->values[mux])
1893 ucontrol->value.enumerated.item[1] = mux;
1898 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1901 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1903 * @kcontrol: mixer control
1904 * @ucontrol: control element information
1906 * Callback to set the value of a dapm semi enumerated double mixer control.
1908 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1909 * used for handling bitfield coded enumeration for example.
1911 * Returns 0 for success.
1913 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1914 struct snd_ctl_elem_value *ucontrol)
1916 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1917 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1918 unsigned int val, mux, change;
1922 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1924 mux = ucontrol->value.enumerated.item[0];
1925 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1926 mask = e->mask << e->shift_l;
1927 if (e->shift_l != e->shift_r) {
1928 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1930 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1931 mask |= e->mask << e->shift_r;
1934 mutex_lock(&widget->codec->mutex);
1935 widget->value = val;
1936 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
1937 dapm_mux_update_power(widget, kcontrol, change, mux, e);
1939 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1940 ret = widget->event(widget,
1941 kcontrol, SND_SOC_DAPM_PRE_REG);
1946 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1948 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1949 ret = widget->event(widget,
1950 kcontrol, SND_SOC_DAPM_POST_REG);
1953 mutex_unlock(&widget->codec->mutex);
1956 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1959 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1961 * @kcontrol: mixer control
1962 * @uinfo: control element information
1964 * Callback to provide information about a pin switch control.
1966 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1967 struct snd_ctl_elem_info *uinfo)
1969 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1971 uinfo->value.integer.min = 0;
1972 uinfo->value.integer.max = 1;
1976 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1979 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1981 * @kcontrol: mixer control
1984 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1985 struct snd_ctl_elem_value *ucontrol)
1987 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1988 const char *pin = (const char *)kcontrol->private_value;
1990 mutex_lock(&codec->mutex);
1992 ucontrol->value.integer.value[0] =
1993 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
1995 mutex_unlock(&codec->mutex);
1999 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2002 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2004 * @kcontrol: mixer control
2007 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2008 struct snd_ctl_elem_value *ucontrol)
2010 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2011 const char *pin = (const char *)kcontrol->private_value;
2013 mutex_lock(&codec->mutex);
2015 if (ucontrol->value.integer.value[0])
2016 snd_soc_dapm_enable_pin(&codec->dapm, pin);
2018 snd_soc_dapm_disable_pin(&codec->dapm, pin);
2020 snd_soc_dapm_sync(&codec->dapm);
2022 mutex_unlock(&codec->mutex);
2026 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2029 * snd_soc_dapm_new_control - create new dapm control
2030 * @dapm: DAPM context
2031 * @widget: widget template
2033 * Creates a new dapm control based upon the template.
2035 * Returns 0 for success else error.
2037 int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2038 const struct snd_soc_dapm_widget *widget)
2040 struct snd_soc_dapm_widget *w;
2043 if ((w = dapm_cnew_widget(widget)) == NULL)
2046 name_len = strlen(widget->name) + 1;
2047 if (dapm->codec->name_prefix)
2048 name_len += 1 + strlen(dapm->codec->name_prefix);
2049 w->name = kmalloc(name_len, GFP_KERNEL);
2050 if (w->name == NULL) {
2054 if (dapm->codec->name_prefix)
2055 snprintf(w->name, name_len, "%s %s",
2056 dapm->codec->name_prefix, widget->name);
2058 snprintf(w->name, name_len, "%s", widget->name);
2062 w->codec = dapm->codec;
2063 INIT_LIST_HEAD(&w->sources);
2064 INIT_LIST_HEAD(&w->sinks);
2065 INIT_LIST_HEAD(&w->list);
2066 list_add(&w->list, &dapm->card->widgets);
2068 /* machine layer set ups unconnected pins and insertions */
2072 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2075 * snd_soc_dapm_new_controls - create new dapm controls
2076 * @dapm: DAPM context
2077 * @widget: widget array
2078 * @num: number of widgets
2080 * Creates new DAPM controls based upon the templates.
2082 * Returns 0 for success else error.
2084 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2085 const struct snd_soc_dapm_widget *widget,
2090 for (i = 0; i < num; i++) {
2091 ret = snd_soc_dapm_new_control(dapm, widget);
2094 "ASoC: Failed to create DAPM control %s: %d\n",
2102 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2104 static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2105 const char *stream, int event)
2107 struct snd_soc_dapm_widget *w;
2109 list_for_each_entry(w, &dapm->card->widgets, list)
2111 if (!w->sname || w->dapm != dapm)
2113 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2114 w->name, w->sname, stream, event);
2115 if (strstr(w->sname, stream)) {
2117 case SND_SOC_DAPM_STREAM_START:
2120 case SND_SOC_DAPM_STREAM_STOP:
2123 case SND_SOC_DAPM_STREAM_SUSPEND:
2124 case SND_SOC_DAPM_STREAM_RESUME:
2125 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2126 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2132 dapm_power_widgets(dapm, event);
2136 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2137 * @rtd: PCM runtime data
2138 * @stream: stream name
2139 * @event: stream event
2141 * Sends a stream event to the dapm core. The core then makes any
2142 * necessary widget power changes.
2144 * Returns 0 for success else error.
2146 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2147 const char *stream, int event)
2149 struct snd_soc_codec *codec = rtd->codec;
2154 mutex_lock(&codec->mutex);
2155 soc_dapm_stream_event(&codec->dapm, stream, event);
2156 mutex_unlock(&codec->mutex);
2159 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
2162 * snd_soc_dapm_enable_pin - enable pin.
2163 * @dapm: DAPM context
2166 * Enables input/output pin and its parents or children widgets iff there is
2167 * a valid audio route and active audio stream.
2168 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2169 * do any widget power switching.
2171 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2173 return snd_soc_dapm_set_pin(dapm, pin, 1);
2175 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2178 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2179 * @dapm: DAPM context
2182 * Enables input/output pin regardless of any other state. This is
2183 * intended for use with microphone bias supplies used in microphone
2186 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2187 * do any widget power switching.
2189 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2192 struct snd_soc_dapm_widget *w;
2194 list_for_each_entry(w, &dapm->card->widgets, list) {
2195 if (w->dapm != dapm)
2197 if (!strcmp(w->name, pin)) {
2198 dev_dbg(w->dapm->dev,
2199 "dapm: force enable pin %s\n", pin);
2206 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2209 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2212 * snd_soc_dapm_disable_pin - disable pin.
2213 * @dapm: DAPM context
2216 * Disables input/output pin and its parents or children widgets.
2217 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2218 * do any widget power switching.
2220 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2223 return snd_soc_dapm_set_pin(dapm, pin, 0);
2225 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2228 * snd_soc_dapm_nc_pin - permanently disable pin.
2229 * @dapm: DAPM context
2232 * Marks the specified pin as being not connected, disabling it along
2233 * any parent or child widgets. At present this is identical to
2234 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2235 * additional things such as disabling controls which only affect
2236 * paths through the pin.
2238 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2239 * do any widget power switching.
2241 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2243 return snd_soc_dapm_set_pin(dapm, pin, 0);
2245 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2248 * snd_soc_dapm_get_pin_status - get audio pin status
2249 * @dapm: DAPM context
2250 * @pin: audio signal pin endpoint (or start point)
2252 * Get audio pin status - connected or disconnected.
2254 * Returns 1 for connected otherwise 0.
2256 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2259 struct snd_soc_dapm_widget *w;
2261 list_for_each_entry(w, &dapm->card->widgets, list) {
2262 if (w->dapm != dapm)
2264 if (!strcmp(w->name, pin))
2265 return w->connected;
2270 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2273 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2274 * @dapm: DAPM context
2275 * @pin: audio signal pin endpoint (or start point)
2277 * Mark the given endpoint or pin as ignoring suspend. When the
2278 * system is disabled a path between two endpoints flagged as ignoring
2279 * suspend will not be disabled. The path must already be enabled via
2280 * normal means at suspend time, it will not be turned on if it was not
2283 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2286 struct snd_soc_dapm_widget *w;
2288 list_for_each_entry(w, &dapm->card->widgets, list) {
2289 if (w->dapm != dapm)
2291 if (!strcmp(w->name, pin)) {
2292 w->ignore_suspend = 1;
2297 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2300 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2303 * snd_soc_dapm_free - free dapm resources
2306 * Free all dapm widgets and resources.
2308 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2310 snd_soc_dapm_sys_remove(dapm->dev);
2311 dapm_free_widgets(dapm);
2313 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2315 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
2317 struct snd_soc_dapm_widget *w;
2318 LIST_HEAD(down_list);
2321 list_for_each_entry(w, &dapm->card->widgets, list) {
2322 if (w->dapm != dapm)
2325 dapm_seq_insert(w, &down_list, dapm_down_seq);
2331 /* If there were no widgets to power down we're already in
2335 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
2336 dapm_seq_run(dapm, &down_list, 0, dapm_down_seq);
2337 snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
2342 * snd_soc_dapm_shutdown - callback for system shutdown
2344 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2346 struct snd_soc_codec *codec;
2348 list_for_each_entry(codec, &card->codec_dev_list, list) {
2349 soc_dapm_shutdown_codec(&codec->dapm);
2350 snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
2354 /* Module information */
2355 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2356 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2357 MODULE_LICENSE("GPL");