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 <linux/slab.h>
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc-dapm.h>
46 #include <sound/initval.h>
48 /* dapm power sequences - make this per codec in the future */
49 static int dapm_up_seq
[] = {
50 [snd_soc_dapm_pre
] = 0,
51 [snd_soc_dapm_supply
] = 1,
52 [snd_soc_dapm_micbias
] = 2,
53 [snd_soc_dapm_aif_in
] = 3,
54 [snd_soc_dapm_aif_out
] = 3,
55 [snd_soc_dapm_mic
] = 4,
56 [snd_soc_dapm_mux
] = 5,
57 [snd_soc_dapm_value_mux
] = 5,
58 [snd_soc_dapm_dac
] = 6,
59 [snd_soc_dapm_mixer
] = 7,
60 [snd_soc_dapm_mixer_named_ctl
] = 7,
61 [snd_soc_dapm_pga
] = 8,
62 [snd_soc_dapm_adc
] = 9,
63 [snd_soc_dapm_hp
] = 10,
64 [snd_soc_dapm_spk
] = 10,
65 [snd_soc_dapm_post
] = 11,
68 static int dapm_down_seq
[] = {
69 [snd_soc_dapm_pre
] = 0,
70 [snd_soc_dapm_adc
] = 1,
71 [snd_soc_dapm_hp
] = 2,
72 [snd_soc_dapm_spk
] = 2,
73 [snd_soc_dapm_pga
] = 4,
74 [snd_soc_dapm_mixer_named_ctl
] = 5,
75 [snd_soc_dapm_mixer
] = 5,
76 [snd_soc_dapm_dac
] = 6,
77 [snd_soc_dapm_mic
] = 7,
78 [snd_soc_dapm_micbias
] = 8,
79 [snd_soc_dapm_mux
] = 9,
80 [snd_soc_dapm_value_mux
] = 9,
81 [snd_soc_dapm_aif_in
] = 10,
82 [snd_soc_dapm_aif_out
] = 10,
83 [snd_soc_dapm_supply
] = 11,
84 [snd_soc_dapm_post
] = 12,
87 static void pop_wait(u32 pop_time
)
90 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
93 static void pop_dbg(u32 pop_time
, const char *fmt
, ...)
107 /* create a new dapm widget */
108 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
109 const struct snd_soc_dapm_widget
*_widget
)
111 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
115 * snd_soc_dapm_set_bias_level - set the bias level for the system
116 * @socdev: audio device
117 * @level: level to configure
119 * Configure the bias (power) levels for the SoC audio device.
121 * Returns 0 for success else error.
123 static int snd_soc_dapm_set_bias_level(struct snd_soc_device
*socdev
,
124 enum snd_soc_bias_level level
)
126 struct snd_soc_card
*card
= socdev
->card
;
127 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
131 case SND_SOC_BIAS_ON
:
132 dev_dbg(socdev
->dev
, "Setting full bias\n");
134 case SND_SOC_BIAS_PREPARE
:
135 dev_dbg(socdev
->dev
, "Setting bias prepare\n");
137 case SND_SOC_BIAS_STANDBY
:
138 dev_dbg(socdev
->dev
, "Setting standby bias\n");
140 case SND_SOC_BIAS_OFF
:
141 dev_dbg(socdev
->dev
, "Setting bias off\n");
144 dev_err(socdev
->dev
, "Setting invalid bias %d\n", level
);
148 if (card
->set_bias_level
)
149 ret
= card
->set_bias_level(card
, level
);
151 if (codec
->set_bias_level
)
152 ret
= codec
->set_bias_level(codec
, level
);
154 codec
->bias_level
= level
;
160 /* set up initial codec paths */
161 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
162 struct snd_soc_dapm_path
*p
, int i
)
165 case snd_soc_dapm_switch
:
166 case snd_soc_dapm_mixer
:
167 case snd_soc_dapm_mixer_named_ctl
: {
169 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
170 w
->kcontrols
[i
].private_value
;
171 unsigned int reg
= mc
->reg
;
172 unsigned int shift
= mc
->shift
;
174 unsigned int mask
= (1 << fls(max
)) - 1;
175 unsigned int invert
= mc
->invert
;
177 val
= snd_soc_read(w
->codec
, reg
);
178 val
= (val
>> shift
) & mask
;
180 if ((invert
&& !val
) || (!invert
&& val
))
186 case snd_soc_dapm_mux
: {
187 struct soc_enum
*e
= (struct soc_enum
*)w
->kcontrols
[i
].private_value
;
188 int val
, item
, bitmask
;
190 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
192 val
= snd_soc_read(w
->codec
, e
->reg
);
193 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
196 for (i
= 0; i
< e
->max
; i
++) {
197 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
202 case snd_soc_dapm_value_mux
: {
203 struct soc_enum
*e
= (struct soc_enum
*)
204 w
->kcontrols
[i
].private_value
;
207 val
= snd_soc_read(w
->codec
, e
->reg
);
208 val
= (val
>> e
->shift_l
) & e
->mask
;
209 for (item
= 0; item
< e
->max
; item
++) {
210 if (val
== e
->values
[item
])
215 for (i
= 0; i
< e
->max
; i
++) {
216 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
221 /* does not effect routing - always connected */
222 case snd_soc_dapm_pga
:
223 case snd_soc_dapm_output
:
224 case snd_soc_dapm_adc
:
225 case snd_soc_dapm_input
:
226 case snd_soc_dapm_dac
:
227 case snd_soc_dapm_micbias
:
228 case snd_soc_dapm_vmid
:
229 case snd_soc_dapm_supply
:
230 case snd_soc_dapm_aif_in
:
231 case snd_soc_dapm_aif_out
:
234 /* does effect routing - dynamically connected */
235 case snd_soc_dapm_hp
:
236 case snd_soc_dapm_mic
:
237 case snd_soc_dapm_spk
:
238 case snd_soc_dapm_line
:
239 case snd_soc_dapm_pre
:
240 case snd_soc_dapm_post
:
246 /* connect mux widget to its interconnecting audio paths */
247 static int dapm_connect_mux(struct snd_soc_codec
*codec
,
248 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
249 struct snd_soc_dapm_path
*path
, const char *control_name
,
250 const struct snd_kcontrol_new
*kcontrol
)
252 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
255 for (i
= 0; i
< e
->max
; i
++) {
256 if (!(strcmp(control_name
, e
->texts
[i
]))) {
257 list_add(&path
->list
, &codec
->dapm_paths
);
258 list_add(&path
->list_sink
, &dest
->sources
);
259 list_add(&path
->list_source
, &src
->sinks
);
260 path
->name
= (char*)e
->texts
[i
];
261 dapm_set_path_status(dest
, path
, 0);
269 /* connect mixer widget to its interconnecting audio paths */
270 static int dapm_connect_mixer(struct snd_soc_codec
*codec
,
271 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
272 struct snd_soc_dapm_path
*path
, const char *control_name
)
276 /* search for mixer kcontrol */
277 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
278 if (!strcmp(control_name
, dest
->kcontrols
[i
].name
)) {
279 list_add(&path
->list
, &codec
->dapm_paths
);
280 list_add(&path
->list_sink
, &dest
->sources
);
281 list_add(&path
->list_source
, &src
->sinks
);
282 path
->name
= dest
->kcontrols
[i
].name
;
283 dapm_set_path_status(dest
, path
, i
);
290 /* update dapm codec register bits */
291 static int dapm_update_bits(struct snd_soc_dapm_widget
*widget
)
294 unsigned int old
, new;
295 struct snd_soc_codec
*codec
= widget
->codec
;
297 /* check for valid widgets */
298 if (widget
->reg
< 0 || widget
->id
== snd_soc_dapm_input
||
299 widget
->id
== snd_soc_dapm_output
||
300 widget
->id
== snd_soc_dapm_hp
||
301 widget
->id
== snd_soc_dapm_mic
||
302 widget
->id
== snd_soc_dapm_line
||
303 widget
->id
== snd_soc_dapm_spk
)
306 power
= widget
->power
;
308 power
= (power
? 0:1);
310 old
= snd_soc_read(codec
, widget
->reg
);
311 new = (old
& ~(0x1 << widget
->shift
)) | (power
<< widget
->shift
);
315 pop_dbg(codec
->pop_time
, "pop test %s : %s in %d ms\n",
316 widget
->name
, widget
->power
? "on" : "off",
318 snd_soc_write(codec
, widget
->reg
, new);
319 pop_wait(codec
->pop_time
);
321 pr_debug("reg %x old %x new %x change %d\n", widget
->reg
,
326 /* ramps the volume up or down to minimise pops before or after a
327 * DAPM power event */
328 static int dapm_set_pga(struct snd_soc_dapm_widget
*widget
, int power
)
330 const struct snd_kcontrol_new
*k
= widget
->kcontrols
;
332 if (widget
->muted
&& !power
)
334 if (!widget
->muted
&& power
)
337 if (widget
->num_kcontrols
&& k
) {
338 struct soc_mixer_control
*mc
=
339 (struct soc_mixer_control
*)k
->private_value
;
340 unsigned int reg
= mc
->reg
;
341 unsigned int shift
= mc
->shift
;
343 unsigned int mask
= (1 << fls(max
)) - 1;
344 unsigned int invert
= mc
->invert
;
348 /* power up has happended, increase volume to last level */
350 for (i
= max
; i
> widget
->saved_value
; i
--)
351 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
353 for (i
= 0; i
< widget
->saved_value
; i
++)
354 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
358 /* power down is about to occur, decrease volume to mute */
359 int val
= snd_soc_read(widget
->codec
, reg
);
360 int i
= widget
->saved_value
= (val
>> shift
) & mask
;
362 for (; i
< mask
; i
++)
363 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
366 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
374 /* create new dapm mixer control */
375 static int dapm_new_mixer(struct snd_soc_codec
*codec
,
376 struct snd_soc_dapm_widget
*w
)
380 struct snd_soc_dapm_path
*path
;
383 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
386 list_for_each_entry(path
, &w
->sources
, list_sink
) {
388 /* mixer/mux paths name must match control name */
389 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
392 /* add dapm control with long name.
393 * for dapm_mixer this is the concatenation of the
394 * mixer and kcontrol name.
395 * for dapm_mixer_named_ctl this is simply the
398 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
399 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
400 name_len
+= 1 + strlen(w
->name
);
402 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
404 if (path
->long_name
== NULL
)
409 snprintf(path
->long_name
, name_len
, "%s %s",
410 w
->name
, w
->kcontrols
[i
].name
);
412 case snd_soc_dapm_mixer_named_ctl
:
413 snprintf(path
->long_name
, name_len
, "%s",
414 w
->kcontrols
[i
].name
);
418 path
->long_name
[name_len
- 1] = '\0';
420 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
422 ret
= snd_ctl_add(codec
->card
, path
->kcontrol
);
424 printk(KERN_ERR
"asoc: failed to add dapm kcontrol %s: %d\n",
427 kfree(path
->long_name
);
428 path
->long_name
= NULL
;
436 /* create new dapm mux control */
437 static int dapm_new_mux(struct snd_soc_codec
*codec
,
438 struct snd_soc_dapm_widget
*w
)
440 struct snd_soc_dapm_path
*path
= NULL
;
441 struct snd_kcontrol
*kcontrol
;
444 if (!w
->num_kcontrols
) {
445 printk(KERN_ERR
"asoc: mux %s has no controls\n", w
->name
);
449 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
450 ret
= snd_ctl_add(codec
->card
, kcontrol
);
454 list_for_each_entry(path
, &w
->sources
, list_sink
)
455 path
->kcontrol
= kcontrol
;
460 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
464 /* create new dapm volume control */
465 static int dapm_new_pga(struct snd_soc_codec
*codec
,
466 struct snd_soc_dapm_widget
*w
)
468 struct snd_kcontrol
*kcontrol
;
471 if (!w
->num_kcontrols
)
474 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
475 ret
= snd_ctl_add(codec
->card
, kcontrol
);
477 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
484 /* reset 'walked' bit for each dapm path */
485 static inline void dapm_clear_walk(struct snd_soc_codec
*codec
)
487 struct snd_soc_dapm_path
*p
;
489 list_for_each_entry(p
, &codec
->dapm_paths
, list
)
494 * Recursively check for a completed path to an active or physically connected
495 * output widget. Returns number of complete paths.
497 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
499 struct snd_soc_dapm_path
*path
;
502 if (widget
->id
== snd_soc_dapm_supply
)
505 switch (widget
->id
) {
506 case snd_soc_dapm_adc
:
507 case snd_soc_dapm_aif_out
:
514 if (widget
->connected
) {
515 /* connected pin ? */
516 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
519 /* connected jack or spk ? */
520 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
521 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sources
)))
525 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
529 if (path
->sink
&& path
->connect
) {
531 con
+= is_connected_output_ep(path
->sink
);
539 * Recursively check for a completed path to an active or physically connected
540 * input widget. Returns number of complete paths.
542 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
544 struct snd_soc_dapm_path
*path
;
547 if (widget
->id
== snd_soc_dapm_supply
)
550 /* active stream ? */
551 switch (widget
->id
) {
552 case snd_soc_dapm_dac
:
553 case snd_soc_dapm_aif_in
:
560 if (widget
->connected
) {
561 /* connected pin ? */
562 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
565 /* connected VMID/Bias for lower pops */
566 if (widget
->id
== snd_soc_dapm_vmid
)
569 /* connected jack ? */
570 if (widget
->id
== snd_soc_dapm_mic
||
571 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sinks
)))
575 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
579 if (path
->source
&& path
->connect
) {
581 con
+= is_connected_input_ep(path
->source
);
589 * Handler for generic register modifier widget.
591 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
592 struct snd_kcontrol
*kcontrol
, int event
)
596 if (SND_SOC_DAPM_EVENT_ON(event
))
601 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
602 w
->mask
<< w
->shift
, val
<< w
->shift
);
606 EXPORT_SYMBOL_GPL(dapm_reg_event
);
608 /* Standard power change method, used to apply power changes to most
611 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
615 /* call any power change event handlers */
617 pr_debug("power %s event for %s flags %x\n",
618 w
->power
? "on" : "off",
619 w
->name
, w
->event_flags
);
621 /* power up pre event */
622 if (w
->power
&& w
->event
&&
623 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
624 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
629 /* power down pre event */
630 if (!w
->power
&& w
->event
&&
631 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
632 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
637 /* Lower PGA volume to reduce pops */
638 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
639 dapm_set_pga(w
, w
->power
);
643 /* Raise PGA volume to reduce pops */
644 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
645 dapm_set_pga(w
, w
->power
);
647 /* power up post event */
648 if (w
->power
&& w
->event
&&
649 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
651 NULL
, SND_SOC_DAPM_POST_PMU
);
656 /* power down post event */
657 if (!w
->power
&& w
->event
&&
658 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
659 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
667 /* Generic check to see if a widget should be powered.
669 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
673 in
= is_connected_input_ep(w
);
674 dapm_clear_walk(w
->codec
);
675 out
= is_connected_output_ep(w
);
676 dapm_clear_walk(w
->codec
);
677 return out
!= 0 && in
!= 0;
680 /* Check to see if an ADC has power */
681 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
686 in
= is_connected_input_ep(w
);
687 dapm_clear_walk(w
->codec
);
690 return dapm_generic_check_power(w
);
694 /* Check to see if a DAC has power */
695 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
700 out
= is_connected_output_ep(w
);
701 dapm_clear_walk(w
->codec
);
704 return dapm_generic_check_power(w
);
708 /* Check to see if a power supply is needed */
709 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
711 struct snd_soc_dapm_path
*path
;
714 /* Check if one of our outputs is connected */
715 list_for_each_entry(path
, &w
->sinks
, list_source
) {
716 if (path
->connected
&&
717 !path
->connected(path
->source
, path
->sink
))
720 if (path
->sink
&& path
->sink
->power_check
&&
721 path
->sink
->power_check(path
->sink
)) {
727 dapm_clear_walk(w
->codec
);
732 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
733 struct snd_soc_dapm_widget
*b
,
736 if (a
->codec
!= b
->codec
)
737 return (unsigned long)a
- (unsigned long)b
;
738 if (sort
[a
->id
] != sort
[b
->id
])
739 return sort
[a
->id
] - sort
[b
->id
];
740 if (a
->reg
!= b
->reg
)
741 return a
->reg
- b
->reg
;
746 /* Insert a widget in order into a DAPM power sequence. */
747 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
748 struct list_head
*list
,
751 struct snd_soc_dapm_widget
*w
;
753 list_for_each_entry(w
, list
, power_list
)
754 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
755 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
759 list_add_tail(&new_widget
->power_list
, list
);
762 /* Apply the coalesced changes from a DAPM sequence */
763 static void dapm_seq_run_coalesced(struct snd_soc_codec
*codec
,
764 struct list_head
*pending
)
766 struct snd_soc_dapm_widget
*w
;
768 unsigned int value
= 0;
769 unsigned int mask
= 0;
770 unsigned int cur_mask
;
772 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
775 list_for_each_entry(w
, pending
, power_list
) {
776 cur_mask
= 1 << w
->shift
;
777 BUG_ON(reg
!= w
->reg
);
788 pop_dbg(codec
->pop_time
,
789 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
790 w
->name
, reg
, value
, mask
);
792 /* power up pre event */
793 if (w
->power
&& w
->event
&&
794 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
795 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMU\n",
797 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
799 pr_err("%s: pre event failed: %d\n",
803 /* power down pre event */
804 if (!w
->power
&& w
->event
&&
805 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
806 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMD\n",
808 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
810 pr_err("%s: pre event failed: %d\n",
814 /* Lower PGA volume to reduce pops */
815 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
816 dapm_set_pga(w
, w
->power
);
820 pop_dbg(codec
->pop_time
,
821 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
822 value
, mask
, reg
, codec
->pop_time
);
823 pop_wait(codec
->pop_time
);
824 snd_soc_update_bits(codec
, reg
, mask
, value
);
827 list_for_each_entry(w
, pending
, power_list
) {
828 /* Raise PGA volume to reduce pops */
829 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
830 dapm_set_pga(w
, w
->power
);
832 /* power up post event */
833 if (w
->power
&& w
->event
&&
834 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
835 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMU\n",
838 NULL
, SND_SOC_DAPM_POST_PMU
);
840 pr_err("%s: post event failed: %d\n",
844 /* power down post event */
845 if (!w
->power
&& w
->event
&&
846 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
847 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMD\n",
849 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
851 pr_err("%s: post event failed: %d\n",
857 /* Apply a DAPM power sequence.
859 * We walk over a pre-sorted list of widgets to apply power to. In
860 * order to minimise the number of writes to the device required
861 * multiple widgets will be updated in a single write where possible.
862 * Currently anything that requires more than a single write is not
865 static void dapm_seq_run(struct snd_soc_codec
*codec
, struct list_head
*list
,
866 int event
, int sort
[])
868 struct snd_soc_dapm_widget
*w
, *n
;
871 int cur_reg
= SND_SOC_NOPM
;
874 list_for_each_entry_safe(w
, n
, list
, power_list
) {
877 /* Do we need to apply any queued changes? */
878 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
879 if (!list_empty(&pending
))
880 dapm_seq_run_coalesced(codec
, &pending
);
882 INIT_LIST_HEAD(&pending
);
884 cur_reg
= SND_SOC_NOPM
;
888 case snd_soc_dapm_pre
:
890 list_for_each_entry_safe_continue(w
, n
, list
,
893 if (event
== SND_SOC_DAPM_STREAM_START
)
895 NULL
, SND_SOC_DAPM_PRE_PMU
);
896 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
898 NULL
, SND_SOC_DAPM_PRE_PMD
);
901 case snd_soc_dapm_post
:
903 list_for_each_entry_safe_continue(w
, n
, list
,
906 if (event
== SND_SOC_DAPM_STREAM_START
)
908 NULL
, SND_SOC_DAPM_POST_PMU
);
909 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
911 NULL
, SND_SOC_DAPM_POST_PMD
);
914 case snd_soc_dapm_input
:
915 case snd_soc_dapm_output
:
916 case snd_soc_dapm_hp
:
917 case snd_soc_dapm_mic
:
918 case snd_soc_dapm_line
:
919 case snd_soc_dapm_spk
:
920 /* No register support currently */
921 ret
= dapm_generic_apply_power(w
);
925 /* Queue it up for application */
926 cur_sort
= sort
[w
->id
];
928 list_move(&w
->power_list
, &pending
);
933 pr_err("Failed to apply widget power: %d\n",
937 if (!list_empty(&pending
))
938 dapm_seq_run_coalesced(codec
, &pending
);
942 * Scan each dapm widget for complete audio path.
943 * A complete path is a route that has valid endpoints i.e.:-
945 * o DAC to output pin.
946 * o Input Pin to ADC.
947 * o Input pin to Output pin (bypass, sidetone)
948 * o DAC to ADC (loopback).
950 static int dapm_power_widgets(struct snd_soc_codec
*codec
, int event
)
952 struct snd_soc_device
*socdev
= codec
->socdev
;
953 struct snd_soc_dapm_widget
*w
;
955 LIST_HEAD(down_list
);
960 /* Check which widgets we need to power and store them in
961 * lists indicating if they should be powered up or down.
963 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
965 case snd_soc_dapm_pre
:
966 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
968 case snd_soc_dapm_post
:
969 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
976 /* If we're suspending then pull down all the
979 case SND_SOC_DAPM_STREAM_SUSPEND
:
984 power
= w
->power_check(w
);
990 if (w
->power
== power
)
994 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
996 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
1003 /* If there are no DAPM widgets then try to figure out power from the
1006 if (list_empty(&codec
->dapm_widgets
)) {
1008 case SND_SOC_DAPM_STREAM_START
:
1009 case SND_SOC_DAPM_STREAM_RESUME
:
1012 case SND_SOC_DAPM_STREAM_SUSPEND
:
1015 case SND_SOC_DAPM_STREAM_NOP
:
1016 switch (codec
->bias_level
) {
1017 case SND_SOC_BIAS_STANDBY
:
1018 case SND_SOC_BIAS_OFF
:
1031 if (sys_power
&& codec
->bias_level
== SND_SOC_BIAS_OFF
) {
1032 ret
= snd_soc_dapm_set_bias_level(socdev
,
1033 SND_SOC_BIAS_STANDBY
);
1035 pr_err("Failed to turn on bias: %d\n", ret
);
1038 /* If we're changing to all on or all off then prepare */
1039 if ((sys_power
&& codec
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
1040 (!sys_power
&& codec
->bias_level
== SND_SOC_BIAS_ON
)) {
1041 ret
= snd_soc_dapm_set_bias_level(socdev
,
1042 SND_SOC_BIAS_PREPARE
);
1044 pr_err("Failed to prepare bias: %d\n", ret
);
1047 /* Power down widgets first; try to avoid amplifying pops. */
1048 dapm_seq_run(codec
, &down_list
, event
, dapm_down_seq
);
1051 dapm_seq_run(codec
, &up_list
, event
, dapm_up_seq
);
1053 /* If we just powered the last thing off drop to standby bias */
1054 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
1055 ret
= snd_soc_dapm_set_bias_level(socdev
,
1056 SND_SOC_BIAS_STANDBY
);
1058 pr_err("Failed to apply standby bias: %d\n", ret
);
1061 /* If we're in standby and can support bias off then do that */
1062 if (codec
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1063 codec
->idle_bias_off
) {
1064 ret
= snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_OFF
);
1066 pr_err("Failed to turn off bias: %d\n", ret
);
1069 /* If we just powered up then move to active bias */
1070 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
1071 ret
= snd_soc_dapm_set_bias_level(socdev
,
1074 pr_err("Failed to apply active bias: %d\n", ret
);
1077 pop_dbg(codec
->pop_time
, "DAPM sequencing finished, waiting %dms\n",
1083 #ifdef CONFIG_DEBUG_FS
1084 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1086 file
->private_data
= inode
->i_private
;
1090 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1091 char __user
*user_buf
,
1092 size_t count
, loff_t
*ppos
)
1094 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1098 struct snd_soc_dapm_path
*p
= NULL
;
1100 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1104 in
= is_connected_input_ep(w
);
1105 dapm_clear_walk(w
->codec
);
1106 out
= is_connected_output_ep(w
);
1107 dapm_clear_walk(w
->codec
);
1109 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1110 w
->name
, w
->power
? "On" : "Off", in
, out
);
1113 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1114 " - R%d(0x%x) bit %d",
1115 w
->reg
, w
->reg
, w
->shift
);
1117 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1120 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1122 w
->active
? "active" : "inactive");
1124 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1125 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1129 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1131 p
->name
? p
->name
: "static",
1134 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1135 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1139 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1141 p
->name
? p
->name
: "static",
1145 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1151 static const struct file_operations dapm_widget_power_fops
= {
1152 .open
= dapm_widget_power_open_file
,
1153 .read
= dapm_widget_power_read_file
,
1156 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1158 struct snd_soc_dapm_widget
*w
;
1161 if (!codec
->debugfs_dapm
)
1164 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1168 d
= debugfs_create_file(w
->name
, 0444,
1169 codec
->debugfs_dapm
, w
,
1170 &dapm_widget_power_fops
);
1173 "ASoC: Failed to create %s debugfs file\n",
1178 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1183 /* test and update the power status of a mux widget */
1184 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1185 struct snd_kcontrol
*kcontrol
, int change
,
1186 int mux
, struct soc_enum
*e
)
1188 struct snd_soc_dapm_path
*path
;
1191 if (widget
->id
!= snd_soc_dapm_mux
&&
1192 widget
->id
!= snd_soc_dapm_value_mux
)
1198 /* find dapm widget path assoc with kcontrol */
1199 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1200 if (path
->kcontrol
!= kcontrol
)
1203 if (!path
->name
|| !e
->texts
[mux
])
1207 /* we now need to match the string in the enum to the path */
1208 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1209 path
->connect
= 1; /* new connection */
1211 path
->connect
= 0; /* old connection must be powered down */
1215 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1220 /* test and update the power status of a mixer or switch widget */
1221 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1222 struct snd_kcontrol
*kcontrol
, int connect
)
1224 struct snd_soc_dapm_path
*path
;
1227 if (widget
->id
!= snd_soc_dapm_mixer
&&
1228 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1229 widget
->id
!= snd_soc_dapm_switch
)
1232 /* find dapm widget path assoc with kcontrol */
1233 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1234 if (path
->kcontrol
!= kcontrol
)
1237 /* found, now check type */
1239 path
->connect
= connect
;
1244 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1249 /* show dapm widget status in sys fs */
1250 static ssize_t
dapm_widget_show(struct device
*dev
,
1251 struct device_attribute
*attr
, char *buf
)
1253 struct snd_soc_device
*devdata
= dev_get_drvdata(dev
);
1254 struct snd_soc_codec
*codec
= devdata
->card
->codec
;
1255 struct snd_soc_dapm_widget
*w
;
1257 char *state
= "not set";
1259 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1261 /* only display widgets that burnm power */
1263 case snd_soc_dapm_hp
:
1264 case snd_soc_dapm_mic
:
1265 case snd_soc_dapm_spk
:
1266 case snd_soc_dapm_line
:
1267 case snd_soc_dapm_micbias
:
1268 case snd_soc_dapm_dac
:
1269 case snd_soc_dapm_adc
:
1270 case snd_soc_dapm_pga
:
1271 case snd_soc_dapm_mixer
:
1272 case snd_soc_dapm_mixer_named_ctl
:
1273 case snd_soc_dapm_supply
:
1275 count
+= sprintf(buf
+ count
, "%s: %s\n",
1276 w
->name
, w
->power
? "On":"Off");
1283 switch (codec
->bias_level
) {
1284 case SND_SOC_BIAS_ON
:
1287 case SND_SOC_BIAS_PREPARE
:
1290 case SND_SOC_BIAS_STANDBY
:
1293 case SND_SOC_BIAS_OFF
:
1297 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1302 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1304 int snd_soc_dapm_sys_add(struct device
*dev
)
1306 return device_create_file(dev
, &dev_attr_dapm_widget
);
1309 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1311 device_remove_file(dev
, &dev_attr_dapm_widget
);
1314 /* free all dapm widgets and resources */
1315 static void dapm_free_widgets(struct snd_soc_codec
*codec
)
1317 struct snd_soc_dapm_widget
*w
, *next_w
;
1318 struct snd_soc_dapm_path
*p
, *next_p
;
1320 list_for_each_entry_safe(w
, next_w
, &codec
->dapm_widgets
, list
) {
1325 list_for_each_entry_safe(p
, next_p
, &codec
->dapm_paths
, list
) {
1327 kfree(p
->long_name
);
1332 static int snd_soc_dapm_set_pin(struct snd_soc_codec
*codec
,
1333 const char *pin
, int status
)
1335 struct snd_soc_dapm_widget
*w
;
1337 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1338 if (!strcmp(w
->name
, pin
)) {
1339 pr_debug("dapm: %s: pin %s\n", codec
->name
, pin
);
1340 w
->connected
= status
;
1345 pr_err("dapm: %s: configuring unknown pin %s\n", codec
->name
, pin
);
1350 * snd_soc_dapm_sync - scan and power dapm paths
1351 * @codec: audio codec
1353 * Walks all dapm audio paths and powers widgets according to their
1354 * stream or path usage.
1356 * Returns 0 for success.
1358 int snd_soc_dapm_sync(struct snd_soc_codec
*codec
)
1360 return dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1362 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1364 static int snd_soc_dapm_add_route(struct snd_soc_codec
*codec
,
1365 const struct snd_soc_dapm_route
*route
)
1367 struct snd_soc_dapm_path
*path
;
1368 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1369 const char *sink
= route
->sink
;
1370 const char *control
= route
->control
;
1371 const char *source
= route
->source
;
1374 /* find src and dest widgets */
1375 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1377 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1381 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1386 if (wsource
== NULL
|| wsink
== NULL
)
1389 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1393 path
->source
= wsource
;
1395 path
->connected
= route
->connected
;
1396 INIT_LIST_HEAD(&path
->list
);
1397 INIT_LIST_HEAD(&path
->list_source
);
1398 INIT_LIST_HEAD(&path
->list_sink
);
1400 /* check for external widgets */
1401 if (wsink
->id
== snd_soc_dapm_input
) {
1402 if (wsource
->id
== snd_soc_dapm_micbias
||
1403 wsource
->id
== snd_soc_dapm_mic
||
1404 wsource
->id
== snd_soc_dapm_line
||
1405 wsource
->id
== snd_soc_dapm_output
)
1408 if (wsource
->id
== snd_soc_dapm_output
) {
1409 if (wsink
->id
== snd_soc_dapm_spk
||
1410 wsink
->id
== snd_soc_dapm_hp
||
1411 wsink
->id
== snd_soc_dapm_line
||
1412 wsink
->id
== snd_soc_dapm_input
)
1416 /* connect static paths */
1417 if (control
== NULL
) {
1418 list_add(&path
->list
, &codec
->dapm_paths
);
1419 list_add(&path
->list_sink
, &wsink
->sources
);
1420 list_add(&path
->list_source
, &wsource
->sinks
);
1425 /* connect dynamic paths */
1427 case snd_soc_dapm_adc
:
1428 case snd_soc_dapm_dac
:
1429 case snd_soc_dapm_pga
:
1430 case snd_soc_dapm_input
:
1431 case snd_soc_dapm_output
:
1432 case snd_soc_dapm_micbias
:
1433 case snd_soc_dapm_vmid
:
1434 case snd_soc_dapm_pre
:
1435 case snd_soc_dapm_post
:
1436 case snd_soc_dapm_supply
:
1437 case snd_soc_dapm_aif_in
:
1438 case snd_soc_dapm_aif_out
:
1439 list_add(&path
->list
, &codec
->dapm_paths
);
1440 list_add(&path
->list_sink
, &wsink
->sources
);
1441 list_add(&path
->list_source
, &wsource
->sinks
);
1444 case snd_soc_dapm_mux
:
1445 case snd_soc_dapm_value_mux
:
1446 ret
= dapm_connect_mux(codec
, wsource
, wsink
, path
, control
,
1447 &wsink
->kcontrols
[0]);
1451 case snd_soc_dapm_switch
:
1452 case snd_soc_dapm_mixer
:
1453 case snd_soc_dapm_mixer_named_ctl
:
1454 ret
= dapm_connect_mixer(codec
, wsource
, wsink
, path
, control
);
1458 case snd_soc_dapm_hp
:
1459 case snd_soc_dapm_mic
:
1460 case snd_soc_dapm_line
:
1461 case snd_soc_dapm_spk
:
1462 list_add(&path
->list
, &codec
->dapm_paths
);
1463 list_add(&path
->list_sink
, &wsink
->sources
);
1464 list_add(&path
->list_source
, &wsource
->sinks
);
1471 printk(KERN_WARNING
"asoc: no dapm match for %s --> %s --> %s\n", source
,
1478 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1480 * @route: audio routes
1481 * @num: number of routes
1483 * Connects 2 dapm widgets together via a named audio path. The sink is
1484 * the widget receiving the audio signal, whilst the source is the sender
1485 * of the audio signal.
1487 * Returns 0 for success else error. On error all resources can be freed
1488 * with a call to snd_soc_card_free().
1490 int snd_soc_dapm_add_routes(struct snd_soc_codec
*codec
,
1491 const struct snd_soc_dapm_route
*route
, int num
)
1495 for (i
= 0; i
< num
; i
++) {
1496 ret
= snd_soc_dapm_add_route(codec
, route
);
1498 printk(KERN_ERR
"Failed to add route %s->%s\n",
1508 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1511 * snd_soc_dapm_new_widgets - add new dapm widgets
1512 * @codec: audio codec
1514 * Checks the codec for any new dapm widgets and creates them if found.
1516 * Returns 0 for success.
1518 int snd_soc_dapm_new_widgets(struct snd_soc_codec
*codec
)
1520 struct snd_soc_dapm_widget
*w
;
1522 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
1528 case snd_soc_dapm_switch
:
1529 case snd_soc_dapm_mixer
:
1530 case snd_soc_dapm_mixer_named_ctl
:
1531 w
->power_check
= dapm_generic_check_power
;
1532 dapm_new_mixer(codec
, w
);
1534 case snd_soc_dapm_mux
:
1535 case snd_soc_dapm_value_mux
:
1536 w
->power_check
= dapm_generic_check_power
;
1537 dapm_new_mux(codec
, w
);
1539 case snd_soc_dapm_adc
:
1540 case snd_soc_dapm_aif_out
:
1541 w
->power_check
= dapm_adc_check_power
;
1543 case snd_soc_dapm_dac
:
1544 case snd_soc_dapm_aif_in
:
1545 w
->power_check
= dapm_dac_check_power
;
1547 case snd_soc_dapm_pga
:
1548 w
->power_check
= dapm_generic_check_power
;
1549 dapm_new_pga(codec
, w
);
1551 case snd_soc_dapm_input
:
1552 case snd_soc_dapm_output
:
1553 case snd_soc_dapm_micbias
:
1554 case snd_soc_dapm_spk
:
1555 case snd_soc_dapm_hp
:
1556 case snd_soc_dapm_mic
:
1557 case snd_soc_dapm_line
:
1558 w
->power_check
= dapm_generic_check_power
;
1560 case snd_soc_dapm_supply
:
1561 w
->power_check
= dapm_supply_check_power
;
1562 case snd_soc_dapm_vmid
:
1563 case snd_soc_dapm_pre
:
1564 case snd_soc_dapm_post
:
1570 dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1573 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1576 * snd_soc_dapm_get_volsw - dapm mixer get callback
1577 * @kcontrol: mixer control
1578 * @ucontrol: control element information
1580 * Callback to get the value of a dapm mixer control.
1582 * Returns 0 for success.
1584 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1585 struct snd_ctl_elem_value
*ucontrol
)
1587 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1588 struct soc_mixer_control
*mc
=
1589 (struct soc_mixer_control
*)kcontrol
->private_value
;
1590 unsigned int reg
= mc
->reg
;
1591 unsigned int shift
= mc
->shift
;
1592 unsigned int rshift
= mc
->rshift
;
1594 unsigned int invert
= mc
->invert
;
1595 unsigned int mask
= (1 << fls(max
)) - 1;
1597 /* return the saved value if we are powered down */
1598 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1599 ucontrol
->value
.integer
.value
[0] = widget
->saved_value
;
1603 ucontrol
->value
.integer
.value
[0] =
1604 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1605 if (shift
!= rshift
)
1606 ucontrol
->value
.integer
.value
[1] =
1607 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1609 ucontrol
->value
.integer
.value
[0] =
1610 max
- ucontrol
->value
.integer
.value
[0];
1611 if (shift
!= rshift
)
1612 ucontrol
->value
.integer
.value
[1] =
1613 max
- ucontrol
->value
.integer
.value
[1];
1618 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1621 * snd_soc_dapm_put_volsw - dapm mixer set callback
1622 * @kcontrol: mixer control
1623 * @ucontrol: control element information
1625 * Callback to set the value of a dapm mixer control.
1627 * Returns 0 for success.
1629 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1630 struct snd_ctl_elem_value
*ucontrol
)
1632 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1633 struct soc_mixer_control
*mc
=
1634 (struct soc_mixer_control
*)kcontrol
->private_value
;
1635 unsigned int reg
= mc
->reg
;
1636 unsigned int shift
= mc
->shift
;
1637 unsigned int rshift
= mc
->rshift
;
1639 unsigned int mask
= (1 << fls(max
)) - 1;
1640 unsigned int invert
= mc
->invert
;
1641 unsigned int val
, val2
, val_mask
;
1645 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1649 val_mask
= mask
<< shift
;
1651 if (shift
!= rshift
) {
1652 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1655 val_mask
|= mask
<< rshift
;
1656 val
|= val2
<< rshift
;
1659 mutex_lock(&widget
->codec
->mutex
);
1660 widget
->value
= val
;
1662 /* save volume value if the widget is powered down */
1663 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1664 widget
->saved_value
= val
;
1665 mutex_unlock(&widget
->codec
->mutex
);
1669 if (snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
)) {
1671 /* new connection */
1672 connect
= invert
? 0:1;
1674 /* old connection must be powered down */
1675 connect
= invert
? 1:0;
1677 dapm_mixer_update_power(widget
, kcontrol
, connect
);
1680 if (widget
->event
) {
1681 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1682 ret
= widget
->event(widget
, kcontrol
,
1683 SND_SOC_DAPM_PRE_REG
);
1689 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1690 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1691 ret
= widget
->event(widget
, kcontrol
,
1692 SND_SOC_DAPM_POST_REG
);
1694 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1697 mutex_unlock(&widget
->codec
->mutex
);
1700 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1703 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1704 * @kcontrol: mixer control
1705 * @ucontrol: control element information
1707 * Callback to get the value of a dapm enumerated double mixer control.
1709 * Returns 0 for success.
1711 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1712 struct snd_ctl_elem_value
*ucontrol
)
1714 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1715 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1716 unsigned int val
, bitmask
;
1718 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1720 val
= snd_soc_read(widget
->codec
, e
->reg
);
1721 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1722 if (e
->shift_l
!= e
->shift_r
)
1723 ucontrol
->value
.enumerated
.item
[1] =
1724 (val
>> e
->shift_r
) & (bitmask
- 1);
1728 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1731 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1732 * @kcontrol: mixer control
1733 * @ucontrol: control element information
1735 * Callback to set the value of a dapm enumerated double mixer control.
1737 * Returns 0 for success.
1739 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1740 struct snd_ctl_elem_value
*ucontrol
)
1742 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1743 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1744 unsigned int val
, mux
, change
;
1745 unsigned int mask
, bitmask
;
1748 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1750 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1752 mux
= ucontrol
->value
.enumerated
.item
[0];
1753 val
= mux
<< e
->shift_l
;
1754 mask
= (bitmask
- 1) << e
->shift_l
;
1755 if (e
->shift_l
!= e
->shift_r
) {
1756 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1758 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1759 mask
|= (bitmask
- 1) << e
->shift_r
;
1762 mutex_lock(&widget
->codec
->mutex
);
1763 widget
->value
= val
;
1764 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1765 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1767 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1768 ret
= widget
->event(widget
,
1769 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1774 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1776 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1777 ret
= widget
->event(widget
,
1778 kcontrol
, SND_SOC_DAPM_POST_REG
);
1781 mutex_unlock(&widget
->codec
->mutex
);
1784 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1787 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1788 * @kcontrol: mixer control
1789 * @ucontrol: control element information
1791 * Returns 0 for success.
1793 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
1794 struct snd_ctl_elem_value
*ucontrol
)
1796 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1798 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
1802 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
1805 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1806 * @kcontrol: mixer control
1807 * @ucontrol: control element information
1809 * Returns 0 for success.
1811 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
1812 struct snd_ctl_elem_value
*ucontrol
)
1814 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1815 struct soc_enum
*e
=
1816 (struct soc_enum
*)kcontrol
->private_value
;
1820 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
1823 mutex_lock(&widget
->codec
->mutex
);
1825 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
1826 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
1827 dapm_mux_update_power(widget
, kcontrol
, change
, widget
->value
, e
);
1829 mutex_unlock(&widget
->codec
->mutex
);
1832 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
1835 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1837 * @kcontrol: mixer control
1838 * @ucontrol: control element information
1840 * Callback to get the value of a dapm semi enumerated double mixer control.
1842 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1843 * used for handling bitfield coded enumeration for example.
1845 * Returns 0 for success.
1847 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1848 struct snd_ctl_elem_value
*ucontrol
)
1850 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1851 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1852 unsigned int reg_val
, val
, mux
;
1854 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1855 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1856 for (mux
= 0; mux
< e
->max
; mux
++) {
1857 if (val
== e
->values
[mux
])
1860 ucontrol
->value
.enumerated
.item
[0] = mux
;
1861 if (e
->shift_l
!= e
->shift_r
) {
1862 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1863 for (mux
= 0; mux
< e
->max
; mux
++) {
1864 if (val
== e
->values
[mux
])
1867 ucontrol
->value
.enumerated
.item
[1] = mux
;
1872 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1875 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1877 * @kcontrol: mixer control
1878 * @ucontrol: control element information
1880 * Callback to set the value of a dapm semi enumerated double mixer control.
1882 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1883 * used for handling bitfield coded enumeration for example.
1885 * Returns 0 for success.
1887 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
1888 struct snd_ctl_elem_value
*ucontrol
)
1890 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1891 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1892 unsigned int val
, mux
, change
;
1896 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1898 mux
= ucontrol
->value
.enumerated
.item
[0];
1899 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1900 mask
= e
->mask
<< e
->shift_l
;
1901 if (e
->shift_l
!= e
->shift_r
) {
1902 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1904 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1905 mask
|= e
->mask
<< e
->shift_r
;
1908 mutex_lock(&widget
->codec
->mutex
);
1909 widget
->value
= val
;
1910 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1911 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1913 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1914 ret
= widget
->event(widget
,
1915 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1920 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1922 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1923 ret
= widget
->event(widget
,
1924 kcontrol
, SND_SOC_DAPM_POST_REG
);
1927 mutex_unlock(&widget
->codec
->mutex
);
1930 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1933 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1935 * @kcontrol: mixer control
1936 * @uinfo: control element information
1938 * Callback to provide information about a pin switch control.
1940 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1941 struct snd_ctl_elem_info
*uinfo
)
1943 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1945 uinfo
->value
.integer
.min
= 0;
1946 uinfo
->value
.integer
.max
= 1;
1950 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1953 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1955 * @kcontrol: mixer control
1958 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1959 struct snd_ctl_elem_value
*ucontrol
)
1961 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1962 const char *pin
= (const char *)kcontrol
->private_value
;
1964 mutex_lock(&codec
->mutex
);
1966 ucontrol
->value
.integer
.value
[0] =
1967 snd_soc_dapm_get_pin_status(codec
, pin
);
1969 mutex_unlock(&codec
->mutex
);
1973 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
1976 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1978 * @kcontrol: mixer control
1981 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
1982 struct snd_ctl_elem_value
*ucontrol
)
1984 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1985 const char *pin
= (const char *)kcontrol
->private_value
;
1987 mutex_lock(&codec
->mutex
);
1989 if (ucontrol
->value
.integer
.value
[0])
1990 snd_soc_dapm_enable_pin(codec
, pin
);
1992 snd_soc_dapm_disable_pin(codec
, pin
);
1994 snd_soc_dapm_sync(codec
);
1996 mutex_unlock(&codec
->mutex
);
2000 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
2003 * snd_soc_dapm_new_control - create new dapm control
2004 * @codec: audio codec
2005 * @widget: widget template
2007 * Creates a new dapm control based upon the template.
2009 * Returns 0 for success else error.
2011 int snd_soc_dapm_new_control(struct snd_soc_codec
*codec
,
2012 const struct snd_soc_dapm_widget
*widget
)
2014 struct snd_soc_dapm_widget
*w
;
2016 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
2020 INIT_LIST_HEAD(&w
->sources
);
2021 INIT_LIST_HEAD(&w
->sinks
);
2022 INIT_LIST_HEAD(&w
->list
);
2023 list_add(&w
->list
, &codec
->dapm_widgets
);
2025 /* machine layer set ups unconnected pins and insertions */
2029 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
2032 * snd_soc_dapm_new_controls - create new dapm controls
2033 * @codec: audio codec
2034 * @widget: widget array
2035 * @num: number of widgets
2037 * Creates new DAPM controls based upon the templates.
2039 * Returns 0 for success else error.
2041 int snd_soc_dapm_new_controls(struct snd_soc_codec
*codec
,
2042 const struct snd_soc_dapm_widget
*widget
,
2047 for (i
= 0; i
< num
; i
++) {
2048 ret
= snd_soc_dapm_new_control(codec
, widget
);
2051 "ASoC: Failed to create DAPM control %s: %d\n",
2059 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2063 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2064 * @codec: audio codec
2065 * @stream: stream name
2066 * @event: stream event
2068 * Sends a stream event to the dapm core. The core then makes any
2069 * necessary widget power changes.
2071 * Returns 0 for success else error.
2073 int snd_soc_dapm_stream_event(struct snd_soc_codec
*codec
,
2074 char *stream
, int event
)
2076 struct snd_soc_dapm_widget
*w
;
2081 mutex_lock(&codec
->mutex
);
2082 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
2086 pr_debug("widget %s\n %s stream %s event %d\n",
2087 w
->name
, w
->sname
, stream
, event
);
2088 if (strstr(w
->sname
, stream
)) {
2090 case SND_SOC_DAPM_STREAM_START
:
2093 case SND_SOC_DAPM_STREAM_STOP
:
2096 case SND_SOC_DAPM_STREAM_SUSPEND
:
2101 case SND_SOC_DAPM_STREAM_RESUME
:
2107 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2109 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2115 dapm_power_widgets(codec
, event
);
2116 mutex_unlock(&codec
->mutex
);
2119 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
2122 * snd_soc_dapm_enable_pin - enable pin.
2126 * Enables input/output pin and its parents or children widgets iff there is
2127 * a valid audio route and active audio stream.
2128 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2129 * do any widget power switching.
2131 int snd_soc_dapm_enable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2133 return snd_soc_dapm_set_pin(codec
, pin
, 1);
2135 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2138 * snd_soc_dapm_disable_pin - disable pin.
2142 * Disables input/output pin and its parents or children widgets.
2143 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2144 * do any widget power switching.
2146 int snd_soc_dapm_disable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2148 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2150 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2153 * snd_soc_dapm_nc_pin - permanently disable pin.
2157 * Marks the specified pin as being not connected, disabling it along
2158 * any parent or child widgets. At present this is identical to
2159 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2160 * additional things such as disabling controls which only affect
2161 * paths through the pin.
2163 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2164 * do any widget power switching.
2166 int snd_soc_dapm_nc_pin(struct snd_soc_codec
*codec
, const char *pin
)
2168 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2170 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2173 * snd_soc_dapm_get_pin_status - get audio pin status
2174 * @codec: audio codec
2175 * @pin: audio signal pin endpoint (or start point)
2177 * Get audio pin status - connected or disconnected.
2179 * Returns 1 for connected otherwise 0.
2181 int snd_soc_dapm_get_pin_status(struct snd_soc_codec
*codec
, const char *pin
)
2183 struct snd_soc_dapm_widget
*w
;
2185 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2186 if (!strcmp(w
->name
, pin
))
2187 return w
->connected
;
2192 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2195 * snd_soc_dapm_free - free dapm resources
2196 * @socdev: SoC device
2198 * Free all dapm widgets and resources.
2200 void snd_soc_dapm_free(struct snd_soc_device
*socdev
)
2202 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2204 snd_soc_dapm_sys_remove(socdev
->dev
);
2205 dapm_free_widgets(codec
);
2207 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2210 * snd_soc_dapm_shutdown - callback for system shutdown
2212 void snd_soc_dapm_shutdown(struct snd_soc_device
*socdev
)
2214 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2215 struct snd_soc_dapm_widget
*w
;
2216 LIST_HEAD(down_list
);
2219 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2221 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2227 /* If there were no widgets to power down we're already in
2231 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_PREPARE
);
2232 dapm_seq_run(codec
, &down_list
, 0, dapm_down_seq
);
2233 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_STANDBY
);
2236 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_OFF
);
2239 /* Module information */
2240 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2241 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2242 MODULE_LICENSE("GPL");