2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
25 * o DAPM power change sequencing - allow for configurable per
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
37 #include <linux/bitops.h>
38 #include <linux/platform_device.h>
39 #include <linux/jiffies.h>
40 #include <linux/debugfs.h>
41 #include <sound/core.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc-dapm.h>
45 #include <sound/initval.h>
49 #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
51 #define dump_dapm(codec, action)
54 /* dapm power sequences - make this per codec in the future */
55 static int dapm_up_seq
[] = {
56 [snd_soc_dapm_pre
] = 0,
57 [snd_soc_dapm_supply
] = 1,
58 [snd_soc_dapm_micbias
] = 2,
59 [snd_soc_dapm_aif_in
] = 3,
60 [snd_soc_dapm_aif_out
] = 3,
61 [snd_soc_dapm_mic
] = 4,
62 [snd_soc_dapm_mux
] = 5,
63 [snd_soc_dapm_value_mux
] = 5,
64 [snd_soc_dapm_dac
] = 6,
65 [snd_soc_dapm_mixer
] = 7,
66 [snd_soc_dapm_mixer_named_ctl
] = 7,
67 [snd_soc_dapm_pga
] = 8,
68 [snd_soc_dapm_adc
] = 9,
69 [snd_soc_dapm_hp
] = 10,
70 [snd_soc_dapm_spk
] = 10,
71 [snd_soc_dapm_post
] = 11,
74 static int dapm_down_seq
[] = {
75 [snd_soc_dapm_pre
] = 0,
76 [snd_soc_dapm_adc
] = 1,
77 [snd_soc_dapm_hp
] = 2,
78 [snd_soc_dapm_spk
] = 2,
79 [snd_soc_dapm_pga
] = 4,
80 [snd_soc_dapm_mixer_named_ctl
] = 5,
81 [snd_soc_dapm_mixer
] = 5,
82 [snd_soc_dapm_dac
] = 6,
83 [snd_soc_dapm_mic
] = 7,
84 [snd_soc_dapm_micbias
] = 8,
85 [snd_soc_dapm_mux
] = 9,
86 [snd_soc_dapm_value_mux
] = 9,
87 [snd_soc_dapm_aif_in
] = 10,
88 [snd_soc_dapm_aif_out
] = 10,
89 [snd_soc_dapm_supply
] = 11,
90 [snd_soc_dapm_post
] = 12,
93 static void pop_wait(u32 pop_time
)
96 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
99 static void pop_dbg(u32 pop_time
, const char *fmt
, ...)
113 /* create a new dapm widget */
114 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
115 const struct snd_soc_dapm_widget
*_widget
)
117 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
121 * snd_soc_dapm_set_bias_level - set the bias level for the system
122 * @socdev: audio device
123 * @level: level to configure
125 * Configure the bias (power) levels for the SoC audio device.
127 * Returns 0 for success else error.
129 static int snd_soc_dapm_set_bias_level(struct snd_soc_device
*socdev
,
130 enum snd_soc_bias_level level
)
132 struct snd_soc_card
*card
= socdev
->card
;
133 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
137 case SND_SOC_BIAS_ON
:
138 dev_dbg(socdev
->dev
, "Setting full bias\n");
140 case SND_SOC_BIAS_PREPARE
:
141 dev_dbg(socdev
->dev
, "Setting bias prepare\n");
143 case SND_SOC_BIAS_STANDBY
:
144 dev_dbg(socdev
->dev
, "Setting standby bias\n");
146 case SND_SOC_BIAS_OFF
:
147 dev_dbg(socdev
->dev
, "Setting bias off\n");
150 dev_err(socdev
->dev
, "Setting invalid bias %d\n", level
);
154 if (card
->set_bias_level
)
155 ret
= card
->set_bias_level(card
, level
);
157 if (codec
->set_bias_level
)
158 ret
= codec
->set_bias_level(codec
, level
);
160 codec
->bias_level
= level
;
166 /* set up initial codec paths */
167 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
168 struct snd_soc_dapm_path
*p
, int i
)
171 case snd_soc_dapm_switch
:
172 case snd_soc_dapm_mixer
:
173 case snd_soc_dapm_mixer_named_ctl
: {
175 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
176 w
->kcontrols
[i
].private_value
;
177 unsigned int reg
= mc
->reg
;
178 unsigned int shift
= mc
->shift
;
180 unsigned int mask
= (1 << fls(max
)) - 1;
181 unsigned int invert
= mc
->invert
;
183 val
= snd_soc_read(w
->codec
, reg
);
184 val
= (val
>> shift
) & mask
;
186 if ((invert
&& !val
) || (!invert
&& val
))
192 case snd_soc_dapm_mux
: {
193 struct soc_enum
*e
= (struct soc_enum
*)w
->kcontrols
[i
].private_value
;
194 int val
, item
, bitmask
;
196 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
198 val
= snd_soc_read(w
->codec
, e
->reg
);
199 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
202 for (i
= 0; i
< e
->max
; i
++) {
203 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
208 case snd_soc_dapm_value_mux
: {
209 struct soc_enum
*e
= (struct soc_enum
*)
210 w
->kcontrols
[i
].private_value
;
213 val
= snd_soc_read(w
->codec
, e
->reg
);
214 val
= (val
>> e
->shift_l
) & e
->mask
;
215 for (item
= 0; item
< e
->max
; item
++) {
216 if (val
== e
->values
[item
])
221 for (i
= 0; i
< e
->max
; i
++) {
222 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
227 /* does not effect routing - always connected */
228 case snd_soc_dapm_pga
:
229 case snd_soc_dapm_output
:
230 case snd_soc_dapm_adc
:
231 case snd_soc_dapm_input
:
232 case snd_soc_dapm_dac
:
233 case snd_soc_dapm_micbias
:
234 case snd_soc_dapm_vmid
:
235 case snd_soc_dapm_supply
:
236 case snd_soc_dapm_aif_in
:
237 case snd_soc_dapm_aif_out
:
240 /* does effect routing - dynamically connected */
241 case snd_soc_dapm_hp
:
242 case snd_soc_dapm_mic
:
243 case snd_soc_dapm_spk
:
244 case snd_soc_dapm_line
:
245 case snd_soc_dapm_pre
:
246 case snd_soc_dapm_post
:
252 /* connect mux widget to its interconnecting audio paths */
253 static int dapm_connect_mux(struct snd_soc_codec
*codec
,
254 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
255 struct snd_soc_dapm_path
*path
, const char *control_name
,
256 const struct snd_kcontrol_new
*kcontrol
)
258 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
261 for (i
= 0; i
< e
->max
; i
++) {
262 if (!(strcmp(control_name
, e
->texts
[i
]))) {
263 list_add(&path
->list
, &codec
->dapm_paths
);
264 list_add(&path
->list_sink
, &dest
->sources
);
265 list_add(&path
->list_source
, &src
->sinks
);
266 path
->name
= (char*)e
->texts
[i
];
267 dapm_set_path_status(dest
, path
, 0);
275 /* connect mixer widget to its interconnecting audio paths */
276 static int dapm_connect_mixer(struct snd_soc_codec
*codec
,
277 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
278 struct snd_soc_dapm_path
*path
, const char *control_name
)
282 /* search for mixer kcontrol */
283 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
284 if (!strcmp(control_name
, dest
->kcontrols
[i
].name
)) {
285 list_add(&path
->list
, &codec
->dapm_paths
);
286 list_add(&path
->list_sink
, &dest
->sources
);
287 list_add(&path
->list_source
, &src
->sinks
);
288 path
->name
= dest
->kcontrols
[i
].name
;
289 dapm_set_path_status(dest
, path
, i
);
296 /* update dapm codec register bits */
297 static int dapm_update_bits(struct snd_soc_dapm_widget
*widget
)
300 unsigned int old
, new;
301 struct snd_soc_codec
*codec
= widget
->codec
;
303 /* check for valid widgets */
304 if (widget
->reg
< 0 || widget
->id
== snd_soc_dapm_input
||
305 widget
->id
== snd_soc_dapm_output
||
306 widget
->id
== snd_soc_dapm_hp
||
307 widget
->id
== snd_soc_dapm_mic
||
308 widget
->id
== snd_soc_dapm_line
||
309 widget
->id
== snd_soc_dapm_spk
)
312 power
= widget
->power
;
314 power
= (power
? 0:1);
316 old
= snd_soc_read(codec
, widget
->reg
);
317 new = (old
& ~(0x1 << widget
->shift
)) | (power
<< widget
->shift
);
321 pop_dbg(codec
->pop_time
, "pop test %s : %s in %d ms\n",
322 widget
->name
, widget
->power
? "on" : "off",
324 snd_soc_write(codec
, widget
->reg
, new);
325 pop_wait(codec
->pop_time
);
327 pr_debug("reg %x old %x new %x change %d\n", widget
->reg
,
332 /* ramps the volume up or down to minimise pops before or after a
333 * DAPM power event */
334 static int dapm_set_pga(struct snd_soc_dapm_widget
*widget
, int power
)
336 const struct snd_kcontrol_new
*k
= widget
->kcontrols
;
338 if (widget
->muted
&& !power
)
340 if (!widget
->muted
&& power
)
343 if (widget
->num_kcontrols
&& k
) {
344 struct soc_mixer_control
*mc
=
345 (struct soc_mixer_control
*)k
->private_value
;
346 unsigned int reg
= mc
->reg
;
347 unsigned int shift
= mc
->shift
;
349 unsigned int mask
= (1 << fls(max
)) - 1;
350 unsigned int invert
= mc
->invert
;
354 /* power up has happended, increase volume to last level */
356 for (i
= max
; i
> widget
->saved_value
; i
--)
357 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
359 for (i
= 0; i
< widget
->saved_value
; i
++)
360 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
364 /* power down is about to occur, decrease volume to mute */
365 int val
= snd_soc_read(widget
->codec
, reg
);
366 int i
= widget
->saved_value
= (val
>> shift
) & mask
;
368 for (; i
< mask
; i
++)
369 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
372 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
380 /* create new dapm mixer control */
381 static int dapm_new_mixer(struct snd_soc_codec
*codec
,
382 struct snd_soc_dapm_widget
*w
)
386 struct snd_soc_dapm_path
*path
;
389 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
392 list_for_each_entry(path
, &w
->sources
, list_sink
) {
394 /* mixer/mux paths name must match control name */
395 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
398 /* add dapm control with long name.
399 * for dapm_mixer this is the concatenation of the
400 * mixer and kcontrol name.
401 * for dapm_mixer_named_ctl this is simply the
404 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
405 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
406 name_len
+= 1 + strlen(w
->name
);
408 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
410 if (path
->long_name
== NULL
)
415 snprintf(path
->long_name
, name_len
, "%s %s",
416 w
->name
, w
->kcontrols
[i
].name
);
418 case snd_soc_dapm_mixer_named_ctl
:
419 snprintf(path
->long_name
, name_len
, "%s",
420 w
->kcontrols
[i
].name
);
424 path
->long_name
[name_len
- 1] = '\0';
426 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
428 ret
= snd_ctl_add(codec
->card
, path
->kcontrol
);
430 printk(KERN_ERR
"asoc: failed to add dapm kcontrol %s: %d\n",
433 kfree(path
->long_name
);
434 path
->long_name
= NULL
;
442 /* create new dapm mux control */
443 static int dapm_new_mux(struct snd_soc_codec
*codec
,
444 struct snd_soc_dapm_widget
*w
)
446 struct snd_soc_dapm_path
*path
= NULL
;
447 struct snd_kcontrol
*kcontrol
;
450 if (!w
->num_kcontrols
) {
451 printk(KERN_ERR
"asoc: mux %s has no controls\n", w
->name
);
455 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
456 ret
= snd_ctl_add(codec
->card
, kcontrol
);
460 list_for_each_entry(path
, &w
->sources
, list_sink
)
461 path
->kcontrol
= kcontrol
;
466 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
470 /* create new dapm volume control */
471 static int dapm_new_pga(struct snd_soc_codec
*codec
,
472 struct snd_soc_dapm_widget
*w
)
474 struct snd_kcontrol
*kcontrol
;
477 if (!w
->num_kcontrols
)
480 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
481 ret
= snd_ctl_add(codec
->card
, kcontrol
);
483 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
490 /* reset 'walked' bit for each dapm path */
491 static inline void dapm_clear_walk(struct snd_soc_codec
*codec
)
493 struct snd_soc_dapm_path
*p
;
495 list_for_each_entry(p
, &codec
->dapm_paths
, list
)
500 * Recursively check for a completed path to an active or physically connected
501 * output widget. Returns number of complete paths.
503 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
505 struct snd_soc_dapm_path
*path
;
508 if (widget
->id
== snd_soc_dapm_supply
)
511 switch (widget
->id
) {
512 case snd_soc_dapm_adc
:
513 case snd_soc_dapm_aif_out
:
520 if (widget
->connected
) {
521 /* connected pin ? */
522 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
525 /* connected jack or spk ? */
526 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
527 widget
->id
== snd_soc_dapm_line
)
531 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
535 if (path
->sink
&& path
->connect
) {
537 con
+= is_connected_output_ep(path
->sink
);
545 * Recursively check for a completed path to an active or physically connected
546 * input widget. Returns number of complete paths.
548 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
550 struct snd_soc_dapm_path
*path
;
553 if (widget
->id
== snd_soc_dapm_supply
)
556 /* active stream ? */
557 switch (widget
->id
) {
558 case snd_soc_dapm_dac
:
559 case snd_soc_dapm_aif_in
:
566 if (widget
->connected
) {
567 /* connected pin ? */
568 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
571 /* connected VMID/Bias for lower pops */
572 if (widget
->id
== snd_soc_dapm_vmid
)
575 /* connected jack ? */
576 if (widget
->id
== snd_soc_dapm_mic
|| widget
->id
== snd_soc_dapm_line
)
580 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
584 if (path
->source
&& path
->connect
) {
586 con
+= is_connected_input_ep(path
->source
);
594 * Handler for generic register modifier widget.
596 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
597 struct snd_kcontrol
*kcontrol
, int event
)
601 if (SND_SOC_DAPM_EVENT_ON(event
))
606 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
607 w
->mask
<< w
->shift
, val
<< w
->shift
);
611 EXPORT_SYMBOL_GPL(dapm_reg_event
);
613 /* Standard power change method, used to apply power changes to most
616 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
620 /* call any power change event handlers */
622 pr_debug("power %s event for %s flags %x\n",
623 w
->power
? "on" : "off",
624 w
->name
, w
->event_flags
);
626 /* power up pre event */
627 if (w
->power
&& w
->event
&&
628 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
629 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
634 /* power down pre event */
635 if (!w
->power
&& w
->event
&&
636 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
637 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
642 /* Lower PGA volume to reduce pops */
643 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
644 dapm_set_pga(w
, w
->power
);
648 /* Raise PGA volume to reduce pops */
649 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
650 dapm_set_pga(w
, w
->power
);
652 /* power up post event */
653 if (w
->power
&& w
->event
&&
654 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
656 NULL
, SND_SOC_DAPM_POST_PMU
);
661 /* power down post event */
662 if (!w
->power
&& w
->event
&&
663 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
664 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
672 /* Generic check to see if a widget should be powered.
674 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
678 in
= is_connected_input_ep(w
);
679 dapm_clear_walk(w
->codec
);
680 out
= is_connected_output_ep(w
);
681 dapm_clear_walk(w
->codec
);
682 return out
!= 0 && in
!= 0;
685 /* Check to see if an ADC has power */
686 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
691 in
= is_connected_input_ep(w
);
692 dapm_clear_walk(w
->codec
);
695 return dapm_generic_check_power(w
);
699 /* Check to see if a DAC has power */
700 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
705 out
= is_connected_output_ep(w
);
706 dapm_clear_walk(w
->codec
);
709 return dapm_generic_check_power(w
);
713 /* Check to see if a power supply is needed */
714 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
716 struct snd_soc_dapm_path
*path
;
719 /* Check if one of our outputs is connected */
720 list_for_each_entry(path
, &w
->sinks
, list_source
) {
721 if (path
->connected
&&
722 !path
->connected(path
->source
, path
->sink
))
725 if (path
->sink
&& path
->sink
->power_check
&&
726 path
->sink
->power_check(path
->sink
)) {
732 dapm_clear_walk(w
->codec
);
737 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
738 struct snd_soc_dapm_widget
*b
,
741 if (sort
[a
->id
] != sort
[b
->id
])
742 return sort
[a
->id
] - sort
[b
->id
];
743 if (a
->reg
!= b
->reg
)
744 return a
->reg
- b
->reg
;
749 /* Insert a widget in order into a DAPM power sequence. */
750 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
751 struct list_head
*list
,
754 struct snd_soc_dapm_widget
*w
;
756 list_for_each_entry(w
, list
, power_list
)
757 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
758 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
762 list_add_tail(&new_widget
->power_list
, list
);
765 /* Apply the coalesced changes from a DAPM sequence */
766 static void dapm_seq_run_coalesced(struct snd_soc_codec
*codec
,
767 struct list_head
*pending
)
769 struct snd_soc_dapm_widget
*w
;
771 unsigned int value
= 0;
772 unsigned int mask
= 0;
773 unsigned int cur_mask
;
775 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
778 list_for_each_entry(w
, pending
, power_list
) {
779 cur_mask
= 1 << w
->shift
;
780 BUG_ON(reg
!= w
->reg
);
791 pop_dbg(codec
->pop_time
,
792 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
793 w
->name
, reg
, value
, mask
);
795 /* power up pre event */
796 if (w
->power
&& w
->event
&&
797 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
798 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMU\n",
800 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
802 pr_err("%s: pre event failed: %d\n",
806 /* power down pre event */
807 if (!w
->power
&& w
->event
&&
808 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
809 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMD\n",
811 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
813 pr_err("%s: pre event failed: %d\n",
817 /* Lower PGA volume to reduce pops */
818 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
819 dapm_set_pga(w
, w
->power
);
823 pop_dbg(codec
->pop_time
,
824 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
825 value
, mask
, reg
, codec
->pop_time
);
826 pop_wait(codec
->pop_time
);
827 snd_soc_update_bits(codec
, reg
, mask
, value
);
830 list_for_each_entry(w
, pending
, power_list
) {
831 /* Raise PGA volume to reduce pops */
832 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
833 dapm_set_pga(w
, w
->power
);
835 /* power up post event */
836 if (w
->power
&& w
->event
&&
837 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
838 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMU\n",
841 NULL
, SND_SOC_DAPM_POST_PMU
);
843 pr_err("%s: post event failed: %d\n",
847 /* power down post event */
848 if (!w
->power
&& w
->event
&&
849 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
850 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMD\n",
852 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
854 pr_err("%s: post event failed: %d\n",
860 /* Apply a DAPM power sequence.
862 * We walk over a pre-sorted list of widgets to apply power to. In
863 * order to minimise the number of writes to the device required
864 * multiple widgets will be updated in a single write where possible.
865 * Currently anything that requires more than a single write is not
868 static void dapm_seq_run(struct snd_soc_codec
*codec
, struct list_head
*list
,
869 int event
, int sort
[])
871 struct snd_soc_dapm_widget
*w
, *n
;
874 int cur_reg
= SND_SOC_NOPM
;
877 list_for_each_entry_safe(w
, n
, list
, power_list
) {
880 /* Do we need to apply any queued changes? */
881 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
882 if (!list_empty(&pending
))
883 dapm_seq_run_coalesced(codec
, &pending
);
885 INIT_LIST_HEAD(&pending
);
887 cur_reg
= SND_SOC_NOPM
;
891 case snd_soc_dapm_pre
:
893 list_for_each_entry_safe_continue(w
, n
, list
,
896 if (event
== SND_SOC_DAPM_STREAM_START
)
898 NULL
, SND_SOC_DAPM_PRE_PMU
);
899 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
901 NULL
, SND_SOC_DAPM_PRE_PMD
);
904 case snd_soc_dapm_post
:
906 list_for_each_entry_safe_continue(w
, n
, list
,
909 if (event
== SND_SOC_DAPM_STREAM_START
)
911 NULL
, SND_SOC_DAPM_POST_PMU
);
912 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
914 NULL
, SND_SOC_DAPM_POST_PMD
);
917 case snd_soc_dapm_input
:
918 case snd_soc_dapm_output
:
919 case snd_soc_dapm_hp
:
920 case snd_soc_dapm_mic
:
921 case snd_soc_dapm_line
:
922 case snd_soc_dapm_spk
:
923 /* No register support currently */
924 ret
= dapm_generic_apply_power(w
);
928 /* Queue it up for application */
929 cur_sort
= sort
[w
->id
];
931 list_move(&w
->power_list
, &pending
);
936 pr_err("Failed to apply widget power: %d\n",
940 if (!list_empty(&pending
))
941 dapm_seq_run_coalesced(codec
, &pending
);
945 * Scan each dapm widget for complete audio path.
946 * A complete path is a route that has valid endpoints i.e.:-
948 * o DAC to output pin.
949 * o Input Pin to ADC.
950 * o Input pin to Output pin (bypass, sidetone)
951 * o DAC to ADC (loopback).
953 static int dapm_power_widgets(struct snd_soc_codec
*codec
, int event
)
955 struct snd_soc_device
*socdev
= codec
->socdev
;
956 struct snd_soc_dapm_widget
*w
;
958 LIST_HEAD(down_list
);
963 /* Check which widgets we need to power and store them in
964 * lists indicating if they should be powered up or down.
966 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
968 case snd_soc_dapm_pre
:
969 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
971 case snd_soc_dapm_post
:
972 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
979 power
= w
->power_check(w
);
983 if (w
->power
== power
)
987 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
989 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
996 /* If there are no DAPM widgets then try to figure out power from the
999 if (list_empty(&codec
->dapm_widgets
)) {
1001 case SND_SOC_DAPM_STREAM_START
:
1002 case SND_SOC_DAPM_STREAM_RESUME
:
1005 case SND_SOC_DAPM_STREAM_NOP
:
1006 sys_power
= codec
->bias_level
!= SND_SOC_BIAS_STANDBY
;
1012 /* If we're changing to all on or all off then prepare */
1013 if ((sys_power
&& codec
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
1014 (!sys_power
&& codec
->bias_level
== SND_SOC_BIAS_ON
)) {
1015 ret
= snd_soc_dapm_set_bias_level(socdev
,
1016 SND_SOC_BIAS_PREPARE
);
1018 pr_err("Failed to prepare bias: %d\n", ret
);
1021 /* Power down widgets first; try to avoid amplifying pops. */
1022 dapm_seq_run(codec
, &down_list
, event
, dapm_down_seq
);
1025 dapm_seq_run(codec
, &up_list
, event
, dapm_up_seq
);
1027 /* If we just powered the last thing off drop to standby bias */
1028 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
1029 ret
= snd_soc_dapm_set_bias_level(socdev
,
1030 SND_SOC_BIAS_STANDBY
);
1032 pr_err("Failed to apply standby bias: %d\n", ret
);
1035 /* If we just powered up then move to active bias */
1036 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
1037 ret
= snd_soc_dapm_set_bias_level(socdev
,
1040 pr_err("Failed to apply active bias: %d\n", ret
);
1043 pop_dbg(codec
->pop_time
, "DAPM sequencing finished, waiting %dms\n",
1050 static void dbg_dump_dapm(struct snd_soc_codec
* codec
, const char *action
)
1052 struct snd_soc_dapm_widget
*w
;
1053 struct snd_soc_dapm_path
*p
= NULL
;
1056 printk("DAPM %s %s\n", codec
->name
, action
);
1058 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1060 /* only display widgets that effect routing */
1062 case snd_soc_dapm_pre
:
1063 case snd_soc_dapm_post
:
1064 case snd_soc_dapm_vmid
:
1066 case snd_soc_dapm_mux
:
1067 case snd_soc_dapm_value_mux
:
1068 case snd_soc_dapm_output
:
1069 case snd_soc_dapm_input
:
1070 case snd_soc_dapm_switch
:
1071 case snd_soc_dapm_hp
:
1072 case snd_soc_dapm_mic
:
1073 case snd_soc_dapm_spk
:
1074 case snd_soc_dapm_line
:
1075 case snd_soc_dapm_micbias
:
1076 case snd_soc_dapm_dac
:
1077 case snd_soc_dapm_adc
:
1078 case snd_soc_dapm_pga
:
1079 case snd_soc_dapm_mixer
:
1080 case snd_soc_dapm_mixer_named_ctl
:
1081 case snd_soc_dapm_supply
:
1082 case snd_soc_dapm_aif_in
:
1083 case snd_soc_dapm_aif_out
:
1085 in
= is_connected_input_ep(w
);
1086 dapm_clear_walk(w
->codec
);
1087 out
= is_connected_output_ep(w
);
1088 dapm_clear_walk(w
->codec
);
1089 printk("%s: %s in %d out %d\n", w
->name
,
1090 w
->power
? "On":"Off",in
, out
);
1092 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1094 printk(" in %s %s\n", p
->name
? p
->name
: "static",
1097 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1099 printk(" out %s %s\n", p
->name
? p
->name
: "static",
1109 #ifdef CONFIG_DEBUG_FS
1110 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1112 file
->private_data
= inode
->i_private
;
1116 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1117 char __user
*user_buf
,
1118 size_t count
, loff_t
*ppos
)
1120 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1124 struct snd_soc_dapm_path
*p
= NULL
;
1126 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1130 in
= is_connected_input_ep(w
);
1131 dapm_clear_walk(w
->codec
);
1132 out
= is_connected_output_ep(w
);
1133 dapm_clear_walk(w
->codec
);
1135 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d\n",
1136 w
->name
, w
->power
? "On" : "Off", in
, out
);
1139 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1141 w
->active
? "active" : "inactive");
1143 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1144 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1148 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1150 p
->name
? p
->name
: "static",
1153 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1154 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1158 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1160 p
->name
? p
->name
: "static",
1164 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1170 static const struct file_operations dapm_widget_power_fops
= {
1171 .open
= dapm_widget_power_open_file
,
1172 .read
= dapm_widget_power_read_file
,
1175 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1177 struct snd_soc_dapm_widget
*w
;
1180 if (!codec
->debugfs_dapm
)
1183 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1187 d
= debugfs_create_file(w
->name
, 0444,
1188 codec
->debugfs_dapm
, w
,
1189 &dapm_widget_power_fops
);
1192 "ASoC: Failed to create %s debugfs file\n",
1197 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1202 /* test and update the power status of a mux widget */
1203 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1204 struct snd_kcontrol
*kcontrol
, int mask
,
1205 int mux
, int val
, struct soc_enum
*e
)
1207 struct snd_soc_dapm_path
*path
;
1210 if (widget
->id
!= snd_soc_dapm_mux
&&
1211 widget
->id
!= snd_soc_dapm_value_mux
)
1214 if (!snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
))
1217 /* find dapm widget path assoc with kcontrol */
1218 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1219 if (path
->kcontrol
!= kcontrol
)
1222 if (!path
->name
|| !e
->texts
[mux
])
1226 /* we now need to match the string in the enum to the path */
1227 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1228 path
->connect
= 1; /* new connection */
1230 path
->connect
= 0; /* old connection must be powered down */
1234 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1235 dump_dapm(widget
->codec
, "mux power update");
1241 /* test and update the power status of a mixer or switch widget */
1242 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1243 struct snd_kcontrol
*kcontrol
, int reg
,
1244 int val_mask
, int val
, int invert
)
1246 struct snd_soc_dapm_path
*path
;
1249 if (widget
->id
!= snd_soc_dapm_mixer
&&
1250 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1251 widget
->id
!= snd_soc_dapm_switch
)
1254 if (!snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
))
1257 /* find dapm widget path assoc with kcontrol */
1258 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1259 if (path
->kcontrol
!= kcontrol
)
1262 /* found, now check type */
1265 /* new connection */
1266 path
->connect
= invert
? 0:1;
1268 /* old connection must be powered down */
1269 path
->connect
= invert
? 1:0;
1274 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1275 dump_dapm(widget
->codec
, "mixer power update");
1281 /* show dapm widget status in sys fs */
1282 static ssize_t
dapm_widget_show(struct device
*dev
,
1283 struct device_attribute
*attr
, char *buf
)
1285 struct snd_soc_device
*devdata
= dev_get_drvdata(dev
);
1286 struct snd_soc_codec
*codec
= devdata
->card
->codec
;
1287 struct snd_soc_dapm_widget
*w
;
1289 char *state
= "not set";
1291 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1293 /* only display widgets that burnm power */
1295 case snd_soc_dapm_hp
:
1296 case snd_soc_dapm_mic
:
1297 case snd_soc_dapm_spk
:
1298 case snd_soc_dapm_line
:
1299 case snd_soc_dapm_micbias
:
1300 case snd_soc_dapm_dac
:
1301 case snd_soc_dapm_adc
:
1302 case snd_soc_dapm_pga
:
1303 case snd_soc_dapm_mixer
:
1304 case snd_soc_dapm_mixer_named_ctl
:
1305 case snd_soc_dapm_supply
:
1307 count
+= sprintf(buf
+ count
, "%s: %s\n",
1308 w
->name
, w
->power
? "On":"Off");
1315 switch (codec
->bias_level
) {
1316 case SND_SOC_BIAS_ON
:
1319 case SND_SOC_BIAS_PREPARE
:
1322 case SND_SOC_BIAS_STANDBY
:
1325 case SND_SOC_BIAS_OFF
:
1329 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1334 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1336 int snd_soc_dapm_sys_add(struct device
*dev
)
1338 return device_create_file(dev
, &dev_attr_dapm_widget
);
1341 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1343 device_remove_file(dev
, &dev_attr_dapm_widget
);
1346 /* free all dapm widgets and resources */
1347 static void dapm_free_widgets(struct snd_soc_codec
*codec
)
1349 struct snd_soc_dapm_widget
*w
, *next_w
;
1350 struct snd_soc_dapm_path
*p
, *next_p
;
1352 list_for_each_entry_safe(w
, next_w
, &codec
->dapm_widgets
, list
) {
1357 list_for_each_entry_safe(p
, next_p
, &codec
->dapm_paths
, list
) {
1359 kfree(p
->long_name
);
1364 static int snd_soc_dapm_set_pin(struct snd_soc_codec
*codec
,
1365 const char *pin
, int status
)
1367 struct snd_soc_dapm_widget
*w
;
1369 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1370 if (!strcmp(w
->name
, pin
)) {
1371 pr_debug("dapm: %s: pin %s\n", codec
->name
, pin
);
1372 w
->connected
= status
;
1377 pr_err("dapm: %s: configuring unknown pin %s\n", codec
->name
, pin
);
1382 * snd_soc_dapm_sync - scan and power dapm paths
1383 * @codec: audio codec
1385 * Walks all dapm audio paths and powers widgets according to their
1386 * stream or path usage.
1388 * Returns 0 for success.
1390 int snd_soc_dapm_sync(struct snd_soc_codec
*codec
)
1392 int ret
= dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1393 dump_dapm(codec
, "sync");
1396 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1398 static int snd_soc_dapm_add_route(struct snd_soc_codec
*codec
,
1399 const struct snd_soc_dapm_route
*route
)
1401 struct snd_soc_dapm_path
*path
;
1402 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1403 const char *sink
= route
->sink
;
1404 const char *control
= route
->control
;
1405 const char *source
= route
->source
;
1408 /* find src and dest widgets */
1409 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1411 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1415 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1420 if (wsource
== NULL
|| wsink
== NULL
)
1423 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1427 path
->source
= wsource
;
1429 path
->connected
= route
->connected
;
1430 INIT_LIST_HEAD(&path
->list
);
1431 INIT_LIST_HEAD(&path
->list_source
);
1432 INIT_LIST_HEAD(&path
->list_sink
);
1434 /* check for external widgets */
1435 if (wsink
->id
== snd_soc_dapm_input
) {
1436 if (wsource
->id
== snd_soc_dapm_micbias
||
1437 wsource
->id
== snd_soc_dapm_mic
||
1438 wsource
->id
== snd_soc_dapm_line
||
1439 wsource
->id
== snd_soc_dapm_output
)
1442 if (wsource
->id
== snd_soc_dapm_output
) {
1443 if (wsink
->id
== snd_soc_dapm_spk
||
1444 wsink
->id
== snd_soc_dapm_hp
||
1445 wsink
->id
== snd_soc_dapm_line
||
1446 wsink
->id
== snd_soc_dapm_input
)
1450 /* connect static paths */
1451 if (control
== NULL
) {
1452 list_add(&path
->list
, &codec
->dapm_paths
);
1453 list_add(&path
->list_sink
, &wsink
->sources
);
1454 list_add(&path
->list_source
, &wsource
->sinks
);
1459 /* connect dynamic paths */
1461 case snd_soc_dapm_adc
:
1462 case snd_soc_dapm_dac
:
1463 case snd_soc_dapm_pga
:
1464 case snd_soc_dapm_input
:
1465 case snd_soc_dapm_output
:
1466 case snd_soc_dapm_micbias
:
1467 case snd_soc_dapm_vmid
:
1468 case snd_soc_dapm_pre
:
1469 case snd_soc_dapm_post
:
1470 case snd_soc_dapm_supply
:
1471 case snd_soc_dapm_aif_in
:
1472 case snd_soc_dapm_aif_out
:
1473 list_add(&path
->list
, &codec
->dapm_paths
);
1474 list_add(&path
->list_sink
, &wsink
->sources
);
1475 list_add(&path
->list_source
, &wsource
->sinks
);
1478 case snd_soc_dapm_mux
:
1479 case snd_soc_dapm_value_mux
:
1480 ret
= dapm_connect_mux(codec
, wsource
, wsink
, path
, control
,
1481 &wsink
->kcontrols
[0]);
1485 case snd_soc_dapm_switch
:
1486 case snd_soc_dapm_mixer
:
1487 case snd_soc_dapm_mixer_named_ctl
:
1488 ret
= dapm_connect_mixer(codec
, wsource
, wsink
, path
, control
);
1492 case snd_soc_dapm_hp
:
1493 case snd_soc_dapm_mic
:
1494 case snd_soc_dapm_line
:
1495 case snd_soc_dapm_spk
:
1496 list_add(&path
->list
, &codec
->dapm_paths
);
1497 list_add(&path
->list_sink
, &wsink
->sources
);
1498 list_add(&path
->list_source
, &wsource
->sinks
);
1505 printk(KERN_WARNING
"asoc: no dapm match for %s --> %s --> %s\n", source
,
1512 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1514 * @route: audio routes
1515 * @num: number of routes
1517 * Connects 2 dapm widgets together via a named audio path. The sink is
1518 * the widget receiving the audio signal, whilst the source is the sender
1519 * of the audio signal.
1521 * Returns 0 for success else error. On error all resources can be freed
1522 * with a call to snd_soc_card_free().
1524 int snd_soc_dapm_add_routes(struct snd_soc_codec
*codec
,
1525 const struct snd_soc_dapm_route
*route
, int num
)
1529 for (i
= 0; i
< num
; i
++) {
1530 ret
= snd_soc_dapm_add_route(codec
, route
);
1532 printk(KERN_ERR
"Failed to add route %s->%s\n",
1542 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1545 * snd_soc_dapm_new_widgets - add new dapm widgets
1546 * @codec: audio codec
1548 * Checks the codec for any new dapm widgets and creates them if found.
1550 * Returns 0 for success.
1552 int snd_soc_dapm_new_widgets(struct snd_soc_codec
*codec
)
1554 struct snd_soc_dapm_widget
*w
;
1556 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
1562 case snd_soc_dapm_switch
:
1563 case snd_soc_dapm_mixer
:
1564 case snd_soc_dapm_mixer_named_ctl
:
1565 w
->power_check
= dapm_generic_check_power
;
1566 dapm_new_mixer(codec
, w
);
1568 case snd_soc_dapm_mux
:
1569 case snd_soc_dapm_value_mux
:
1570 w
->power_check
= dapm_generic_check_power
;
1571 dapm_new_mux(codec
, w
);
1573 case snd_soc_dapm_adc
:
1574 case snd_soc_dapm_aif_out
:
1575 w
->power_check
= dapm_adc_check_power
;
1577 case snd_soc_dapm_dac
:
1578 case snd_soc_dapm_aif_in
:
1579 w
->power_check
= dapm_dac_check_power
;
1581 case snd_soc_dapm_pga
:
1582 w
->power_check
= dapm_generic_check_power
;
1583 dapm_new_pga(codec
, w
);
1585 case snd_soc_dapm_input
:
1586 case snd_soc_dapm_output
:
1587 case snd_soc_dapm_micbias
:
1588 case snd_soc_dapm_spk
:
1589 case snd_soc_dapm_hp
:
1590 case snd_soc_dapm_mic
:
1591 case snd_soc_dapm_line
:
1592 w
->power_check
= dapm_generic_check_power
;
1594 case snd_soc_dapm_supply
:
1595 w
->power_check
= dapm_supply_check_power
;
1596 case snd_soc_dapm_vmid
:
1597 case snd_soc_dapm_pre
:
1598 case snd_soc_dapm_post
:
1604 dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1607 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1610 * snd_soc_dapm_get_volsw - dapm mixer get callback
1611 * @kcontrol: mixer control
1612 * @ucontrol: control element information
1614 * Callback to get the value of a dapm mixer control.
1616 * Returns 0 for success.
1618 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1619 struct snd_ctl_elem_value
*ucontrol
)
1621 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1622 struct soc_mixer_control
*mc
=
1623 (struct soc_mixer_control
*)kcontrol
->private_value
;
1624 unsigned int reg
= mc
->reg
;
1625 unsigned int shift
= mc
->shift
;
1626 unsigned int rshift
= mc
->rshift
;
1628 unsigned int invert
= mc
->invert
;
1629 unsigned int mask
= (1 << fls(max
)) - 1;
1631 /* return the saved value if we are powered down */
1632 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1633 ucontrol
->value
.integer
.value
[0] = widget
->saved_value
;
1637 ucontrol
->value
.integer
.value
[0] =
1638 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1639 if (shift
!= rshift
)
1640 ucontrol
->value
.integer
.value
[1] =
1641 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1643 ucontrol
->value
.integer
.value
[0] =
1644 max
- ucontrol
->value
.integer
.value
[0];
1645 if (shift
!= rshift
)
1646 ucontrol
->value
.integer
.value
[1] =
1647 max
- ucontrol
->value
.integer
.value
[1];
1652 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1655 * snd_soc_dapm_put_volsw - dapm mixer set callback
1656 * @kcontrol: mixer control
1657 * @ucontrol: control element information
1659 * Callback to set the value of a dapm mixer control.
1661 * Returns 0 for success.
1663 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1664 struct snd_ctl_elem_value
*ucontrol
)
1666 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1667 struct soc_mixer_control
*mc
=
1668 (struct soc_mixer_control
*)kcontrol
->private_value
;
1669 unsigned int reg
= mc
->reg
;
1670 unsigned int shift
= mc
->shift
;
1671 unsigned int rshift
= mc
->rshift
;
1673 unsigned int mask
= (1 << fls(max
)) - 1;
1674 unsigned int invert
= mc
->invert
;
1675 unsigned int val
, val2
, val_mask
;
1678 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1682 val_mask
= mask
<< shift
;
1684 if (shift
!= rshift
) {
1685 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1688 val_mask
|= mask
<< rshift
;
1689 val
|= val2
<< rshift
;
1692 mutex_lock(&widget
->codec
->mutex
);
1693 widget
->value
= val
;
1695 /* save volume value if the widget is powered down */
1696 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1697 widget
->saved_value
= val
;
1698 mutex_unlock(&widget
->codec
->mutex
);
1702 dapm_mixer_update_power(widget
, kcontrol
, reg
, val_mask
, val
, invert
);
1703 if (widget
->event
) {
1704 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1705 ret
= widget
->event(widget
, kcontrol
,
1706 SND_SOC_DAPM_PRE_REG
);
1712 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1713 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1714 ret
= widget
->event(widget
, kcontrol
,
1715 SND_SOC_DAPM_POST_REG
);
1717 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1720 mutex_unlock(&widget
->codec
->mutex
);
1723 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1726 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1727 * @kcontrol: mixer control
1728 * @ucontrol: control element information
1730 * Callback to get the value of a dapm enumerated double mixer control.
1732 * Returns 0 for success.
1734 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1735 struct snd_ctl_elem_value
*ucontrol
)
1737 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1738 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1739 unsigned int val
, bitmask
;
1741 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1743 val
= snd_soc_read(widget
->codec
, e
->reg
);
1744 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1745 if (e
->shift_l
!= e
->shift_r
)
1746 ucontrol
->value
.enumerated
.item
[1] =
1747 (val
>> e
->shift_r
) & (bitmask
- 1);
1751 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1754 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1755 * @kcontrol: mixer control
1756 * @ucontrol: control element information
1758 * Callback to set the value of a dapm enumerated double mixer control.
1760 * Returns 0 for success.
1762 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1763 struct snd_ctl_elem_value
*ucontrol
)
1765 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1766 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1767 unsigned int val
, mux
;
1768 unsigned int mask
, bitmask
;
1771 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1773 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1775 mux
= ucontrol
->value
.enumerated
.item
[0];
1776 val
= mux
<< e
->shift_l
;
1777 mask
= (bitmask
- 1) << e
->shift_l
;
1778 if (e
->shift_l
!= e
->shift_r
) {
1779 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1781 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1782 mask
|= (bitmask
- 1) << e
->shift_r
;
1785 mutex_lock(&widget
->codec
->mutex
);
1786 widget
->value
= val
;
1787 dapm_mux_update_power(widget
, kcontrol
, mask
, mux
, val
, e
);
1788 if (widget
->event
) {
1789 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1790 ret
= widget
->event(widget
,
1791 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1795 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1796 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1797 ret
= widget
->event(widget
,
1798 kcontrol
, SND_SOC_DAPM_POST_REG
);
1800 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1803 mutex_unlock(&widget
->codec
->mutex
);
1806 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1809 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1811 * @kcontrol: mixer control
1812 * @ucontrol: control element information
1814 * Callback to get the value of a dapm semi enumerated double mixer control.
1816 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1817 * used for handling bitfield coded enumeration for example.
1819 * Returns 0 for success.
1821 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1822 struct snd_ctl_elem_value
*ucontrol
)
1824 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1825 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1826 unsigned int reg_val
, val
, mux
;
1828 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1829 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1830 for (mux
= 0; mux
< e
->max
; mux
++) {
1831 if (val
== e
->values
[mux
])
1834 ucontrol
->value
.enumerated
.item
[0] = mux
;
1835 if (e
->shift_l
!= e
->shift_r
) {
1836 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1837 for (mux
= 0; mux
< e
->max
; mux
++) {
1838 if (val
== e
->values
[mux
])
1841 ucontrol
->value
.enumerated
.item
[1] = mux
;
1846 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1849 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1851 * @kcontrol: mixer control
1852 * @ucontrol: control element information
1854 * Callback to set the value of a dapm semi enumerated double mixer control.
1856 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1857 * used for handling bitfield coded enumeration for example.
1859 * Returns 0 for success.
1861 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
1862 struct snd_ctl_elem_value
*ucontrol
)
1864 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1865 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1866 unsigned int val
, mux
;
1870 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1872 mux
= ucontrol
->value
.enumerated
.item
[0];
1873 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1874 mask
= e
->mask
<< e
->shift_l
;
1875 if (e
->shift_l
!= e
->shift_r
) {
1876 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1878 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1879 mask
|= e
->mask
<< e
->shift_r
;
1882 mutex_lock(&widget
->codec
->mutex
);
1883 widget
->value
= val
;
1884 dapm_mux_update_power(widget
, kcontrol
, mask
, mux
, val
, e
);
1885 if (widget
->event
) {
1886 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1887 ret
= widget
->event(widget
,
1888 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1892 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1893 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1894 ret
= widget
->event(widget
,
1895 kcontrol
, SND_SOC_DAPM_POST_REG
);
1897 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1900 mutex_unlock(&widget
->codec
->mutex
);
1903 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1906 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1908 * @kcontrol: mixer control
1909 * @uinfo: control element information
1911 * Callback to provide information about a pin switch control.
1913 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1914 struct snd_ctl_elem_info
*uinfo
)
1916 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1918 uinfo
->value
.integer
.min
= 0;
1919 uinfo
->value
.integer
.max
= 1;
1923 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1926 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1928 * @kcontrol: mixer control
1931 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1932 struct snd_ctl_elem_value
*ucontrol
)
1934 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1935 const char *pin
= (const char *)kcontrol
->private_value
;
1937 mutex_lock(&codec
->mutex
);
1939 ucontrol
->value
.integer
.value
[0] =
1940 snd_soc_dapm_get_pin_status(codec
, pin
);
1942 mutex_unlock(&codec
->mutex
);
1946 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
1949 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1951 * @kcontrol: mixer control
1954 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
1955 struct snd_ctl_elem_value
*ucontrol
)
1957 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1958 const char *pin
= (const char *)kcontrol
->private_value
;
1960 mutex_lock(&codec
->mutex
);
1962 if (ucontrol
->value
.integer
.value
[0])
1963 snd_soc_dapm_enable_pin(codec
, pin
);
1965 snd_soc_dapm_disable_pin(codec
, pin
);
1967 snd_soc_dapm_sync(codec
);
1969 mutex_unlock(&codec
->mutex
);
1973 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
1976 * snd_soc_dapm_new_control - create new dapm control
1977 * @codec: audio codec
1978 * @widget: widget template
1980 * Creates a new dapm control based upon the template.
1982 * Returns 0 for success else error.
1984 int snd_soc_dapm_new_control(struct snd_soc_codec
*codec
,
1985 const struct snd_soc_dapm_widget
*widget
)
1987 struct snd_soc_dapm_widget
*w
;
1989 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
1993 INIT_LIST_HEAD(&w
->sources
);
1994 INIT_LIST_HEAD(&w
->sinks
);
1995 INIT_LIST_HEAD(&w
->list
);
1996 list_add(&w
->list
, &codec
->dapm_widgets
);
1998 /* machine layer set ups unconnected pins and insertions */
2002 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
2005 * snd_soc_dapm_new_controls - create new dapm controls
2006 * @codec: audio codec
2007 * @widget: widget array
2008 * @num: number of widgets
2010 * Creates new DAPM controls based upon the templates.
2012 * Returns 0 for success else error.
2014 int snd_soc_dapm_new_controls(struct snd_soc_codec
*codec
,
2015 const struct snd_soc_dapm_widget
*widget
,
2020 for (i
= 0; i
< num
; i
++) {
2021 ret
= snd_soc_dapm_new_control(codec
, widget
);
2024 "ASoC: Failed to create DAPM control %s: %d\n",
2032 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2036 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2037 * @codec: audio codec
2038 * @stream: stream name
2039 * @event: stream event
2041 * Sends a stream event to the dapm core. The core then makes any
2042 * necessary widget power changes.
2044 * Returns 0 for success else error.
2046 int snd_soc_dapm_stream_event(struct snd_soc_codec
*codec
,
2047 char *stream
, int event
)
2049 struct snd_soc_dapm_widget
*w
;
2054 mutex_lock(&codec
->mutex
);
2055 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
2059 pr_debug("widget %s\n %s stream %s event %d\n",
2060 w
->name
, w
->sname
, stream
, event
);
2061 if (strstr(w
->sname
, stream
)) {
2063 case SND_SOC_DAPM_STREAM_START
:
2066 case SND_SOC_DAPM_STREAM_STOP
:
2069 case SND_SOC_DAPM_STREAM_SUSPEND
:
2074 case SND_SOC_DAPM_STREAM_RESUME
:
2080 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2082 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2087 mutex_unlock(&codec
->mutex
);
2089 dapm_power_widgets(codec
, event
);
2090 dump_dapm(codec
, __func__
);
2093 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
2096 * snd_soc_dapm_enable_pin - enable pin.
2100 * Enables input/output pin and its parents or children widgets iff there is
2101 * a valid audio route and active audio stream.
2102 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2103 * do any widget power switching.
2105 int snd_soc_dapm_enable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2107 return snd_soc_dapm_set_pin(codec
, pin
, 1);
2109 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2112 * snd_soc_dapm_disable_pin - disable pin.
2116 * Disables input/output pin and its parents or children widgets.
2117 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2118 * do any widget power switching.
2120 int snd_soc_dapm_disable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2122 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2124 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2127 * snd_soc_dapm_nc_pin - permanently disable pin.
2131 * Marks the specified pin as being not connected, disabling it along
2132 * any parent or child widgets. At present this is identical to
2133 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2134 * additional things such as disabling controls which only affect
2135 * paths through the pin.
2137 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2138 * do any widget power switching.
2140 int snd_soc_dapm_nc_pin(struct snd_soc_codec
*codec
, const char *pin
)
2142 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2144 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2147 * snd_soc_dapm_get_pin_status - get audio pin status
2148 * @codec: audio codec
2149 * @pin: audio signal pin endpoint (or start point)
2151 * Get audio pin status - connected or disconnected.
2153 * Returns 1 for connected otherwise 0.
2155 int snd_soc_dapm_get_pin_status(struct snd_soc_codec
*codec
, const char *pin
)
2157 struct snd_soc_dapm_widget
*w
;
2159 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2160 if (!strcmp(w
->name
, pin
))
2161 return w
->connected
;
2166 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2169 * snd_soc_dapm_free - free dapm resources
2170 * @socdev: SoC device
2172 * Free all dapm widgets and resources.
2174 void snd_soc_dapm_free(struct snd_soc_device
*socdev
)
2176 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2178 snd_soc_dapm_sys_remove(socdev
->dev
);
2179 dapm_free_widgets(codec
);
2181 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2184 * snd_soc_dapm_shutdown - callback for system shutdown
2186 void snd_soc_dapm_shutdown(struct snd_soc_device
*socdev
)
2188 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2189 struct snd_soc_dapm_widget
*w
;
2190 LIST_HEAD(down_list
);
2193 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2195 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2201 /* If there were no widgets to power down we're already in
2205 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_PREPARE
);
2206 dapm_seq_run(codec
, &down_list
, 0, dapm_down_seq
);
2207 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_STANDBY
);
2210 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_OFF
);
2213 /* Module information */
2214 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2215 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2216 MODULE_LICENSE("GPL");