2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
25 * o DAPM power change sequencing - allow for configurable per
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
37 #include <linux/bitops.h>
38 #include <linux/platform_device.h>
39 #include <linux/jiffies.h>
40 #include <linux/debugfs.h>
41 #include <sound/core.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc-dapm.h>
45 #include <sound/initval.h>
49 #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
51 #define dump_dapm(codec, action)
54 /* dapm power sequences - make this per codec in the future */
55 static int dapm_up_seq
[] = {
56 [snd_soc_dapm_pre
] = 0,
57 [snd_soc_dapm_supply
] = 1,
58 [snd_soc_dapm_micbias
] = 2,
59 [snd_soc_dapm_aif_in
] = 3,
60 [snd_soc_dapm_aif_out
] = 3,
61 [snd_soc_dapm_mic
] = 4,
62 [snd_soc_dapm_mux
] = 5,
63 [snd_soc_dapm_value_mux
] = 5,
64 [snd_soc_dapm_dac
] = 6,
65 [snd_soc_dapm_mixer
] = 7,
66 [snd_soc_dapm_mixer_named_ctl
] = 7,
67 [snd_soc_dapm_pga
] = 8,
68 [snd_soc_dapm_adc
] = 9,
69 [snd_soc_dapm_hp
] = 10,
70 [snd_soc_dapm_spk
] = 10,
71 [snd_soc_dapm_post
] = 11,
74 static int dapm_down_seq
[] = {
75 [snd_soc_dapm_pre
] = 0,
76 [snd_soc_dapm_adc
] = 1,
77 [snd_soc_dapm_hp
] = 2,
78 [snd_soc_dapm_spk
] = 2,
79 [snd_soc_dapm_pga
] = 4,
80 [snd_soc_dapm_mixer_named_ctl
] = 5,
81 [snd_soc_dapm_mixer
] = 5,
82 [snd_soc_dapm_dac
] = 6,
83 [snd_soc_dapm_mic
] = 7,
84 [snd_soc_dapm_micbias
] = 8,
85 [snd_soc_dapm_mux
] = 9,
86 [snd_soc_dapm_value_mux
] = 9,
87 [snd_soc_dapm_aif_in
] = 10,
88 [snd_soc_dapm_aif_out
] = 10,
89 [snd_soc_dapm_supply
] = 11,
90 [snd_soc_dapm_post
] = 12,
93 static void pop_wait(u32 pop_time
)
96 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
99 static void pop_dbg(u32 pop_time
, const char *fmt
, ...)
113 /* create a new dapm widget */
114 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
115 const struct snd_soc_dapm_widget
*_widget
)
117 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
121 * snd_soc_dapm_set_bias_level - set the bias level for the system
122 * @socdev: audio device
123 * @level: level to configure
125 * Configure the bias (power) levels for the SoC audio device.
127 * Returns 0 for success else error.
129 static int snd_soc_dapm_set_bias_level(struct snd_soc_device
*socdev
,
130 enum snd_soc_bias_level level
)
132 struct snd_soc_card
*card
= socdev
->card
;
133 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
137 case SND_SOC_BIAS_ON
:
138 dev_dbg(socdev
->dev
, "Setting full bias\n");
140 case SND_SOC_BIAS_PREPARE
:
141 dev_dbg(socdev
->dev
, "Setting bias prepare\n");
143 case SND_SOC_BIAS_STANDBY
:
144 dev_dbg(socdev
->dev
, "Setting standby bias\n");
146 case SND_SOC_BIAS_OFF
:
147 dev_dbg(socdev
->dev
, "Setting bias off\n");
150 dev_err(socdev
->dev
, "Setting invalid bias %d\n", level
);
154 if (card
->set_bias_level
)
155 ret
= card
->set_bias_level(card
, level
);
157 if (codec
->set_bias_level
)
158 ret
= codec
->set_bias_level(codec
, level
);
160 codec
->bias_level
= level
;
166 /* set up initial codec paths */
167 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
168 struct snd_soc_dapm_path
*p
, int i
)
171 case snd_soc_dapm_switch
:
172 case snd_soc_dapm_mixer
:
173 case snd_soc_dapm_mixer_named_ctl
: {
175 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
176 w
->kcontrols
[i
].private_value
;
177 unsigned int reg
= mc
->reg
;
178 unsigned int shift
= mc
->shift
;
180 unsigned int mask
= (1 << fls(max
)) - 1;
181 unsigned int invert
= mc
->invert
;
183 val
= snd_soc_read(w
->codec
, reg
);
184 val
= (val
>> shift
) & mask
;
186 if ((invert
&& !val
) || (!invert
&& val
))
192 case snd_soc_dapm_mux
: {
193 struct soc_enum
*e
= (struct soc_enum
*)w
->kcontrols
[i
].private_value
;
194 int val
, item
, bitmask
;
196 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
198 val
= snd_soc_read(w
->codec
, e
->reg
);
199 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
202 for (i
= 0; i
< e
->max
; i
++) {
203 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
208 case snd_soc_dapm_value_mux
: {
209 struct soc_enum
*e
= (struct soc_enum
*)
210 w
->kcontrols
[i
].private_value
;
213 val
= snd_soc_read(w
->codec
, e
->reg
);
214 val
= (val
>> e
->shift_l
) & e
->mask
;
215 for (item
= 0; item
< e
->max
; item
++) {
216 if (val
== e
->values
[item
])
221 for (i
= 0; i
< e
->max
; i
++) {
222 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
227 /* does not effect routing - always connected */
228 case snd_soc_dapm_pga
:
229 case snd_soc_dapm_output
:
230 case snd_soc_dapm_adc
:
231 case snd_soc_dapm_input
:
232 case snd_soc_dapm_dac
:
233 case snd_soc_dapm_micbias
:
234 case snd_soc_dapm_vmid
:
235 case snd_soc_dapm_supply
:
236 case snd_soc_dapm_aif_in
:
237 case snd_soc_dapm_aif_out
:
240 /* does effect routing - dynamically connected */
241 case snd_soc_dapm_hp
:
242 case snd_soc_dapm_mic
:
243 case snd_soc_dapm_spk
:
244 case snd_soc_dapm_line
:
245 case snd_soc_dapm_pre
:
246 case snd_soc_dapm_post
:
252 /* connect mux widget to its interconnecting audio paths */
253 static int dapm_connect_mux(struct snd_soc_codec
*codec
,
254 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
255 struct snd_soc_dapm_path
*path
, const char *control_name
,
256 const struct snd_kcontrol_new
*kcontrol
)
258 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
261 for (i
= 0; i
< e
->max
; i
++) {
262 if (!(strcmp(control_name
, e
->texts
[i
]))) {
263 list_add(&path
->list
, &codec
->dapm_paths
);
264 list_add(&path
->list_sink
, &dest
->sources
);
265 list_add(&path
->list_source
, &src
->sinks
);
266 path
->name
= (char*)e
->texts
[i
];
267 dapm_set_path_status(dest
, path
, 0);
275 /* connect mixer widget to its interconnecting audio paths */
276 static int dapm_connect_mixer(struct snd_soc_codec
*codec
,
277 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
278 struct snd_soc_dapm_path
*path
, const char *control_name
)
282 /* search for mixer kcontrol */
283 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
284 if (!strcmp(control_name
, dest
->kcontrols
[i
].name
)) {
285 list_add(&path
->list
, &codec
->dapm_paths
);
286 list_add(&path
->list_sink
, &dest
->sources
);
287 list_add(&path
->list_source
, &src
->sinks
);
288 path
->name
= dest
->kcontrols
[i
].name
;
289 dapm_set_path_status(dest
, path
, i
);
296 /* update dapm codec register bits */
297 static int dapm_update_bits(struct snd_soc_dapm_widget
*widget
)
300 unsigned int old
, new;
301 struct snd_soc_codec
*codec
= widget
->codec
;
303 /* check for valid widgets */
304 if (widget
->reg
< 0 || widget
->id
== snd_soc_dapm_input
||
305 widget
->id
== snd_soc_dapm_output
||
306 widget
->id
== snd_soc_dapm_hp
||
307 widget
->id
== snd_soc_dapm_mic
||
308 widget
->id
== snd_soc_dapm_line
||
309 widget
->id
== snd_soc_dapm_spk
)
312 power
= widget
->power
;
314 power
= (power
? 0:1);
316 old
= snd_soc_read(codec
, widget
->reg
);
317 new = (old
& ~(0x1 << widget
->shift
)) | (power
<< widget
->shift
);
321 pop_dbg(codec
->pop_time
, "pop test %s : %s in %d ms\n",
322 widget
->name
, widget
->power
? "on" : "off",
324 snd_soc_write(codec
, widget
->reg
, new);
325 pop_wait(codec
->pop_time
);
327 pr_debug("reg %x old %x new %x change %d\n", widget
->reg
,
332 /* ramps the volume up or down to minimise pops before or after a
333 * DAPM power event */
334 static int dapm_set_pga(struct snd_soc_dapm_widget
*widget
, int power
)
336 const struct snd_kcontrol_new
*k
= widget
->kcontrols
;
338 if (widget
->muted
&& !power
)
340 if (!widget
->muted
&& power
)
343 if (widget
->num_kcontrols
&& k
) {
344 struct soc_mixer_control
*mc
=
345 (struct soc_mixer_control
*)k
->private_value
;
346 unsigned int reg
= mc
->reg
;
347 unsigned int shift
= mc
->shift
;
349 unsigned int mask
= (1 << fls(max
)) - 1;
350 unsigned int invert
= mc
->invert
;
354 /* power up has happended, increase volume to last level */
356 for (i
= max
; i
> widget
->saved_value
; i
--)
357 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
359 for (i
= 0; i
< widget
->saved_value
; i
++)
360 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
364 /* power down is about to occur, decrease volume to mute */
365 int val
= snd_soc_read(widget
->codec
, reg
);
366 int i
= widget
->saved_value
= (val
>> shift
) & mask
;
368 for (; i
< mask
; i
++)
369 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
372 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
380 /* create new dapm mixer control */
381 static int dapm_new_mixer(struct snd_soc_codec
*codec
,
382 struct snd_soc_dapm_widget
*w
)
386 struct snd_soc_dapm_path
*path
;
389 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
392 list_for_each_entry(path
, &w
->sources
, list_sink
) {
394 /* mixer/mux paths name must match control name */
395 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
398 /* add dapm control with long name.
399 * for dapm_mixer this is the concatenation of the
400 * mixer and kcontrol name.
401 * for dapm_mixer_named_ctl this is simply the
404 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
405 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
406 name_len
+= 1 + strlen(w
->name
);
408 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
410 if (path
->long_name
== NULL
)
415 snprintf(path
->long_name
, name_len
, "%s %s",
416 w
->name
, w
->kcontrols
[i
].name
);
418 case snd_soc_dapm_mixer_named_ctl
:
419 snprintf(path
->long_name
, name_len
, "%s",
420 w
->kcontrols
[i
].name
);
424 path
->long_name
[name_len
- 1] = '\0';
426 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
428 ret
= snd_ctl_add(codec
->card
, path
->kcontrol
);
430 printk(KERN_ERR
"asoc: failed to add dapm kcontrol %s: %d\n",
433 kfree(path
->long_name
);
434 path
->long_name
= NULL
;
442 /* create new dapm mux control */
443 static int dapm_new_mux(struct snd_soc_codec
*codec
,
444 struct snd_soc_dapm_widget
*w
)
446 struct snd_soc_dapm_path
*path
= NULL
;
447 struct snd_kcontrol
*kcontrol
;
450 if (!w
->num_kcontrols
) {
451 printk(KERN_ERR
"asoc: mux %s has no controls\n", w
->name
);
455 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
456 ret
= snd_ctl_add(codec
->card
, kcontrol
);
460 list_for_each_entry(path
, &w
->sources
, list_sink
)
461 path
->kcontrol
= kcontrol
;
466 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
470 /* create new dapm volume control */
471 static int dapm_new_pga(struct snd_soc_codec
*codec
,
472 struct snd_soc_dapm_widget
*w
)
474 struct snd_kcontrol
*kcontrol
;
477 if (!w
->num_kcontrols
)
480 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
481 ret
= snd_ctl_add(codec
->card
, kcontrol
);
483 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
490 /* reset 'walked' bit for each dapm path */
491 static inline void dapm_clear_walk(struct snd_soc_codec
*codec
)
493 struct snd_soc_dapm_path
*p
;
495 list_for_each_entry(p
, &codec
->dapm_paths
, list
)
500 * Recursively check for a completed path to an active or physically connected
501 * output widget. Returns number of complete paths.
503 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
505 struct snd_soc_dapm_path
*path
;
508 if (widget
->id
== snd_soc_dapm_supply
)
511 switch (widget
->id
) {
512 case snd_soc_dapm_adc
:
513 case snd_soc_dapm_aif_out
:
520 if (widget
->connected
) {
521 /* connected pin ? */
522 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
525 /* connected jack or spk ? */
526 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
527 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sources
)))
531 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
535 if (path
->sink
&& path
->connect
) {
537 con
+= is_connected_output_ep(path
->sink
);
545 * Recursively check for a completed path to an active or physically connected
546 * input widget. Returns number of complete paths.
548 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
550 struct snd_soc_dapm_path
*path
;
553 if (widget
->id
== snd_soc_dapm_supply
)
556 /* active stream ? */
557 switch (widget
->id
) {
558 case snd_soc_dapm_dac
:
559 case snd_soc_dapm_aif_in
:
566 if (widget
->connected
) {
567 /* connected pin ? */
568 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
571 /* connected VMID/Bias for lower pops */
572 if (widget
->id
== snd_soc_dapm_vmid
)
575 /* connected jack ? */
576 if (widget
->id
== snd_soc_dapm_mic
||
577 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sinks
)))
581 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
585 if (path
->source
&& path
->connect
) {
587 con
+= is_connected_input_ep(path
->source
);
595 * Handler for generic register modifier widget.
597 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
598 struct snd_kcontrol
*kcontrol
, int event
)
602 if (SND_SOC_DAPM_EVENT_ON(event
))
607 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
608 w
->mask
<< w
->shift
, val
<< w
->shift
);
612 EXPORT_SYMBOL_GPL(dapm_reg_event
);
614 /* Standard power change method, used to apply power changes to most
617 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
621 /* call any power change event handlers */
623 pr_debug("power %s event for %s flags %x\n",
624 w
->power
? "on" : "off",
625 w
->name
, w
->event_flags
);
627 /* power up pre event */
628 if (w
->power
&& w
->event
&&
629 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
630 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
635 /* power down pre event */
636 if (!w
->power
&& w
->event
&&
637 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
638 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
643 /* Lower PGA volume to reduce pops */
644 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
645 dapm_set_pga(w
, w
->power
);
649 /* Raise PGA volume to reduce pops */
650 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
651 dapm_set_pga(w
, w
->power
);
653 /* power up post event */
654 if (w
->power
&& w
->event
&&
655 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
657 NULL
, SND_SOC_DAPM_POST_PMU
);
662 /* power down post event */
663 if (!w
->power
&& w
->event
&&
664 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
665 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
673 /* Generic check to see if a widget should be powered.
675 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
679 in
= is_connected_input_ep(w
);
680 dapm_clear_walk(w
->codec
);
681 out
= is_connected_output_ep(w
);
682 dapm_clear_walk(w
->codec
);
683 return out
!= 0 && in
!= 0;
686 /* Check to see if an ADC has power */
687 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
692 in
= is_connected_input_ep(w
);
693 dapm_clear_walk(w
->codec
);
696 return dapm_generic_check_power(w
);
700 /* Check to see if a DAC has power */
701 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
706 out
= is_connected_output_ep(w
);
707 dapm_clear_walk(w
->codec
);
710 return dapm_generic_check_power(w
);
714 /* Check to see if a power supply is needed */
715 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
717 struct snd_soc_dapm_path
*path
;
720 /* Check if one of our outputs is connected */
721 list_for_each_entry(path
, &w
->sinks
, list_source
) {
722 if (path
->connected
&&
723 !path
->connected(path
->source
, path
->sink
))
726 if (path
->sink
&& path
->sink
->power_check
&&
727 path
->sink
->power_check(path
->sink
)) {
733 dapm_clear_walk(w
->codec
);
738 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
739 struct snd_soc_dapm_widget
*b
,
742 if (sort
[a
->id
] != sort
[b
->id
])
743 return sort
[a
->id
] - sort
[b
->id
];
744 if (a
->reg
!= b
->reg
)
745 return a
->reg
- b
->reg
;
750 /* Insert a widget in order into a DAPM power sequence. */
751 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
752 struct list_head
*list
,
755 struct snd_soc_dapm_widget
*w
;
757 list_for_each_entry(w
, list
, power_list
)
758 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
759 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
763 list_add_tail(&new_widget
->power_list
, list
);
766 /* Apply the coalesced changes from a DAPM sequence */
767 static void dapm_seq_run_coalesced(struct snd_soc_codec
*codec
,
768 struct list_head
*pending
)
770 struct snd_soc_dapm_widget
*w
;
772 unsigned int value
= 0;
773 unsigned int mask
= 0;
774 unsigned int cur_mask
;
776 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
779 list_for_each_entry(w
, pending
, power_list
) {
780 cur_mask
= 1 << w
->shift
;
781 BUG_ON(reg
!= w
->reg
);
792 pop_dbg(codec
->pop_time
,
793 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
794 w
->name
, reg
, value
, mask
);
796 /* power up pre event */
797 if (w
->power
&& w
->event
&&
798 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
799 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMU\n",
801 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
803 pr_err("%s: pre event failed: %d\n",
807 /* power down pre event */
808 if (!w
->power
&& w
->event
&&
809 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
810 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMD\n",
812 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
814 pr_err("%s: pre event failed: %d\n",
818 /* Lower PGA volume to reduce pops */
819 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
820 dapm_set_pga(w
, w
->power
);
824 pop_dbg(codec
->pop_time
,
825 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
826 value
, mask
, reg
, codec
->pop_time
);
827 pop_wait(codec
->pop_time
);
828 snd_soc_update_bits(codec
, reg
, mask
, value
);
831 list_for_each_entry(w
, pending
, power_list
) {
832 /* Raise PGA volume to reduce pops */
833 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
834 dapm_set_pga(w
, w
->power
);
836 /* power up post event */
837 if (w
->power
&& w
->event
&&
838 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
839 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMU\n",
842 NULL
, SND_SOC_DAPM_POST_PMU
);
844 pr_err("%s: post event failed: %d\n",
848 /* power down post event */
849 if (!w
->power
&& w
->event
&&
850 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
851 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMD\n",
853 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
855 pr_err("%s: post event failed: %d\n",
861 /* Apply a DAPM power sequence.
863 * We walk over a pre-sorted list of widgets to apply power to. In
864 * order to minimise the number of writes to the device required
865 * multiple widgets will be updated in a single write where possible.
866 * Currently anything that requires more than a single write is not
869 static void dapm_seq_run(struct snd_soc_codec
*codec
, struct list_head
*list
,
870 int event
, int sort
[])
872 struct snd_soc_dapm_widget
*w
, *n
;
875 int cur_reg
= SND_SOC_NOPM
;
878 list_for_each_entry_safe(w
, n
, list
, power_list
) {
881 /* Do we need to apply any queued changes? */
882 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
883 if (!list_empty(&pending
))
884 dapm_seq_run_coalesced(codec
, &pending
);
886 INIT_LIST_HEAD(&pending
);
888 cur_reg
= SND_SOC_NOPM
;
892 case snd_soc_dapm_pre
:
894 list_for_each_entry_safe_continue(w
, n
, list
,
897 if (event
== SND_SOC_DAPM_STREAM_START
)
899 NULL
, SND_SOC_DAPM_PRE_PMU
);
900 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
902 NULL
, SND_SOC_DAPM_PRE_PMD
);
905 case snd_soc_dapm_post
:
907 list_for_each_entry_safe_continue(w
, n
, list
,
910 if (event
== SND_SOC_DAPM_STREAM_START
)
912 NULL
, SND_SOC_DAPM_POST_PMU
);
913 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
915 NULL
, SND_SOC_DAPM_POST_PMD
);
918 case snd_soc_dapm_input
:
919 case snd_soc_dapm_output
:
920 case snd_soc_dapm_hp
:
921 case snd_soc_dapm_mic
:
922 case snd_soc_dapm_line
:
923 case snd_soc_dapm_spk
:
924 /* No register support currently */
925 ret
= dapm_generic_apply_power(w
);
929 /* Queue it up for application */
930 cur_sort
= sort
[w
->id
];
932 list_move(&w
->power_list
, &pending
);
937 pr_err("Failed to apply widget power: %d\n",
941 if (!list_empty(&pending
))
942 dapm_seq_run_coalesced(codec
, &pending
);
946 * Scan each dapm widget for complete audio path.
947 * A complete path is a route that has valid endpoints i.e.:-
949 * o DAC to output pin.
950 * o Input Pin to ADC.
951 * o Input pin to Output pin (bypass, sidetone)
952 * o DAC to ADC (loopback).
954 static int dapm_power_widgets(struct snd_soc_codec
*codec
, int event
)
956 struct snd_soc_device
*socdev
= codec
->socdev
;
957 struct snd_soc_dapm_widget
*w
;
959 LIST_HEAD(down_list
);
964 /* Check which widgets we need to power and store them in
965 * lists indicating if they should be powered up or down.
967 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
969 case snd_soc_dapm_pre
:
970 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
972 case snd_soc_dapm_post
:
973 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
980 power
= w
->power_check(w
);
984 if (w
->power
== power
)
988 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
990 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
997 /* If there are no DAPM widgets then try to figure out power from the
1000 if (list_empty(&codec
->dapm_widgets
)) {
1002 case SND_SOC_DAPM_STREAM_START
:
1003 case SND_SOC_DAPM_STREAM_RESUME
:
1006 case SND_SOC_DAPM_STREAM_NOP
:
1007 sys_power
= codec
->bias_level
!= SND_SOC_BIAS_STANDBY
;
1013 /* If we're changing to all on or all off then prepare */
1014 if ((sys_power
&& codec
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
1015 (!sys_power
&& codec
->bias_level
== SND_SOC_BIAS_ON
)) {
1016 ret
= snd_soc_dapm_set_bias_level(socdev
,
1017 SND_SOC_BIAS_PREPARE
);
1019 pr_err("Failed to prepare bias: %d\n", ret
);
1022 /* Power down widgets first; try to avoid amplifying pops. */
1023 dapm_seq_run(codec
, &down_list
, event
, dapm_down_seq
);
1026 dapm_seq_run(codec
, &up_list
, event
, dapm_up_seq
);
1028 /* If we just powered the last thing off drop to standby bias */
1029 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
1030 ret
= snd_soc_dapm_set_bias_level(socdev
,
1031 SND_SOC_BIAS_STANDBY
);
1033 pr_err("Failed to apply standby bias: %d\n", ret
);
1036 /* If we just powered up then move to active bias */
1037 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
1038 ret
= snd_soc_dapm_set_bias_level(socdev
,
1041 pr_err("Failed to apply active bias: %d\n", ret
);
1044 pop_dbg(codec
->pop_time
, "DAPM sequencing finished, waiting %dms\n",
1051 static void dbg_dump_dapm(struct snd_soc_codec
* codec
, const char *action
)
1053 struct snd_soc_dapm_widget
*w
;
1054 struct snd_soc_dapm_path
*p
= NULL
;
1057 printk("DAPM %s %s\n", codec
->name
, action
);
1059 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1061 /* only display widgets that effect routing */
1063 case snd_soc_dapm_pre
:
1064 case snd_soc_dapm_post
:
1065 case snd_soc_dapm_vmid
:
1067 case snd_soc_dapm_mux
:
1068 case snd_soc_dapm_value_mux
:
1069 case snd_soc_dapm_output
:
1070 case snd_soc_dapm_input
:
1071 case snd_soc_dapm_switch
:
1072 case snd_soc_dapm_hp
:
1073 case snd_soc_dapm_mic
:
1074 case snd_soc_dapm_spk
:
1075 case snd_soc_dapm_line
:
1076 case snd_soc_dapm_micbias
:
1077 case snd_soc_dapm_dac
:
1078 case snd_soc_dapm_adc
:
1079 case snd_soc_dapm_pga
:
1080 case snd_soc_dapm_mixer
:
1081 case snd_soc_dapm_mixer_named_ctl
:
1082 case snd_soc_dapm_supply
:
1083 case snd_soc_dapm_aif_in
:
1084 case snd_soc_dapm_aif_out
:
1086 in
= is_connected_input_ep(w
);
1087 dapm_clear_walk(w
->codec
);
1088 out
= is_connected_output_ep(w
);
1089 dapm_clear_walk(w
->codec
);
1090 printk("%s: %s in %d out %d\n", w
->name
,
1091 w
->power
? "On":"Off",in
, out
);
1093 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1095 printk(" in %s %s\n", p
->name
? p
->name
: "static",
1098 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1100 printk(" out %s %s\n", p
->name
? p
->name
: "static",
1110 #ifdef CONFIG_DEBUG_FS
1111 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1113 file
->private_data
= inode
->i_private
;
1117 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1118 char __user
*user_buf
,
1119 size_t count
, loff_t
*ppos
)
1121 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1125 struct snd_soc_dapm_path
*p
= NULL
;
1127 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1131 in
= is_connected_input_ep(w
);
1132 dapm_clear_walk(w
->codec
);
1133 out
= is_connected_output_ep(w
);
1134 dapm_clear_walk(w
->codec
);
1136 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d\n",
1137 w
->name
, w
->power
? "On" : "Off", in
, out
);
1140 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1142 w
->active
? "active" : "inactive");
1144 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1145 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1149 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1151 p
->name
? p
->name
: "static",
1154 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1155 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1159 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1161 p
->name
? p
->name
: "static",
1165 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1171 static const struct file_operations dapm_widget_power_fops
= {
1172 .open
= dapm_widget_power_open_file
,
1173 .read
= dapm_widget_power_read_file
,
1176 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1178 struct snd_soc_dapm_widget
*w
;
1181 if (!codec
->debugfs_dapm
)
1184 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1188 d
= debugfs_create_file(w
->name
, 0444,
1189 codec
->debugfs_dapm
, w
,
1190 &dapm_widget_power_fops
);
1193 "ASoC: Failed to create %s debugfs file\n",
1198 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1203 /* test and update the power status of a mux widget */
1204 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1205 struct snd_kcontrol
*kcontrol
, int change
,
1206 int mux
, struct soc_enum
*e
)
1208 struct snd_soc_dapm_path
*path
;
1211 if (widget
->id
!= snd_soc_dapm_mux
&&
1212 widget
->id
!= snd_soc_dapm_value_mux
)
1218 /* find dapm widget path assoc with kcontrol */
1219 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1220 if (path
->kcontrol
!= kcontrol
)
1223 if (!path
->name
|| !e
->texts
[mux
])
1227 /* we now need to match the string in the enum to the path */
1228 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1229 path
->connect
= 1; /* new connection */
1231 path
->connect
= 0; /* old connection must be powered down */
1235 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1236 dump_dapm(widget
->codec
, "mux power update");
1242 /* test and update the power status of a mixer or switch widget */
1243 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1244 struct snd_kcontrol
*kcontrol
, int reg
,
1245 int val_mask
, int val
, int invert
)
1247 struct snd_soc_dapm_path
*path
;
1250 if (widget
->id
!= snd_soc_dapm_mixer
&&
1251 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1252 widget
->id
!= snd_soc_dapm_switch
)
1255 if (!snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
))
1258 /* find dapm widget path assoc with kcontrol */
1259 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1260 if (path
->kcontrol
!= kcontrol
)
1263 /* found, now check type */
1266 /* new connection */
1267 path
->connect
= invert
? 0:1;
1269 /* old connection must be powered down */
1270 path
->connect
= invert
? 1:0;
1275 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1276 dump_dapm(widget
->codec
, "mixer power update");
1282 /* show dapm widget status in sys fs */
1283 static ssize_t
dapm_widget_show(struct device
*dev
,
1284 struct device_attribute
*attr
, char *buf
)
1286 struct snd_soc_device
*devdata
= dev_get_drvdata(dev
);
1287 struct snd_soc_codec
*codec
= devdata
->card
->codec
;
1288 struct snd_soc_dapm_widget
*w
;
1290 char *state
= "not set";
1292 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1294 /* only display widgets that burnm power */
1296 case snd_soc_dapm_hp
:
1297 case snd_soc_dapm_mic
:
1298 case snd_soc_dapm_spk
:
1299 case snd_soc_dapm_line
:
1300 case snd_soc_dapm_micbias
:
1301 case snd_soc_dapm_dac
:
1302 case snd_soc_dapm_adc
:
1303 case snd_soc_dapm_pga
:
1304 case snd_soc_dapm_mixer
:
1305 case snd_soc_dapm_mixer_named_ctl
:
1306 case snd_soc_dapm_supply
:
1308 count
+= sprintf(buf
+ count
, "%s: %s\n",
1309 w
->name
, w
->power
? "On":"Off");
1316 switch (codec
->bias_level
) {
1317 case SND_SOC_BIAS_ON
:
1320 case SND_SOC_BIAS_PREPARE
:
1323 case SND_SOC_BIAS_STANDBY
:
1326 case SND_SOC_BIAS_OFF
:
1330 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1335 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1337 int snd_soc_dapm_sys_add(struct device
*dev
)
1339 return device_create_file(dev
, &dev_attr_dapm_widget
);
1342 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1344 device_remove_file(dev
, &dev_attr_dapm_widget
);
1347 /* free all dapm widgets and resources */
1348 static void dapm_free_widgets(struct snd_soc_codec
*codec
)
1350 struct snd_soc_dapm_widget
*w
, *next_w
;
1351 struct snd_soc_dapm_path
*p
, *next_p
;
1353 list_for_each_entry_safe(w
, next_w
, &codec
->dapm_widgets
, list
) {
1358 list_for_each_entry_safe(p
, next_p
, &codec
->dapm_paths
, list
) {
1360 kfree(p
->long_name
);
1365 static int snd_soc_dapm_set_pin(struct snd_soc_codec
*codec
,
1366 const char *pin
, int status
)
1368 struct snd_soc_dapm_widget
*w
;
1370 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1371 if (!strcmp(w
->name
, pin
)) {
1372 pr_debug("dapm: %s: pin %s\n", codec
->name
, pin
);
1373 w
->connected
= status
;
1378 pr_err("dapm: %s: configuring unknown pin %s\n", codec
->name
, pin
);
1383 * snd_soc_dapm_sync - scan and power dapm paths
1384 * @codec: audio codec
1386 * Walks all dapm audio paths and powers widgets according to their
1387 * stream or path usage.
1389 * Returns 0 for success.
1391 int snd_soc_dapm_sync(struct snd_soc_codec
*codec
)
1393 int ret
= dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1394 dump_dapm(codec
, "sync");
1397 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1399 static int snd_soc_dapm_add_route(struct snd_soc_codec
*codec
,
1400 const struct snd_soc_dapm_route
*route
)
1402 struct snd_soc_dapm_path
*path
;
1403 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1404 const char *sink
= route
->sink
;
1405 const char *control
= route
->control
;
1406 const char *source
= route
->source
;
1409 /* find src and dest widgets */
1410 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1412 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1416 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1421 if (wsource
== NULL
|| wsink
== NULL
)
1424 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1428 path
->source
= wsource
;
1430 path
->connected
= route
->connected
;
1431 INIT_LIST_HEAD(&path
->list
);
1432 INIT_LIST_HEAD(&path
->list_source
);
1433 INIT_LIST_HEAD(&path
->list_sink
);
1435 /* check for external widgets */
1436 if (wsink
->id
== snd_soc_dapm_input
) {
1437 if (wsource
->id
== snd_soc_dapm_micbias
||
1438 wsource
->id
== snd_soc_dapm_mic
||
1439 wsource
->id
== snd_soc_dapm_line
||
1440 wsource
->id
== snd_soc_dapm_output
)
1443 if (wsource
->id
== snd_soc_dapm_output
) {
1444 if (wsink
->id
== snd_soc_dapm_spk
||
1445 wsink
->id
== snd_soc_dapm_hp
||
1446 wsink
->id
== snd_soc_dapm_line
||
1447 wsink
->id
== snd_soc_dapm_input
)
1451 /* connect static paths */
1452 if (control
== NULL
) {
1453 list_add(&path
->list
, &codec
->dapm_paths
);
1454 list_add(&path
->list_sink
, &wsink
->sources
);
1455 list_add(&path
->list_source
, &wsource
->sinks
);
1460 /* connect dynamic paths */
1462 case snd_soc_dapm_adc
:
1463 case snd_soc_dapm_dac
:
1464 case snd_soc_dapm_pga
:
1465 case snd_soc_dapm_input
:
1466 case snd_soc_dapm_output
:
1467 case snd_soc_dapm_micbias
:
1468 case snd_soc_dapm_vmid
:
1469 case snd_soc_dapm_pre
:
1470 case snd_soc_dapm_post
:
1471 case snd_soc_dapm_supply
:
1472 case snd_soc_dapm_aif_in
:
1473 case snd_soc_dapm_aif_out
:
1474 list_add(&path
->list
, &codec
->dapm_paths
);
1475 list_add(&path
->list_sink
, &wsink
->sources
);
1476 list_add(&path
->list_source
, &wsource
->sinks
);
1479 case snd_soc_dapm_mux
:
1480 case snd_soc_dapm_value_mux
:
1481 ret
= dapm_connect_mux(codec
, wsource
, wsink
, path
, control
,
1482 &wsink
->kcontrols
[0]);
1486 case snd_soc_dapm_switch
:
1487 case snd_soc_dapm_mixer
:
1488 case snd_soc_dapm_mixer_named_ctl
:
1489 ret
= dapm_connect_mixer(codec
, wsource
, wsink
, path
, control
);
1493 case snd_soc_dapm_hp
:
1494 case snd_soc_dapm_mic
:
1495 case snd_soc_dapm_line
:
1496 case snd_soc_dapm_spk
:
1497 list_add(&path
->list
, &codec
->dapm_paths
);
1498 list_add(&path
->list_sink
, &wsink
->sources
);
1499 list_add(&path
->list_source
, &wsource
->sinks
);
1506 printk(KERN_WARNING
"asoc: no dapm match for %s --> %s --> %s\n", source
,
1513 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1515 * @route: audio routes
1516 * @num: number of routes
1518 * Connects 2 dapm widgets together via a named audio path. The sink is
1519 * the widget receiving the audio signal, whilst the source is the sender
1520 * of the audio signal.
1522 * Returns 0 for success else error. On error all resources can be freed
1523 * with a call to snd_soc_card_free().
1525 int snd_soc_dapm_add_routes(struct snd_soc_codec
*codec
,
1526 const struct snd_soc_dapm_route
*route
, int num
)
1530 for (i
= 0; i
< num
; i
++) {
1531 ret
= snd_soc_dapm_add_route(codec
, route
);
1533 printk(KERN_ERR
"Failed to add route %s->%s\n",
1543 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1546 * snd_soc_dapm_new_widgets - add new dapm widgets
1547 * @codec: audio codec
1549 * Checks the codec for any new dapm widgets and creates them if found.
1551 * Returns 0 for success.
1553 int snd_soc_dapm_new_widgets(struct snd_soc_codec
*codec
)
1555 struct snd_soc_dapm_widget
*w
;
1557 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
1563 case snd_soc_dapm_switch
:
1564 case snd_soc_dapm_mixer
:
1565 case snd_soc_dapm_mixer_named_ctl
:
1566 w
->power_check
= dapm_generic_check_power
;
1567 dapm_new_mixer(codec
, w
);
1569 case snd_soc_dapm_mux
:
1570 case snd_soc_dapm_value_mux
:
1571 w
->power_check
= dapm_generic_check_power
;
1572 dapm_new_mux(codec
, w
);
1574 case snd_soc_dapm_adc
:
1575 case snd_soc_dapm_aif_out
:
1576 w
->power_check
= dapm_adc_check_power
;
1578 case snd_soc_dapm_dac
:
1579 case snd_soc_dapm_aif_in
:
1580 w
->power_check
= dapm_dac_check_power
;
1582 case snd_soc_dapm_pga
:
1583 w
->power_check
= dapm_generic_check_power
;
1584 dapm_new_pga(codec
, w
);
1586 case snd_soc_dapm_input
:
1587 case snd_soc_dapm_output
:
1588 case snd_soc_dapm_micbias
:
1589 case snd_soc_dapm_spk
:
1590 case snd_soc_dapm_hp
:
1591 case snd_soc_dapm_mic
:
1592 case snd_soc_dapm_line
:
1593 w
->power_check
= dapm_generic_check_power
;
1595 case snd_soc_dapm_supply
:
1596 w
->power_check
= dapm_supply_check_power
;
1597 case snd_soc_dapm_vmid
:
1598 case snd_soc_dapm_pre
:
1599 case snd_soc_dapm_post
:
1605 dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1608 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1611 * snd_soc_dapm_get_volsw - dapm mixer get callback
1612 * @kcontrol: mixer control
1613 * @ucontrol: control element information
1615 * Callback to get the value of a dapm mixer control.
1617 * Returns 0 for success.
1619 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1620 struct snd_ctl_elem_value
*ucontrol
)
1622 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1623 struct soc_mixer_control
*mc
=
1624 (struct soc_mixer_control
*)kcontrol
->private_value
;
1625 unsigned int reg
= mc
->reg
;
1626 unsigned int shift
= mc
->shift
;
1627 unsigned int rshift
= mc
->rshift
;
1629 unsigned int invert
= mc
->invert
;
1630 unsigned int mask
= (1 << fls(max
)) - 1;
1632 /* return the saved value if we are powered down */
1633 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1634 ucontrol
->value
.integer
.value
[0] = widget
->saved_value
;
1638 ucontrol
->value
.integer
.value
[0] =
1639 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1640 if (shift
!= rshift
)
1641 ucontrol
->value
.integer
.value
[1] =
1642 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1644 ucontrol
->value
.integer
.value
[0] =
1645 max
- ucontrol
->value
.integer
.value
[0];
1646 if (shift
!= rshift
)
1647 ucontrol
->value
.integer
.value
[1] =
1648 max
- ucontrol
->value
.integer
.value
[1];
1653 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1656 * snd_soc_dapm_put_volsw - dapm mixer set callback
1657 * @kcontrol: mixer control
1658 * @ucontrol: control element information
1660 * Callback to set the value of a dapm mixer control.
1662 * Returns 0 for success.
1664 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1665 struct snd_ctl_elem_value
*ucontrol
)
1667 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1668 struct soc_mixer_control
*mc
=
1669 (struct soc_mixer_control
*)kcontrol
->private_value
;
1670 unsigned int reg
= mc
->reg
;
1671 unsigned int shift
= mc
->shift
;
1672 unsigned int rshift
= mc
->rshift
;
1674 unsigned int mask
= (1 << fls(max
)) - 1;
1675 unsigned int invert
= mc
->invert
;
1676 unsigned int val
, val2
, val_mask
;
1679 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1683 val_mask
= mask
<< shift
;
1685 if (shift
!= rshift
) {
1686 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1689 val_mask
|= mask
<< rshift
;
1690 val
|= val2
<< rshift
;
1693 mutex_lock(&widget
->codec
->mutex
);
1694 widget
->value
= val
;
1696 /* save volume value if the widget is powered down */
1697 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1698 widget
->saved_value
= val
;
1699 mutex_unlock(&widget
->codec
->mutex
);
1703 dapm_mixer_update_power(widget
, kcontrol
, reg
, val_mask
, val
, invert
);
1704 if (widget
->event
) {
1705 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1706 ret
= widget
->event(widget
, kcontrol
,
1707 SND_SOC_DAPM_PRE_REG
);
1713 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1714 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1715 ret
= widget
->event(widget
, kcontrol
,
1716 SND_SOC_DAPM_POST_REG
);
1718 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1721 mutex_unlock(&widget
->codec
->mutex
);
1724 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1727 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1728 * @kcontrol: mixer control
1729 * @ucontrol: control element information
1731 * Callback to get the value of a dapm enumerated double mixer control.
1733 * Returns 0 for success.
1735 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1736 struct snd_ctl_elem_value
*ucontrol
)
1738 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1739 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1740 unsigned int val
, bitmask
;
1742 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1744 val
= snd_soc_read(widget
->codec
, e
->reg
);
1745 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1746 if (e
->shift_l
!= e
->shift_r
)
1747 ucontrol
->value
.enumerated
.item
[1] =
1748 (val
>> e
->shift_r
) & (bitmask
- 1);
1752 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1755 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1756 * @kcontrol: mixer control
1757 * @ucontrol: control element information
1759 * Callback to set the value of a dapm enumerated double mixer control.
1761 * Returns 0 for success.
1763 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1764 struct snd_ctl_elem_value
*ucontrol
)
1766 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1767 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1768 unsigned int val
, mux
, change
;
1769 unsigned int mask
, bitmask
;
1772 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1774 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1776 mux
= ucontrol
->value
.enumerated
.item
[0];
1777 val
= mux
<< e
->shift_l
;
1778 mask
= (bitmask
- 1) << e
->shift_l
;
1779 if (e
->shift_l
!= e
->shift_r
) {
1780 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1782 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1783 mask
|= (bitmask
- 1) << e
->shift_r
;
1786 mutex_lock(&widget
->codec
->mutex
);
1787 widget
->value
= val
;
1788 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1789 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1791 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1792 ret
= widget
->event(widget
,
1793 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1798 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1800 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1801 ret
= widget
->event(widget
,
1802 kcontrol
, SND_SOC_DAPM_POST_REG
);
1805 mutex_unlock(&widget
->codec
->mutex
);
1808 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1811 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1812 * @kcontrol: mixer control
1813 * @ucontrol: control element information
1815 * Returns 0 for success.
1817 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
1818 struct snd_ctl_elem_value
*ucontrol
)
1820 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1822 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
1826 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
1829 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1830 * @kcontrol: mixer control
1831 * @ucontrol: control element information
1833 * Returns 0 for success.
1835 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
1836 struct snd_ctl_elem_value
*ucontrol
)
1838 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1839 struct soc_enum
*e
=
1840 (struct soc_enum
*)kcontrol
->private_value
;
1844 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
1847 mutex_lock(&widget
->codec
->mutex
);
1849 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
1850 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
1851 dapm_mux_update_power(widget
, kcontrol
, change
, widget
->value
, e
);
1853 mutex_unlock(&widget
->codec
->mutex
);
1856 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
1859 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1861 * @kcontrol: mixer control
1862 * @ucontrol: control element information
1864 * Callback to get the value of a dapm semi enumerated double mixer control.
1866 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1867 * used for handling bitfield coded enumeration for example.
1869 * Returns 0 for success.
1871 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1872 struct snd_ctl_elem_value
*ucontrol
)
1874 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1875 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1876 unsigned int reg_val
, val
, mux
;
1878 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1879 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1880 for (mux
= 0; mux
< e
->max
; mux
++) {
1881 if (val
== e
->values
[mux
])
1884 ucontrol
->value
.enumerated
.item
[0] = mux
;
1885 if (e
->shift_l
!= e
->shift_r
) {
1886 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1887 for (mux
= 0; mux
< e
->max
; mux
++) {
1888 if (val
== e
->values
[mux
])
1891 ucontrol
->value
.enumerated
.item
[1] = mux
;
1896 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1899 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1901 * @kcontrol: mixer control
1902 * @ucontrol: control element information
1904 * Callback to set the value of a dapm semi enumerated double mixer control.
1906 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1907 * used for handling bitfield coded enumeration for example.
1909 * Returns 0 for success.
1911 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
1912 struct snd_ctl_elem_value
*ucontrol
)
1914 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1915 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1916 unsigned int val
, mux
, change
;
1920 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1922 mux
= ucontrol
->value
.enumerated
.item
[0];
1923 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1924 mask
= e
->mask
<< e
->shift_l
;
1925 if (e
->shift_l
!= e
->shift_r
) {
1926 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1928 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1929 mask
|= e
->mask
<< e
->shift_r
;
1932 mutex_lock(&widget
->codec
->mutex
);
1933 widget
->value
= val
;
1934 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1935 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1937 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1938 ret
= widget
->event(widget
,
1939 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1944 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1946 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1947 ret
= widget
->event(widget
,
1948 kcontrol
, SND_SOC_DAPM_POST_REG
);
1951 mutex_unlock(&widget
->codec
->mutex
);
1954 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1957 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1959 * @kcontrol: mixer control
1960 * @uinfo: control element information
1962 * Callback to provide information about a pin switch control.
1964 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1965 struct snd_ctl_elem_info
*uinfo
)
1967 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1969 uinfo
->value
.integer
.min
= 0;
1970 uinfo
->value
.integer
.max
= 1;
1974 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1977 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1979 * @kcontrol: mixer control
1982 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1983 struct snd_ctl_elem_value
*ucontrol
)
1985 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1986 const char *pin
= (const char *)kcontrol
->private_value
;
1988 mutex_lock(&codec
->mutex
);
1990 ucontrol
->value
.integer
.value
[0] =
1991 snd_soc_dapm_get_pin_status(codec
, pin
);
1993 mutex_unlock(&codec
->mutex
);
1997 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
2000 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2002 * @kcontrol: mixer control
2005 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
2006 struct snd_ctl_elem_value
*ucontrol
)
2008 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2009 const char *pin
= (const char *)kcontrol
->private_value
;
2011 mutex_lock(&codec
->mutex
);
2013 if (ucontrol
->value
.integer
.value
[0])
2014 snd_soc_dapm_enable_pin(codec
, pin
);
2016 snd_soc_dapm_disable_pin(codec
, pin
);
2018 snd_soc_dapm_sync(codec
);
2020 mutex_unlock(&codec
->mutex
);
2024 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
2027 * snd_soc_dapm_new_control - create new dapm control
2028 * @codec: audio codec
2029 * @widget: widget template
2031 * Creates a new dapm control based upon the template.
2033 * Returns 0 for success else error.
2035 int snd_soc_dapm_new_control(struct snd_soc_codec
*codec
,
2036 const struct snd_soc_dapm_widget
*widget
)
2038 struct snd_soc_dapm_widget
*w
;
2040 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
2044 INIT_LIST_HEAD(&w
->sources
);
2045 INIT_LIST_HEAD(&w
->sinks
);
2046 INIT_LIST_HEAD(&w
->list
);
2047 list_add(&w
->list
, &codec
->dapm_widgets
);
2049 /* machine layer set ups unconnected pins and insertions */
2053 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
2056 * snd_soc_dapm_new_controls - create new dapm controls
2057 * @codec: audio codec
2058 * @widget: widget array
2059 * @num: number of widgets
2061 * Creates new DAPM controls based upon the templates.
2063 * Returns 0 for success else error.
2065 int snd_soc_dapm_new_controls(struct snd_soc_codec
*codec
,
2066 const struct snd_soc_dapm_widget
*widget
,
2071 for (i
= 0; i
< num
; i
++) {
2072 ret
= snd_soc_dapm_new_control(codec
, widget
);
2075 "ASoC: Failed to create DAPM control %s: %d\n",
2083 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2087 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2088 * @codec: audio codec
2089 * @stream: stream name
2090 * @event: stream event
2092 * Sends a stream event to the dapm core. The core then makes any
2093 * necessary widget power changes.
2095 * Returns 0 for success else error.
2097 int snd_soc_dapm_stream_event(struct snd_soc_codec
*codec
,
2098 char *stream
, int event
)
2100 struct snd_soc_dapm_widget
*w
;
2105 mutex_lock(&codec
->mutex
);
2106 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
2110 pr_debug("widget %s\n %s stream %s event %d\n",
2111 w
->name
, w
->sname
, stream
, event
);
2112 if (strstr(w
->sname
, stream
)) {
2114 case SND_SOC_DAPM_STREAM_START
:
2117 case SND_SOC_DAPM_STREAM_STOP
:
2120 case SND_SOC_DAPM_STREAM_SUSPEND
:
2125 case SND_SOC_DAPM_STREAM_RESUME
:
2131 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2133 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2138 mutex_unlock(&codec
->mutex
);
2140 dapm_power_widgets(codec
, event
);
2141 dump_dapm(codec
, __func__
);
2144 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
2147 * snd_soc_dapm_enable_pin - enable pin.
2151 * Enables input/output pin and its parents or children widgets iff there is
2152 * a valid audio route and active audio stream.
2153 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2154 * do any widget power switching.
2156 int snd_soc_dapm_enable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2158 return snd_soc_dapm_set_pin(codec
, pin
, 1);
2160 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2163 * snd_soc_dapm_disable_pin - disable pin.
2167 * Disables input/output pin and its parents or children widgets.
2168 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2169 * do any widget power switching.
2171 int snd_soc_dapm_disable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2173 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2175 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2178 * snd_soc_dapm_nc_pin - permanently disable pin.
2182 * Marks the specified pin as being not connected, disabling it along
2183 * any parent or child widgets. At present this is identical to
2184 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2185 * additional things such as disabling controls which only affect
2186 * paths through the pin.
2188 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2189 * do any widget power switching.
2191 int snd_soc_dapm_nc_pin(struct snd_soc_codec
*codec
, const char *pin
)
2193 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2195 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2198 * snd_soc_dapm_get_pin_status - get audio pin status
2199 * @codec: audio codec
2200 * @pin: audio signal pin endpoint (or start point)
2202 * Get audio pin status - connected or disconnected.
2204 * Returns 1 for connected otherwise 0.
2206 int snd_soc_dapm_get_pin_status(struct snd_soc_codec
*codec
, const char *pin
)
2208 struct snd_soc_dapm_widget
*w
;
2210 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2211 if (!strcmp(w
->name
, pin
))
2212 return w
->connected
;
2217 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2220 * snd_soc_dapm_free - free dapm resources
2221 * @socdev: SoC device
2223 * Free all dapm widgets and resources.
2225 void snd_soc_dapm_free(struct snd_soc_device
*socdev
)
2227 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2229 snd_soc_dapm_sys_remove(socdev
->dev
);
2230 dapm_free_widgets(codec
);
2232 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2235 * snd_soc_dapm_shutdown - callback for system shutdown
2237 void snd_soc_dapm_shutdown(struct snd_soc_device
*socdev
)
2239 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2240 struct snd_soc_dapm_widget
*w
;
2241 LIST_HEAD(down_list
);
2244 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2246 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2252 /* If there were no widgets to power down we're already in
2256 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_PREPARE
);
2257 dapm_seq_run(codec
, &down_list
, 0, dapm_down_seq
);
2258 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_STANDBY
);
2261 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_OFF
);
2264 /* Module information */
2265 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2266 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2267 MODULE_LICENSE("GPL");