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 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) {
654 p = list_entry(lp, struct snd_soc_dapm_path, list_source);
656 printk(" out %s %s\n", p->name ? p->name : "static",
666 /* test and update the power status of a mux widget */
667 int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
668 struct snd_kcontrol *kcontrol, int mask, 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);
700 EXPORT_SYMBOL_GPL(dapm_mux_update_power);
702 /* test and update the power status of a mixer widget */
703 int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
704 struct snd_kcontrol *kcontrol, int reg, 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);
736 EXPORT_SYMBOL_GPL(dapm_mixer_update_power);
738 /* show dapm widget status in sys fs */
739 static ssize_t dapm_widget_show(struct device *dev,
740 struct device_attribute *attr, char *buf)
742 struct snd_soc_device *devdata = dev_get_drvdata(dev);
743 struct snd_soc_codec *codec = devdata->codec;
744 struct snd_soc_dapm_widget *w;
746 char *state = "not set";
748 list_for_each_entry(w, &codec->dapm_widgets, list) {
750 /* only display widgets that burnm power */
752 case snd_soc_dapm_hp:
753 case snd_soc_dapm_mic:
754 case snd_soc_dapm_spk:
755 case snd_soc_dapm_line:
756 case snd_soc_dapm_micbias:
757 case snd_soc_dapm_dac:
758 case snd_soc_dapm_adc:
759 case snd_soc_dapm_pga:
760 case snd_soc_dapm_mixer:
762 count += sprintf(buf + count, "%s: %s\n",
763 w->name, w->power ? "On":"Off");
770 switch(codec->dapm_state){
771 case SNDRV_CTL_POWER_D0:
774 case SNDRV_CTL_POWER_D1:
777 case SNDRV_CTL_POWER_D2:
780 case SNDRV_CTL_POWER_D3hot:
783 case SNDRV_CTL_POWER_D3cold:
787 count += sprintf(buf + count, "PM State: %s\n", state);
792 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
794 int snd_soc_dapm_sys_add(struct device *dev)
799 ret = device_create_file(dev, &dev_attr_dapm_widget);
804 static void snd_soc_dapm_sys_remove(struct device *dev)
807 device_remove_file(dev, &dev_attr_dapm_widget);
810 /* free all dapm widgets and resources */
811 void dapm_free_widgets(struct snd_soc_codec *codec)
813 struct snd_soc_dapm_widget *w, *next_w;
814 struct snd_soc_dapm_path *p, *next_p;
816 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
821 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
829 * snd_soc_dapm_sync_endpoints - scan and power dapm paths
830 * @codec: audio codec
832 * Walks all dapm audio paths and powers widgets according to their
833 * stream or path usage.
835 * Returns 0 for success.
837 int snd_soc_dapm_sync_endpoints(struct snd_soc_codec *codec)
839 return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
841 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_endpoints);
844 * snd_soc_dapm_connect_input - connect dapm widgets
845 * @codec: audio codec
846 * @sink: name of target widget
847 * @control: mixer control name
848 * @source: name of source name
850 * Connects 2 dapm widgets together via a named audio path. The sink is
851 * the widget receiving the audio signal, whilst the source is the sender
852 * of the audio signal.
854 * Returns 0 for success else error.
856 int snd_soc_dapm_connect_input(struct snd_soc_codec *codec, const char *sink,
857 const char * control, const char *source)
859 struct snd_soc_dapm_path *path;
860 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
863 /* find src and dest widgets */
864 list_for_each_entry(w, &codec->dapm_widgets, list) {
866 if (!wsink && !(strcmp(w->name, sink))) {
870 if (!wsource && !(strcmp(w->name, source))) {
875 if (wsource == NULL || wsink == NULL)
878 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
882 path->source = wsource;
884 INIT_LIST_HEAD(&path->list);
885 INIT_LIST_HEAD(&path->list_source);
886 INIT_LIST_HEAD(&path->list_sink);
888 /* check for external widgets */
889 if (wsink->id == snd_soc_dapm_input) {
890 if (wsource->id == snd_soc_dapm_micbias ||
891 wsource->id == snd_soc_dapm_mic ||
892 wsink->id == snd_soc_dapm_line)
895 if (wsource->id == snd_soc_dapm_output) {
896 if (wsink->id == snd_soc_dapm_spk ||
897 wsink->id == snd_soc_dapm_hp ||
898 wsink->id == snd_soc_dapm_line)
902 /* connect static paths */
903 if (control == NULL) {
904 list_add(&path->list, &codec->dapm_paths);
905 list_add(&path->list_sink, &wsink->sources);
906 list_add(&path->list_source, &wsource->sinks);
911 /* connect dynamic paths */
913 case snd_soc_dapm_adc:
914 case snd_soc_dapm_dac:
915 case snd_soc_dapm_pga:
916 case snd_soc_dapm_input:
917 case snd_soc_dapm_output:
918 case snd_soc_dapm_micbias:
919 case snd_soc_dapm_vmid:
920 case snd_soc_dapm_pre:
921 case snd_soc_dapm_post:
922 list_add(&path->list, &codec->dapm_paths);
923 list_add(&path->list_sink, &wsink->sources);
924 list_add(&path->list_source, &wsource->sinks);
927 case snd_soc_dapm_mux:
928 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
929 &wsink->kcontrols[0]);
933 case snd_soc_dapm_switch:
934 case snd_soc_dapm_mixer:
935 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
939 case snd_soc_dapm_hp:
940 case snd_soc_dapm_mic:
941 case snd_soc_dapm_line:
942 case snd_soc_dapm_spk:
943 list_add(&path->list, &codec->dapm_paths);
944 list_add(&path->list_sink, &wsink->sources);
945 list_add(&path->list_source, &wsource->sinks);
952 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
957 EXPORT_SYMBOL_GPL(snd_soc_dapm_connect_input);
960 * snd_soc_dapm_new_widgets - add new dapm widgets
961 * @codec: audio codec
963 * Checks the codec for any new dapm widgets and creates them if found.
965 * Returns 0 for success.
967 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
969 struct snd_soc_dapm_widget *w;
971 mutex_lock(&codec->mutex);
972 list_for_each_entry(w, &codec->dapm_widgets, list)
978 case snd_soc_dapm_switch:
979 case snd_soc_dapm_mixer:
980 dapm_new_mixer(codec, w);
982 case snd_soc_dapm_mux:
983 dapm_new_mux(codec, w);
985 case snd_soc_dapm_adc:
986 case snd_soc_dapm_dac:
987 case snd_soc_dapm_pga:
988 dapm_new_pga(codec, w);
990 case snd_soc_dapm_input:
991 case snd_soc_dapm_output:
992 case snd_soc_dapm_micbias:
993 case snd_soc_dapm_spk:
994 case snd_soc_dapm_hp:
995 case snd_soc_dapm_mic:
996 case snd_soc_dapm_line:
997 case snd_soc_dapm_vmid:
998 case snd_soc_dapm_pre:
999 case snd_soc_dapm_post:
1005 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1006 mutex_unlock(&codec->mutex);
1009 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1012 * snd_soc_dapm_get_volsw - dapm mixer get callback
1013 * @kcontrol: mixer control
1014 * @uinfo: control element information
1016 * Callback to get the value of a dapm mixer control.
1018 * Returns 0 for success.
1020 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1021 struct snd_ctl_elem_value *ucontrol)
1023 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1024 int reg = kcontrol->private_value & 0xff;
1025 int shift = (kcontrol->private_value >> 8) & 0x0f;
1026 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1027 int mask = (kcontrol->private_value >> 16) & 0xff;
1028 int invert = (kcontrol->private_value >> 24) & 0x01;
1030 /* return the saved value if we are powered down */
1031 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1032 ucontrol->value.integer.value[0] = widget->saved_value;
1036 ucontrol->value.integer.value[0] =
1037 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1038 if (shift != rshift)
1039 ucontrol->value.integer.value[1] =
1040 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1042 ucontrol->value.integer.value[0] =
1043 mask - ucontrol->value.integer.value[0];
1044 if (shift != rshift)
1045 ucontrol->value.integer.value[1] =
1046 mask - ucontrol->value.integer.value[1];
1051 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1054 * snd_soc_dapm_put_volsw - dapm mixer set callback
1055 * @kcontrol: mixer control
1056 * @uinfo: control element information
1058 * Callback to set the value of a dapm mixer control.
1060 * Returns 0 for success.
1062 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1063 struct snd_ctl_elem_value *ucontrol)
1065 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1066 int reg = kcontrol->private_value & 0xff;
1067 int shift = (kcontrol->private_value >> 8) & 0x0f;
1068 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1069 int mask = (kcontrol->private_value >> 16) & 0xff;
1070 int invert = (kcontrol->private_value >> 24) & 0x01;
1071 unsigned short val, val2, val_mask;
1074 val = (ucontrol->value.integer.value[0] & mask);
1078 val_mask = mask << shift;
1080 if (shift != rshift) {
1081 val2 = (ucontrol->value.integer.value[1] & mask);
1084 val_mask |= mask << rshift;
1085 val |= val2 << rshift;
1088 mutex_lock(&widget->codec->mutex);
1089 widget->value = val;
1091 /* save volume value if the widget is powered down */
1092 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1093 widget->saved_value = val;
1094 mutex_unlock(&widget->codec->mutex);
1098 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1099 if (widget->event) {
1100 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1101 ret = widget->event(widget, SND_SOC_DAPM_PRE_REG);
1105 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1106 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1107 ret = widget->event(widget, SND_SOC_DAPM_POST_REG);
1109 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1112 mutex_unlock(&widget->codec->mutex);
1115 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1118 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1119 * @kcontrol: mixer control
1120 * @uinfo: control element information
1122 * Callback to get the value of a dapm enumerated double mixer control.
1124 * Returns 0 for success.
1126 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1127 struct snd_ctl_elem_value *ucontrol)
1129 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1130 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1131 unsigned short val, bitmask;
1133 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1135 val = snd_soc_read(widget->codec, e->reg);
1136 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1137 if (e->shift_l != e->shift_r)
1138 ucontrol->value.enumerated.item[1] =
1139 (val >> e->shift_r) & (bitmask - 1);
1143 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1146 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1147 * @kcontrol: mixer control
1148 * @uinfo: control element information
1150 * Callback to set the value of a dapm enumerated double mixer control.
1152 * Returns 0 for success.
1154 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1155 struct snd_ctl_elem_value *ucontrol)
1157 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1158 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1159 unsigned short val, mux;
1160 unsigned short mask, bitmask;
1163 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1165 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1167 mux = ucontrol->value.enumerated.item[0];
1168 val = mux << e->shift_l;
1169 mask = (bitmask - 1) << e->shift_l;
1170 if (e->shift_l != e->shift_r) {
1171 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1173 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1174 mask |= (bitmask - 1) << e->shift_r;
1177 mutex_lock(&widget->codec->mutex);
1178 widget->value = val;
1179 dapm_mux_update_power(widget, kcontrol, mask, mux, e);
1180 if (widget->event) {
1181 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1182 ret = widget->event(widget, SND_SOC_DAPM_PRE_REG);
1186 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1187 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1188 ret = widget->event(widget, SND_SOC_DAPM_POST_REG);
1190 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1193 mutex_unlock(&widget->codec->mutex);
1196 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1199 * snd_soc_dapm_new_control - create new dapm control
1200 * @codec: audio codec
1201 * @widget: widget template
1203 * Creates a new dapm control based upon the template.
1205 * Returns 0 for success else error.
1207 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1208 const struct snd_soc_dapm_widget *widget)
1210 struct snd_soc_dapm_widget *w;
1212 if ((w = dapm_cnew_widget(widget)) == NULL)
1216 INIT_LIST_HEAD(&w->sources);
1217 INIT_LIST_HEAD(&w->sinks);
1218 INIT_LIST_HEAD(&w->list);
1219 list_add(&w->list, &codec->dapm_widgets);
1221 /* machine layer set ups unconnected pins and insertions */
1225 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1228 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1229 * @codec: audio codec
1230 * @stream: stream name
1231 * @event: stream event
1233 * Sends a stream event to the dapm core. The core then makes any
1234 * necessary widget power changes.
1236 * Returns 0 for success else error.
1238 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1239 char *stream, int event)
1241 struct snd_soc_dapm_widget *w;
1243 mutex_lock(&codec->mutex);
1244 list_for_each_entry(w, &codec->dapm_widgets, list)
1248 dbg("widget %s\n %s stream %s event %d\n", w->name, w->sname,
1250 if (strstr(w->sname, stream)) {
1252 case SND_SOC_DAPM_STREAM_START:
1255 case SND_SOC_DAPM_STREAM_STOP:
1258 case SND_SOC_DAPM_STREAM_SUSPEND:
1263 case SND_SOC_DAPM_STREAM_RESUME:
1269 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1271 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1276 mutex_unlock(&codec->mutex);
1278 dapm_power_widgets(codec, event);
1279 dump_dapm(codec, __FUNCTION__);
1282 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1285 * snd_soc_dapm_set_endpoint - set audio endpoint status
1286 * @codec: audio codec
1287 * @endpoint: audio signal endpoint (or start point)
1288 * @status: point status
1290 * Set audio endpoint status - connected or disconnected.
1292 * Returns 0 for success else error.
1294 int snd_soc_dapm_set_endpoint(struct snd_soc_codec *codec,
1295 char *endpoint, int status)
1297 struct snd_soc_dapm_widget *w;
1299 list_for_each_entry(w, &codec->dapm_widgets, list) {
1300 if (!strcmp(w->name, endpoint)) {
1301 w->connected = status;
1307 EXPORT_SYMBOL_GPL(snd_soc_dapm_set_endpoint);
1310 * snd_soc_dapm_free - free dapm resources
1311 * @socdev: SoC device
1313 * Free all dapm widgets and resources.
1315 void snd_soc_dapm_free(struct snd_soc_device *socdev)
1317 struct snd_soc_codec *codec = socdev->codec;
1319 snd_soc_dapm_sys_remove(socdev->dev);
1320 dapm_free_widgets(codec);
1322 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1324 /* Module information */
1325 MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1326 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1327 MODULE_LICENSE("GPL");