2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
25 * o DAPM power change sequencing - allow for configurable per
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
37 #include <linux/bitops.h>
38 #include <linux/platform_device.h>
39 #include <linux/jiffies.h>
40 #include <linux/debugfs.h>
41 #include <linux/slab.h>
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc-dapm.h>
46 #include <sound/initval.h>
48 /* dapm power sequences - make this per codec in the future */
49 static int dapm_up_seq
[] = {
50 [snd_soc_dapm_pre
] = 0,
51 [snd_soc_dapm_supply
] = 1,
52 [snd_soc_dapm_micbias
] = 2,
53 [snd_soc_dapm_aif_in
] = 3,
54 [snd_soc_dapm_aif_out
] = 3,
55 [snd_soc_dapm_mic
] = 4,
56 [snd_soc_dapm_mux
] = 5,
57 [snd_soc_dapm_value_mux
] = 5,
58 [snd_soc_dapm_dac
] = 6,
59 [snd_soc_dapm_mixer
] = 7,
60 [snd_soc_dapm_mixer_named_ctl
] = 7,
61 [snd_soc_dapm_pga
] = 8,
62 [snd_soc_dapm_adc
] = 9,
63 [snd_soc_dapm_hp
] = 10,
64 [snd_soc_dapm_spk
] = 10,
65 [snd_soc_dapm_post
] = 11,
68 static int dapm_down_seq
[] = {
69 [snd_soc_dapm_pre
] = 0,
70 [snd_soc_dapm_adc
] = 1,
71 [snd_soc_dapm_hp
] = 2,
72 [snd_soc_dapm_spk
] = 2,
73 [snd_soc_dapm_pga
] = 4,
74 [snd_soc_dapm_mixer_named_ctl
] = 5,
75 [snd_soc_dapm_mixer
] = 5,
76 [snd_soc_dapm_dac
] = 6,
77 [snd_soc_dapm_mic
] = 7,
78 [snd_soc_dapm_micbias
] = 8,
79 [snd_soc_dapm_mux
] = 9,
80 [snd_soc_dapm_value_mux
] = 9,
81 [snd_soc_dapm_aif_in
] = 10,
82 [snd_soc_dapm_aif_out
] = 10,
83 [snd_soc_dapm_supply
] = 11,
84 [snd_soc_dapm_post
] = 12,
87 static void pop_wait(u32 pop_time
)
90 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
93 static void pop_dbg(u32 pop_time
, const char *fmt
, ...)
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 pop_wait(codec
->pop_time
);
318 snd_soc_write(codec
, widget
->reg
, new);
320 pr_debug("reg %x old %x new %x change %d\n", widget
->reg
,
325 /* create new dapm mixer control */
326 static int dapm_new_mixer(struct snd_soc_codec
*codec
,
327 struct snd_soc_dapm_widget
*w
)
331 struct snd_soc_dapm_path
*path
;
334 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
337 list_for_each_entry(path
, &w
->sources
, list_sink
) {
339 /* mixer/mux paths name must match control name */
340 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
343 /* add dapm control with long name.
344 * for dapm_mixer this is the concatenation of the
345 * mixer and kcontrol name.
346 * for dapm_mixer_named_ctl this is simply the
349 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
350 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
351 name_len
+= 1 + strlen(w
->name
);
353 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
355 if (path
->long_name
== NULL
)
360 snprintf(path
->long_name
, name_len
, "%s %s",
361 w
->name
, w
->kcontrols
[i
].name
);
363 case snd_soc_dapm_mixer_named_ctl
:
364 snprintf(path
->long_name
, name_len
, "%s",
365 w
->kcontrols
[i
].name
);
369 path
->long_name
[name_len
- 1] = '\0';
371 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
373 ret
= snd_ctl_add(codec
->card
, path
->kcontrol
);
375 printk(KERN_ERR
"asoc: failed to add dapm kcontrol %s: %d\n",
378 kfree(path
->long_name
);
379 path
->long_name
= NULL
;
387 /* create new dapm mux control */
388 static int dapm_new_mux(struct snd_soc_codec
*codec
,
389 struct snd_soc_dapm_widget
*w
)
391 struct snd_soc_dapm_path
*path
= NULL
;
392 struct snd_kcontrol
*kcontrol
;
395 if (!w
->num_kcontrols
) {
396 printk(KERN_ERR
"asoc: mux %s has no controls\n", w
->name
);
400 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
401 ret
= snd_ctl_add(codec
->card
, kcontrol
);
405 list_for_each_entry(path
, &w
->sources
, list_sink
)
406 path
->kcontrol
= kcontrol
;
411 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
415 /* create new dapm volume control */
416 static int dapm_new_pga(struct snd_soc_codec
*codec
,
417 struct snd_soc_dapm_widget
*w
)
419 if (w
->num_kcontrols
)
420 pr_err("asoc: PGA controls not supported: '%s'\n", w
->name
);
425 /* reset 'walked' bit for each dapm path */
426 static inline void dapm_clear_walk(struct snd_soc_codec
*codec
)
428 struct snd_soc_dapm_path
*p
;
430 list_for_each_entry(p
, &codec
->dapm_paths
, list
)
434 /* We implement power down on suspend by checking the power state of
435 * the ALSA card - when we are suspending the ALSA state for the card
438 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
440 struct snd_soc_codec
*codec
= widget
->codec
;
442 switch (snd_power_get_state(codec
->card
)) {
443 case SNDRV_CTL_POWER_D3hot
:
444 case SNDRV_CTL_POWER_D3cold
:
445 if (widget
->ignore_suspend
)
446 pr_debug("%s ignoring suspend\n", widget
->name
);
447 return widget
->ignore_suspend
;
454 * Recursively check for a completed path to an active or physically connected
455 * output widget. Returns number of complete paths.
457 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
459 struct snd_soc_dapm_path
*path
;
462 if (widget
->id
== snd_soc_dapm_supply
)
465 switch (widget
->id
) {
466 case snd_soc_dapm_adc
:
467 case snd_soc_dapm_aif_out
:
469 return snd_soc_dapm_suspend_check(widget
);
474 if (widget
->connected
) {
475 /* connected pin ? */
476 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
477 return snd_soc_dapm_suspend_check(widget
);
479 /* connected jack or spk ? */
480 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
481 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sources
)))
482 return snd_soc_dapm_suspend_check(widget
);
485 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
489 if (path
->sink
&& path
->connect
) {
491 con
+= is_connected_output_ep(path
->sink
);
499 * Recursively check for a completed path to an active or physically connected
500 * input widget. Returns number of complete paths.
502 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
504 struct snd_soc_dapm_path
*path
;
507 if (widget
->id
== snd_soc_dapm_supply
)
510 /* active stream ? */
511 switch (widget
->id
) {
512 case snd_soc_dapm_dac
:
513 case snd_soc_dapm_aif_in
:
515 return snd_soc_dapm_suspend_check(widget
);
520 if (widget
->connected
) {
521 /* connected pin ? */
522 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
523 return snd_soc_dapm_suspend_check(widget
);
525 /* connected VMID/Bias for lower pops */
526 if (widget
->id
== snd_soc_dapm_vmid
)
527 return snd_soc_dapm_suspend_check(widget
);
529 /* connected jack ? */
530 if (widget
->id
== snd_soc_dapm_mic
||
531 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sinks
)))
532 return snd_soc_dapm_suspend_check(widget
);
535 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
539 if (path
->source
&& path
->connect
) {
541 con
+= is_connected_input_ep(path
->source
);
549 * Handler for generic register modifier widget.
551 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
552 struct snd_kcontrol
*kcontrol
, int event
)
556 if (SND_SOC_DAPM_EVENT_ON(event
))
561 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
562 w
->mask
<< w
->shift
, val
<< w
->shift
);
566 EXPORT_SYMBOL_GPL(dapm_reg_event
);
568 /* Standard power change method, used to apply power changes to most
571 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
575 /* call any power change event handlers */
577 pr_debug("power %s event for %s flags %x\n",
578 w
->power
? "on" : "off",
579 w
->name
, w
->event_flags
);
581 /* power up pre event */
582 if (w
->power
&& w
->event
&&
583 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
584 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
589 /* power down pre event */
590 if (!w
->power
&& w
->event
&&
591 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
592 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
599 /* power up post event */
600 if (w
->power
&& w
->event
&&
601 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
603 NULL
, SND_SOC_DAPM_POST_PMU
);
608 /* power down post event */
609 if (!w
->power
&& w
->event
&&
610 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
611 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
619 /* Generic check to see if a widget should be powered.
621 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
625 in
= is_connected_input_ep(w
);
626 dapm_clear_walk(w
->codec
);
627 out
= is_connected_output_ep(w
);
628 dapm_clear_walk(w
->codec
);
629 return out
!= 0 && in
!= 0;
632 /* Check to see if an ADC has power */
633 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
638 in
= is_connected_input_ep(w
);
639 dapm_clear_walk(w
->codec
);
642 return dapm_generic_check_power(w
);
646 /* Check to see if a DAC has power */
647 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
652 out
= is_connected_output_ep(w
);
653 dapm_clear_walk(w
->codec
);
656 return dapm_generic_check_power(w
);
660 /* Check to see if a power supply is needed */
661 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
663 struct snd_soc_dapm_path
*path
;
666 /* Check if one of our outputs is connected */
667 list_for_each_entry(path
, &w
->sinks
, list_source
) {
668 if (path
->connected
&&
669 !path
->connected(path
->source
, path
->sink
))
672 if (path
->sink
&& path
->sink
->power_check
&&
673 path
->sink
->power_check(path
->sink
)) {
679 dapm_clear_walk(w
->codec
);
684 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
685 struct snd_soc_dapm_widget
*b
,
688 if (a
->codec
!= b
->codec
)
689 return (unsigned long)a
- (unsigned long)b
;
690 if (sort
[a
->id
] != sort
[b
->id
])
691 return sort
[a
->id
] - sort
[b
->id
];
692 if (a
->reg
!= b
->reg
)
693 return a
->reg
- b
->reg
;
698 /* Insert a widget in order into a DAPM power sequence. */
699 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
700 struct list_head
*list
,
703 struct snd_soc_dapm_widget
*w
;
705 list_for_each_entry(w
, list
, power_list
)
706 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
707 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
711 list_add_tail(&new_widget
->power_list
, list
);
714 /* Apply the coalesced changes from a DAPM sequence */
715 static void dapm_seq_run_coalesced(struct snd_soc_codec
*codec
,
716 struct list_head
*pending
)
718 struct snd_soc_dapm_widget
*w
;
720 unsigned int value
= 0;
721 unsigned int mask
= 0;
722 unsigned int cur_mask
;
724 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
727 list_for_each_entry(w
, pending
, power_list
) {
728 cur_mask
= 1 << w
->shift
;
729 BUG_ON(reg
!= w
->reg
);
740 pop_dbg(codec
->pop_time
,
741 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
742 w
->name
, reg
, value
, mask
);
744 /* power up pre event */
745 if (w
->power
&& w
->event
&&
746 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
747 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMU\n",
749 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
751 pr_err("%s: pre event failed: %d\n",
755 /* power down pre event */
756 if (!w
->power
&& w
->event
&&
757 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
758 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMD\n",
760 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
762 pr_err("%s: pre event failed: %d\n",
768 pop_dbg(codec
->pop_time
,
769 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
770 value
, mask
, reg
, codec
->pop_time
);
771 pop_wait(codec
->pop_time
);
772 snd_soc_update_bits(codec
, reg
, mask
, value
);
775 list_for_each_entry(w
, pending
, power_list
) {
776 /* power up post event */
777 if (w
->power
&& w
->event
&&
778 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
779 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMU\n",
782 NULL
, SND_SOC_DAPM_POST_PMU
);
784 pr_err("%s: post event failed: %d\n",
788 /* power down post event */
789 if (!w
->power
&& w
->event
&&
790 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
791 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMD\n",
793 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
795 pr_err("%s: post event failed: %d\n",
801 /* Apply a DAPM power sequence.
803 * We walk over a pre-sorted list of widgets to apply power to. In
804 * order to minimise the number of writes to the device required
805 * multiple widgets will be updated in a single write where possible.
806 * Currently anything that requires more than a single write is not
809 static void dapm_seq_run(struct snd_soc_codec
*codec
, struct list_head
*list
,
810 int event
, int sort
[])
812 struct snd_soc_dapm_widget
*w
, *n
;
815 int cur_reg
= SND_SOC_NOPM
;
818 list_for_each_entry_safe(w
, n
, list
, power_list
) {
821 /* Do we need to apply any queued changes? */
822 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
823 if (!list_empty(&pending
))
824 dapm_seq_run_coalesced(codec
, &pending
);
826 INIT_LIST_HEAD(&pending
);
828 cur_reg
= SND_SOC_NOPM
;
832 case snd_soc_dapm_pre
:
834 list_for_each_entry_safe_continue(w
, n
, list
,
837 if (event
== SND_SOC_DAPM_STREAM_START
)
839 NULL
, SND_SOC_DAPM_PRE_PMU
);
840 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
842 NULL
, SND_SOC_DAPM_PRE_PMD
);
845 case snd_soc_dapm_post
:
847 list_for_each_entry_safe_continue(w
, n
, list
,
850 if (event
== SND_SOC_DAPM_STREAM_START
)
852 NULL
, SND_SOC_DAPM_POST_PMU
);
853 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
855 NULL
, SND_SOC_DAPM_POST_PMD
);
858 case snd_soc_dapm_input
:
859 case snd_soc_dapm_output
:
860 case snd_soc_dapm_hp
:
861 case snd_soc_dapm_mic
:
862 case snd_soc_dapm_line
:
863 case snd_soc_dapm_spk
:
864 /* No register support currently */
865 ret
= dapm_generic_apply_power(w
);
869 /* Queue it up for application */
870 cur_sort
= sort
[w
->id
];
872 list_move(&w
->power_list
, &pending
);
877 pr_err("Failed to apply widget power: %d\n",
881 if (!list_empty(&pending
))
882 dapm_seq_run_coalesced(codec
, &pending
);
886 * Scan each dapm widget for complete audio path.
887 * A complete path is a route that has valid endpoints i.e.:-
889 * o DAC to output pin.
890 * o Input Pin to ADC.
891 * o Input pin to Output pin (bypass, sidetone)
892 * o DAC to ADC (loopback).
894 static int dapm_power_widgets(struct snd_soc_codec
*codec
, int event
)
896 struct snd_soc_device
*socdev
= codec
->socdev
;
897 struct snd_soc_dapm_widget
*w
;
899 LIST_HEAD(down_list
);
904 /* Check which widgets we need to power and store them in
905 * lists indicating if they should be powered up or down.
907 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
909 case snd_soc_dapm_pre
:
910 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
912 case snd_soc_dapm_post
:
913 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
921 power
= w
->power_check(w
);
927 if (w
->power
== power
)
931 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
933 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
940 /* If there are no DAPM widgets then try to figure out power from the
943 if (list_empty(&codec
->dapm_widgets
)) {
945 case SND_SOC_DAPM_STREAM_START
:
946 case SND_SOC_DAPM_STREAM_RESUME
:
949 case SND_SOC_DAPM_STREAM_SUSPEND
:
952 case SND_SOC_DAPM_STREAM_NOP
:
953 switch (codec
->bias_level
) {
954 case SND_SOC_BIAS_STANDBY
:
955 case SND_SOC_BIAS_OFF
:
968 if (sys_power
&& codec
->bias_level
== SND_SOC_BIAS_OFF
) {
969 ret
= snd_soc_dapm_set_bias_level(socdev
,
970 SND_SOC_BIAS_STANDBY
);
972 pr_err("Failed to turn on bias: %d\n", ret
);
975 /* If we're changing to all on or all off then prepare */
976 if ((sys_power
&& codec
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
977 (!sys_power
&& codec
->bias_level
== SND_SOC_BIAS_ON
)) {
978 ret
= snd_soc_dapm_set_bias_level(socdev
,
979 SND_SOC_BIAS_PREPARE
);
981 pr_err("Failed to prepare bias: %d\n", ret
);
984 /* Power down widgets first; try to avoid amplifying pops. */
985 dapm_seq_run(codec
, &down_list
, event
, dapm_down_seq
);
988 dapm_seq_run(codec
, &up_list
, event
, dapm_up_seq
);
990 /* If we just powered the last thing off drop to standby bias */
991 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
992 ret
= snd_soc_dapm_set_bias_level(socdev
,
993 SND_SOC_BIAS_STANDBY
);
995 pr_err("Failed to apply standby bias: %d\n", ret
);
998 /* If we're in standby and can support bias off then do that */
999 if (codec
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1000 codec
->idle_bias_off
) {
1001 ret
= snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_OFF
);
1003 pr_err("Failed to turn off bias: %d\n", ret
);
1006 /* If we just powered up then move to active bias */
1007 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
1008 ret
= snd_soc_dapm_set_bias_level(socdev
,
1011 pr_err("Failed to apply active bias: %d\n", ret
);
1014 pop_dbg(codec
->pop_time
, "DAPM sequencing finished, waiting %dms\n",
1016 pop_wait(codec
->pop_time
);
1021 #ifdef CONFIG_DEBUG_FS
1022 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1024 file
->private_data
= inode
->i_private
;
1028 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1029 char __user
*user_buf
,
1030 size_t count
, loff_t
*ppos
)
1032 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1036 struct snd_soc_dapm_path
*p
= NULL
;
1038 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1042 in
= is_connected_input_ep(w
);
1043 dapm_clear_walk(w
->codec
);
1044 out
= is_connected_output_ep(w
);
1045 dapm_clear_walk(w
->codec
);
1047 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1048 w
->name
, w
->power
? "On" : "Off", in
, out
);
1051 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1052 " - R%d(0x%x) bit %d",
1053 w
->reg
, w
->reg
, w
->shift
);
1055 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1058 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1060 w
->active
? "active" : "inactive");
1062 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1063 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1067 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1069 p
->name
? p
->name
: "static",
1072 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1073 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1077 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1079 p
->name
? p
->name
: "static",
1083 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1089 static const struct file_operations dapm_widget_power_fops
= {
1090 .open
= dapm_widget_power_open_file
,
1091 .read
= dapm_widget_power_read_file
,
1094 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1096 struct snd_soc_dapm_widget
*w
;
1099 if (!codec
->debugfs_dapm
)
1102 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1106 d
= debugfs_create_file(w
->name
, 0444,
1107 codec
->debugfs_dapm
, w
,
1108 &dapm_widget_power_fops
);
1111 "ASoC: Failed to create %s debugfs file\n",
1116 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1121 /* test and update the power status of a mux widget */
1122 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1123 struct snd_kcontrol
*kcontrol
, int change
,
1124 int mux
, struct soc_enum
*e
)
1126 struct snd_soc_dapm_path
*path
;
1129 if (widget
->id
!= snd_soc_dapm_mux
&&
1130 widget
->id
!= snd_soc_dapm_value_mux
)
1136 /* find dapm widget path assoc with kcontrol */
1137 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1138 if (path
->kcontrol
!= kcontrol
)
1141 if (!path
->name
|| !e
->texts
[mux
])
1145 /* we now need to match the string in the enum to the path */
1146 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1147 path
->connect
= 1; /* new connection */
1149 path
->connect
= 0; /* old connection must be powered down */
1153 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1158 /* test and update the power status of a mixer or switch widget */
1159 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1160 struct snd_kcontrol
*kcontrol
, int connect
)
1162 struct snd_soc_dapm_path
*path
;
1165 if (widget
->id
!= snd_soc_dapm_mixer
&&
1166 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1167 widget
->id
!= snd_soc_dapm_switch
)
1170 /* find dapm widget path assoc with kcontrol */
1171 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1172 if (path
->kcontrol
!= kcontrol
)
1175 /* found, now check type */
1177 path
->connect
= connect
;
1182 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1187 /* show dapm widget status in sys fs */
1188 static ssize_t
dapm_widget_show(struct device
*dev
,
1189 struct device_attribute
*attr
, char *buf
)
1191 struct snd_soc_device
*devdata
= dev_get_drvdata(dev
);
1192 struct snd_soc_codec
*codec
= devdata
->card
->codec
;
1193 struct snd_soc_dapm_widget
*w
;
1195 char *state
= "not set";
1197 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1199 /* only display widgets that burnm power */
1201 case snd_soc_dapm_hp
:
1202 case snd_soc_dapm_mic
:
1203 case snd_soc_dapm_spk
:
1204 case snd_soc_dapm_line
:
1205 case snd_soc_dapm_micbias
:
1206 case snd_soc_dapm_dac
:
1207 case snd_soc_dapm_adc
:
1208 case snd_soc_dapm_pga
:
1209 case snd_soc_dapm_mixer
:
1210 case snd_soc_dapm_mixer_named_ctl
:
1211 case snd_soc_dapm_supply
:
1213 count
+= sprintf(buf
+ count
, "%s: %s\n",
1214 w
->name
, w
->power
? "On":"Off");
1221 switch (codec
->bias_level
) {
1222 case SND_SOC_BIAS_ON
:
1225 case SND_SOC_BIAS_PREPARE
:
1228 case SND_SOC_BIAS_STANDBY
:
1231 case SND_SOC_BIAS_OFF
:
1235 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1240 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1242 int snd_soc_dapm_sys_add(struct device
*dev
)
1244 return device_create_file(dev
, &dev_attr_dapm_widget
);
1247 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1249 device_remove_file(dev
, &dev_attr_dapm_widget
);
1252 /* free all dapm widgets and resources */
1253 static void dapm_free_widgets(struct snd_soc_codec
*codec
)
1255 struct snd_soc_dapm_widget
*w
, *next_w
;
1256 struct snd_soc_dapm_path
*p
, *next_p
;
1258 list_for_each_entry_safe(w
, next_w
, &codec
->dapm_widgets
, list
) {
1263 list_for_each_entry_safe(p
, next_p
, &codec
->dapm_paths
, list
) {
1265 kfree(p
->long_name
);
1270 static int snd_soc_dapm_set_pin(struct snd_soc_codec
*codec
,
1271 const char *pin
, int status
)
1273 struct snd_soc_dapm_widget
*w
;
1275 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1276 if (!strcmp(w
->name
, pin
)) {
1277 pr_debug("dapm: %s: pin %s\n", codec
->name
, pin
);
1278 w
->connected
= status
;
1279 /* Allow disabling of forced pins */
1286 pr_err("dapm: %s: configuring unknown pin %s\n", codec
->name
, pin
);
1291 * snd_soc_dapm_sync - scan and power dapm paths
1292 * @codec: audio codec
1294 * Walks all dapm audio paths and powers widgets according to their
1295 * stream or path usage.
1297 * Returns 0 for success.
1299 int snd_soc_dapm_sync(struct snd_soc_codec
*codec
)
1301 return dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1303 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1305 static int snd_soc_dapm_add_route(struct snd_soc_codec
*codec
,
1306 const struct snd_soc_dapm_route
*route
)
1308 struct snd_soc_dapm_path
*path
;
1309 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1310 const char *sink
= route
->sink
;
1311 const char *control
= route
->control
;
1312 const char *source
= route
->source
;
1315 /* find src and dest widgets */
1316 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1318 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1322 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1327 if (wsource
== NULL
|| wsink
== NULL
)
1330 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1334 path
->source
= wsource
;
1336 path
->connected
= route
->connected
;
1337 INIT_LIST_HEAD(&path
->list
);
1338 INIT_LIST_HEAD(&path
->list_source
);
1339 INIT_LIST_HEAD(&path
->list_sink
);
1341 /* check for external widgets */
1342 if (wsink
->id
== snd_soc_dapm_input
) {
1343 if (wsource
->id
== snd_soc_dapm_micbias
||
1344 wsource
->id
== snd_soc_dapm_mic
||
1345 wsource
->id
== snd_soc_dapm_line
||
1346 wsource
->id
== snd_soc_dapm_output
)
1349 if (wsource
->id
== snd_soc_dapm_output
) {
1350 if (wsink
->id
== snd_soc_dapm_spk
||
1351 wsink
->id
== snd_soc_dapm_hp
||
1352 wsink
->id
== snd_soc_dapm_line
||
1353 wsink
->id
== snd_soc_dapm_input
)
1357 /* connect static paths */
1358 if (control
== NULL
) {
1359 list_add(&path
->list
, &codec
->dapm_paths
);
1360 list_add(&path
->list_sink
, &wsink
->sources
);
1361 list_add(&path
->list_source
, &wsource
->sinks
);
1366 /* connect dynamic paths */
1368 case snd_soc_dapm_adc
:
1369 case snd_soc_dapm_dac
:
1370 case snd_soc_dapm_pga
:
1371 case snd_soc_dapm_input
:
1372 case snd_soc_dapm_output
:
1373 case snd_soc_dapm_micbias
:
1374 case snd_soc_dapm_vmid
:
1375 case snd_soc_dapm_pre
:
1376 case snd_soc_dapm_post
:
1377 case snd_soc_dapm_supply
:
1378 case snd_soc_dapm_aif_in
:
1379 case snd_soc_dapm_aif_out
:
1380 list_add(&path
->list
, &codec
->dapm_paths
);
1381 list_add(&path
->list_sink
, &wsink
->sources
);
1382 list_add(&path
->list_source
, &wsource
->sinks
);
1385 case snd_soc_dapm_mux
:
1386 case snd_soc_dapm_value_mux
:
1387 ret
= dapm_connect_mux(codec
, wsource
, wsink
, path
, control
,
1388 &wsink
->kcontrols
[0]);
1392 case snd_soc_dapm_switch
:
1393 case snd_soc_dapm_mixer
:
1394 case snd_soc_dapm_mixer_named_ctl
:
1395 ret
= dapm_connect_mixer(codec
, wsource
, wsink
, path
, control
);
1399 case snd_soc_dapm_hp
:
1400 case snd_soc_dapm_mic
:
1401 case snd_soc_dapm_line
:
1402 case snd_soc_dapm_spk
:
1403 list_add(&path
->list
, &codec
->dapm_paths
);
1404 list_add(&path
->list_sink
, &wsink
->sources
);
1405 list_add(&path
->list_source
, &wsource
->sinks
);
1412 printk(KERN_WARNING
"asoc: no dapm match for %s --> %s --> %s\n", source
,
1419 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1421 * @route: audio routes
1422 * @num: number of routes
1424 * Connects 2 dapm widgets together via a named audio path. The sink is
1425 * the widget receiving the audio signal, whilst the source is the sender
1426 * of the audio signal.
1428 * Returns 0 for success else error. On error all resources can be freed
1429 * with a call to snd_soc_card_free().
1431 int snd_soc_dapm_add_routes(struct snd_soc_codec
*codec
,
1432 const struct snd_soc_dapm_route
*route
, int num
)
1436 for (i
= 0; i
< num
; i
++) {
1437 ret
= snd_soc_dapm_add_route(codec
, route
);
1439 printk(KERN_ERR
"Failed to add route %s->%s\n",
1449 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1452 * snd_soc_dapm_new_widgets - add new dapm widgets
1453 * @codec: audio codec
1455 * Checks the codec for any new dapm widgets and creates them if found.
1457 * Returns 0 for success.
1459 int snd_soc_dapm_new_widgets(struct snd_soc_codec
*codec
)
1461 struct snd_soc_dapm_widget
*w
;
1463 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
1469 case snd_soc_dapm_switch
:
1470 case snd_soc_dapm_mixer
:
1471 case snd_soc_dapm_mixer_named_ctl
:
1472 w
->power_check
= dapm_generic_check_power
;
1473 dapm_new_mixer(codec
, w
);
1475 case snd_soc_dapm_mux
:
1476 case snd_soc_dapm_value_mux
:
1477 w
->power_check
= dapm_generic_check_power
;
1478 dapm_new_mux(codec
, w
);
1480 case snd_soc_dapm_adc
:
1481 case snd_soc_dapm_aif_out
:
1482 w
->power_check
= dapm_adc_check_power
;
1484 case snd_soc_dapm_dac
:
1485 case snd_soc_dapm_aif_in
:
1486 w
->power_check
= dapm_dac_check_power
;
1488 case snd_soc_dapm_pga
:
1489 w
->power_check
= dapm_generic_check_power
;
1490 dapm_new_pga(codec
, w
);
1492 case snd_soc_dapm_input
:
1493 case snd_soc_dapm_output
:
1494 case snd_soc_dapm_micbias
:
1495 case snd_soc_dapm_spk
:
1496 case snd_soc_dapm_hp
:
1497 case snd_soc_dapm_mic
:
1498 case snd_soc_dapm_line
:
1499 w
->power_check
= dapm_generic_check_power
;
1501 case snd_soc_dapm_supply
:
1502 w
->power_check
= dapm_supply_check_power
;
1503 case snd_soc_dapm_vmid
:
1504 case snd_soc_dapm_pre
:
1505 case snd_soc_dapm_post
:
1511 dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1514 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1517 * snd_soc_dapm_get_volsw - dapm mixer get callback
1518 * @kcontrol: mixer control
1519 * @ucontrol: control element information
1521 * Callback to get the value of a dapm mixer control.
1523 * Returns 0 for success.
1525 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1526 struct snd_ctl_elem_value
*ucontrol
)
1528 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1529 struct soc_mixer_control
*mc
=
1530 (struct soc_mixer_control
*)kcontrol
->private_value
;
1531 unsigned int reg
= mc
->reg
;
1532 unsigned int shift
= mc
->shift
;
1533 unsigned int rshift
= mc
->rshift
;
1535 unsigned int invert
= mc
->invert
;
1536 unsigned int mask
= (1 << fls(max
)) - 1;
1538 ucontrol
->value
.integer
.value
[0] =
1539 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1540 if (shift
!= rshift
)
1541 ucontrol
->value
.integer
.value
[1] =
1542 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1544 ucontrol
->value
.integer
.value
[0] =
1545 max
- ucontrol
->value
.integer
.value
[0];
1546 if (shift
!= rshift
)
1547 ucontrol
->value
.integer
.value
[1] =
1548 max
- ucontrol
->value
.integer
.value
[1];
1553 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1556 * snd_soc_dapm_put_volsw - dapm mixer set callback
1557 * @kcontrol: mixer control
1558 * @ucontrol: control element information
1560 * Callback to set the value of a dapm mixer control.
1562 * Returns 0 for success.
1564 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1565 struct snd_ctl_elem_value
*ucontrol
)
1567 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1568 struct soc_mixer_control
*mc
=
1569 (struct soc_mixer_control
*)kcontrol
->private_value
;
1570 unsigned int reg
= mc
->reg
;
1571 unsigned int shift
= mc
->shift
;
1572 unsigned int rshift
= mc
->rshift
;
1574 unsigned int mask
= (1 << fls(max
)) - 1;
1575 unsigned int invert
= mc
->invert
;
1576 unsigned int val
, val2
, val_mask
;
1580 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1584 val_mask
= mask
<< shift
;
1586 if (shift
!= rshift
) {
1587 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1590 val_mask
|= mask
<< rshift
;
1591 val
|= val2
<< rshift
;
1594 mutex_lock(&widget
->codec
->mutex
);
1595 widget
->value
= val
;
1597 if (snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
)) {
1599 /* new connection */
1600 connect
= invert
? 0:1;
1602 /* old connection must be powered down */
1603 connect
= invert
? 1:0;
1605 dapm_mixer_update_power(widget
, kcontrol
, connect
);
1608 if (widget
->event
) {
1609 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1610 ret
= widget
->event(widget
, kcontrol
,
1611 SND_SOC_DAPM_PRE_REG
);
1617 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1618 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1619 ret
= widget
->event(widget
, kcontrol
,
1620 SND_SOC_DAPM_POST_REG
);
1622 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1625 mutex_unlock(&widget
->codec
->mutex
);
1628 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1631 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1632 * @kcontrol: mixer control
1633 * @ucontrol: control element information
1635 * Callback to get the value of a dapm enumerated double mixer control.
1637 * Returns 0 for success.
1639 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1640 struct snd_ctl_elem_value
*ucontrol
)
1642 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1643 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1644 unsigned int val
, bitmask
;
1646 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1648 val
= snd_soc_read(widget
->codec
, e
->reg
);
1649 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1650 if (e
->shift_l
!= e
->shift_r
)
1651 ucontrol
->value
.enumerated
.item
[1] =
1652 (val
>> e
->shift_r
) & (bitmask
- 1);
1656 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1659 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1660 * @kcontrol: mixer control
1661 * @ucontrol: control element information
1663 * Callback to set the value of a dapm enumerated double mixer control.
1665 * Returns 0 for success.
1667 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1668 struct snd_ctl_elem_value
*ucontrol
)
1670 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1671 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1672 unsigned int val
, mux
, change
;
1673 unsigned int mask
, bitmask
;
1676 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1678 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1680 mux
= ucontrol
->value
.enumerated
.item
[0];
1681 val
= mux
<< e
->shift_l
;
1682 mask
= (bitmask
- 1) << e
->shift_l
;
1683 if (e
->shift_l
!= e
->shift_r
) {
1684 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1686 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1687 mask
|= (bitmask
- 1) << e
->shift_r
;
1690 mutex_lock(&widget
->codec
->mutex
);
1691 widget
->value
= val
;
1692 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1693 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1695 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1696 ret
= widget
->event(widget
,
1697 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1702 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1704 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1705 ret
= widget
->event(widget
,
1706 kcontrol
, SND_SOC_DAPM_POST_REG
);
1709 mutex_unlock(&widget
->codec
->mutex
);
1712 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1715 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1716 * @kcontrol: mixer control
1717 * @ucontrol: control element information
1719 * Returns 0 for success.
1721 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
1722 struct snd_ctl_elem_value
*ucontrol
)
1724 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1726 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
1730 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
1733 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1734 * @kcontrol: mixer control
1735 * @ucontrol: control element information
1737 * Returns 0 for success.
1739 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
1740 struct snd_ctl_elem_value
*ucontrol
)
1742 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1743 struct soc_enum
*e
=
1744 (struct soc_enum
*)kcontrol
->private_value
;
1748 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
1751 mutex_lock(&widget
->codec
->mutex
);
1753 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
1754 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
1755 dapm_mux_update_power(widget
, kcontrol
, change
, widget
->value
, e
);
1757 mutex_unlock(&widget
->codec
->mutex
);
1760 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
1763 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1765 * @kcontrol: mixer control
1766 * @ucontrol: control element information
1768 * Callback to get the value of a dapm semi enumerated double mixer control.
1770 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1771 * used for handling bitfield coded enumeration for example.
1773 * Returns 0 for success.
1775 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1776 struct snd_ctl_elem_value
*ucontrol
)
1778 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1779 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1780 unsigned int reg_val
, val
, mux
;
1782 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1783 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1784 for (mux
= 0; mux
< e
->max
; mux
++) {
1785 if (val
== e
->values
[mux
])
1788 ucontrol
->value
.enumerated
.item
[0] = mux
;
1789 if (e
->shift_l
!= e
->shift_r
) {
1790 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1791 for (mux
= 0; mux
< e
->max
; mux
++) {
1792 if (val
== e
->values
[mux
])
1795 ucontrol
->value
.enumerated
.item
[1] = mux
;
1800 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1803 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1805 * @kcontrol: mixer control
1806 * @ucontrol: control element information
1808 * Callback to set the value of a dapm semi enumerated double mixer control.
1810 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1811 * used for handling bitfield coded enumeration for example.
1813 * Returns 0 for success.
1815 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
1816 struct snd_ctl_elem_value
*ucontrol
)
1818 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1819 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1820 unsigned int val
, mux
, change
;
1824 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1826 mux
= ucontrol
->value
.enumerated
.item
[0];
1827 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1828 mask
= e
->mask
<< e
->shift_l
;
1829 if (e
->shift_l
!= e
->shift_r
) {
1830 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1832 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1833 mask
|= e
->mask
<< e
->shift_r
;
1836 mutex_lock(&widget
->codec
->mutex
);
1837 widget
->value
= val
;
1838 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1839 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1841 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1842 ret
= widget
->event(widget
,
1843 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1848 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1850 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1851 ret
= widget
->event(widget
,
1852 kcontrol
, SND_SOC_DAPM_POST_REG
);
1855 mutex_unlock(&widget
->codec
->mutex
);
1858 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1861 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1863 * @kcontrol: mixer control
1864 * @uinfo: control element information
1866 * Callback to provide information about a pin switch control.
1868 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1869 struct snd_ctl_elem_info
*uinfo
)
1871 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1873 uinfo
->value
.integer
.min
= 0;
1874 uinfo
->value
.integer
.max
= 1;
1878 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1881 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1883 * @kcontrol: mixer control
1886 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1887 struct snd_ctl_elem_value
*ucontrol
)
1889 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1890 const char *pin
= (const char *)kcontrol
->private_value
;
1892 mutex_lock(&codec
->mutex
);
1894 ucontrol
->value
.integer
.value
[0] =
1895 snd_soc_dapm_get_pin_status(codec
, pin
);
1897 mutex_unlock(&codec
->mutex
);
1901 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
1904 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1906 * @kcontrol: mixer control
1909 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
1910 struct snd_ctl_elem_value
*ucontrol
)
1912 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1913 const char *pin
= (const char *)kcontrol
->private_value
;
1915 mutex_lock(&codec
->mutex
);
1917 if (ucontrol
->value
.integer
.value
[0])
1918 snd_soc_dapm_enable_pin(codec
, pin
);
1920 snd_soc_dapm_disable_pin(codec
, pin
);
1922 snd_soc_dapm_sync(codec
);
1924 mutex_unlock(&codec
->mutex
);
1928 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
1931 * snd_soc_dapm_new_control - create new dapm control
1932 * @codec: audio codec
1933 * @widget: widget template
1935 * Creates a new dapm control based upon the template.
1937 * Returns 0 for success else error.
1939 int snd_soc_dapm_new_control(struct snd_soc_codec
*codec
,
1940 const struct snd_soc_dapm_widget
*widget
)
1942 struct snd_soc_dapm_widget
*w
;
1944 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
1948 INIT_LIST_HEAD(&w
->sources
);
1949 INIT_LIST_HEAD(&w
->sinks
);
1950 INIT_LIST_HEAD(&w
->list
);
1951 list_add(&w
->list
, &codec
->dapm_widgets
);
1953 /* machine layer set ups unconnected pins and insertions */
1957 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
1960 * snd_soc_dapm_new_controls - create new dapm controls
1961 * @codec: audio codec
1962 * @widget: widget array
1963 * @num: number of widgets
1965 * Creates new DAPM controls based upon the templates.
1967 * Returns 0 for success else error.
1969 int snd_soc_dapm_new_controls(struct snd_soc_codec
*codec
,
1970 const struct snd_soc_dapm_widget
*widget
,
1975 for (i
= 0; i
< num
; i
++) {
1976 ret
= snd_soc_dapm_new_control(codec
, widget
);
1979 "ASoC: Failed to create DAPM control %s: %d\n",
1987 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
1991 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1992 * @codec: audio codec
1993 * @stream: stream name
1994 * @event: stream event
1996 * Sends a stream event to the dapm core. The core then makes any
1997 * necessary widget power changes.
1999 * Returns 0 for success else error.
2001 int snd_soc_dapm_stream_event(struct snd_soc_codec
*codec
,
2002 char *stream
, int event
)
2004 struct snd_soc_dapm_widget
*w
;
2009 mutex_lock(&codec
->mutex
);
2010 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
2014 pr_debug("widget %s\n %s stream %s event %d\n",
2015 w
->name
, w
->sname
, stream
, event
);
2016 if (strstr(w
->sname
, stream
)) {
2018 case SND_SOC_DAPM_STREAM_START
:
2021 case SND_SOC_DAPM_STREAM_STOP
:
2024 case SND_SOC_DAPM_STREAM_SUSPEND
:
2025 case SND_SOC_DAPM_STREAM_RESUME
:
2026 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2027 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2033 dapm_power_widgets(codec
, event
);
2034 mutex_unlock(&codec
->mutex
);
2037 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
2040 * snd_soc_dapm_enable_pin - enable pin.
2044 * Enables input/output pin and its parents or children widgets iff there is
2045 * a valid audio route and active audio stream.
2046 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2047 * do any widget power switching.
2049 int snd_soc_dapm_enable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2051 return snd_soc_dapm_set_pin(codec
, pin
, 1);
2053 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2056 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2060 * Enables input/output pin regardless of any other state. This is
2061 * intended for use with microphone bias supplies used in microphone
2064 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2065 * do any widget power switching.
2067 int snd_soc_dapm_force_enable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2069 struct snd_soc_dapm_widget
*w
;
2071 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2072 if (!strcmp(w
->name
, pin
)) {
2073 pr_debug("dapm: %s: pin %s\n", codec
->name
, pin
);
2080 pr_err("dapm: %s: configuring unknown pin %s\n", codec
->name
, pin
);
2083 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
2086 * snd_soc_dapm_disable_pin - disable pin.
2090 * Disables input/output pin and its parents or children widgets.
2091 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2092 * do any widget power switching.
2094 int snd_soc_dapm_disable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2096 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2098 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2101 * snd_soc_dapm_nc_pin - permanently disable pin.
2105 * Marks the specified pin as being not connected, disabling it along
2106 * any parent or child widgets. At present this is identical to
2107 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2108 * additional things such as disabling controls which only affect
2109 * paths through the pin.
2111 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2112 * do any widget power switching.
2114 int snd_soc_dapm_nc_pin(struct snd_soc_codec
*codec
, const char *pin
)
2116 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2118 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2121 * snd_soc_dapm_get_pin_status - get audio pin status
2122 * @codec: audio codec
2123 * @pin: audio signal pin endpoint (or start point)
2125 * Get audio pin status - connected or disconnected.
2127 * Returns 1 for connected otherwise 0.
2129 int snd_soc_dapm_get_pin_status(struct snd_soc_codec
*codec
, const char *pin
)
2131 struct snd_soc_dapm_widget
*w
;
2133 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2134 if (!strcmp(w
->name
, pin
))
2135 return w
->connected
;
2140 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2143 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2144 * @codec: audio codec
2145 * @pin: audio signal pin endpoint (or start point)
2147 * Mark the given endpoint or pin as ignoring suspend. When the
2148 * system is disabled a path between two endpoints flagged as ignoring
2149 * suspend will not be disabled. The path must already be enabled via
2150 * normal means at suspend time, it will not be turned on if it was not
2153 int snd_soc_dapm_ignore_suspend(struct snd_soc_codec
*codec
, const char *pin
)
2155 struct snd_soc_dapm_widget
*w
;
2157 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2158 if (!strcmp(w
->name
, pin
)) {
2159 w
->ignore_suspend
= 1;
2164 pr_err("Unknown DAPM pin: %s\n", pin
);
2167 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
2170 * snd_soc_dapm_free - free dapm resources
2171 * @socdev: SoC device
2173 * Free all dapm widgets and resources.
2175 void snd_soc_dapm_free(struct snd_soc_device
*socdev
)
2177 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2179 snd_soc_dapm_sys_remove(socdev
->dev
);
2180 dapm_free_widgets(codec
);
2182 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2185 * snd_soc_dapm_shutdown - callback for system shutdown
2187 void snd_soc_dapm_shutdown(struct snd_soc_device
*socdev
)
2189 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2190 struct snd_soc_dapm_widget
*w
;
2191 LIST_HEAD(down_list
);
2194 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2196 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2202 /* If there were no widgets to power down we're already in
2206 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_PREPARE
);
2207 dapm_seq_run(codec
, &down_list
, 0, dapm_down_seq
);
2208 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_STANDBY
);
2211 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_OFF
);
2214 /* Module information */
2215 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2216 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2217 MODULE_LICENSE("GPL");