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 <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc-dapm.h>
44 #include <sound/initval.h>
48 #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
50 #define dump_dapm(codec, action)
53 /* dapm power sequences - make this per codec in the future */
54 static int dapm_up_seq[] = {
55 snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
56 snd_soc_dapm_mux, snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_pga,
57 snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
59 static int dapm_down_seq[] = {
60 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
61 snd_soc_dapm_pga, snd_soc_dapm_mixer, snd_soc_dapm_dac, snd_soc_dapm_mic,
62 snd_soc_dapm_micbias, snd_soc_dapm_mux, snd_soc_dapm_post
65 static int dapm_status = 1;
66 module_param(dapm_status, int, 0);
67 MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
69 static void pop_wait(u32 pop_time)
72 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
75 static void pop_dbg(u32 pop_time, const char *fmt, ...)
89 /* create a new dapm widget */
90 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
91 const struct snd_soc_dapm_widget *_widget)
93 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
96 /* set up initial codec paths */
97 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
98 struct snd_soc_dapm_path *p, int i)
101 case snd_soc_dapm_switch:
102 case snd_soc_dapm_mixer: {
104 struct soc_mixer_control *mc = (struct soc_mixer_control *)
105 w->kcontrols[i].private_value;
106 unsigned int reg = mc->reg;
107 unsigned int shift = mc->shift;
109 unsigned int mask = (1 << fls(max)) - 1;
110 unsigned int invert = mc->invert;
112 val = snd_soc_read(w->codec, reg);
113 val = (val >> shift) & mask;
115 if ((invert && !val) || (!invert && val))
121 case snd_soc_dapm_mux: {
122 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
123 int val, item, bitmask;
125 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
127 val = snd_soc_read(w->codec, e->reg);
128 item = (val >> e->shift_l) & (bitmask - 1);
131 for (i = 0; i < e->max; i++) {
132 if (!(strcmp(p->name, e->texts[i])) && item == i)
137 /* does not effect routing - always connected */
138 case snd_soc_dapm_pga:
139 case snd_soc_dapm_output:
140 case snd_soc_dapm_adc:
141 case snd_soc_dapm_input:
142 case snd_soc_dapm_dac:
143 case snd_soc_dapm_micbias:
144 case snd_soc_dapm_vmid:
147 /* does effect routing - dynamically connected */
148 case snd_soc_dapm_hp:
149 case snd_soc_dapm_mic:
150 case snd_soc_dapm_spk:
151 case snd_soc_dapm_line:
152 case snd_soc_dapm_pre:
153 case snd_soc_dapm_post:
159 /* connect mux widget to it's interconnecting audio paths */
160 static int dapm_connect_mux(struct snd_soc_codec *codec,
161 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
162 struct snd_soc_dapm_path *path, const char *control_name,
163 const struct snd_kcontrol_new *kcontrol)
165 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
168 for (i = 0; i < e->max; i++) {
169 if (!(strcmp(control_name, e->texts[i]))) {
170 list_add(&path->list, &codec->dapm_paths);
171 list_add(&path->list_sink, &dest->sources);
172 list_add(&path->list_source, &src->sinks);
173 path->name = (char*)e->texts[i];
174 dapm_set_path_status(dest, path, 0);
182 /* connect mixer widget to it's interconnecting audio paths */
183 static int dapm_connect_mixer(struct snd_soc_codec *codec,
184 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
185 struct snd_soc_dapm_path *path, const char *control_name)
189 /* search for mixer kcontrol */
190 for (i = 0; i < dest->num_kcontrols; i++) {
191 if (!strcmp(control_name, dest->kcontrols[i].name)) {
192 list_add(&path->list, &codec->dapm_paths);
193 list_add(&path->list_sink, &dest->sources);
194 list_add(&path->list_source, &src->sinks);
195 path->name = dest->kcontrols[i].name;
196 dapm_set_path_status(dest, path, i);
203 /* update dapm codec register bits */
204 static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
207 unsigned short old, new;
208 struct snd_soc_codec *codec = widget->codec;
210 /* check for valid widgets */
211 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
212 widget->id == snd_soc_dapm_output ||
213 widget->id == snd_soc_dapm_hp ||
214 widget->id == snd_soc_dapm_mic ||
215 widget->id == snd_soc_dapm_line ||
216 widget->id == snd_soc_dapm_spk)
219 power = widget->power;
221 power = (power ? 0:1);
223 old = snd_soc_read(codec, widget->reg);
224 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
228 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
229 widget->name, widget->power ? "on" : "off",
231 snd_soc_write(codec, widget->reg, new);
232 pop_wait(codec->pop_time);
234 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
239 /* ramps the volume up or down to minimise pops before or after a
240 * DAPM power event */
241 static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
243 const struct snd_kcontrol_new *k = widget->kcontrols;
245 if (widget->muted && !power)
247 if (!widget->muted && power)
250 if (widget->num_kcontrols && k) {
251 struct soc_mixer_control *mc =
252 (struct soc_mixer_control *)k->private_value;
253 unsigned int reg = mc->reg;
254 unsigned int shift = mc->shift;
256 unsigned int mask = (1 << fls(max)) - 1;
257 unsigned int invert = mc->invert;
261 /* power up has happended, increase volume to last level */
263 for (i = max; i > widget->saved_value; i--)
264 snd_soc_update_bits(widget->codec, reg, mask, i);
266 for (i = 0; i < widget->saved_value; i++)
267 snd_soc_update_bits(widget->codec, reg, mask, i);
271 /* power down is about to occur, decrease volume to mute */
272 int val = snd_soc_read(widget->codec, reg);
273 int i = widget->saved_value = (val >> shift) & mask;
275 for (; i < mask; i++)
276 snd_soc_update_bits(widget->codec, reg, mask, i);
279 snd_soc_update_bits(widget->codec, reg, mask, i);
287 /* create new dapm mixer control */
288 static int dapm_new_mixer(struct snd_soc_codec *codec,
289 struct snd_soc_dapm_widget *w)
293 struct snd_soc_dapm_path *path;
296 for (i = 0; i < w->num_kcontrols; i++) {
299 list_for_each_entry(path, &w->sources, list_sink) {
301 /* mixer/mux paths name must match control name */
302 if (path->name != (char*)w->kcontrols[i].name)
305 /* add dapm control with long name */
306 name_len = 2 + strlen(w->name)
307 + strlen(w->kcontrols[i].name);
308 path->long_name = kmalloc(name_len, GFP_KERNEL);
309 if (path->long_name == NULL)
312 snprintf(path->long_name, name_len, "%s %s",
313 w->name, w->kcontrols[i].name);
314 path->long_name[name_len - 1] = '\0';
316 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
318 ret = snd_ctl_add(codec->card, path->kcontrol);
320 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
322 kfree(path->long_name);
323 path->long_name = NULL;
331 /* create new dapm mux control */
332 static int dapm_new_mux(struct snd_soc_codec *codec,
333 struct snd_soc_dapm_widget *w)
335 struct snd_soc_dapm_path *path = NULL;
336 struct snd_kcontrol *kcontrol;
339 if (!w->num_kcontrols) {
340 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
344 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
345 ret = snd_ctl_add(codec->card, kcontrol);
349 list_for_each_entry(path, &w->sources, list_sink)
350 path->kcontrol = kcontrol;
355 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
359 /* create new dapm volume control */
360 static int dapm_new_pga(struct snd_soc_codec *codec,
361 struct snd_soc_dapm_widget *w)
363 struct snd_kcontrol *kcontrol;
366 if (!w->num_kcontrols)
369 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
370 ret = snd_ctl_add(codec->card, kcontrol);
372 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
379 /* reset 'walked' bit for each dapm path */
380 static inline void dapm_clear_walk(struct snd_soc_codec *codec)
382 struct snd_soc_dapm_path *p;
384 list_for_each_entry(p, &codec->dapm_paths, list)
389 * Recursively check for a completed path to an active or physically connected
390 * output widget. Returns number of complete paths.
392 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
394 struct snd_soc_dapm_path *path;
397 if (widget->id == snd_soc_dapm_adc && widget->active)
400 if (widget->connected) {
401 /* connected pin ? */
402 if (widget->id == snd_soc_dapm_output && !widget->ext)
405 /* connected jack or spk ? */
406 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
407 widget->id == snd_soc_dapm_line)
411 list_for_each_entry(path, &widget->sinks, list_source) {
415 if (path->sink && path->connect) {
417 con += is_connected_output_ep(path->sink);
425 * Recursively check for a completed path to an active or physically connected
426 * input widget. Returns number of complete paths.
428 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
430 struct snd_soc_dapm_path *path;
433 /* active stream ? */
434 if (widget->id == snd_soc_dapm_dac && widget->active)
437 if (widget->connected) {
438 /* connected pin ? */
439 if (widget->id == snd_soc_dapm_input && !widget->ext)
442 /* connected VMID/Bias for lower pops */
443 if (widget->id == snd_soc_dapm_vmid)
446 /* connected jack ? */
447 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
451 list_for_each_entry(path, &widget->sources, list_sink) {
455 if (path->source && path->connect) {
457 con += is_connected_input_ep(path->source);
465 * Handler for generic register modifier widget.
467 int dapm_reg_event(struct snd_soc_dapm_widget *w,
468 struct snd_kcontrol *kcontrol, int event)
472 if (SND_SOC_DAPM_EVENT_ON(event))
477 snd_soc_update_bits(w->codec, -(w->reg + 1),
478 w->mask << w->shift, val << w->shift);
482 EXPORT_SYMBOL_GPL(dapm_reg_event);
485 * Scan each dapm widget for complete audio path.
486 * A complete path is a route that has valid endpoints i.e.:-
488 * o DAC to output pin.
489 * o Input Pin to ADC.
490 * o Input pin to Output pin (bypass, sidetone)
491 * o DAC to ADC (loopback).
493 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
495 struct snd_soc_dapm_widget *w;
496 int in, out, i, c = 1, *seq = NULL, ret = 0, power_change, power;
498 /* do we have a sequenced stream event */
499 if (event == SND_SOC_DAPM_STREAM_START) {
500 c = ARRAY_SIZE(dapm_up_seq);
502 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
503 c = ARRAY_SIZE(dapm_down_seq);
507 for(i = 0; i < c; i++) {
508 list_for_each_entry(w, &codec->dapm_widgets, list) {
510 /* is widget in stream order */
511 if (seq && seq[i] && w->id != seq[i])
514 /* vmid - no action */
515 if (w->id == snd_soc_dapm_vmid)
519 if (w->id == snd_soc_dapm_adc && w->active) {
520 in = is_connected_input_ep(w);
521 dapm_clear_walk(w->codec);
522 w->power = (in != 0) ? 1 : 0;
528 if (w->id == snd_soc_dapm_dac && w->active) {
529 out = is_connected_output_ep(w);
530 dapm_clear_walk(w->codec);
531 w->power = (out != 0) ? 1 : 0;
536 /* pre and post event widgets */
537 if (w->id == snd_soc_dapm_pre) {
541 if (event == SND_SOC_DAPM_STREAM_START) {
543 NULL, SND_SOC_DAPM_PRE_PMU);
546 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
548 NULL, SND_SOC_DAPM_PRE_PMD);
554 if (w->id == snd_soc_dapm_post) {
558 if (event == SND_SOC_DAPM_STREAM_START) {
560 NULL, SND_SOC_DAPM_POST_PMU);
563 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
565 NULL, SND_SOC_DAPM_POST_PMD);
572 /* all other widgets */
573 in = is_connected_input_ep(w);
574 dapm_clear_walk(w->codec);
575 out = is_connected_output_ep(w);
576 dapm_clear_walk(w->codec);
577 power = (out != 0 && in != 0) ? 1 : 0;
578 power_change = (w->power == power) ? 0: 1;
584 /* call any power change event handlers */
586 pr_debug("power %s event for %s flags %x\n",
587 w->power ? "on" : "off",
588 w->name, w->event_flags);
590 /* power up pre event */
591 if (power && w->event &&
592 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
593 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
598 /* power down pre event */
599 if (!power && w->event &&
600 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
601 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
606 /* Lower PGA volume to reduce pops */
607 if (w->id == snd_soc_dapm_pga && !power)
608 dapm_set_pga(w, power);
612 /* Raise PGA volume to reduce pops */
613 if (w->id == snd_soc_dapm_pga && power)
614 dapm_set_pga(w, power);
616 /* power up post event */
617 if (power && w->event &&
618 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
620 NULL, SND_SOC_DAPM_POST_PMU);
625 /* power down post event */
626 if (!power && w->event &&
627 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
628 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
639 static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
641 struct snd_soc_dapm_widget *w;
642 struct snd_soc_dapm_path *p = NULL;
645 printk("DAPM %s %s\n", codec->name, action);
647 list_for_each_entry(w, &codec->dapm_widgets, list) {
649 /* only display widgets that effect routing */
651 case snd_soc_dapm_pre:
652 case snd_soc_dapm_post:
653 case snd_soc_dapm_vmid:
655 case snd_soc_dapm_mux:
656 case snd_soc_dapm_output:
657 case snd_soc_dapm_input:
658 case snd_soc_dapm_switch:
659 case snd_soc_dapm_hp:
660 case snd_soc_dapm_mic:
661 case snd_soc_dapm_spk:
662 case snd_soc_dapm_line:
663 case snd_soc_dapm_micbias:
664 case snd_soc_dapm_dac:
665 case snd_soc_dapm_adc:
666 case snd_soc_dapm_pga:
667 case snd_soc_dapm_mixer:
669 in = is_connected_input_ep(w);
670 dapm_clear_walk(w->codec);
671 out = is_connected_output_ep(w);
672 dapm_clear_walk(w->codec);
673 printk("%s: %s in %d out %d\n", w->name,
674 w->power ? "On":"Off",in, out);
676 list_for_each_entry(p, &w->sources, list_sink) {
678 printk(" in %s %s\n", p->name ? p->name : "static",
681 list_for_each_entry(p, &w->sinks, list_source) {
683 printk(" out %s %s\n", p->name ? p->name : "static",
693 /* test and update the power status of a mux widget */
694 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
695 struct snd_kcontrol *kcontrol, int mask,
696 int mux, int val, struct soc_enum *e)
698 struct snd_soc_dapm_path *path;
701 if (widget->id != snd_soc_dapm_mux)
704 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
707 /* find dapm widget path assoc with kcontrol */
708 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
709 if (path->kcontrol != kcontrol)
712 if (!path->name || !e->texts[mux])
716 /* we now need to match the string in the enum to the path */
717 if (!(strcmp(path->name, e->texts[mux])))
718 path->connect = 1; /* new connection */
720 path->connect = 0; /* old connection must be powered down */
724 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
725 dump_dapm(widget->codec, "mux power update");
731 /* test and update the power status of a mixer or switch widget */
732 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
733 struct snd_kcontrol *kcontrol, int reg,
734 int val_mask, int val, int invert)
736 struct snd_soc_dapm_path *path;
739 if (widget->id != snd_soc_dapm_mixer &&
740 widget->id != snd_soc_dapm_switch)
743 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
746 /* find dapm widget path assoc with kcontrol */
747 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
748 if (path->kcontrol != kcontrol)
751 /* found, now check type */
755 path->connect = invert ? 0:1;
757 /* old connection must be powered down */
758 path->connect = invert ? 1:0;
763 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
764 dump_dapm(widget->codec, "mixer power update");
770 /* show dapm widget status in sys fs */
771 static ssize_t dapm_widget_show(struct device *dev,
772 struct device_attribute *attr, char *buf)
774 struct snd_soc_device *devdata = dev_get_drvdata(dev);
775 struct snd_soc_codec *codec = devdata->codec;
776 struct snd_soc_dapm_widget *w;
778 char *state = "not set";
780 list_for_each_entry(w, &codec->dapm_widgets, list) {
782 /* only display widgets that burnm power */
784 case snd_soc_dapm_hp:
785 case snd_soc_dapm_mic:
786 case snd_soc_dapm_spk:
787 case snd_soc_dapm_line:
788 case snd_soc_dapm_micbias:
789 case snd_soc_dapm_dac:
790 case snd_soc_dapm_adc:
791 case snd_soc_dapm_pga:
792 case snd_soc_dapm_mixer:
794 count += sprintf(buf + count, "%s: %s\n",
795 w->name, w->power ? "On":"Off");
802 switch (codec->bias_level) {
803 case SND_SOC_BIAS_ON:
806 case SND_SOC_BIAS_PREPARE:
809 case SND_SOC_BIAS_STANDBY:
812 case SND_SOC_BIAS_OFF:
816 count += sprintf(buf + count, "PM State: %s\n", state);
821 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
823 int snd_soc_dapm_sys_add(struct device *dev)
830 ret = device_create_file(dev, &dev_attr_dapm_widget);
833 return device_create_file(dev, &dev_attr_dapm_widget);
836 static void snd_soc_dapm_sys_remove(struct device *dev)
839 device_remove_file(dev, &dev_attr_dapm_widget);
843 /* free all dapm widgets and resources */
844 static void dapm_free_widgets(struct snd_soc_codec *codec)
846 struct snd_soc_dapm_widget *w, *next_w;
847 struct snd_soc_dapm_path *p, *next_p;
849 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
854 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
861 static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
862 char *pin, int status)
864 struct snd_soc_dapm_widget *w;
866 list_for_each_entry(w, &codec->dapm_widgets, list) {
867 if (!strcmp(w->name, pin)) {
868 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
869 w->connected = status;
874 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
879 * snd_soc_dapm_sync - scan and power dapm paths
880 * @codec: audio codec
882 * Walks all dapm audio paths and powers widgets according to their
883 * stream or path usage.
885 * Returns 0 for success.
887 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
889 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
890 dump_dapm(codec, "sync");
893 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
895 static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
896 const char *sink, const char *control, const char *source)
898 struct snd_soc_dapm_path *path;
899 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
902 /* find src and dest widgets */
903 list_for_each_entry(w, &codec->dapm_widgets, list) {
905 if (!wsink && !(strcmp(w->name, sink))) {
909 if (!wsource && !(strcmp(w->name, source))) {
914 if (wsource == NULL || wsink == NULL)
917 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
921 path->source = wsource;
923 INIT_LIST_HEAD(&path->list);
924 INIT_LIST_HEAD(&path->list_source);
925 INIT_LIST_HEAD(&path->list_sink);
927 /* check for external widgets */
928 if (wsink->id == snd_soc_dapm_input) {
929 if (wsource->id == snd_soc_dapm_micbias ||
930 wsource->id == snd_soc_dapm_mic ||
931 wsink->id == snd_soc_dapm_line ||
932 wsink->id == snd_soc_dapm_output)
935 if (wsource->id == snd_soc_dapm_output) {
936 if (wsink->id == snd_soc_dapm_spk ||
937 wsink->id == snd_soc_dapm_hp ||
938 wsink->id == snd_soc_dapm_line ||
939 wsink->id == snd_soc_dapm_input)
943 /* connect static paths */
944 if (control == NULL) {
945 list_add(&path->list, &codec->dapm_paths);
946 list_add(&path->list_sink, &wsink->sources);
947 list_add(&path->list_source, &wsource->sinks);
952 /* connect dynamic paths */
954 case snd_soc_dapm_adc:
955 case snd_soc_dapm_dac:
956 case snd_soc_dapm_pga:
957 case snd_soc_dapm_input:
958 case snd_soc_dapm_output:
959 case snd_soc_dapm_micbias:
960 case snd_soc_dapm_vmid:
961 case snd_soc_dapm_pre:
962 case snd_soc_dapm_post:
963 list_add(&path->list, &codec->dapm_paths);
964 list_add(&path->list_sink, &wsink->sources);
965 list_add(&path->list_source, &wsource->sinks);
968 case snd_soc_dapm_mux:
969 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
970 &wsink->kcontrols[0]);
974 case snd_soc_dapm_switch:
975 case snd_soc_dapm_mixer:
976 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
980 case snd_soc_dapm_hp:
981 case snd_soc_dapm_mic:
982 case snd_soc_dapm_line:
983 case snd_soc_dapm_spk:
984 list_add(&path->list, &codec->dapm_paths);
985 list_add(&path->list_sink, &wsink->sources);
986 list_add(&path->list_source, &wsource->sinks);
993 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1000 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1002 * @route: audio routes
1003 * @num: number of routes
1005 * Connects 2 dapm widgets together via a named audio path. The sink is
1006 * the widget receiving the audio signal, whilst the source is the sender
1007 * of the audio signal.
1009 * Returns 0 for success else error. On error all resources can be freed
1010 * with a call to snd_soc_card_free().
1012 int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1013 const struct snd_soc_dapm_route *route, int num)
1017 for (i = 0; i < num; i++) {
1018 ret = snd_soc_dapm_add_route(codec, route->sink,
1019 route->control, route->source);
1021 printk(KERN_ERR "Failed to add route %s->%s\n",
1031 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1034 * snd_soc_dapm_new_widgets - add new dapm widgets
1035 * @codec: audio codec
1037 * Checks the codec for any new dapm widgets and creates them if found.
1039 * Returns 0 for success.
1041 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1043 struct snd_soc_dapm_widget *w;
1045 list_for_each_entry(w, &codec->dapm_widgets, list)
1051 case snd_soc_dapm_switch:
1052 case snd_soc_dapm_mixer:
1053 dapm_new_mixer(codec, w);
1055 case snd_soc_dapm_mux:
1056 dapm_new_mux(codec, w);
1058 case snd_soc_dapm_adc:
1059 case snd_soc_dapm_dac:
1060 case snd_soc_dapm_pga:
1061 dapm_new_pga(codec, w);
1063 case snd_soc_dapm_input:
1064 case snd_soc_dapm_output:
1065 case snd_soc_dapm_micbias:
1066 case snd_soc_dapm_spk:
1067 case snd_soc_dapm_hp:
1068 case snd_soc_dapm_mic:
1069 case snd_soc_dapm_line:
1070 case snd_soc_dapm_vmid:
1071 case snd_soc_dapm_pre:
1072 case snd_soc_dapm_post:
1078 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1081 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1084 * snd_soc_dapm_get_volsw - dapm mixer get callback
1085 * @kcontrol: mixer control
1086 * @uinfo: control element information
1088 * Callback to get the value of a dapm mixer control.
1090 * Returns 0 for success.
1092 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1093 struct snd_ctl_elem_value *ucontrol)
1095 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1096 struct soc_mixer_control *mc =
1097 (struct soc_mixer_control *)kcontrol->private_value;
1098 unsigned int reg = mc->reg;
1099 unsigned int shift = mc->shift;
1100 unsigned int rshift = mc->rshift;
1102 unsigned int invert = mc->invert;
1103 unsigned int mask = (1 << fls(max)) - 1;
1105 /* return the saved value if we are powered down */
1106 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1107 ucontrol->value.integer.value[0] = widget->saved_value;
1111 ucontrol->value.integer.value[0] =
1112 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1113 if (shift != rshift)
1114 ucontrol->value.integer.value[1] =
1115 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1117 ucontrol->value.integer.value[0] =
1118 max - ucontrol->value.integer.value[0];
1119 if (shift != rshift)
1120 ucontrol->value.integer.value[1] =
1121 max - ucontrol->value.integer.value[1];
1126 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1129 * snd_soc_dapm_put_volsw - dapm mixer set callback
1130 * @kcontrol: mixer control
1131 * @uinfo: control element information
1133 * Callback to set the value of a dapm mixer control.
1135 * Returns 0 for success.
1137 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1138 struct snd_ctl_elem_value *ucontrol)
1140 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1141 struct soc_mixer_control *mc =
1142 (struct soc_mixer_control *)kcontrol->private_value;
1143 unsigned int reg = mc->reg;
1144 unsigned int shift = mc->shift;
1145 unsigned int rshift = mc->rshift;
1147 unsigned int mask = (1 << fls(max)) - 1;
1148 unsigned int invert = mc->invert;
1149 unsigned short val, val2, val_mask;
1152 val = (ucontrol->value.integer.value[0] & mask);
1156 val_mask = mask << shift;
1158 if (shift != rshift) {
1159 val2 = (ucontrol->value.integer.value[1] & mask);
1162 val_mask |= mask << rshift;
1163 val |= val2 << rshift;
1166 mutex_lock(&widget->codec->mutex);
1167 widget->value = val;
1169 /* save volume value if the widget is powered down */
1170 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1171 widget->saved_value = val;
1172 mutex_unlock(&widget->codec->mutex);
1176 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1177 if (widget->event) {
1178 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1179 ret = widget->event(widget, kcontrol,
1180 SND_SOC_DAPM_PRE_REG);
1186 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1187 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1188 ret = widget->event(widget, kcontrol,
1189 SND_SOC_DAPM_POST_REG);
1191 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1194 mutex_unlock(&widget->codec->mutex);
1197 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1200 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1201 * @kcontrol: mixer control
1202 * @uinfo: control element information
1204 * Callback to get the value of a dapm enumerated double mixer control.
1206 * Returns 0 for success.
1208 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1209 struct snd_ctl_elem_value *ucontrol)
1211 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1212 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1213 unsigned short val, bitmask;
1215 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1217 val = snd_soc_read(widget->codec, e->reg);
1218 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1219 if (e->shift_l != e->shift_r)
1220 ucontrol->value.enumerated.item[1] =
1221 (val >> e->shift_r) & (bitmask - 1);
1225 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1228 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1229 * @kcontrol: mixer control
1230 * @uinfo: control element information
1232 * Callback to set the value of a dapm enumerated double mixer control.
1234 * Returns 0 for success.
1236 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1237 struct snd_ctl_elem_value *ucontrol)
1239 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1240 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1241 unsigned short val, mux;
1242 unsigned short mask, bitmask;
1245 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1247 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1249 mux = ucontrol->value.enumerated.item[0];
1250 val = mux << e->shift_l;
1251 mask = (bitmask - 1) << e->shift_l;
1252 if (e->shift_l != e->shift_r) {
1253 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1255 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1256 mask |= (bitmask - 1) << e->shift_r;
1259 mutex_lock(&widget->codec->mutex);
1260 widget->value = val;
1261 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1262 if (widget->event) {
1263 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1264 ret = widget->event(widget,
1265 kcontrol, SND_SOC_DAPM_PRE_REG);
1269 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1270 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1271 ret = widget->event(widget,
1272 kcontrol, SND_SOC_DAPM_POST_REG);
1274 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1277 mutex_unlock(&widget->codec->mutex);
1280 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1283 * snd_soc_dapm_new_control - create new dapm control
1284 * @codec: audio codec
1285 * @widget: widget template
1287 * Creates a new dapm control based upon the template.
1289 * Returns 0 for success else error.
1291 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1292 const struct snd_soc_dapm_widget *widget)
1294 struct snd_soc_dapm_widget *w;
1296 if ((w = dapm_cnew_widget(widget)) == NULL)
1300 INIT_LIST_HEAD(&w->sources);
1301 INIT_LIST_HEAD(&w->sinks);
1302 INIT_LIST_HEAD(&w->list);
1303 list_add(&w->list, &codec->dapm_widgets);
1305 /* machine layer set ups unconnected pins and insertions */
1309 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1312 * snd_soc_dapm_new_controls - create new dapm controls
1313 * @codec: audio codec
1314 * @widget: widget array
1315 * @num: number of widgets
1317 * Creates new DAPM controls based upon the templates.
1319 * Returns 0 for success else error.
1321 int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1322 const struct snd_soc_dapm_widget *widget,
1327 for (i = 0; i < num; i++) {
1328 ret = snd_soc_dapm_new_control(codec, widget);
1335 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1339 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1340 * @codec: audio codec
1341 * @stream: stream name
1342 * @event: stream event
1344 * Sends a stream event to the dapm core. The core then makes any
1345 * necessary widget power changes.
1347 * Returns 0 for success else error.
1349 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1350 char *stream, int event)
1352 struct snd_soc_dapm_widget *w;
1357 mutex_lock(&codec->mutex);
1358 list_for_each_entry(w, &codec->dapm_widgets, list)
1362 pr_debug("widget %s\n %s stream %s event %d\n",
1363 w->name, w->sname, stream, event);
1364 if (strstr(w->sname, stream)) {
1366 case SND_SOC_DAPM_STREAM_START:
1369 case SND_SOC_DAPM_STREAM_STOP:
1372 case SND_SOC_DAPM_STREAM_SUSPEND:
1377 case SND_SOC_DAPM_STREAM_RESUME:
1383 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1385 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1390 mutex_unlock(&codec->mutex);
1392 dapm_power_widgets(codec, event);
1393 dump_dapm(codec, __func__);
1396 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1399 * snd_soc_dapm_set_bias_level - set the bias level for the system
1400 * @socdev: audio device
1401 * @level: level to configure
1403 * Configure the bias (power) levels for the SoC audio device.
1405 * Returns 0 for success else error.
1407 int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1408 enum snd_soc_bias_level level)
1410 struct snd_soc_codec *codec = socdev->codec;
1411 struct snd_soc_machine *machine = socdev->machine;
1414 if (machine->set_bias_level)
1415 ret = machine->set_bias_level(machine, level);
1416 if (ret == 0 && codec->set_bias_level)
1417 ret = codec->set_bias_level(codec, level);
1423 * snd_soc_dapm_enable_pin - enable pin.
1424 * @snd_soc_codec: SoC codec
1427 * Enables input/output pin and it's parents or children widgets iff there is
1428 * a valid audio route and active audio stream.
1429 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1430 * do any widget power switching.
1432 int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, char *pin)
1434 return snd_soc_dapm_set_pin(codec, pin, 1);
1436 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
1439 * snd_soc_dapm_disable_pin - disable pin.
1443 * Disables input/output pin and it's parents or children widgets.
1444 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1445 * do any widget power switching.
1447 int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, char *pin)
1449 return snd_soc_dapm_set_pin(codec, pin, 0);
1451 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1454 * snd_soc_dapm_nc_pin - permanently disable pin.
1458 * Marks the specified pin as being not connected, disabling it along
1459 * any parent or child widgets. At present this is identical to
1460 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1461 * additional things such as disabling controls which only affect
1462 * paths through the pin.
1464 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1465 * do any widget power switching.
1467 int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, char *pin)
1469 return snd_soc_dapm_set_pin(codec, pin, 0);
1471 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1474 * snd_soc_dapm_get_pin_status - get audio pin status
1475 * @codec: audio codec
1476 * @pin: audio signal pin endpoint (or start point)
1478 * Get audio pin status - connected or disconnected.
1480 * Returns 1 for connected otherwise 0.
1482 int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, char *pin)
1484 struct snd_soc_dapm_widget *w;
1486 list_for_each_entry(w, &codec->dapm_widgets, list) {
1487 if (!strcmp(w->name, pin))
1488 return w->connected;
1493 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
1496 * snd_soc_dapm_free - free dapm resources
1497 * @socdev: SoC device
1499 * Free all dapm widgets and resources.
1501 void snd_soc_dapm_free(struct snd_soc_device *socdev)
1503 struct snd_soc_codec *codec = socdev->codec;
1505 snd_soc_dapm_sys_remove(socdev->dev);
1506 dapm_free_widgets(codec);
1508 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1510 /* Module information */
1511 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1512 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1513 MODULE_LICENSE("GPL");