2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood
6 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 * 12th Aug 2005 Initial version.
15 * 25th Oct 2005 Implemented path power domain.
16 * 18th Dec 2005 Implemented machine and stream level power domain.
19 * o Changes power status of internal codec blocks depending on the
20 * dynamic configuration of codec internal audio paths and active
22 * o Platform power domain - can support external components i.e. amps and
23 * mic/meadphone insertion events.
24 * o Automatic Mic Bias support
25 * o Jack insertion power event initiation - e.g. hp insertion will enable
27 * o Delayed powerdown of audio susbsytem to reduce pops between a quick
31 * o DAPM power change sequencing - allow for configurable per
33 * o Support for analogue bias optimisation.
34 * o Support for reduced codec oversampling rates.
35 * o Support for reduced codec bias currents.
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/init.h>
41 #include <linux/delay.h>
43 #include <linux/bitops.h>
44 #include <linux/platform_device.h>
45 #include <linux/jiffies.h>
46 #include <sound/driver.h>
47 #include <sound/core.h>
48 #include <sound/pcm.h>
49 #include <sound/pcm_params.h>
50 #include <sound/soc-dapm.h>
51 #include <sound/initval.h>
56 #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
57 #define dbg(format, arg...) printk(format, ## arg)
59 #define dump_dapm(codec, action)
60 #define dbg(format, arg...)
65 #define POP_TIME 500 /* 500 msecs - change if pop debug is too fast */
66 #define pop_wait(time) schedule_timeout_interruptible(msecs_to_jiffies(time))
67 #define pop_dbg(format, arg...) printk(format, ## arg); pop_wait(POP_TIME)
69 #define pop_dbg(format, arg...)
70 #define pop_wait(time)
73 /* dapm power sequences - make this per codec in the future */
74 static int dapm_up_seq[] = {
75 snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
76 snd_soc_dapm_mux, snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_pga,
77 snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
79 static int dapm_down_seq[] = {
80 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
81 snd_soc_dapm_pga, snd_soc_dapm_mixer, snd_soc_dapm_dac, snd_soc_dapm_mic,
82 snd_soc_dapm_micbias, snd_soc_dapm_mux, snd_soc_dapm_post
85 static int dapm_status = 1;
86 module_param(dapm_status, int, 0);
87 MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
89 /* create a new dapm widget */
90 static struct snd_soc_dapm_widget *dapm_cnew_widget(
91 const struct snd_soc_dapm_widget *_widget)
93 struct snd_soc_dapm_widget* widget;
94 widget = kmalloc(sizeof(struct snd_soc_dapm_widget), GFP_KERNEL);
98 memcpy(widget, _widget, sizeof(struct snd_soc_dapm_widget));
102 /* set up initial codec paths */
103 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
104 struct snd_soc_dapm_path *p, int i)
107 case snd_soc_dapm_switch:
108 case snd_soc_dapm_mixer: {
110 int reg = w->kcontrols[i].private_value & 0xff;
111 int shift = (w->kcontrols[i].private_value >> 8) & 0x0f;
112 int mask = (w->kcontrols[i].private_value >> 16) & 0xff;
113 int invert = (w->kcontrols[i].private_value >> 24) & 0x01;
115 val = snd_soc_read(w->codec, reg);
116 val = (val >> shift) & mask;
118 if ((invert && !val) || (!invert && val))
124 case snd_soc_dapm_mux: {
125 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
126 int val, item, bitmask;
128 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
130 val = snd_soc_read(w->codec, e->reg);
131 item = (val >> e->shift_l) & (bitmask - 1);
134 for (i = 0; i < e->mask; i++) {
135 if (!(strcmp(p->name, e->texts[i])) && item == i)
140 /* does not effect routing - always connected */
141 case snd_soc_dapm_pga:
142 case snd_soc_dapm_output:
143 case snd_soc_dapm_adc:
144 case snd_soc_dapm_input:
145 case snd_soc_dapm_dac:
146 case snd_soc_dapm_micbias:
147 case snd_soc_dapm_vmid:
150 /* does effect routing - dynamically connected */
151 case snd_soc_dapm_hp:
152 case snd_soc_dapm_mic:
153 case snd_soc_dapm_spk:
154 case snd_soc_dapm_line:
155 case snd_soc_dapm_pre:
156 case snd_soc_dapm_post:
162 /* connect mux widget to it's interconnecting audio paths */
163 static int dapm_connect_mux(struct snd_soc_codec *codec,
164 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
165 struct snd_soc_dapm_path *path, const char *control_name,
166 const struct snd_kcontrol_new *kcontrol)
168 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
171 for (i = 0; i < e->mask; i++) {
172 if (!(strcmp(control_name, e->texts[i]))) {
173 list_add(&path->list, &codec->dapm_paths);
174 list_add(&path->list_sink, &dest->sources);
175 list_add(&path->list_source, &src->sinks);
176 path->name = (char*)e->texts[i];
177 dapm_set_path_status(dest, path, 0);
185 /* connect mixer widget to it's interconnecting audio paths */
186 static int dapm_connect_mixer(struct snd_soc_codec *codec,
187 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
188 struct snd_soc_dapm_path *path, const char *control_name)
192 /* search for mixer kcontrol */
193 for (i = 0; i < dest->num_kcontrols; i++) {
194 if (!strcmp(control_name, dest->kcontrols[i].name)) {
195 list_add(&path->list, &codec->dapm_paths);
196 list_add(&path->list_sink, &dest->sources);
197 list_add(&path->list_source, &src->sinks);
198 path->name = dest->kcontrols[i].name;
199 dapm_set_path_status(dest, path, i);
206 /* update dapm codec register bits */
207 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
210 unsigned short old, new;
211 struct snd_soc_codec *codec = widget->codec;
213 /* check for valid widgets */
214 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
215 widget->id == snd_soc_dapm_output ||
216 widget->id == snd_soc_dapm_hp ||
217 widget->id == snd_soc_dapm_mic ||
218 widget->id == snd_soc_dapm_line ||
219 widget->id == snd_soc_dapm_spk)
222 power = widget->power;
224 power = (power ? 0:1);
226 old = snd_soc_read(codec, widget->reg);
227 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
231 pop_dbg("pop test %s : %s in %d ms\n", widget->name,
232 widget->power ? "on" : "off", POP_TIME);
233 snd_soc_write(codec, widget->reg, new);
236 dbg("reg old %x new %x change %d\n", old, new, change);
240 /* ramps the volume up or down to minimise pops before or after a
241 * DAPM power event */
242 static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
244 const struct snd_kcontrol_new *k = widget->kcontrols;
246 if (widget->muted && !power)
248 if (!widget->muted && power)
251 if (widget->num_kcontrols && k) {
252 int reg = k->private_value & 0xff;
253 int shift = (k->private_value >> 8) & 0x0f;
254 int mask = (k->private_value >> 16) & 0xff;
255 int invert = (k->private_value >> 24) & 0x01;
259 /* power up has happended, increase volume to last level */
261 for (i = mask; i > widget->saved_value; i--)
262 snd_soc_update_bits(widget->codec, reg, mask, i);
264 for (i = 0; i < widget->saved_value; i++)
265 snd_soc_update_bits(widget->codec, reg, mask, i);
269 /* power down is about to occur, decrease volume to mute */
270 int val = snd_soc_read(widget->codec, reg);
271 int i = widget->saved_value = (val >> shift) & mask;
273 for (; i < mask; i++)
274 snd_soc_update_bits(widget->codec, reg, mask, i);
277 snd_soc_update_bits(widget->codec, reg, mask, i);
285 /* create new dapm mixer control */
286 static int dapm_new_mixer(struct snd_soc_codec *codec,
287 struct snd_soc_dapm_widget *w)
291 struct snd_soc_dapm_path *path;
294 for (i = 0; i < w->num_kcontrols; i++) {
297 list_for_each_entry(path, &w->sources, list_sink) {
299 /* mixer/mux paths name must match control name */
300 if (path->name != (char*)w->kcontrols[i].name)
303 /* add dapm control with long name */
304 snprintf(name, 32, "%s %s", w->name, w->kcontrols[i].name);
305 path->long_name = kstrdup (name, GFP_KERNEL);
306 if (path->long_name == NULL)
309 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
311 ret = snd_ctl_add(codec->card, path->kcontrol);
313 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
315 kfree(path->long_name);
316 path->long_name = NULL;
324 /* create new dapm mux control */
325 static int dapm_new_mux(struct snd_soc_codec *codec,
326 struct snd_soc_dapm_widget *w)
328 struct snd_soc_dapm_path *path = NULL;
329 struct snd_kcontrol *kcontrol;
332 if (!w->num_kcontrols) {
333 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
337 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
338 ret = snd_ctl_add(codec->card, kcontrol);
342 list_for_each_entry(path, &w->sources, list_sink)
343 path->kcontrol = kcontrol;
348 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
352 /* create new dapm volume control */
353 static int dapm_new_pga(struct snd_soc_codec *codec,
354 struct snd_soc_dapm_widget *w)
356 struct snd_kcontrol *kcontrol;
359 if (!w->num_kcontrols)
362 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
363 ret = snd_ctl_add(codec->card, kcontrol);
365 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
372 /* reset 'walked' bit for each dapm path */
373 static inline void dapm_clear_walk(struct snd_soc_codec *codec)
375 struct snd_soc_dapm_path *p;
377 list_for_each_entry(p, &codec->dapm_paths, list)
382 * Recursively check for a completed path to an active or physically connected
383 * output widget. Returns number of complete paths.
385 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
387 struct snd_soc_dapm_path *path;
390 if (widget->id == snd_soc_dapm_adc && widget->active)
393 if (widget->connected) {
394 /* connected pin ? */
395 if (widget->id == snd_soc_dapm_output && !widget->ext)
398 /* connected jack or spk ? */
399 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
400 widget->id == snd_soc_dapm_line)
404 list_for_each_entry(path, &widget->sinks, list_source) {
408 if (path->sink && path->connect) {
410 con += is_connected_output_ep(path->sink);
418 * Recursively check for a completed path to an active or physically connected
419 * input widget. Returns number of complete paths.
421 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
423 struct snd_soc_dapm_path *path;
426 /* active stream ? */
427 if (widget->id == snd_soc_dapm_dac && widget->active)
430 if (widget->connected) {
431 /* connected pin ? */
432 if (widget->id == snd_soc_dapm_input && !widget->ext)
435 /* connected VMID/Bias for lower pops */
436 if (widget->id == snd_soc_dapm_vmid)
439 /* connected jack ? */
440 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
444 list_for_each_entry(path, &widget->sources, list_sink) {
448 if (path->source && path->connect) {
450 con += is_connected_input_ep(path->source);
458 * Scan each dapm widget for complete audio path.
459 * A complete path is a route that has valid endpoints i.e.:-
461 * o DAC to output pin.
462 * o Input Pin to ADC.
463 * o Input pin to Output pin (bypass, sidetone)
464 * o DAC to ADC (loopback).
466 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
468 struct snd_soc_dapm_widget *w;
469 int in, out, i, c = 1, *seq = NULL, ret = 0, power_change, power;
471 /* do we have a sequenced stream event */
472 if (event == SND_SOC_DAPM_STREAM_START) {
473 c = ARRAY_SIZE(dapm_up_seq);
475 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
476 c = ARRAY_SIZE(dapm_down_seq);
480 for(i = 0; i < c; i++) {
481 list_for_each_entry(w, &codec->dapm_widgets, list) {
483 /* is widget in stream order */
484 if (seq && seq[i] && w->id != seq[i])
487 /* vmid - no action */
488 if (w->id == snd_soc_dapm_vmid)
492 if (w->id == snd_soc_dapm_adc && w->active) {
493 in = is_connected_input_ep(w);
494 dapm_clear_walk(w->codec);
495 w->power = (in != 0) ? 1 : 0;
501 if (w->id == snd_soc_dapm_dac && w->active) {
502 out = is_connected_output_ep(w);
503 dapm_clear_walk(w->codec);
504 w->power = (out != 0) ? 1 : 0;
509 /* programmable gain/attenuation */
510 if (w->id == snd_soc_dapm_pga) {
512 in = is_connected_input_ep(w);
513 dapm_clear_walk(w->codec);
514 out = is_connected_output_ep(w);
515 dapm_clear_walk(w->codec);
516 w->power = on = (out != 0 && in != 0) ? 1 : 0;
519 dapm_set_pga(w, on); /* lower volume to reduce pops */
522 dapm_set_pga(w, on); /* restore volume from zero */
527 /* pre and post event widgets */
528 if (w->id == snd_soc_dapm_pre) {
532 if (event == SND_SOC_DAPM_STREAM_START) {
533 ret = w->event(w, SND_SOC_DAPM_PRE_PMU);
536 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
537 ret = w->event(w, SND_SOC_DAPM_PRE_PMD);
543 if (w->id == snd_soc_dapm_post) {
547 if (event == SND_SOC_DAPM_STREAM_START) {
548 ret = w->event(w, SND_SOC_DAPM_POST_PMU);
551 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
552 ret = w->event(w, SND_SOC_DAPM_POST_PMD);
559 /* all other widgets */
560 in = is_connected_input_ep(w);
561 dapm_clear_walk(w->codec);
562 out = is_connected_output_ep(w);
563 dapm_clear_walk(w->codec);
564 power = (out != 0 && in != 0) ? 1 : 0;
565 power_change = (w->power == power) ? 0: 1;
568 /* call any power change event handlers */
571 dbg("power %s event for %s flags %x\n",
572 w->power ? "on" : "off", w->name, w->event_flags);
575 if (w->event_flags & SND_SOC_DAPM_PRE_PMU) {
576 ret = w->event(w, SND_SOC_DAPM_PRE_PMU);
581 if (w->event_flags & SND_SOC_DAPM_POST_PMU){
582 ret = w->event(w, SND_SOC_DAPM_POST_PMU);
587 /* power down event */
588 if (w->event_flags & SND_SOC_DAPM_PRE_PMD) {
589 ret = w->event(w, SND_SOC_DAPM_PRE_PMD);
594 if (w->event_flags & SND_SOC_DAPM_POST_PMD) {
595 ret = w->event(w, SND_SOC_DAPM_POST_PMD);
601 /* no event handler */
611 static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
613 struct snd_soc_dapm_widget *w;
614 struct snd_soc_dapm_path *p = NULL;
617 printk("DAPM %s %s\n", codec->name, action);
619 list_for_each_entry(w, &codec->dapm_widgets, list) {
621 /* only display widgets that effect routing */
623 case snd_soc_dapm_pre:
624 case snd_soc_dapm_post:
625 case snd_soc_dapm_vmid:
627 case snd_soc_dapm_mux:
628 case snd_soc_dapm_output:
629 case snd_soc_dapm_input:
630 case snd_soc_dapm_switch:
631 case snd_soc_dapm_hp:
632 case snd_soc_dapm_mic:
633 case snd_soc_dapm_spk:
634 case snd_soc_dapm_line:
635 case snd_soc_dapm_micbias:
636 case snd_soc_dapm_dac:
637 case snd_soc_dapm_adc:
638 case snd_soc_dapm_pga:
639 case snd_soc_dapm_mixer:
641 in = is_connected_input_ep(w);
642 dapm_clear_walk(w->codec);
643 out = is_connected_output_ep(w);
644 dapm_clear_walk(w->codec);
645 printk("%s: %s in %d out %d\n", w->name,
646 w->power ? "On":"Off",in, out);
648 list_for_each_entry(p, &w->sources, list_sink) {
650 printk(" in %s %s\n", p->name ? p->name : "static",
653 list_for_each_entry(p, &w->sinks, list_source) {
655 printk(" out %s %s\n", p->name ? p->name : "static",
665 /* test and update the power status of a mux widget */
666 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
667 struct snd_kcontrol *kcontrol, int mask,
668 int val, struct soc_enum* e)
670 struct snd_soc_dapm_path *path;
673 if (widget->id != snd_soc_dapm_mux)
676 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
679 /* find dapm widget path assoc with kcontrol */
680 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
681 if (path->kcontrol != kcontrol)
684 if (!path->name || ! e->texts[val])
688 /* we now need to match the string in the enum to the path */
689 if (!(strcmp(path->name, e->texts[val])))
690 path->connect = 1; /* new connection */
692 path->connect = 0; /* old connection must be powered down */
696 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
701 /* test and update the power status of a mixer widget */
702 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
703 struct snd_kcontrol *kcontrol, int reg,
704 int val_mask, int val, int invert)
706 struct snd_soc_dapm_path *path;
709 if (widget->id != snd_soc_dapm_mixer)
712 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
715 /* find dapm widget path assoc with kcontrol */
716 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
717 if (path->kcontrol != kcontrol)
720 /* found, now check type */
724 path->connect = invert ? 0:1;
726 /* old connection must be powered down */
727 path->connect = invert ? 1:0;
732 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
737 /* show dapm widget status in sys fs */
738 static ssize_t dapm_widget_show(struct device *dev,
739 struct device_attribute *attr, char *buf)
741 struct snd_soc_device *devdata = dev_get_drvdata(dev);
742 struct snd_soc_codec *codec = devdata->codec;
743 struct snd_soc_dapm_widget *w;
745 char *state = "not set";
747 list_for_each_entry(w, &codec->dapm_widgets, list) {
749 /* only display widgets that burnm power */
751 case snd_soc_dapm_hp:
752 case snd_soc_dapm_mic:
753 case snd_soc_dapm_spk:
754 case snd_soc_dapm_line:
755 case snd_soc_dapm_micbias:
756 case snd_soc_dapm_dac:
757 case snd_soc_dapm_adc:
758 case snd_soc_dapm_pga:
759 case snd_soc_dapm_mixer:
761 count += sprintf(buf + count, "%s: %s\n",
762 w->name, w->power ? "On":"Off");
769 switch(codec->dapm_state){
770 case SNDRV_CTL_POWER_D0:
773 case SNDRV_CTL_POWER_D1:
776 case SNDRV_CTL_POWER_D2:
779 case SNDRV_CTL_POWER_D3hot:
782 case SNDRV_CTL_POWER_D3cold:
786 count += sprintf(buf + count, "PM State: %s\n", state);
791 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
793 int snd_soc_dapm_sys_add(struct device *dev)
798 ret = device_create_file(dev, &dev_attr_dapm_widget);
803 static void snd_soc_dapm_sys_remove(struct device *dev)
806 device_remove_file(dev, &dev_attr_dapm_widget);
809 /* free all dapm widgets and resources */
810 static void dapm_free_widgets(struct snd_soc_codec *codec)
812 struct snd_soc_dapm_widget *w, *next_w;
813 struct snd_soc_dapm_path *p, *next_p;
815 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
820 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
828 * snd_soc_dapm_sync_endpoints - scan and power dapm paths
829 * @codec: audio codec
831 * Walks all dapm audio paths and powers widgets according to their
832 * stream or path usage.
834 * Returns 0 for success.
836 int snd_soc_dapm_sync_endpoints(struct snd_soc_codec *codec)
838 return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
840 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_endpoints);
843 * snd_soc_dapm_connect_input - connect dapm widgets
844 * @codec: audio codec
845 * @sink: name of target widget
846 * @control: mixer control name
847 * @source: name of source name
849 * Connects 2 dapm widgets together via a named audio path. The sink is
850 * the widget receiving the audio signal, whilst the source is the sender
851 * of the audio signal.
853 * Returns 0 for success else error.
855 int snd_soc_dapm_connect_input(struct snd_soc_codec *codec, const char *sink,
856 const char * control, const char *source)
858 struct snd_soc_dapm_path *path;
859 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
862 /* find src and dest widgets */
863 list_for_each_entry(w, &codec->dapm_widgets, list) {
865 if (!wsink && !(strcmp(w->name, sink))) {
869 if (!wsource && !(strcmp(w->name, source))) {
874 if (wsource == NULL || wsink == NULL)
877 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
881 path->source = wsource;
883 INIT_LIST_HEAD(&path->list);
884 INIT_LIST_HEAD(&path->list_source);
885 INIT_LIST_HEAD(&path->list_sink);
887 /* check for external widgets */
888 if (wsink->id == snd_soc_dapm_input) {
889 if (wsource->id == snd_soc_dapm_micbias ||
890 wsource->id == snd_soc_dapm_mic ||
891 wsink->id == snd_soc_dapm_line)
894 if (wsource->id == snd_soc_dapm_output) {
895 if (wsink->id == snd_soc_dapm_spk ||
896 wsink->id == snd_soc_dapm_hp ||
897 wsink->id == snd_soc_dapm_line)
901 /* connect static paths */
902 if (control == NULL) {
903 list_add(&path->list, &codec->dapm_paths);
904 list_add(&path->list_sink, &wsink->sources);
905 list_add(&path->list_source, &wsource->sinks);
910 /* connect dynamic paths */
912 case snd_soc_dapm_adc:
913 case snd_soc_dapm_dac:
914 case snd_soc_dapm_pga:
915 case snd_soc_dapm_input:
916 case snd_soc_dapm_output:
917 case snd_soc_dapm_micbias:
918 case snd_soc_dapm_vmid:
919 case snd_soc_dapm_pre:
920 case snd_soc_dapm_post:
921 list_add(&path->list, &codec->dapm_paths);
922 list_add(&path->list_sink, &wsink->sources);
923 list_add(&path->list_source, &wsource->sinks);
926 case snd_soc_dapm_mux:
927 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
928 &wsink->kcontrols[0]);
932 case snd_soc_dapm_switch:
933 case snd_soc_dapm_mixer:
934 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
938 case snd_soc_dapm_hp:
939 case snd_soc_dapm_mic:
940 case snd_soc_dapm_line:
941 case snd_soc_dapm_spk:
942 list_add(&path->list, &codec->dapm_paths);
943 list_add(&path->list_sink, &wsink->sources);
944 list_add(&path->list_source, &wsource->sinks);
951 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
956 EXPORT_SYMBOL_GPL(snd_soc_dapm_connect_input);
959 * snd_soc_dapm_new_widgets - add new dapm widgets
960 * @codec: audio codec
962 * Checks the codec for any new dapm widgets and creates them if found.
964 * Returns 0 for success.
966 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
968 struct snd_soc_dapm_widget *w;
970 mutex_lock(&codec->mutex);
971 list_for_each_entry(w, &codec->dapm_widgets, list)
977 case snd_soc_dapm_switch:
978 case snd_soc_dapm_mixer:
979 dapm_new_mixer(codec, w);
981 case snd_soc_dapm_mux:
982 dapm_new_mux(codec, w);
984 case snd_soc_dapm_adc:
985 case snd_soc_dapm_dac:
986 case snd_soc_dapm_pga:
987 dapm_new_pga(codec, w);
989 case snd_soc_dapm_input:
990 case snd_soc_dapm_output:
991 case snd_soc_dapm_micbias:
992 case snd_soc_dapm_spk:
993 case snd_soc_dapm_hp:
994 case snd_soc_dapm_mic:
995 case snd_soc_dapm_line:
996 case snd_soc_dapm_vmid:
997 case snd_soc_dapm_pre:
998 case snd_soc_dapm_post:
1004 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1005 mutex_unlock(&codec->mutex);
1008 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1011 * snd_soc_dapm_get_volsw - dapm mixer get callback
1012 * @kcontrol: mixer control
1013 * @uinfo: control element information
1015 * Callback to get the value of a dapm mixer control.
1017 * Returns 0 for success.
1019 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1020 struct snd_ctl_elem_value *ucontrol)
1022 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1023 int reg = kcontrol->private_value & 0xff;
1024 int shift = (kcontrol->private_value >> 8) & 0x0f;
1025 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1026 int mask = (kcontrol->private_value >> 16) & 0xff;
1027 int invert = (kcontrol->private_value >> 24) & 0x01;
1029 /* return the saved value if we are powered down */
1030 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1031 ucontrol->value.integer.value[0] = widget->saved_value;
1035 ucontrol->value.integer.value[0] =
1036 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1037 if (shift != rshift)
1038 ucontrol->value.integer.value[1] =
1039 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1041 ucontrol->value.integer.value[0] =
1042 mask - ucontrol->value.integer.value[0];
1043 if (shift != rshift)
1044 ucontrol->value.integer.value[1] =
1045 mask - ucontrol->value.integer.value[1];
1050 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1053 * snd_soc_dapm_put_volsw - dapm mixer set callback
1054 * @kcontrol: mixer control
1055 * @uinfo: control element information
1057 * Callback to set the value of a dapm mixer control.
1059 * Returns 0 for success.
1061 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1062 struct snd_ctl_elem_value *ucontrol)
1064 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1065 int reg = kcontrol->private_value & 0xff;
1066 int shift = (kcontrol->private_value >> 8) & 0x0f;
1067 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1068 int mask = (kcontrol->private_value >> 16) & 0xff;
1069 int invert = (kcontrol->private_value >> 24) & 0x01;
1070 unsigned short val, val2, val_mask;
1073 val = (ucontrol->value.integer.value[0] & mask);
1077 val_mask = mask << shift;
1079 if (shift != rshift) {
1080 val2 = (ucontrol->value.integer.value[1] & mask);
1083 val_mask |= mask << rshift;
1084 val |= val2 << rshift;
1087 mutex_lock(&widget->codec->mutex);
1088 widget->value = val;
1090 /* save volume value if the widget is powered down */
1091 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1092 widget->saved_value = val;
1093 mutex_unlock(&widget->codec->mutex);
1097 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1098 if (widget->event) {
1099 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1100 ret = widget->event(widget, SND_SOC_DAPM_PRE_REG);
1104 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1105 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1106 ret = widget->event(widget, SND_SOC_DAPM_POST_REG);
1108 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1111 mutex_unlock(&widget->codec->mutex);
1114 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1117 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1118 * @kcontrol: mixer control
1119 * @uinfo: control element information
1121 * Callback to get the value of a dapm enumerated double mixer control.
1123 * Returns 0 for success.
1125 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1126 struct snd_ctl_elem_value *ucontrol)
1128 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1129 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1130 unsigned short val, bitmask;
1132 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1134 val = snd_soc_read(widget->codec, e->reg);
1135 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1136 if (e->shift_l != e->shift_r)
1137 ucontrol->value.enumerated.item[1] =
1138 (val >> e->shift_r) & (bitmask - 1);
1142 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1145 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1146 * @kcontrol: mixer control
1147 * @uinfo: control element information
1149 * Callback to set the value of a dapm enumerated double mixer control.
1151 * Returns 0 for success.
1153 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1154 struct snd_ctl_elem_value *ucontrol)
1156 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1157 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1158 unsigned short val, mux;
1159 unsigned short mask, bitmask;
1162 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1164 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1166 mux = ucontrol->value.enumerated.item[0];
1167 val = mux << e->shift_l;
1168 mask = (bitmask - 1) << e->shift_l;
1169 if (e->shift_l != e->shift_r) {
1170 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1172 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1173 mask |= (bitmask - 1) << e->shift_r;
1176 mutex_lock(&widget->codec->mutex);
1177 widget->value = val;
1178 dapm_mux_update_power(widget, kcontrol, mask, mux, e);
1179 if (widget->event) {
1180 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1181 ret = widget->event(widget, SND_SOC_DAPM_PRE_REG);
1185 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1186 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1187 ret = widget->event(widget, SND_SOC_DAPM_POST_REG);
1189 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1192 mutex_unlock(&widget->codec->mutex);
1195 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1198 * snd_soc_dapm_new_control - create new dapm control
1199 * @codec: audio codec
1200 * @widget: widget template
1202 * Creates a new dapm control based upon the template.
1204 * Returns 0 for success else error.
1206 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1207 const struct snd_soc_dapm_widget *widget)
1209 struct snd_soc_dapm_widget *w;
1211 if ((w = dapm_cnew_widget(widget)) == NULL)
1215 INIT_LIST_HEAD(&w->sources);
1216 INIT_LIST_HEAD(&w->sinks);
1217 INIT_LIST_HEAD(&w->list);
1218 list_add(&w->list, &codec->dapm_widgets);
1220 /* machine layer set ups unconnected pins and insertions */
1224 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1227 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1228 * @codec: audio codec
1229 * @stream: stream name
1230 * @event: stream event
1232 * Sends a stream event to the dapm core. The core then makes any
1233 * necessary widget power changes.
1235 * Returns 0 for success else error.
1237 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1238 char *stream, int event)
1240 struct snd_soc_dapm_widget *w;
1242 mutex_lock(&codec->mutex);
1243 list_for_each_entry(w, &codec->dapm_widgets, list)
1247 dbg("widget %s\n %s stream %s event %d\n", w->name, w->sname,
1249 if (strstr(w->sname, stream)) {
1251 case SND_SOC_DAPM_STREAM_START:
1254 case SND_SOC_DAPM_STREAM_STOP:
1257 case SND_SOC_DAPM_STREAM_SUSPEND:
1262 case SND_SOC_DAPM_STREAM_RESUME:
1268 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1270 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1275 mutex_unlock(&codec->mutex);
1277 dapm_power_widgets(codec, event);
1278 dump_dapm(codec, __FUNCTION__);
1281 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1284 * snd_soc_dapm_set_endpoint - set audio endpoint status
1285 * @codec: audio codec
1286 * @endpoint: audio signal endpoint (or start point)
1287 * @status: point status
1289 * Set audio endpoint status - connected or disconnected.
1291 * Returns 0 for success else error.
1293 int snd_soc_dapm_set_endpoint(struct snd_soc_codec *codec,
1294 char *endpoint, int status)
1296 struct snd_soc_dapm_widget *w;
1298 list_for_each_entry(w, &codec->dapm_widgets, list) {
1299 if (!strcmp(w->name, endpoint)) {
1300 w->connected = status;
1306 EXPORT_SYMBOL_GPL(snd_soc_dapm_set_endpoint);
1309 * snd_soc_dapm_free - free dapm resources
1310 * @socdev: SoC device
1312 * Free all dapm widgets and resources.
1314 void snd_soc_dapm_free(struct snd_soc_device *socdev)
1316 struct snd_soc_codec *codec = socdev->codec;
1318 snd_soc_dapm_sys_remove(socdev->dev);
1319 dapm_free_widgets(codec);
1321 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1323 /* Module information */
1324 MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1325 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1326 MODULE_LICENSE("GPL");