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>
47 /* dapm power sequences - make this per codec in the future */
48 static int dapm_up_seq
[] = {
49 [snd_soc_dapm_pre
] = 0,
50 [snd_soc_dapm_supply
] = 1,
51 [snd_soc_dapm_micbias
] = 2,
52 [snd_soc_dapm_aif_in
] = 3,
53 [snd_soc_dapm_aif_out
] = 3,
54 [snd_soc_dapm_mic
] = 4,
55 [snd_soc_dapm_mux
] = 5,
56 [snd_soc_dapm_value_mux
] = 5,
57 [snd_soc_dapm_dac
] = 6,
58 [snd_soc_dapm_mixer
] = 7,
59 [snd_soc_dapm_mixer_named_ctl
] = 7,
60 [snd_soc_dapm_pga
] = 8,
61 [snd_soc_dapm_adc
] = 9,
62 [snd_soc_dapm_hp
] = 10,
63 [snd_soc_dapm_spk
] = 10,
64 [snd_soc_dapm_post
] = 11,
67 static int dapm_down_seq
[] = {
68 [snd_soc_dapm_pre
] = 0,
69 [snd_soc_dapm_adc
] = 1,
70 [snd_soc_dapm_hp
] = 2,
71 [snd_soc_dapm_spk
] = 2,
72 [snd_soc_dapm_pga
] = 4,
73 [snd_soc_dapm_mixer_named_ctl
] = 5,
74 [snd_soc_dapm_mixer
] = 5,
75 [snd_soc_dapm_dac
] = 6,
76 [snd_soc_dapm_mic
] = 7,
77 [snd_soc_dapm_micbias
] = 8,
78 [snd_soc_dapm_mux
] = 9,
79 [snd_soc_dapm_value_mux
] = 9,
80 [snd_soc_dapm_aif_in
] = 10,
81 [snd_soc_dapm_aif_out
] = 10,
82 [snd_soc_dapm_supply
] = 11,
83 [snd_soc_dapm_post
] = 12,
86 static void pop_wait(u32 pop_time
)
89 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
92 static void pop_dbg(u32 pop_time
, const char *fmt
, ...)
106 /* create a new dapm widget */
107 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
108 const struct snd_soc_dapm_widget
*_widget
)
110 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
114 * snd_soc_dapm_set_bias_level - set the bias level for the system
115 * @socdev: audio device
116 * @level: level to configure
118 * Configure the bias (power) levels for the SoC audio device.
120 * Returns 0 for success else error.
122 static int snd_soc_dapm_set_bias_level(struct snd_soc_device
*socdev
,
123 enum snd_soc_bias_level level
)
125 struct snd_soc_card
*card
= socdev
->card
;
126 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
130 case SND_SOC_BIAS_ON
:
131 dev_dbg(socdev
->dev
, "Setting full bias\n");
133 case SND_SOC_BIAS_PREPARE
:
134 dev_dbg(socdev
->dev
, "Setting bias prepare\n");
136 case SND_SOC_BIAS_STANDBY
:
137 dev_dbg(socdev
->dev
, "Setting standby bias\n");
139 case SND_SOC_BIAS_OFF
:
140 dev_dbg(socdev
->dev
, "Setting bias off\n");
143 dev_err(socdev
->dev
, "Setting invalid bias %d\n", level
);
147 if (card
->set_bias_level
)
148 ret
= card
->set_bias_level(card
, level
);
150 if (codec
->set_bias_level
)
151 ret
= codec
->set_bias_level(codec
, level
);
153 codec
->bias_level
= level
;
159 /* set up initial codec paths */
160 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
161 struct snd_soc_dapm_path
*p
, int i
)
164 case snd_soc_dapm_switch
:
165 case snd_soc_dapm_mixer
:
166 case snd_soc_dapm_mixer_named_ctl
: {
168 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
169 w
->kcontrols
[i
].private_value
;
170 unsigned int reg
= mc
->reg
;
171 unsigned int shift
= mc
->shift
;
173 unsigned int mask
= (1 << fls(max
)) - 1;
174 unsigned int invert
= mc
->invert
;
176 val
= snd_soc_read(w
->codec
, reg
);
177 val
= (val
>> shift
) & mask
;
179 if ((invert
&& !val
) || (!invert
&& val
))
185 case snd_soc_dapm_mux
: {
186 struct soc_enum
*e
= (struct soc_enum
*)w
->kcontrols
[i
].private_value
;
187 int val
, item
, bitmask
;
189 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
191 val
= snd_soc_read(w
->codec
, e
->reg
);
192 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
195 for (i
= 0; i
< e
->max
; i
++) {
196 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
201 case snd_soc_dapm_value_mux
: {
202 struct soc_enum
*e
= (struct soc_enum
*)
203 w
->kcontrols
[i
].private_value
;
206 val
= snd_soc_read(w
->codec
, e
->reg
);
207 val
= (val
>> e
->shift_l
) & e
->mask
;
208 for (item
= 0; item
< e
->max
; item
++) {
209 if (val
== e
->values
[item
])
214 for (i
= 0; i
< e
->max
; i
++) {
215 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
220 /* does not effect routing - always connected */
221 case snd_soc_dapm_pga
:
222 case snd_soc_dapm_output
:
223 case snd_soc_dapm_adc
:
224 case snd_soc_dapm_input
:
225 case snd_soc_dapm_dac
:
226 case snd_soc_dapm_micbias
:
227 case snd_soc_dapm_vmid
:
228 case snd_soc_dapm_supply
:
229 case snd_soc_dapm_aif_in
:
230 case snd_soc_dapm_aif_out
:
233 /* does effect routing - dynamically connected */
234 case snd_soc_dapm_hp
:
235 case snd_soc_dapm_mic
:
236 case snd_soc_dapm_spk
:
237 case snd_soc_dapm_line
:
238 case snd_soc_dapm_pre
:
239 case snd_soc_dapm_post
:
245 /* connect mux widget to its interconnecting audio paths */
246 static int dapm_connect_mux(struct snd_soc_codec
*codec
,
247 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
248 struct snd_soc_dapm_path
*path
, const char *control_name
,
249 const struct snd_kcontrol_new
*kcontrol
)
251 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
254 for (i
= 0; i
< e
->max
; i
++) {
255 if (!(strcmp(control_name
, e
->texts
[i
]))) {
256 list_add(&path
->list
, &codec
->dapm_paths
);
257 list_add(&path
->list_sink
, &dest
->sources
);
258 list_add(&path
->list_source
, &src
->sinks
);
259 path
->name
= (char*)e
->texts
[i
];
260 dapm_set_path_status(dest
, path
, 0);
268 /* connect mixer widget to its interconnecting audio paths */
269 static int dapm_connect_mixer(struct snd_soc_codec
*codec
,
270 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
271 struct snd_soc_dapm_path
*path
, const char *control_name
)
275 /* search for mixer kcontrol */
276 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
277 if (!strcmp(control_name
, dest
->kcontrols
[i
].name
)) {
278 list_add(&path
->list
, &codec
->dapm_paths
);
279 list_add(&path
->list_sink
, &dest
->sources
);
280 list_add(&path
->list_source
, &src
->sinks
);
281 path
->name
= dest
->kcontrols
[i
].name
;
282 dapm_set_path_status(dest
, path
, i
);
289 /* update dapm codec register bits */
290 static int dapm_update_bits(struct snd_soc_dapm_widget
*widget
)
293 unsigned int old
, new;
294 struct snd_soc_codec
*codec
= widget
->codec
;
296 /* check for valid widgets */
297 if (widget
->reg
< 0 || widget
->id
== snd_soc_dapm_input
||
298 widget
->id
== snd_soc_dapm_output
||
299 widget
->id
== snd_soc_dapm_hp
||
300 widget
->id
== snd_soc_dapm_mic
||
301 widget
->id
== snd_soc_dapm_line
||
302 widget
->id
== snd_soc_dapm_spk
)
305 power
= widget
->power
;
307 power
= (power
? 0:1);
309 old
= snd_soc_read(codec
, widget
->reg
);
310 new = (old
& ~(0x1 << widget
->shift
)) | (power
<< widget
->shift
);
314 pop_dbg(codec
->pop_time
, "pop test %s : %s in %d ms\n",
315 widget
->name
, widget
->power
? "on" : "off",
317 snd_soc_write(codec
, widget
->reg
, new);
318 pop_wait(codec
->pop_time
);
320 pr_debug("reg %x old %x new %x change %d\n", widget
->reg
,
325 /* ramps the volume up or down to minimise pops before or after a
326 * DAPM power event */
327 static int dapm_set_pga(struct snd_soc_dapm_widget
*widget
, int power
)
329 const struct snd_kcontrol_new
*k
= widget
->kcontrols
;
331 if (widget
->muted
&& !power
)
333 if (!widget
->muted
&& power
)
336 if (widget
->num_kcontrols
&& k
) {
337 struct soc_mixer_control
*mc
=
338 (struct soc_mixer_control
*)k
->private_value
;
339 unsigned int reg
= mc
->reg
;
340 unsigned int shift
= mc
->shift
;
342 unsigned int mask
= (1 << fls(max
)) - 1;
343 unsigned int invert
= mc
->invert
;
347 /* power up has happended, increase volume to last level */
349 for (i
= max
; i
> widget
->saved_value
; i
--)
350 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
352 for (i
= 0; i
< widget
->saved_value
; i
++)
353 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
357 /* power down is about to occur, decrease volume to mute */
358 int val
= snd_soc_read(widget
->codec
, reg
);
359 int i
= widget
->saved_value
= (val
>> shift
) & mask
;
361 for (; i
< mask
; i
++)
362 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
365 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
373 /* create new dapm mixer control */
374 static int dapm_new_mixer(struct snd_soc_codec
*codec
,
375 struct snd_soc_dapm_widget
*w
)
379 struct snd_soc_dapm_path
*path
;
382 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
385 list_for_each_entry(path
, &w
->sources
, list_sink
) {
387 /* mixer/mux paths name must match control name */
388 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
391 /* add dapm control with long name.
392 * for dapm_mixer this is the concatenation of the
393 * mixer and kcontrol name.
394 * for dapm_mixer_named_ctl this is simply the
397 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
398 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
399 name_len
+= 1 + strlen(w
->name
);
401 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
403 if (path
->long_name
== NULL
)
408 snprintf(path
->long_name
, name_len
, "%s %s",
409 w
->name
, w
->kcontrols
[i
].name
);
411 case snd_soc_dapm_mixer_named_ctl
:
412 snprintf(path
->long_name
, name_len
, "%s",
413 w
->kcontrols
[i
].name
);
417 path
->long_name
[name_len
- 1] = '\0';
419 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
421 ret
= snd_ctl_add(codec
->card
, path
->kcontrol
);
423 printk(KERN_ERR
"asoc: failed to add dapm kcontrol %s: %d\n",
426 kfree(path
->long_name
);
427 path
->long_name
= NULL
;
435 /* create new dapm mux control */
436 static int dapm_new_mux(struct snd_soc_codec
*codec
,
437 struct snd_soc_dapm_widget
*w
)
439 struct snd_soc_dapm_path
*path
= NULL
;
440 struct snd_kcontrol
*kcontrol
;
443 if (!w
->num_kcontrols
) {
444 printk(KERN_ERR
"asoc: mux %s has no controls\n", w
->name
);
448 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
449 ret
= snd_ctl_add(codec
->card
, kcontrol
);
453 list_for_each_entry(path
, &w
->sources
, list_sink
)
454 path
->kcontrol
= kcontrol
;
459 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
463 /* create new dapm volume control */
464 static int dapm_new_pga(struct snd_soc_codec
*codec
,
465 struct snd_soc_dapm_widget
*w
)
467 struct snd_kcontrol
*kcontrol
;
470 if (!w
->num_kcontrols
)
473 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
474 ret
= snd_ctl_add(codec
->card
, kcontrol
);
476 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
483 /* reset 'walked' bit for each dapm path */
484 static inline void dapm_clear_walk(struct snd_soc_codec
*codec
)
486 struct snd_soc_dapm_path
*p
;
488 list_for_each_entry(p
, &codec
->dapm_paths
, list
)
493 * Recursively check for a completed path to an active or physically connected
494 * output widget. Returns number of complete paths.
496 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
498 struct snd_soc_dapm_path
*path
;
501 if (widget
->id
== snd_soc_dapm_supply
)
504 switch (widget
->id
) {
505 case snd_soc_dapm_adc
:
506 case snd_soc_dapm_aif_out
:
513 if (widget
->connected
) {
514 /* connected pin ? */
515 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
518 /* connected jack or spk ? */
519 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
520 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sources
)))
524 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
528 if (path
->sink
&& path
->connect
) {
530 con
+= is_connected_output_ep(path
->sink
);
538 * Recursively check for a completed path to an active or physically connected
539 * input widget. Returns number of complete paths.
541 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
543 struct snd_soc_dapm_path
*path
;
546 if (widget
->id
== snd_soc_dapm_supply
)
549 /* active stream ? */
550 switch (widget
->id
) {
551 case snd_soc_dapm_dac
:
552 case snd_soc_dapm_aif_in
:
559 if (widget
->connected
) {
560 /* connected pin ? */
561 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
564 /* connected VMID/Bias for lower pops */
565 if (widget
->id
== snd_soc_dapm_vmid
)
568 /* connected jack ? */
569 if (widget
->id
== snd_soc_dapm_mic
||
570 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sinks
)))
574 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
578 if (path
->source
&& path
->connect
) {
580 con
+= is_connected_input_ep(path
->source
);
588 * Handler for generic register modifier widget.
590 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
591 struct snd_kcontrol
*kcontrol
, int event
)
595 if (SND_SOC_DAPM_EVENT_ON(event
))
600 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
601 w
->mask
<< w
->shift
, val
<< w
->shift
);
605 EXPORT_SYMBOL_GPL(dapm_reg_event
);
607 /* Standard power change method, used to apply power changes to most
610 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
614 /* call any power change event handlers */
616 pr_debug("power %s event for %s flags %x\n",
617 w
->power
? "on" : "off",
618 w
->name
, w
->event_flags
);
620 /* power up pre event */
621 if (w
->power
&& w
->event
&&
622 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
623 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
628 /* power down pre event */
629 if (!w
->power
&& w
->event
&&
630 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
631 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
636 /* Lower PGA volume to reduce pops */
637 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
638 dapm_set_pga(w
, w
->power
);
642 /* Raise PGA volume to reduce pops */
643 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
644 dapm_set_pga(w
, w
->power
);
646 /* power up post event */
647 if (w
->power
&& w
->event
&&
648 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
650 NULL
, SND_SOC_DAPM_POST_PMU
);
655 /* power down post event */
656 if (!w
->power
&& w
->event
&&
657 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
658 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
666 /* Generic check to see if a widget should be powered.
668 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
672 in
= is_connected_input_ep(w
);
673 dapm_clear_walk(w
->codec
);
674 out
= is_connected_output_ep(w
);
675 dapm_clear_walk(w
->codec
);
676 return out
!= 0 && in
!= 0;
679 /* Check to see if an ADC has power */
680 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
685 in
= is_connected_input_ep(w
);
686 dapm_clear_walk(w
->codec
);
689 return dapm_generic_check_power(w
);
693 /* Check to see if a DAC has power */
694 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
699 out
= is_connected_output_ep(w
);
700 dapm_clear_walk(w
->codec
);
703 return dapm_generic_check_power(w
);
707 /* Check to see if a power supply is needed */
708 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
710 struct snd_soc_dapm_path
*path
;
713 /* Check if one of our outputs is connected */
714 list_for_each_entry(path
, &w
->sinks
, list_source
) {
715 if (path
->connected
&&
716 !path
->connected(path
->source
, path
->sink
))
719 if (path
->sink
&& path
->sink
->power_check
&&
720 path
->sink
->power_check(path
->sink
)) {
726 dapm_clear_walk(w
->codec
);
731 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
732 struct snd_soc_dapm_widget
*b
,
735 if (a
->codec
!= b
->codec
)
736 return (unsigned long)a
- (unsigned long)b
;
737 if (sort
[a
->id
] != sort
[b
->id
])
738 return sort
[a
->id
] - sort
[b
->id
];
739 if (a
->reg
!= b
->reg
)
740 return a
->reg
- b
->reg
;
745 /* Insert a widget in order into a DAPM power sequence. */
746 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
747 struct list_head
*list
,
750 struct snd_soc_dapm_widget
*w
;
752 list_for_each_entry(w
, list
, power_list
)
753 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
754 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
758 list_add_tail(&new_widget
->power_list
, list
);
761 /* Apply the coalesced changes from a DAPM sequence */
762 static void dapm_seq_run_coalesced(struct snd_soc_codec
*codec
,
763 struct list_head
*pending
)
765 struct snd_soc_dapm_widget
*w
;
767 unsigned int value
= 0;
768 unsigned int mask
= 0;
769 unsigned int cur_mask
;
771 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
774 list_for_each_entry(w
, pending
, power_list
) {
775 cur_mask
= 1 << w
->shift
;
776 BUG_ON(reg
!= w
->reg
);
787 pop_dbg(codec
->pop_time
,
788 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
789 w
->name
, reg
, value
, mask
);
791 /* power up pre event */
792 if (w
->power
&& w
->event
&&
793 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
794 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMU\n",
796 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
798 pr_err("%s: pre event failed: %d\n",
802 /* power down pre event */
803 if (!w
->power
&& w
->event
&&
804 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
805 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMD\n",
807 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
809 pr_err("%s: pre event failed: %d\n",
813 /* Lower PGA volume to reduce pops */
814 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
815 dapm_set_pga(w
, w
->power
);
819 pop_dbg(codec
->pop_time
,
820 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
821 value
, mask
, reg
, codec
->pop_time
);
822 pop_wait(codec
->pop_time
);
823 snd_soc_update_bits(codec
, reg
, mask
, value
);
826 list_for_each_entry(w
, pending
, power_list
) {
827 /* Raise PGA volume to reduce pops */
828 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
829 dapm_set_pga(w
, w
->power
);
831 /* power up post event */
832 if (w
->power
&& w
->event
&&
833 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
834 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMU\n",
837 NULL
, SND_SOC_DAPM_POST_PMU
);
839 pr_err("%s: post event failed: %d\n",
843 /* power down post event */
844 if (!w
->power
&& w
->event
&&
845 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
846 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMD\n",
848 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
850 pr_err("%s: post event failed: %d\n",
856 /* Apply a DAPM power sequence.
858 * We walk over a pre-sorted list of widgets to apply power to. In
859 * order to minimise the number of writes to the device required
860 * multiple widgets will be updated in a single write where possible.
861 * Currently anything that requires more than a single write is not
864 static void dapm_seq_run(struct snd_soc_codec
*codec
, struct list_head
*list
,
865 int event
, int sort
[])
867 struct snd_soc_dapm_widget
*w
, *n
;
870 int cur_reg
= SND_SOC_NOPM
;
873 list_for_each_entry_safe(w
, n
, list
, power_list
) {
876 /* Do we need to apply any queued changes? */
877 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
878 if (!list_empty(&pending
))
879 dapm_seq_run_coalesced(codec
, &pending
);
881 INIT_LIST_HEAD(&pending
);
883 cur_reg
= SND_SOC_NOPM
;
887 case snd_soc_dapm_pre
:
889 list_for_each_entry_safe_continue(w
, n
, list
,
892 if (event
== SND_SOC_DAPM_STREAM_START
)
894 NULL
, SND_SOC_DAPM_PRE_PMU
);
895 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
897 NULL
, SND_SOC_DAPM_PRE_PMD
);
900 case snd_soc_dapm_post
:
902 list_for_each_entry_safe_continue(w
, n
, list
,
905 if (event
== SND_SOC_DAPM_STREAM_START
)
907 NULL
, SND_SOC_DAPM_POST_PMU
);
908 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
910 NULL
, SND_SOC_DAPM_POST_PMD
);
913 case snd_soc_dapm_input
:
914 case snd_soc_dapm_output
:
915 case snd_soc_dapm_hp
:
916 case snd_soc_dapm_mic
:
917 case snd_soc_dapm_line
:
918 case snd_soc_dapm_spk
:
919 /* No register support currently */
920 ret
= dapm_generic_apply_power(w
);
924 /* Queue it up for application */
925 cur_sort
= sort
[w
->id
];
927 list_move(&w
->power_list
, &pending
);
932 pr_err("Failed to apply widget power: %d\n",
936 if (!list_empty(&pending
))
937 dapm_seq_run_coalesced(codec
, &pending
);
941 * Scan each dapm widget for complete audio path.
942 * A complete path is a route that has valid endpoints i.e.:-
944 * o DAC to output pin.
945 * o Input Pin to ADC.
946 * o Input pin to Output pin (bypass, sidetone)
947 * o DAC to ADC (loopback).
949 static int dapm_power_widgets(struct snd_soc_codec
*codec
, int event
)
951 struct snd_soc_device
*socdev
= codec
->socdev
;
952 struct snd_soc_dapm_widget
*w
;
954 LIST_HEAD(down_list
);
959 /* Check which widgets we need to power and store them in
960 * lists indicating if they should be powered up or down.
962 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
964 case snd_soc_dapm_pre
:
965 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
967 case snd_soc_dapm_post
:
968 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
975 /* If we're suspending then pull down all the
978 case SND_SOC_DAPM_STREAM_SUSPEND
:
983 power
= w
->power_check(w
);
989 if (w
->power
== power
)
993 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
995 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
1002 /* If there are no DAPM widgets then try to figure out power from the
1005 if (list_empty(&codec
->dapm_widgets
)) {
1007 case SND_SOC_DAPM_STREAM_START
:
1008 case SND_SOC_DAPM_STREAM_RESUME
:
1011 case SND_SOC_DAPM_STREAM_SUSPEND
:
1014 case SND_SOC_DAPM_STREAM_NOP
:
1015 switch (codec
->bias_level
) {
1016 case SND_SOC_BIAS_STANDBY
:
1017 case SND_SOC_BIAS_OFF
:
1030 if (sys_power
&& codec
->bias_level
== SND_SOC_BIAS_OFF
) {
1031 ret
= snd_soc_dapm_set_bias_level(socdev
,
1032 SND_SOC_BIAS_STANDBY
);
1034 pr_err("Failed to turn on bias: %d\n", ret
);
1037 /* If we're changing to all on or all off then prepare */
1038 if ((sys_power
&& codec
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
1039 (!sys_power
&& codec
->bias_level
== SND_SOC_BIAS_ON
)) {
1040 ret
= snd_soc_dapm_set_bias_level(socdev
,
1041 SND_SOC_BIAS_PREPARE
);
1043 pr_err("Failed to prepare bias: %d\n", ret
);
1046 /* Power down widgets first; try to avoid amplifying pops. */
1047 dapm_seq_run(codec
, &down_list
, event
, dapm_down_seq
);
1050 dapm_seq_run(codec
, &up_list
, event
, dapm_up_seq
);
1052 /* If we just powered the last thing off drop to standby bias */
1053 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
1054 ret
= snd_soc_dapm_set_bias_level(socdev
,
1055 SND_SOC_BIAS_STANDBY
);
1057 pr_err("Failed to apply standby bias: %d\n", ret
);
1060 /* If we're in standby and can support bias off then do that */
1061 if (codec
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1062 codec
->idle_bias_off
) {
1063 ret
= snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_OFF
);
1065 pr_err("Failed to turn off bias: %d\n", ret
);
1068 /* If we just powered up then move to active bias */
1069 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
1070 ret
= snd_soc_dapm_set_bias_level(socdev
,
1073 pr_err("Failed to apply active bias: %d\n", ret
);
1076 pop_dbg(codec
->pop_time
, "DAPM sequencing finished, waiting %dms\n",
1082 #ifdef CONFIG_DEBUG_FS
1083 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1085 file
->private_data
= inode
->i_private
;
1089 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1090 char __user
*user_buf
,
1091 size_t count
, loff_t
*ppos
)
1093 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1097 struct snd_soc_dapm_path
*p
= NULL
;
1099 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1103 in
= is_connected_input_ep(w
);
1104 dapm_clear_walk(w
->codec
);
1105 out
= is_connected_output_ep(w
);
1106 dapm_clear_walk(w
->codec
);
1108 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1109 w
->name
, w
->power
? "On" : "Off", in
, out
);
1112 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1113 " - R%d(0x%x) bit %d",
1114 w
->reg
, w
->reg
, w
->shift
);
1116 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1119 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1121 w
->active
? "active" : "inactive");
1123 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1124 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1128 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1130 p
->name
? p
->name
: "static",
1133 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1134 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1138 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1140 p
->name
? p
->name
: "static",
1144 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1150 static const struct file_operations dapm_widget_power_fops
= {
1151 .open
= dapm_widget_power_open_file
,
1152 .read
= dapm_widget_power_read_file
,
1155 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1157 struct snd_soc_dapm_widget
*w
;
1160 if (!codec
->debugfs_dapm
)
1163 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1167 d
= debugfs_create_file(w
->name
, 0444,
1168 codec
->debugfs_dapm
, w
,
1169 &dapm_widget_power_fops
);
1172 "ASoC: Failed to create %s debugfs file\n",
1177 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1182 /* test and update the power status of a mux widget */
1183 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1184 struct snd_kcontrol
*kcontrol
, int change
,
1185 int mux
, struct soc_enum
*e
)
1187 struct snd_soc_dapm_path
*path
;
1190 if (widget
->id
!= snd_soc_dapm_mux
&&
1191 widget
->id
!= snd_soc_dapm_value_mux
)
1197 /* find dapm widget path assoc with kcontrol */
1198 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1199 if (path
->kcontrol
!= kcontrol
)
1202 if (!path
->name
|| !e
->texts
[mux
])
1206 /* we now need to match the string in the enum to the path */
1207 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1208 path
->connect
= 1; /* new connection */
1210 path
->connect
= 0; /* old connection must be powered down */
1214 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1219 /* test and update the power status of a mixer or switch widget */
1220 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1221 struct snd_kcontrol
*kcontrol
, int connect
)
1223 struct snd_soc_dapm_path
*path
;
1226 if (widget
->id
!= snd_soc_dapm_mixer
&&
1227 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1228 widget
->id
!= snd_soc_dapm_switch
)
1231 /* find dapm widget path assoc with kcontrol */
1232 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1233 if (path
->kcontrol
!= kcontrol
)
1236 /* found, now check type */
1238 path
->connect
= connect
;
1243 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1248 /* show dapm widget status in sys fs */
1249 static ssize_t
dapm_widget_show(struct device
*dev
,
1250 struct device_attribute
*attr
, char *buf
)
1252 struct snd_soc_device
*devdata
= dev_get_drvdata(dev
);
1253 struct snd_soc_codec
*codec
= devdata
->card
->codec
;
1254 struct snd_soc_dapm_widget
*w
;
1256 char *state
= "not set";
1258 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1260 /* only display widgets that burnm power */
1262 case snd_soc_dapm_hp
:
1263 case snd_soc_dapm_mic
:
1264 case snd_soc_dapm_spk
:
1265 case snd_soc_dapm_line
:
1266 case snd_soc_dapm_micbias
:
1267 case snd_soc_dapm_dac
:
1268 case snd_soc_dapm_adc
:
1269 case snd_soc_dapm_pga
:
1270 case snd_soc_dapm_mixer
:
1271 case snd_soc_dapm_mixer_named_ctl
:
1272 case snd_soc_dapm_supply
:
1274 count
+= sprintf(buf
+ count
, "%s: %s\n",
1275 w
->name
, w
->power
? "On":"Off");
1282 switch (codec
->bias_level
) {
1283 case SND_SOC_BIAS_ON
:
1286 case SND_SOC_BIAS_PREPARE
:
1289 case SND_SOC_BIAS_STANDBY
:
1292 case SND_SOC_BIAS_OFF
:
1296 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1301 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1303 int snd_soc_dapm_sys_add(struct device
*dev
)
1305 return device_create_file(dev
, &dev_attr_dapm_widget
);
1308 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1310 device_remove_file(dev
, &dev_attr_dapm_widget
);
1313 /* free all dapm widgets and resources */
1314 static void dapm_free_widgets(struct snd_soc_codec
*codec
)
1316 struct snd_soc_dapm_widget
*w
, *next_w
;
1317 struct snd_soc_dapm_path
*p
, *next_p
;
1319 list_for_each_entry_safe(w
, next_w
, &codec
->dapm_widgets
, list
) {
1324 list_for_each_entry_safe(p
, next_p
, &codec
->dapm_paths
, list
) {
1326 kfree(p
->long_name
);
1331 static int snd_soc_dapm_set_pin(struct snd_soc_codec
*codec
,
1332 const char *pin
, int status
)
1334 struct snd_soc_dapm_widget
*w
;
1336 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1337 if (!strcmp(w
->name
, pin
)) {
1338 pr_debug("dapm: %s: pin %s\n", codec
->name
, pin
);
1339 w
->connected
= status
;
1344 pr_err("dapm: %s: configuring unknown pin %s\n", codec
->name
, pin
);
1349 * snd_soc_dapm_sync - scan and power dapm paths
1350 * @codec: audio codec
1352 * Walks all dapm audio paths and powers widgets according to their
1353 * stream or path usage.
1355 * Returns 0 for success.
1357 int snd_soc_dapm_sync(struct snd_soc_codec
*codec
)
1359 return dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1361 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1363 static int snd_soc_dapm_add_route(struct snd_soc_codec
*codec
,
1364 const struct snd_soc_dapm_route
*route
)
1366 struct snd_soc_dapm_path
*path
;
1367 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1368 const char *sink
= route
->sink
;
1369 const char *control
= route
->control
;
1370 const char *source
= route
->source
;
1373 /* find src and dest widgets */
1374 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1376 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1380 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1385 if (wsource
== NULL
|| wsink
== NULL
)
1388 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1392 path
->source
= wsource
;
1394 path
->connected
= route
->connected
;
1395 INIT_LIST_HEAD(&path
->list
);
1396 INIT_LIST_HEAD(&path
->list_source
);
1397 INIT_LIST_HEAD(&path
->list_sink
);
1399 /* check for external widgets */
1400 if (wsink
->id
== snd_soc_dapm_input
) {
1401 if (wsource
->id
== snd_soc_dapm_micbias
||
1402 wsource
->id
== snd_soc_dapm_mic
||
1403 wsource
->id
== snd_soc_dapm_line
||
1404 wsource
->id
== snd_soc_dapm_output
)
1407 if (wsource
->id
== snd_soc_dapm_output
) {
1408 if (wsink
->id
== snd_soc_dapm_spk
||
1409 wsink
->id
== snd_soc_dapm_hp
||
1410 wsink
->id
== snd_soc_dapm_line
||
1411 wsink
->id
== snd_soc_dapm_input
)
1415 /* connect static paths */
1416 if (control
== NULL
) {
1417 list_add(&path
->list
, &codec
->dapm_paths
);
1418 list_add(&path
->list_sink
, &wsink
->sources
);
1419 list_add(&path
->list_source
, &wsource
->sinks
);
1424 /* connect dynamic paths */
1426 case snd_soc_dapm_adc
:
1427 case snd_soc_dapm_dac
:
1428 case snd_soc_dapm_pga
:
1429 case snd_soc_dapm_input
:
1430 case snd_soc_dapm_output
:
1431 case snd_soc_dapm_micbias
:
1432 case snd_soc_dapm_vmid
:
1433 case snd_soc_dapm_pre
:
1434 case snd_soc_dapm_post
:
1435 case snd_soc_dapm_supply
:
1436 case snd_soc_dapm_aif_in
:
1437 case snd_soc_dapm_aif_out
:
1438 list_add(&path
->list
, &codec
->dapm_paths
);
1439 list_add(&path
->list_sink
, &wsink
->sources
);
1440 list_add(&path
->list_source
, &wsource
->sinks
);
1443 case snd_soc_dapm_mux
:
1444 case snd_soc_dapm_value_mux
:
1445 ret
= dapm_connect_mux(codec
, wsource
, wsink
, path
, control
,
1446 &wsink
->kcontrols
[0]);
1450 case snd_soc_dapm_switch
:
1451 case snd_soc_dapm_mixer
:
1452 case snd_soc_dapm_mixer_named_ctl
:
1453 ret
= dapm_connect_mixer(codec
, wsource
, wsink
, path
, control
);
1457 case snd_soc_dapm_hp
:
1458 case snd_soc_dapm_mic
:
1459 case snd_soc_dapm_line
:
1460 case snd_soc_dapm_spk
:
1461 list_add(&path
->list
, &codec
->dapm_paths
);
1462 list_add(&path
->list_sink
, &wsink
->sources
);
1463 list_add(&path
->list_source
, &wsource
->sinks
);
1470 printk(KERN_WARNING
"asoc: no dapm match for %s --> %s --> %s\n", source
,
1477 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1479 * @route: audio routes
1480 * @num: number of routes
1482 * Connects 2 dapm widgets together via a named audio path. The sink is
1483 * the widget receiving the audio signal, whilst the source is the sender
1484 * of the audio signal.
1486 * Returns 0 for success else error. On error all resources can be freed
1487 * with a call to snd_soc_card_free().
1489 int snd_soc_dapm_add_routes(struct snd_soc_codec
*codec
,
1490 const struct snd_soc_dapm_route
*route
, int num
)
1494 for (i
= 0; i
< num
; i
++) {
1495 ret
= snd_soc_dapm_add_route(codec
, route
);
1497 printk(KERN_ERR
"Failed to add route %s->%s\n",
1507 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1510 * snd_soc_dapm_new_widgets - add new dapm widgets
1511 * @codec: audio codec
1513 * Checks the codec for any new dapm widgets and creates them if found.
1515 * Returns 0 for success.
1517 int snd_soc_dapm_new_widgets(struct snd_soc_codec
*codec
)
1519 struct snd_soc_dapm_widget
*w
;
1521 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
1527 case snd_soc_dapm_switch
:
1528 case snd_soc_dapm_mixer
:
1529 case snd_soc_dapm_mixer_named_ctl
:
1530 w
->power_check
= dapm_generic_check_power
;
1531 dapm_new_mixer(codec
, w
);
1533 case snd_soc_dapm_mux
:
1534 case snd_soc_dapm_value_mux
:
1535 w
->power_check
= dapm_generic_check_power
;
1536 dapm_new_mux(codec
, w
);
1538 case snd_soc_dapm_adc
:
1539 case snd_soc_dapm_aif_out
:
1540 w
->power_check
= dapm_adc_check_power
;
1542 case snd_soc_dapm_dac
:
1543 case snd_soc_dapm_aif_in
:
1544 w
->power_check
= dapm_dac_check_power
;
1546 case snd_soc_dapm_pga
:
1547 w
->power_check
= dapm_generic_check_power
;
1548 dapm_new_pga(codec
, w
);
1550 case snd_soc_dapm_input
:
1551 case snd_soc_dapm_output
:
1552 case snd_soc_dapm_micbias
:
1553 case snd_soc_dapm_spk
:
1554 case snd_soc_dapm_hp
:
1555 case snd_soc_dapm_mic
:
1556 case snd_soc_dapm_line
:
1557 w
->power_check
= dapm_generic_check_power
;
1559 case snd_soc_dapm_supply
:
1560 w
->power_check
= dapm_supply_check_power
;
1561 case snd_soc_dapm_vmid
:
1562 case snd_soc_dapm_pre
:
1563 case snd_soc_dapm_post
:
1569 dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1572 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1575 * snd_soc_dapm_get_volsw - dapm mixer get callback
1576 * @kcontrol: mixer control
1577 * @ucontrol: control element information
1579 * Callback to get the value of a dapm mixer control.
1581 * Returns 0 for success.
1583 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1584 struct snd_ctl_elem_value
*ucontrol
)
1586 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1587 struct soc_mixer_control
*mc
=
1588 (struct soc_mixer_control
*)kcontrol
->private_value
;
1589 unsigned int reg
= mc
->reg
;
1590 unsigned int shift
= mc
->shift
;
1591 unsigned int rshift
= mc
->rshift
;
1593 unsigned int invert
= mc
->invert
;
1594 unsigned int mask
= (1 << fls(max
)) - 1;
1596 /* return the saved value if we are powered down */
1597 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1598 ucontrol
->value
.integer
.value
[0] = widget
->saved_value
;
1602 ucontrol
->value
.integer
.value
[0] =
1603 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1604 if (shift
!= rshift
)
1605 ucontrol
->value
.integer
.value
[1] =
1606 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1608 ucontrol
->value
.integer
.value
[0] =
1609 max
- ucontrol
->value
.integer
.value
[0];
1610 if (shift
!= rshift
)
1611 ucontrol
->value
.integer
.value
[1] =
1612 max
- ucontrol
->value
.integer
.value
[1];
1617 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1620 * snd_soc_dapm_put_volsw - dapm mixer set callback
1621 * @kcontrol: mixer control
1622 * @ucontrol: control element information
1624 * Callback to set the value of a dapm mixer control.
1626 * Returns 0 for success.
1628 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1629 struct snd_ctl_elem_value
*ucontrol
)
1631 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1632 struct soc_mixer_control
*mc
=
1633 (struct soc_mixer_control
*)kcontrol
->private_value
;
1634 unsigned int reg
= mc
->reg
;
1635 unsigned int shift
= mc
->shift
;
1636 unsigned int rshift
= mc
->rshift
;
1638 unsigned int mask
= (1 << fls(max
)) - 1;
1639 unsigned int invert
= mc
->invert
;
1640 unsigned int val
, val2
, val_mask
;
1644 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1648 val_mask
= mask
<< shift
;
1650 if (shift
!= rshift
) {
1651 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1654 val_mask
|= mask
<< rshift
;
1655 val
|= val2
<< rshift
;
1658 mutex_lock(&widget
->codec
->mutex
);
1659 widget
->value
= val
;
1661 /* save volume value if the widget is powered down */
1662 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1663 widget
->saved_value
= val
;
1664 mutex_unlock(&widget
->codec
->mutex
);
1668 if (snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
)) {
1670 /* new connection */
1671 connect
= invert
? 0:1;
1673 /* old connection must be powered down */
1674 connect
= invert
? 1:0;
1676 dapm_mixer_update_power(widget
, kcontrol
, connect
);
1679 if (widget
->event
) {
1680 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1681 ret
= widget
->event(widget
, kcontrol
,
1682 SND_SOC_DAPM_PRE_REG
);
1688 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1689 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1690 ret
= widget
->event(widget
, kcontrol
,
1691 SND_SOC_DAPM_POST_REG
);
1693 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1696 mutex_unlock(&widget
->codec
->mutex
);
1699 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1702 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1703 * @kcontrol: mixer control
1704 * @ucontrol: control element information
1706 * Callback to get the value of a dapm enumerated double mixer control.
1708 * Returns 0 for success.
1710 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1711 struct snd_ctl_elem_value
*ucontrol
)
1713 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1714 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1715 unsigned int val
, bitmask
;
1717 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1719 val
= snd_soc_read(widget
->codec
, e
->reg
);
1720 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1721 if (e
->shift_l
!= e
->shift_r
)
1722 ucontrol
->value
.enumerated
.item
[1] =
1723 (val
>> e
->shift_r
) & (bitmask
- 1);
1727 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1730 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1731 * @kcontrol: mixer control
1732 * @ucontrol: control element information
1734 * Callback to set the value of a dapm enumerated double mixer control.
1736 * Returns 0 for success.
1738 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1739 struct snd_ctl_elem_value
*ucontrol
)
1741 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1742 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1743 unsigned int val
, mux
, change
;
1744 unsigned int mask
, bitmask
;
1747 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1749 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1751 mux
= ucontrol
->value
.enumerated
.item
[0];
1752 val
= mux
<< e
->shift_l
;
1753 mask
= (bitmask
- 1) << e
->shift_l
;
1754 if (e
->shift_l
!= e
->shift_r
) {
1755 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1757 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1758 mask
|= (bitmask
- 1) << e
->shift_r
;
1761 mutex_lock(&widget
->codec
->mutex
);
1762 widget
->value
= val
;
1763 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1764 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1766 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1767 ret
= widget
->event(widget
,
1768 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1773 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1775 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1776 ret
= widget
->event(widget
,
1777 kcontrol
, SND_SOC_DAPM_POST_REG
);
1780 mutex_unlock(&widget
->codec
->mutex
);
1783 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1786 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1787 * @kcontrol: mixer control
1788 * @ucontrol: control element information
1790 * Returns 0 for success.
1792 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
1793 struct snd_ctl_elem_value
*ucontrol
)
1795 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1797 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
1801 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
1804 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1805 * @kcontrol: mixer control
1806 * @ucontrol: control element information
1808 * Returns 0 for success.
1810 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
1811 struct snd_ctl_elem_value
*ucontrol
)
1813 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1814 struct soc_enum
*e
=
1815 (struct soc_enum
*)kcontrol
->private_value
;
1819 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
1822 mutex_lock(&widget
->codec
->mutex
);
1824 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
1825 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
1826 dapm_mux_update_power(widget
, kcontrol
, change
, widget
->value
, e
);
1828 mutex_unlock(&widget
->codec
->mutex
);
1831 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
1834 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1836 * @kcontrol: mixer control
1837 * @ucontrol: control element information
1839 * Callback to get the value of a dapm semi enumerated double mixer control.
1841 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1842 * used for handling bitfield coded enumeration for example.
1844 * Returns 0 for success.
1846 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1847 struct snd_ctl_elem_value
*ucontrol
)
1849 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1850 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1851 unsigned int reg_val
, val
, mux
;
1853 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1854 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1855 for (mux
= 0; mux
< e
->max
; mux
++) {
1856 if (val
== e
->values
[mux
])
1859 ucontrol
->value
.enumerated
.item
[0] = mux
;
1860 if (e
->shift_l
!= e
->shift_r
) {
1861 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1862 for (mux
= 0; mux
< e
->max
; mux
++) {
1863 if (val
== e
->values
[mux
])
1866 ucontrol
->value
.enumerated
.item
[1] = mux
;
1871 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1874 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1876 * @kcontrol: mixer control
1877 * @ucontrol: control element information
1879 * Callback to set the value of a dapm semi enumerated double mixer control.
1881 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1882 * used for handling bitfield coded enumeration for example.
1884 * Returns 0 for success.
1886 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
1887 struct snd_ctl_elem_value
*ucontrol
)
1889 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1890 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1891 unsigned int val
, mux
, change
;
1895 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1897 mux
= ucontrol
->value
.enumerated
.item
[0];
1898 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1899 mask
= e
->mask
<< e
->shift_l
;
1900 if (e
->shift_l
!= e
->shift_r
) {
1901 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1903 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1904 mask
|= e
->mask
<< e
->shift_r
;
1907 mutex_lock(&widget
->codec
->mutex
);
1908 widget
->value
= val
;
1909 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1910 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1912 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1913 ret
= widget
->event(widget
,
1914 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1919 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1921 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1922 ret
= widget
->event(widget
,
1923 kcontrol
, SND_SOC_DAPM_POST_REG
);
1926 mutex_unlock(&widget
->codec
->mutex
);
1929 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1932 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1934 * @kcontrol: mixer control
1935 * @uinfo: control element information
1937 * Callback to provide information about a pin switch control.
1939 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1940 struct snd_ctl_elem_info
*uinfo
)
1942 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1944 uinfo
->value
.integer
.min
= 0;
1945 uinfo
->value
.integer
.max
= 1;
1949 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1952 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1954 * @kcontrol: mixer control
1957 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1958 struct snd_ctl_elem_value
*ucontrol
)
1960 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1961 const char *pin
= (const char *)kcontrol
->private_value
;
1963 mutex_lock(&codec
->mutex
);
1965 ucontrol
->value
.integer
.value
[0] =
1966 snd_soc_dapm_get_pin_status(codec
, pin
);
1968 mutex_unlock(&codec
->mutex
);
1972 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
1975 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1977 * @kcontrol: mixer control
1980 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
1981 struct snd_ctl_elem_value
*ucontrol
)
1983 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1984 const char *pin
= (const char *)kcontrol
->private_value
;
1986 mutex_lock(&codec
->mutex
);
1988 if (ucontrol
->value
.integer
.value
[0])
1989 snd_soc_dapm_enable_pin(codec
, pin
);
1991 snd_soc_dapm_disable_pin(codec
, pin
);
1993 snd_soc_dapm_sync(codec
);
1995 mutex_unlock(&codec
->mutex
);
1999 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
2002 * snd_soc_dapm_new_control - create new dapm control
2003 * @codec: audio codec
2004 * @widget: widget template
2006 * Creates a new dapm control based upon the template.
2008 * Returns 0 for success else error.
2010 int snd_soc_dapm_new_control(struct snd_soc_codec
*codec
,
2011 const struct snd_soc_dapm_widget
*widget
)
2013 struct snd_soc_dapm_widget
*w
;
2015 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
2019 INIT_LIST_HEAD(&w
->sources
);
2020 INIT_LIST_HEAD(&w
->sinks
);
2021 INIT_LIST_HEAD(&w
->list
);
2022 list_add(&w
->list
, &codec
->dapm_widgets
);
2024 /* machine layer set ups unconnected pins and insertions */
2028 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
2031 * snd_soc_dapm_new_controls - create new dapm controls
2032 * @codec: audio codec
2033 * @widget: widget array
2034 * @num: number of widgets
2036 * Creates new DAPM controls based upon the templates.
2038 * Returns 0 for success else error.
2040 int snd_soc_dapm_new_controls(struct snd_soc_codec
*codec
,
2041 const struct snd_soc_dapm_widget
*widget
,
2046 for (i
= 0; i
< num
; i
++) {
2047 ret
= snd_soc_dapm_new_control(codec
, widget
);
2050 "ASoC: Failed to create DAPM control %s: %d\n",
2058 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2062 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2063 * @codec: audio codec
2064 * @stream: stream name
2065 * @event: stream event
2067 * Sends a stream event to the dapm core. The core then makes any
2068 * necessary widget power changes.
2070 * Returns 0 for success else error.
2072 int snd_soc_dapm_stream_event(struct snd_soc_codec
*codec
,
2073 char *stream
, int event
)
2075 struct snd_soc_dapm_widget
*w
;
2080 mutex_lock(&codec
->mutex
);
2081 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
2085 pr_debug("widget %s\n %s stream %s event %d\n",
2086 w
->name
, w
->sname
, stream
, event
);
2087 if (strstr(w
->sname
, stream
)) {
2089 case SND_SOC_DAPM_STREAM_START
:
2092 case SND_SOC_DAPM_STREAM_STOP
:
2095 case SND_SOC_DAPM_STREAM_SUSPEND
:
2100 case SND_SOC_DAPM_STREAM_RESUME
:
2106 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2108 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2114 dapm_power_widgets(codec
, event
);
2115 mutex_unlock(&codec
->mutex
);
2118 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
2121 * snd_soc_dapm_enable_pin - enable pin.
2125 * Enables input/output pin and its parents or children widgets iff there is
2126 * a valid audio route and active audio stream.
2127 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2128 * do any widget power switching.
2130 int snd_soc_dapm_enable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2132 return snd_soc_dapm_set_pin(codec
, pin
, 1);
2134 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2137 * snd_soc_dapm_disable_pin - disable pin.
2141 * Disables input/output pin and its parents or children widgets.
2142 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2143 * do any widget power switching.
2145 int snd_soc_dapm_disable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2147 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2149 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2152 * snd_soc_dapm_nc_pin - permanently disable pin.
2156 * Marks the specified pin as being not connected, disabling it along
2157 * any parent or child widgets. At present this is identical to
2158 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2159 * additional things such as disabling controls which only affect
2160 * paths through the pin.
2162 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2163 * do any widget power switching.
2165 int snd_soc_dapm_nc_pin(struct snd_soc_codec
*codec
, const char *pin
)
2167 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2169 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2172 * snd_soc_dapm_get_pin_status - get audio pin status
2173 * @codec: audio codec
2174 * @pin: audio signal pin endpoint (or start point)
2176 * Get audio pin status - connected or disconnected.
2178 * Returns 1 for connected otherwise 0.
2180 int snd_soc_dapm_get_pin_status(struct snd_soc_codec
*codec
, const char *pin
)
2182 struct snd_soc_dapm_widget
*w
;
2184 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2185 if (!strcmp(w
->name
, pin
))
2186 return w
->connected
;
2191 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2194 * snd_soc_dapm_free - free dapm resources
2195 * @socdev: SoC device
2197 * Free all dapm widgets and resources.
2199 void snd_soc_dapm_free(struct snd_soc_device
*socdev
)
2201 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2203 snd_soc_dapm_sys_remove(socdev
->dev
);
2204 dapm_free_widgets(codec
);
2206 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2209 * snd_soc_dapm_shutdown - callback for system shutdown
2211 void snd_soc_dapm_shutdown(struct snd_soc_device
*socdev
)
2213 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2214 struct snd_soc_dapm_widget
*w
;
2215 LIST_HEAD(down_list
);
2218 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2220 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2226 /* If there were no widgets to power down we're already in
2230 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_PREPARE
);
2231 dapm_seq_run(codec
, &down_list
, 0, dapm_down_seq
);
2232 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_STANDBY
);
2235 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_OFF
);
2238 /* Module information */
2239 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2240 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2241 MODULE_LICENSE("GPL");