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 snprintf(name, 32, "%s %s", w->name, w->kcontrols[i].name);
307 path->long_name = kstrdup (name, GFP_KERNEL);
308 if (path->long_name == NULL)
311 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
313 ret = snd_ctl_add(codec->card, path->kcontrol);
315 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
317 kfree(path->long_name);
318 path->long_name = NULL;
326 /* create new dapm mux control */
327 static int dapm_new_mux(struct snd_soc_codec *codec,
328 struct snd_soc_dapm_widget *w)
330 struct snd_soc_dapm_path *path = NULL;
331 struct snd_kcontrol *kcontrol;
334 if (!w->num_kcontrols) {
335 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
339 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
340 ret = snd_ctl_add(codec->card, kcontrol);
344 list_for_each_entry(path, &w->sources, list_sink)
345 path->kcontrol = kcontrol;
350 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
354 /* create new dapm volume control */
355 static int dapm_new_pga(struct snd_soc_codec *codec,
356 struct snd_soc_dapm_widget *w)
358 struct snd_kcontrol *kcontrol;
361 if (!w->num_kcontrols)
364 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
365 ret = snd_ctl_add(codec->card, kcontrol);
367 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
374 /* reset 'walked' bit for each dapm path */
375 static inline void dapm_clear_walk(struct snd_soc_codec *codec)
377 struct snd_soc_dapm_path *p;
379 list_for_each_entry(p, &codec->dapm_paths, list)
384 * Recursively check for a completed path to an active or physically connected
385 * output widget. Returns number of complete paths.
387 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
389 struct snd_soc_dapm_path *path;
392 if (widget->id == snd_soc_dapm_adc && widget->active)
395 if (widget->connected) {
396 /* connected pin ? */
397 if (widget->id == snd_soc_dapm_output && !widget->ext)
400 /* connected jack or spk ? */
401 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
402 widget->id == snd_soc_dapm_line)
406 list_for_each_entry(path, &widget->sinks, list_source) {
410 if (path->sink && path->connect) {
412 con += is_connected_output_ep(path->sink);
420 * Recursively check for a completed path to an active or physically connected
421 * input widget. Returns number of complete paths.
423 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
425 struct snd_soc_dapm_path *path;
428 /* active stream ? */
429 if (widget->id == snd_soc_dapm_dac && widget->active)
432 if (widget->connected) {
433 /* connected pin ? */
434 if (widget->id == snd_soc_dapm_input && !widget->ext)
437 /* connected VMID/Bias for lower pops */
438 if (widget->id == snd_soc_dapm_vmid)
441 /* connected jack ? */
442 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
446 list_for_each_entry(path, &widget->sources, list_sink) {
450 if (path->source && path->connect) {
452 con += is_connected_input_ep(path->source);
460 * Handler for generic register modifier widget.
462 int dapm_reg_event(struct snd_soc_dapm_widget *w,
463 struct snd_kcontrol *kcontrol, int event)
467 if (SND_SOC_DAPM_EVENT_ON(event))
472 snd_soc_update_bits(w->codec, -(w->reg + 1),
473 w->mask << w->shift, val << w->shift);
477 EXPORT_SYMBOL_GPL(dapm_reg_event);
480 * Scan each dapm widget for complete audio path.
481 * A complete path is a route that has valid endpoints i.e.:-
483 * o DAC to output pin.
484 * o Input Pin to ADC.
485 * o Input pin to Output pin (bypass, sidetone)
486 * o DAC to ADC (loopback).
488 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
490 struct snd_soc_dapm_widget *w;
491 int in, out, i, c = 1, *seq = NULL, ret = 0, power_change, power;
493 /* do we have a sequenced stream event */
494 if (event == SND_SOC_DAPM_STREAM_START) {
495 c = ARRAY_SIZE(dapm_up_seq);
497 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
498 c = ARRAY_SIZE(dapm_down_seq);
502 for(i = 0; i < c; i++) {
503 list_for_each_entry(w, &codec->dapm_widgets, list) {
505 /* is widget in stream order */
506 if (seq && seq[i] && w->id != seq[i])
509 /* vmid - no action */
510 if (w->id == snd_soc_dapm_vmid)
514 if (w->id == snd_soc_dapm_adc && w->active) {
515 in = is_connected_input_ep(w);
516 dapm_clear_walk(w->codec);
517 w->power = (in != 0) ? 1 : 0;
523 if (w->id == snd_soc_dapm_dac && w->active) {
524 out = is_connected_output_ep(w);
525 dapm_clear_walk(w->codec);
526 w->power = (out != 0) ? 1 : 0;
531 /* pre and post event widgets */
532 if (w->id == snd_soc_dapm_pre) {
536 if (event == SND_SOC_DAPM_STREAM_START) {
538 NULL, SND_SOC_DAPM_PRE_PMU);
541 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
543 NULL, SND_SOC_DAPM_PRE_PMD);
549 if (w->id == snd_soc_dapm_post) {
553 if (event == SND_SOC_DAPM_STREAM_START) {
555 NULL, SND_SOC_DAPM_POST_PMU);
558 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
560 NULL, SND_SOC_DAPM_POST_PMD);
567 /* all other widgets */
568 in = is_connected_input_ep(w);
569 dapm_clear_walk(w->codec);
570 out = is_connected_output_ep(w);
571 dapm_clear_walk(w->codec);
572 power = (out != 0 && in != 0) ? 1 : 0;
573 power_change = (w->power == power) ? 0: 1;
579 /* call any power change event handlers */
581 pr_debug("power %s event for %s flags %x\n",
582 w->power ? "on" : "off",
583 w->name, w->event_flags);
585 /* power up pre event */
586 if (power && w->event &&
587 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
588 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
593 /* power down pre event */
594 if (!power && w->event &&
595 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
596 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
601 /* Lower PGA volume to reduce pops */
602 if (w->id == snd_soc_dapm_pga && !power)
603 dapm_set_pga(w, power);
607 /* Raise PGA volume to reduce pops */
608 if (w->id == snd_soc_dapm_pga && power)
609 dapm_set_pga(w, power);
611 /* power up post event */
612 if (power && w->event &&
613 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
615 NULL, SND_SOC_DAPM_POST_PMU);
620 /* power down post event */
621 if (!power && w->event &&
622 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
623 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
634 static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
636 struct snd_soc_dapm_widget *w;
637 struct snd_soc_dapm_path *p = NULL;
640 printk("DAPM %s %s\n", codec->name, action);
642 list_for_each_entry(w, &codec->dapm_widgets, list) {
644 /* only display widgets that effect routing */
646 case snd_soc_dapm_pre:
647 case snd_soc_dapm_post:
648 case snd_soc_dapm_vmid:
650 case snd_soc_dapm_mux:
651 case snd_soc_dapm_output:
652 case snd_soc_dapm_input:
653 case snd_soc_dapm_switch:
654 case snd_soc_dapm_hp:
655 case snd_soc_dapm_mic:
656 case snd_soc_dapm_spk:
657 case snd_soc_dapm_line:
658 case snd_soc_dapm_micbias:
659 case snd_soc_dapm_dac:
660 case snd_soc_dapm_adc:
661 case snd_soc_dapm_pga:
662 case snd_soc_dapm_mixer:
664 in = is_connected_input_ep(w);
665 dapm_clear_walk(w->codec);
666 out = is_connected_output_ep(w);
667 dapm_clear_walk(w->codec);
668 printk("%s: %s in %d out %d\n", w->name,
669 w->power ? "On":"Off",in, out);
671 list_for_each_entry(p, &w->sources, list_sink) {
673 printk(" in %s %s\n", p->name ? p->name : "static",
676 list_for_each_entry(p, &w->sinks, list_source) {
678 printk(" out %s %s\n", p->name ? p->name : "static",
688 /* test and update the power status of a mux widget */
689 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
690 struct snd_kcontrol *kcontrol, int mask,
691 int mux, int val, struct soc_enum *e)
693 struct snd_soc_dapm_path *path;
696 if (widget->id != snd_soc_dapm_mux)
699 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
702 /* find dapm widget path assoc with kcontrol */
703 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
704 if (path->kcontrol != kcontrol)
707 if (!path->name || !e->texts[mux])
711 /* we now need to match the string in the enum to the path */
712 if (!(strcmp(path->name, e->texts[mux])))
713 path->connect = 1; /* new connection */
715 path->connect = 0; /* old connection must be powered down */
719 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
720 dump_dapm(widget->codec, "mux power update");
726 /* test and update the power status of a mixer or switch widget */
727 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
728 struct snd_kcontrol *kcontrol, int reg,
729 int val_mask, int val, int invert)
731 struct snd_soc_dapm_path *path;
734 if (widget->id != snd_soc_dapm_mixer &&
735 widget->id != snd_soc_dapm_switch)
738 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
741 /* find dapm widget path assoc with kcontrol */
742 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
743 if (path->kcontrol != kcontrol)
746 /* found, now check type */
750 path->connect = invert ? 0:1;
752 /* old connection must be powered down */
753 path->connect = invert ? 1:0;
758 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
759 dump_dapm(widget->codec, "mixer power update");
765 /* show dapm widget status in sys fs */
766 static ssize_t dapm_widget_show(struct device *dev,
767 struct device_attribute *attr, char *buf)
769 struct snd_soc_device *devdata = dev_get_drvdata(dev);
770 struct snd_soc_codec *codec = devdata->codec;
771 struct snd_soc_dapm_widget *w;
773 char *state = "not set";
775 list_for_each_entry(w, &codec->dapm_widgets, list) {
777 /* only display widgets that burnm power */
779 case snd_soc_dapm_hp:
780 case snd_soc_dapm_mic:
781 case snd_soc_dapm_spk:
782 case snd_soc_dapm_line:
783 case snd_soc_dapm_micbias:
784 case snd_soc_dapm_dac:
785 case snd_soc_dapm_adc:
786 case snd_soc_dapm_pga:
787 case snd_soc_dapm_mixer:
789 count += sprintf(buf + count, "%s: %s\n",
790 w->name, w->power ? "On":"Off");
797 switch (codec->bias_level) {
798 case SND_SOC_BIAS_ON:
801 case SND_SOC_BIAS_PREPARE:
804 case SND_SOC_BIAS_STANDBY:
807 case SND_SOC_BIAS_OFF:
811 count += sprintf(buf + count, "PM State: %s\n", state);
816 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
818 int snd_soc_dapm_sys_add(struct device *dev)
823 ret = device_create_file(dev, &dev_attr_dapm_widget);
826 return device_create_file(dev, &dev_attr_dapm_widget);
829 static void snd_soc_dapm_sys_remove(struct device *dev)
832 device_remove_file(dev, &dev_attr_dapm_widget);
836 /* free all dapm widgets and resources */
837 static void dapm_free_widgets(struct snd_soc_codec *codec)
839 struct snd_soc_dapm_widget *w, *next_w;
840 struct snd_soc_dapm_path *p, *next_p;
842 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
847 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
854 static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
855 char *pin, int status)
857 struct snd_soc_dapm_widget *w;
859 list_for_each_entry(w, &codec->dapm_widgets, list) {
860 if (!strcmp(w->name, pin)) {
861 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
862 w->connected = status;
867 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
872 * snd_soc_dapm_sync - scan and power dapm paths
873 * @codec: audio codec
875 * Walks all dapm audio paths and powers widgets according to their
876 * stream or path usage.
878 * Returns 0 for success.
880 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
882 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
883 dump_dapm(codec, "sync");
886 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
888 static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
889 const char *sink, const char *control, const char *source)
891 struct snd_soc_dapm_path *path;
892 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
895 /* find src and dest widgets */
896 list_for_each_entry(w, &codec->dapm_widgets, list) {
898 if (!wsink && !(strcmp(w->name, sink))) {
902 if (!wsource && !(strcmp(w->name, source))) {
907 if (wsource == NULL || wsink == NULL)
910 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
914 path->source = wsource;
916 INIT_LIST_HEAD(&path->list);
917 INIT_LIST_HEAD(&path->list_source);
918 INIT_LIST_HEAD(&path->list_sink);
920 /* check for external widgets */
921 if (wsink->id == snd_soc_dapm_input) {
922 if (wsource->id == snd_soc_dapm_micbias ||
923 wsource->id == snd_soc_dapm_mic ||
924 wsink->id == snd_soc_dapm_line ||
925 wsink->id == snd_soc_dapm_output)
928 if (wsource->id == snd_soc_dapm_output) {
929 if (wsink->id == snd_soc_dapm_spk ||
930 wsink->id == snd_soc_dapm_hp ||
931 wsink->id == snd_soc_dapm_line ||
932 wsink->id == snd_soc_dapm_input)
936 /* connect static paths */
937 if (control == NULL) {
938 list_add(&path->list, &codec->dapm_paths);
939 list_add(&path->list_sink, &wsink->sources);
940 list_add(&path->list_source, &wsource->sinks);
945 /* connect dynamic paths */
947 case snd_soc_dapm_adc:
948 case snd_soc_dapm_dac:
949 case snd_soc_dapm_pga:
950 case snd_soc_dapm_input:
951 case snd_soc_dapm_output:
952 case snd_soc_dapm_micbias:
953 case snd_soc_dapm_vmid:
954 case snd_soc_dapm_pre:
955 case snd_soc_dapm_post:
956 list_add(&path->list, &codec->dapm_paths);
957 list_add(&path->list_sink, &wsink->sources);
958 list_add(&path->list_source, &wsource->sinks);
961 case snd_soc_dapm_mux:
962 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
963 &wsink->kcontrols[0]);
967 case snd_soc_dapm_switch:
968 case snd_soc_dapm_mixer:
969 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
973 case snd_soc_dapm_hp:
974 case snd_soc_dapm_mic:
975 case snd_soc_dapm_line:
976 case snd_soc_dapm_spk:
977 list_add(&path->list, &codec->dapm_paths);
978 list_add(&path->list_sink, &wsink->sources);
979 list_add(&path->list_source, &wsource->sinks);
986 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
993 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
995 * @route: audio routes
996 * @num: number of routes
998 * Connects 2 dapm widgets together via a named audio path. The sink is
999 * the widget receiving the audio signal, whilst the source is the sender
1000 * of the audio signal.
1002 * Returns 0 for success else error. On error all resources can be freed
1003 * with a call to snd_soc_card_free().
1005 int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1006 const struct snd_soc_dapm_route *route, int num)
1010 for (i = 0; i < num; i++) {
1011 ret = snd_soc_dapm_add_route(codec, route->sink,
1012 route->control, route->source);
1014 printk(KERN_ERR "Failed to add route %s->%s\n",
1024 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1027 * snd_soc_dapm_new_widgets - add new dapm widgets
1028 * @codec: audio codec
1030 * Checks the codec for any new dapm widgets and creates them if found.
1032 * Returns 0 for success.
1034 int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1036 struct snd_soc_dapm_widget *w;
1038 list_for_each_entry(w, &codec->dapm_widgets, list)
1044 case snd_soc_dapm_switch:
1045 case snd_soc_dapm_mixer:
1046 dapm_new_mixer(codec, w);
1048 case snd_soc_dapm_mux:
1049 dapm_new_mux(codec, w);
1051 case snd_soc_dapm_adc:
1052 case snd_soc_dapm_dac:
1053 case snd_soc_dapm_pga:
1054 dapm_new_pga(codec, w);
1056 case snd_soc_dapm_input:
1057 case snd_soc_dapm_output:
1058 case snd_soc_dapm_micbias:
1059 case snd_soc_dapm_spk:
1060 case snd_soc_dapm_hp:
1061 case snd_soc_dapm_mic:
1062 case snd_soc_dapm_line:
1063 case snd_soc_dapm_vmid:
1064 case snd_soc_dapm_pre:
1065 case snd_soc_dapm_post:
1071 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1074 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1077 * snd_soc_dapm_get_volsw - dapm mixer get callback
1078 * @kcontrol: mixer control
1079 * @uinfo: control element information
1081 * Callback to get the value of a dapm mixer control.
1083 * Returns 0 for success.
1085 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1086 struct snd_ctl_elem_value *ucontrol)
1088 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1089 struct soc_mixer_control *mc =
1090 (struct soc_mixer_control *)kcontrol->private_value;
1091 unsigned int reg = mc->reg;
1092 unsigned int shift = mc->shift;
1093 unsigned int rshift = mc->rshift;
1095 unsigned int invert = mc->invert;
1096 unsigned int mask = (1 << fls(max)) - 1;
1098 /* return the saved value if we are powered down */
1099 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1100 ucontrol->value.integer.value[0] = widget->saved_value;
1104 ucontrol->value.integer.value[0] =
1105 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1106 if (shift != rshift)
1107 ucontrol->value.integer.value[1] =
1108 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1110 ucontrol->value.integer.value[0] =
1111 max - ucontrol->value.integer.value[0];
1112 if (shift != rshift)
1113 ucontrol->value.integer.value[1] =
1114 max - ucontrol->value.integer.value[1];
1119 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1122 * snd_soc_dapm_put_volsw - dapm mixer set callback
1123 * @kcontrol: mixer control
1124 * @uinfo: control element information
1126 * Callback to set the value of a dapm mixer control.
1128 * Returns 0 for success.
1130 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1131 struct snd_ctl_elem_value *ucontrol)
1133 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1134 struct soc_mixer_control *mc =
1135 (struct soc_mixer_control *)kcontrol->private_value;
1136 unsigned int reg = mc->reg;
1137 unsigned int shift = mc->shift;
1138 unsigned int rshift = mc->rshift;
1140 unsigned int mask = (1 << fls(max)) - 1;
1141 unsigned int invert = mc->invert;
1142 unsigned short val, val2, val_mask;
1145 val = (ucontrol->value.integer.value[0] & mask);
1149 val_mask = mask << shift;
1151 if (shift != rshift) {
1152 val2 = (ucontrol->value.integer.value[1] & mask);
1155 val_mask |= mask << rshift;
1156 val |= val2 << rshift;
1159 mutex_lock(&widget->codec->mutex);
1160 widget->value = val;
1162 /* save volume value if the widget is powered down */
1163 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1164 widget->saved_value = val;
1165 mutex_unlock(&widget->codec->mutex);
1169 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1170 if (widget->event) {
1171 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1172 ret = widget->event(widget, kcontrol,
1173 SND_SOC_DAPM_PRE_REG);
1179 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1180 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1181 ret = widget->event(widget, kcontrol,
1182 SND_SOC_DAPM_POST_REG);
1184 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1187 mutex_unlock(&widget->codec->mutex);
1190 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1193 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1194 * @kcontrol: mixer control
1195 * @uinfo: control element information
1197 * Callback to get the value of a dapm enumerated double mixer control.
1199 * Returns 0 for success.
1201 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1202 struct snd_ctl_elem_value *ucontrol)
1204 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1205 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1206 unsigned short val, bitmask;
1208 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1210 val = snd_soc_read(widget->codec, e->reg);
1211 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1212 if (e->shift_l != e->shift_r)
1213 ucontrol->value.enumerated.item[1] =
1214 (val >> e->shift_r) & (bitmask - 1);
1218 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1221 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1222 * @kcontrol: mixer control
1223 * @uinfo: control element information
1225 * Callback to set the value of a dapm enumerated double mixer control.
1227 * Returns 0 for success.
1229 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1230 struct snd_ctl_elem_value *ucontrol)
1232 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1233 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1234 unsigned short val, mux;
1235 unsigned short mask, bitmask;
1238 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1240 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1242 mux = ucontrol->value.enumerated.item[0];
1243 val = mux << e->shift_l;
1244 mask = (bitmask - 1) << e->shift_l;
1245 if (e->shift_l != e->shift_r) {
1246 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1248 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1249 mask |= (bitmask - 1) << e->shift_r;
1252 mutex_lock(&widget->codec->mutex);
1253 widget->value = val;
1254 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
1255 if (widget->event) {
1256 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1257 ret = widget->event(widget,
1258 kcontrol, SND_SOC_DAPM_PRE_REG);
1262 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1263 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1264 ret = widget->event(widget,
1265 kcontrol, SND_SOC_DAPM_POST_REG);
1267 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1270 mutex_unlock(&widget->codec->mutex);
1273 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1276 * snd_soc_dapm_new_control - create new dapm control
1277 * @codec: audio codec
1278 * @widget: widget template
1280 * Creates a new dapm control based upon the template.
1282 * Returns 0 for success else error.
1284 int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1285 const struct snd_soc_dapm_widget *widget)
1287 struct snd_soc_dapm_widget *w;
1289 if ((w = dapm_cnew_widget(widget)) == NULL)
1293 INIT_LIST_HEAD(&w->sources);
1294 INIT_LIST_HEAD(&w->sinks);
1295 INIT_LIST_HEAD(&w->list);
1296 list_add(&w->list, &codec->dapm_widgets);
1298 /* machine layer set ups unconnected pins and insertions */
1302 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1305 * snd_soc_dapm_new_controls - create new dapm controls
1306 * @codec: audio codec
1307 * @widget: widget array
1308 * @num: number of widgets
1310 * Creates new DAPM controls based upon the templates.
1312 * Returns 0 for success else error.
1314 int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1315 const struct snd_soc_dapm_widget *widget,
1320 for (i = 0; i < num; i++) {
1321 ret = snd_soc_dapm_new_control(codec, widget);
1328 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1332 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1333 * @codec: audio codec
1334 * @stream: stream name
1335 * @event: stream event
1337 * Sends a stream event to the dapm core. The core then makes any
1338 * necessary widget power changes.
1340 * Returns 0 for success else error.
1342 int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1343 char *stream, int event)
1345 struct snd_soc_dapm_widget *w;
1350 mutex_lock(&codec->mutex);
1351 list_for_each_entry(w, &codec->dapm_widgets, list)
1355 pr_debug("widget %s\n %s stream %s event %d\n",
1356 w->name, w->sname, stream, event);
1357 if (strstr(w->sname, stream)) {
1359 case SND_SOC_DAPM_STREAM_START:
1362 case SND_SOC_DAPM_STREAM_STOP:
1365 case SND_SOC_DAPM_STREAM_SUSPEND:
1370 case SND_SOC_DAPM_STREAM_RESUME:
1376 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1378 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1383 mutex_unlock(&codec->mutex);
1385 dapm_power_widgets(codec, event);
1386 dump_dapm(codec, __func__);
1389 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1392 * snd_soc_dapm_set_bias_level - set the bias level for the system
1393 * @socdev: audio device
1394 * @level: level to configure
1396 * Configure the bias (power) levels for the SoC audio device.
1398 * Returns 0 for success else error.
1400 int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1401 enum snd_soc_bias_level level)
1403 struct snd_soc_codec *codec = socdev->codec;
1404 struct snd_soc_machine *machine = socdev->machine;
1407 if (machine->set_bias_level)
1408 ret = machine->set_bias_level(machine, level);
1409 if (ret == 0 && codec->set_bias_level)
1410 ret = codec->set_bias_level(codec, level);
1416 * snd_soc_dapm_enable_pin - enable pin.
1417 * @snd_soc_codec: SoC codec
1420 * Enables input/output pin and it's parents or children widgets iff there is
1421 * a valid audio route and active audio stream.
1422 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1423 * do any widget power switching.
1425 int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, char *pin)
1427 return snd_soc_dapm_set_pin(codec, pin, 1);
1429 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
1432 * snd_soc_dapm_disable_pin - disable pin.
1436 * Disables input/output pin and it's parents or children widgets.
1437 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1438 * do any widget power switching.
1440 int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, char *pin)
1442 return snd_soc_dapm_set_pin(codec, pin, 0);
1444 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1447 * snd_soc_dapm_nc_pin - permanently disable pin.
1451 * Marks the specified pin as being not connected, disabling it along
1452 * any parent or child widgets. At present this is identical to
1453 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1454 * additional things such as disabling controls which only affect
1455 * paths through the pin.
1457 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1458 * do any widget power switching.
1460 int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, char *pin)
1462 return snd_soc_dapm_set_pin(codec, pin, 0);
1464 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1467 * snd_soc_dapm_get_pin_status - get audio pin status
1468 * @codec: audio codec
1469 * @pin: audio signal pin endpoint (or start point)
1471 * Get audio pin status - connected or disconnected.
1473 * Returns 1 for connected otherwise 0.
1475 int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, char *pin)
1477 struct snd_soc_dapm_widget *w;
1479 list_for_each_entry(w, &codec->dapm_widgets, list) {
1480 if (!strcmp(w->name, pin))
1481 return w->connected;
1486 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
1489 * snd_soc_dapm_free - free dapm resources
1490 * @socdev: SoC device
1492 * Free all dapm widgets and resources.
1494 void snd_soc_dapm_free(struct snd_soc_device *socdev)
1496 struct snd_soc_codec *codec = socdev->codec;
1498 snd_soc_dapm_sys_remove(socdev->dev);
1499 dapm_free_widgets(codec);
1501 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1503 /* Module information */
1504 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1505 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1506 MODULE_LICENSE("GPL");