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.h>
46 #include <sound/soc-dapm.h>
47 #include <sound/initval.h>
49 /* dapm power sequences - make this per codec in the future */
50 static int dapm_up_seq
[] = {
51 [snd_soc_dapm_pre
] = 0,
52 [snd_soc_dapm_supply
] = 1,
53 [snd_soc_dapm_micbias
] = 2,
54 [snd_soc_dapm_aif_in
] = 3,
55 [snd_soc_dapm_aif_out
] = 3,
56 [snd_soc_dapm_mic
] = 4,
57 [snd_soc_dapm_mux
] = 5,
58 [snd_soc_dapm_value_mux
] = 5,
59 [snd_soc_dapm_dac
] = 6,
60 [snd_soc_dapm_mixer
] = 7,
61 [snd_soc_dapm_mixer_named_ctl
] = 7,
62 [snd_soc_dapm_pga
] = 8,
63 [snd_soc_dapm_adc
] = 9,
64 [snd_soc_dapm_hp
] = 10,
65 [snd_soc_dapm_spk
] = 10,
66 [snd_soc_dapm_post
] = 11,
69 static int dapm_down_seq
[] = {
70 [snd_soc_dapm_pre
] = 0,
71 [snd_soc_dapm_adc
] = 1,
72 [snd_soc_dapm_hp
] = 2,
73 [snd_soc_dapm_spk
] = 2,
74 [snd_soc_dapm_pga
] = 4,
75 [snd_soc_dapm_mixer_named_ctl
] = 5,
76 [snd_soc_dapm_mixer
] = 5,
77 [snd_soc_dapm_dac
] = 6,
78 [snd_soc_dapm_mic
] = 7,
79 [snd_soc_dapm_micbias
] = 8,
80 [snd_soc_dapm_mux
] = 9,
81 [snd_soc_dapm_value_mux
] = 9,
82 [snd_soc_dapm_aif_in
] = 10,
83 [snd_soc_dapm_aif_out
] = 10,
84 [snd_soc_dapm_supply
] = 11,
85 [snd_soc_dapm_post
] = 12,
88 static void pop_wait(u32 pop_time
)
91 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
94 static void pop_dbg(u32 pop_time
, const char *fmt
, ...)
107 /* create a new dapm widget */
108 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
109 const struct snd_soc_dapm_widget
*_widget
)
111 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
115 * snd_soc_dapm_set_bias_level - set the bias level for the system
116 * @card: audio device
117 * @level: level to configure
119 * Configure the bias (power) levels for the SoC audio device.
121 * Returns 0 for success else error.
123 static int snd_soc_dapm_set_bias_level(struct snd_soc_card
*card
,
124 struct snd_soc_dapm_context
*dapm
,
125 enum snd_soc_bias_level level
)
130 case SND_SOC_BIAS_ON
:
131 dev_dbg(dapm
->dev
, "Setting full bias\n");
133 case SND_SOC_BIAS_PREPARE
:
134 dev_dbg(dapm
->dev
, "Setting bias prepare\n");
136 case SND_SOC_BIAS_STANDBY
:
137 dev_dbg(dapm
->dev
, "Setting standby bias\n");
139 case SND_SOC_BIAS_OFF
:
140 dev_dbg(dapm
->dev
, "Setting bias off\n");
143 dev_err(dapm
->dev
, "Setting invalid bias %d\n", level
);
147 if (card
&& card
->set_bias_level
)
148 ret
= card
->set_bias_level(card
, level
);
150 if (dapm
->codec
&& dapm
->codec
->driver
->set_bias_level
)
151 ret
= dapm
->codec
->driver
->set_bias_level(dapm
->codec
, level
);
153 dapm
->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_dapm_context
*dapm
,
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
, &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_dapm_context
*dapm
,
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
, &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
;
295 struct snd_soc_dapm_context
*dapm
= widget
->dapm
;
296 struct snd_soc_card
*card
= dapm
->card
;
298 /* check for valid widgets */
299 if (widget
->reg
< 0 || widget
->id
== snd_soc_dapm_input
||
300 widget
->id
== snd_soc_dapm_output
||
301 widget
->id
== snd_soc_dapm_hp
||
302 widget
->id
== snd_soc_dapm_mic
||
303 widget
->id
== snd_soc_dapm_line
||
304 widget
->id
== snd_soc_dapm_spk
)
307 power
= widget
->power
;
309 power
= (power
? 0:1);
311 old
= snd_soc_read(codec
, widget
->reg
);
312 new = (old
& ~(0x1 << widget
->shift
)) | (power
<< widget
->shift
);
316 pop_dbg(card
->pop_time
, "pop test %s : %s in %d ms\n",
317 widget
->name
, widget
->power
? "on" : "off",
319 pop_wait(card
->pop_time
);
320 snd_soc_write(codec
, widget
->reg
, new);
322 pr_debug("reg %x old %x new %x change %d\n", widget
->reg
,
327 /* create new dapm mixer control */
328 static int dapm_new_mixer(struct snd_soc_dapm_context
*dapm
,
329 struct snd_soc_dapm_widget
*w
)
333 struct snd_soc_dapm_path
*path
;
334 struct snd_card
*card
= dapm
->codec
->card
->snd_card
;
337 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
340 list_for_each_entry(path
, &w
->sources
, list_sink
) {
342 /* mixer/mux paths name must match control name */
343 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
346 /* add dapm control with long name.
347 * for dapm_mixer this is the concatenation of the
348 * mixer and kcontrol name.
349 * for dapm_mixer_named_ctl this is simply the
352 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
353 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
354 name_len
+= 1 + strlen(w
->name
);
356 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
358 if (path
->long_name
== NULL
)
363 snprintf(path
->long_name
, name_len
, "%s %s",
364 w
->name
, w
->kcontrols
[i
].name
);
366 case snd_soc_dapm_mixer_named_ctl
:
367 snprintf(path
->long_name
, name_len
, "%s",
368 w
->kcontrols
[i
].name
);
372 path
->long_name
[name_len
- 1] = '\0';
374 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
376 ret
= snd_ctl_add(card
, path
->kcontrol
);
378 printk(KERN_ERR
"asoc: failed to add dapm kcontrol %s: %d\n",
381 kfree(path
->long_name
);
382 path
->long_name
= NULL
;
390 /* create new dapm mux control */
391 static int dapm_new_mux(struct snd_soc_dapm_context
*dapm
,
392 struct snd_soc_dapm_widget
*w
)
394 struct snd_soc_dapm_path
*path
= NULL
;
395 struct snd_kcontrol
*kcontrol
;
396 struct snd_card
*card
= dapm
->codec
->card
->snd_card
;
399 if (!w
->num_kcontrols
) {
400 printk(KERN_ERR
"asoc: mux %s has no controls\n", w
->name
);
404 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
405 ret
= snd_ctl_add(card
, kcontrol
);
410 list_for_each_entry(path
, &w
->sources
, list_sink
)
411 path
->kcontrol
= kcontrol
;
416 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
420 /* create new dapm volume control */
421 static int dapm_new_pga(struct snd_soc_dapm_context
*dapm
,
422 struct snd_soc_dapm_widget
*w
)
424 if (w
->num_kcontrols
)
425 pr_err("asoc: PGA controls not supported: '%s'\n", w
->name
);
430 /* reset 'walked' bit for each dapm path */
431 static inline void dapm_clear_walk(struct snd_soc_dapm_context
*dapm
)
433 struct snd_soc_dapm_path
*p
;
435 list_for_each_entry(p
, &dapm
->paths
, list
)
439 /* We implement power down on suspend by checking the power state of
440 * the ALSA card - when we are suspending the ALSA state for the card
443 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
445 int level
= snd_power_get_state(widget
->dapm
->codec
->card
->snd_card
);
448 case SNDRV_CTL_POWER_D3hot
:
449 case SNDRV_CTL_POWER_D3cold
:
450 if (widget
->ignore_suspend
)
451 pr_debug("%s ignoring suspend\n", widget
->name
);
452 return widget
->ignore_suspend
;
459 * Recursively check for a completed path to an active or physically connected
460 * output widget. Returns number of complete paths.
462 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
464 struct snd_soc_dapm_path
*path
;
467 if (widget
->id
== snd_soc_dapm_supply
)
470 switch (widget
->id
) {
471 case snd_soc_dapm_adc
:
472 case snd_soc_dapm_aif_out
:
474 return snd_soc_dapm_suspend_check(widget
);
479 if (widget
->connected
) {
480 /* connected pin ? */
481 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
482 return snd_soc_dapm_suspend_check(widget
);
484 /* connected jack or spk ? */
485 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
486 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sources
)))
487 return snd_soc_dapm_suspend_check(widget
);
490 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
494 if (path
->sink
&& path
->connect
) {
496 con
+= is_connected_output_ep(path
->sink
);
504 * Recursively check for a completed path to an active or physically connected
505 * input widget. Returns number of complete paths.
507 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
509 struct snd_soc_dapm_path
*path
;
512 if (widget
->id
== snd_soc_dapm_supply
)
515 /* active stream ? */
516 switch (widget
->id
) {
517 case snd_soc_dapm_dac
:
518 case snd_soc_dapm_aif_in
:
520 return snd_soc_dapm_suspend_check(widget
);
525 if (widget
->connected
) {
526 /* connected pin ? */
527 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
528 return snd_soc_dapm_suspend_check(widget
);
530 /* connected VMID/Bias for lower pops */
531 if (widget
->id
== snd_soc_dapm_vmid
)
532 return snd_soc_dapm_suspend_check(widget
);
534 /* connected jack ? */
535 if (widget
->id
== snd_soc_dapm_mic
||
536 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sinks
)))
537 return snd_soc_dapm_suspend_check(widget
);
540 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
544 if (path
->source
&& path
->connect
) {
546 con
+= is_connected_input_ep(path
->source
);
554 * Handler for generic register modifier widget.
556 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
557 struct snd_kcontrol
*kcontrol
, int event
)
561 if (SND_SOC_DAPM_EVENT_ON(event
))
566 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
567 w
->mask
<< w
->shift
, val
<< w
->shift
);
571 EXPORT_SYMBOL_GPL(dapm_reg_event
);
573 /* Standard power change method, used to apply power changes to most
576 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
580 /* call any power change event handlers */
582 pr_debug("power %s event for %s flags %x\n",
583 w
->power
? "on" : "off",
584 w
->name
, w
->event_flags
);
586 /* power up pre event */
587 if (w
->power
&& w
->event
&&
588 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
589 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
594 /* power down pre event */
595 if (!w
->power
&& w
->event
&&
596 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
597 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
604 /* power up post event */
605 if (w
->power
&& w
->event
&&
606 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
608 NULL
, SND_SOC_DAPM_POST_PMU
);
613 /* power down post event */
614 if (!w
->power
&& w
->event
&&
615 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
616 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
624 /* Generic check to see if a widget should be powered.
626 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
630 in
= is_connected_input_ep(w
);
631 dapm_clear_walk(w
->dapm
);
632 out
= is_connected_output_ep(w
);
633 dapm_clear_walk(w
->dapm
);
634 return out
!= 0 && in
!= 0;
637 /* Check to see if an ADC has power */
638 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
643 in
= is_connected_input_ep(w
);
644 dapm_clear_walk(w
->dapm
);
647 return dapm_generic_check_power(w
);
651 /* Check to see if a DAC has power */
652 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
657 out
= is_connected_output_ep(w
);
658 dapm_clear_walk(w
->dapm
);
661 return dapm_generic_check_power(w
);
665 /* Check to see if a power supply is needed */
666 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
668 struct snd_soc_dapm_path
*path
;
671 /* Check if one of our outputs is connected */
672 list_for_each_entry(path
, &w
->sinks
, list_source
) {
673 if (path
->connected
&&
674 !path
->connected(path
->source
, path
->sink
))
677 if (path
->sink
&& path
->sink
->power_check
&&
678 path
->sink
->power_check(path
->sink
)) {
684 dapm_clear_walk(w
->dapm
);
689 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
690 struct snd_soc_dapm_widget
*b
,
693 if (a
->codec
!= b
->codec
)
694 return (unsigned long)a
- (unsigned long)b
;
695 if (sort
[a
->id
] != sort
[b
->id
])
696 return sort
[a
->id
] - sort
[b
->id
];
697 if (a
->reg
!= b
->reg
)
698 return a
->reg
- b
->reg
;
703 /* Insert a widget in order into a DAPM power sequence. */
704 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
705 struct list_head
*list
,
708 struct snd_soc_dapm_widget
*w
;
710 list_for_each_entry(w
, list
, power_list
)
711 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
712 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
716 list_add_tail(&new_widget
->power_list
, list
);
719 /* Apply the coalesced changes from a DAPM sequence */
720 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context
*dapm
,
721 struct list_head
*pending
)
723 struct snd_soc_dapm_widget
*w
;
724 struct snd_soc_card
*card
= dapm
->card
;
726 unsigned int value
= 0;
727 unsigned int mask
= 0;
728 unsigned int cur_mask
;
730 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
733 list_for_each_entry(w
, pending
, power_list
) {
734 cur_mask
= 1 << w
->shift
;
735 BUG_ON(reg
!= w
->reg
);
746 pop_dbg(card
->pop_time
,
747 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
748 w
->name
, reg
, value
, mask
);
750 /* power up pre event */
751 if (w
->power
&& w
->event
&&
752 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
753 pop_dbg(card
->pop_time
, "pop test : %s PRE_PMU\n",
755 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
757 pr_err("%s: pre event failed: %d\n",
761 /* power down pre event */
762 if (!w
->power
&& w
->event
&&
763 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
764 pop_dbg(card
->pop_time
, "pop test : %s PRE_PMD\n",
766 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
768 pr_err("%s: pre event failed: %d\n",
774 pop_dbg(card
->pop_time
,
775 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
776 value
, mask
, reg
, card
->pop_time
);
777 pop_wait(card
->pop_time
);
778 snd_soc_update_bits(dapm
->codec
, reg
, mask
, value
);
781 list_for_each_entry(w
, pending
, power_list
) {
782 /* power up post event */
783 if (w
->power
&& w
->event
&&
784 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
785 pop_dbg(card
->pop_time
, "pop test : %s POST_PMU\n",
788 NULL
, SND_SOC_DAPM_POST_PMU
);
790 pr_err("%s: post event failed: %d\n",
794 /* power down post event */
795 if (!w
->power
&& w
->event
&&
796 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
797 pop_dbg(card
->pop_time
, "pop test : %s POST_PMD\n",
799 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
801 pr_err("%s: post event failed: %d\n",
807 /* Apply a DAPM power sequence.
809 * We walk over a pre-sorted list of widgets to apply power to. In
810 * order to minimise the number of writes to the device required
811 * multiple widgets will be updated in a single write where possible.
812 * Currently anything that requires more than a single write is not
815 static void dapm_seq_run(struct snd_soc_dapm_context
*dapm
,
816 struct list_head
*list
, int event
, int sort
[])
818 struct snd_soc_dapm_widget
*w
, *n
;
821 int cur_reg
= SND_SOC_NOPM
;
824 list_for_each_entry_safe(w
, n
, list
, power_list
) {
827 /* Do we need to apply any queued changes? */
828 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
829 if (!list_empty(&pending
))
830 dapm_seq_run_coalesced(dapm
, &pending
);
832 INIT_LIST_HEAD(&pending
);
834 cur_reg
= SND_SOC_NOPM
;
838 case snd_soc_dapm_pre
:
840 list_for_each_entry_safe_continue(w
, n
, list
,
843 if (event
== SND_SOC_DAPM_STREAM_START
)
845 NULL
, SND_SOC_DAPM_PRE_PMU
);
846 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
848 NULL
, SND_SOC_DAPM_PRE_PMD
);
851 case snd_soc_dapm_post
:
853 list_for_each_entry_safe_continue(w
, n
, list
,
856 if (event
== SND_SOC_DAPM_STREAM_START
)
858 NULL
, SND_SOC_DAPM_POST_PMU
);
859 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
861 NULL
, SND_SOC_DAPM_POST_PMD
);
864 case snd_soc_dapm_input
:
865 case snd_soc_dapm_output
:
866 case snd_soc_dapm_hp
:
867 case snd_soc_dapm_mic
:
868 case snd_soc_dapm_line
:
869 case snd_soc_dapm_spk
:
870 /* No register support currently */
871 ret
= dapm_generic_apply_power(w
);
875 /* Queue it up for application */
876 cur_sort
= sort
[w
->id
];
878 list_move(&w
->power_list
, &pending
);
883 pr_err("Failed to apply widget power: %d\n",
887 if (!list_empty(&pending
))
888 dapm_seq_run_coalesced(dapm
, &pending
);
892 * Scan each dapm widget for complete audio path.
893 * A complete path is a route that has valid endpoints i.e.:-
895 * o DAC to output pin.
896 * o Input Pin to ADC.
897 * o Input pin to Output pin (bypass, sidetone)
898 * o DAC to ADC (loopback).
900 static int dapm_power_widgets(struct snd_soc_dapm_context
*dapm
, int event
)
902 struct snd_soc_card
*card
= dapm
->codec
->card
;
903 struct snd_soc_dapm_widget
*w
;
905 LIST_HEAD(down_list
);
910 /* Check which widgets we need to power and store them in
911 * lists indicating if they should be powered up or down.
913 list_for_each_entry(w
, &dapm
->widgets
, list
) {
915 case snd_soc_dapm_pre
:
916 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
918 case snd_soc_dapm_post
:
919 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
927 power
= w
->power_check(w
);
933 if (w
->power
== power
)
937 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
939 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
946 /* If there are no DAPM widgets then try to figure out power from the
949 if (list_empty(&dapm
->widgets
)) {
951 case SND_SOC_DAPM_STREAM_START
:
952 case SND_SOC_DAPM_STREAM_RESUME
:
955 case SND_SOC_DAPM_STREAM_SUSPEND
:
958 case SND_SOC_DAPM_STREAM_NOP
:
959 switch (dapm
->bias_level
) {
960 case SND_SOC_BIAS_STANDBY
:
961 case SND_SOC_BIAS_OFF
:
974 if (sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_OFF
) {
975 ret
= snd_soc_dapm_set_bias_level(card
, dapm
,
976 SND_SOC_BIAS_STANDBY
);
978 pr_err("Failed to turn on bias: %d\n", ret
);
981 /* If we're changing to all on or all off then prepare */
982 if ((sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
983 (!sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_ON
)) {
984 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_PREPARE
);
986 pr_err("Failed to prepare bias: %d\n", ret
);
989 /* Power down widgets first; try to avoid amplifying pops. */
990 dapm_seq_run(dapm
, &down_list
, event
, dapm_down_seq
);
993 dapm_seq_run(dapm
, &up_list
, event
, dapm_up_seq
);
995 /* If we just powered the last thing off drop to standby bias */
996 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
997 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_STANDBY
);
999 pr_err("Failed to apply standby bias: %d\n", ret
);
1002 /* If we're in standby and can support bias off then do that */
1003 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1004 dapm
->idle_bias_off
) {
1005 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_OFF
);
1007 pr_err("Failed to turn off bias: %d\n", ret
);
1010 /* If we just powered up then move to active bias */
1011 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
1012 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_ON
);
1014 pr_err("Failed to apply active bias: %d\n", ret
);
1017 pop_dbg(card
->pop_time
, "DAPM sequencing finished, waiting %dms\n",
1019 pop_wait(card
->pop_time
);
1024 #ifdef CONFIG_DEBUG_FS
1025 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1027 file
->private_data
= inode
->i_private
;
1031 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1032 char __user
*user_buf
,
1033 size_t count
, loff_t
*ppos
)
1035 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1039 struct snd_soc_dapm_path
*p
= NULL
;
1041 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1045 in
= is_connected_input_ep(w
);
1046 dapm_clear_walk(w
->dapm
);
1047 out
= is_connected_output_ep(w
);
1048 dapm_clear_walk(w
->dapm
);
1050 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1051 w
->name
, w
->power
? "On" : "Off", in
, out
);
1054 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1055 " - R%d(0x%x) bit %d",
1056 w
->reg
, w
->reg
, w
->shift
);
1058 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1061 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1063 w
->active
? "active" : "inactive");
1065 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1066 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1070 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1072 p
->name
? p
->name
: "static",
1075 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1076 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1080 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1082 p
->name
? p
->name
: "static",
1086 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1092 static const struct file_operations dapm_widget_power_fops
= {
1093 .open
= dapm_widget_power_open_file
,
1094 .read
= dapm_widget_power_read_file
,
1095 .llseek
= default_llseek
,
1098 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
)
1100 struct snd_soc_dapm_widget
*w
;
1103 if (!dapm
->debugfs_dapm
)
1106 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1110 d
= debugfs_create_file(w
->name
, 0444,
1111 dapm
->debugfs_dapm
, w
,
1112 &dapm_widget_power_fops
);
1115 "ASoC: Failed to create %s debugfs file\n",
1120 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
)
1125 /* test and update the power status of a mux widget */
1126 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1127 struct snd_kcontrol
*kcontrol
, int change
,
1128 int mux
, struct soc_enum
*e
)
1130 struct snd_soc_dapm_path
*path
;
1133 if (widget
->id
!= snd_soc_dapm_mux
&&
1134 widget
->id
!= snd_soc_dapm_value_mux
)
1140 /* find dapm widget path assoc with kcontrol */
1141 list_for_each_entry(path
, &widget
->dapm
->paths
, list
) {
1142 if (path
->kcontrol
!= kcontrol
)
1145 if (!path
->name
|| !e
->texts
[mux
])
1149 /* we now need to match the string in the enum to the path */
1150 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1151 path
->connect
= 1; /* new connection */
1153 path
->connect
= 0; /* old connection must be powered down */
1157 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1162 /* test and update the power status of a mixer or switch widget */
1163 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1164 struct snd_kcontrol
*kcontrol
, int connect
)
1166 struct snd_soc_dapm_path
*path
;
1169 if (widget
->id
!= snd_soc_dapm_mixer
&&
1170 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1171 widget
->id
!= snd_soc_dapm_switch
)
1174 /* find dapm widget path assoc with kcontrol */
1175 list_for_each_entry(path
, &widget
->dapm
->paths
, list
) {
1176 if (path
->kcontrol
!= kcontrol
)
1179 /* found, now check type */
1181 path
->connect
= connect
;
1186 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1191 /* show dapm widget status in sys fs */
1192 static ssize_t
dapm_widget_show(struct device
*dev
,
1193 struct device_attribute
*attr
, char *buf
)
1195 struct snd_soc_pcm_runtime
*rtd
=
1196 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
1197 struct snd_soc_codec
*codec
=rtd
->codec
;
1198 struct snd_soc_dapm_widget
*w
;
1200 char *state
= "not set";
1202 list_for_each_entry(w
, &codec
->dapm
.widgets
, list
) {
1204 /* only display widgets that burnm power */
1206 case snd_soc_dapm_hp
:
1207 case snd_soc_dapm_mic
:
1208 case snd_soc_dapm_spk
:
1209 case snd_soc_dapm_line
:
1210 case snd_soc_dapm_micbias
:
1211 case snd_soc_dapm_dac
:
1212 case snd_soc_dapm_adc
:
1213 case snd_soc_dapm_pga
:
1214 case snd_soc_dapm_mixer
:
1215 case snd_soc_dapm_mixer_named_ctl
:
1216 case snd_soc_dapm_supply
:
1218 count
+= sprintf(buf
+ count
, "%s: %s\n",
1219 w
->name
, w
->power
? "On":"Off");
1226 switch (codec
->dapm
.bias_level
) {
1227 case SND_SOC_BIAS_ON
:
1230 case SND_SOC_BIAS_PREPARE
:
1233 case SND_SOC_BIAS_STANDBY
:
1236 case SND_SOC_BIAS_OFF
:
1240 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1245 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1247 int snd_soc_dapm_sys_add(struct device
*dev
)
1249 return device_create_file(dev
, &dev_attr_dapm_widget
);
1252 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1254 device_remove_file(dev
, &dev_attr_dapm_widget
);
1257 /* free all dapm widgets and resources */
1258 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
1260 struct snd_soc_dapm_widget
*w
, *next_w
;
1261 struct snd_soc_dapm_path
*p
, *next_p
;
1263 list_for_each_entry_safe(w
, next_w
, &dapm
->widgets
, list
) {
1268 list_for_each_entry_safe(p
, next_p
, &dapm
->paths
, list
) {
1270 kfree(p
->long_name
);
1275 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
1276 const char *pin
, int status
)
1278 struct snd_soc_dapm_widget
*w
;
1280 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1281 if (!strcmp(w
->name
, pin
)) {
1282 pr_debug("dapm: %s: pin %s\n", dapm
->codec
->name
, pin
);
1283 w
->connected
= status
;
1284 /* Allow disabling of forced pins */
1291 pr_err("dapm: %s: configuring unknown pin %s\n",
1292 dapm
->codec
->name
, pin
);
1297 * snd_soc_dapm_sync - scan and power dapm paths
1298 * @dapm: DAPM context
1300 * Walks all dapm audio paths and powers widgets according to their
1301 * stream or path usage.
1303 * Returns 0 for success.
1305 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
1307 return dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1309 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1311 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
1312 const struct snd_soc_dapm_route
*route
)
1314 struct snd_soc_dapm_path
*path
;
1315 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1316 const char *sink
= route
->sink
;
1317 const char *control
= route
->control
;
1318 const char *source
= route
->source
;
1321 /* find src and dest widgets */
1322 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1324 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1328 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1333 if (wsource
== NULL
|| wsink
== NULL
)
1336 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1340 path
->source
= wsource
;
1342 path
->connected
= route
->connected
;
1343 INIT_LIST_HEAD(&path
->list
);
1344 INIT_LIST_HEAD(&path
->list_source
);
1345 INIT_LIST_HEAD(&path
->list_sink
);
1347 /* check for external widgets */
1348 if (wsink
->id
== snd_soc_dapm_input
) {
1349 if (wsource
->id
== snd_soc_dapm_micbias
||
1350 wsource
->id
== snd_soc_dapm_mic
||
1351 wsource
->id
== snd_soc_dapm_line
||
1352 wsource
->id
== snd_soc_dapm_output
)
1355 if (wsource
->id
== snd_soc_dapm_output
) {
1356 if (wsink
->id
== snd_soc_dapm_spk
||
1357 wsink
->id
== snd_soc_dapm_hp
||
1358 wsink
->id
== snd_soc_dapm_line
||
1359 wsink
->id
== snd_soc_dapm_input
)
1363 /* connect static paths */
1364 if (control
== NULL
) {
1365 list_add(&path
->list
, &dapm
->paths
);
1366 list_add(&path
->list_sink
, &wsink
->sources
);
1367 list_add(&path
->list_source
, &wsource
->sinks
);
1372 /* connect dynamic paths */
1374 case snd_soc_dapm_adc
:
1375 case snd_soc_dapm_dac
:
1376 case snd_soc_dapm_pga
:
1377 case snd_soc_dapm_input
:
1378 case snd_soc_dapm_output
:
1379 case snd_soc_dapm_micbias
:
1380 case snd_soc_dapm_vmid
:
1381 case snd_soc_dapm_pre
:
1382 case snd_soc_dapm_post
:
1383 case snd_soc_dapm_supply
:
1384 case snd_soc_dapm_aif_in
:
1385 case snd_soc_dapm_aif_out
:
1386 list_add(&path
->list
, &dapm
->paths
);
1387 list_add(&path
->list_sink
, &wsink
->sources
);
1388 list_add(&path
->list_source
, &wsource
->sinks
);
1391 case snd_soc_dapm_mux
:
1392 case snd_soc_dapm_value_mux
:
1393 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
1394 &wsink
->kcontrols
[0]);
1398 case snd_soc_dapm_switch
:
1399 case snd_soc_dapm_mixer
:
1400 case snd_soc_dapm_mixer_named_ctl
:
1401 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
1405 case snd_soc_dapm_hp
:
1406 case snd_soc_dapm_mic
:
1407 case snd_soc_dapm_line
:
1408 case snd_soc_dapm_spk
:
1409 list_add(&path
->list
, &dapm
->paths
);
1410 list_add(&path
->list_sink
, &wsink
->sources
);
1411 list_add(&path
->list_source
, &wsource
->sinks
);
1418 printk(KERN_WARNING
"asoc: no dapm match for %s --> %s --> %s\n", source
,
1425 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1426 * @dapm: DAPM context
1427 * @route: audio routes
1428 * @num: number of routes
1430 * Connects 2 dapm widgets together via a named audio path. The sink is
1431 * the widget receiving the audio signal, whilst the source is the sender
1432 * of the audio signal.
1434 * Returns 0 for success else error. On error all resources can be freed
1435 * with a call to snd_soc_card_free().
1437 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
1438 const struct snd_soc_dapm_route
*route
, int num
)
1442 for (i
= 0; i
< num
; i
++) {
1443 ret
= snd_soc_dapm_add_route(dapm
, route
);
1445 printk(KERN_ERR
"Failed to add route %s->%s\n",
1455 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1458 * snd_soc_dapm_new_widgets - add new dapm widgets
1459 * @dapm: DAPM context
1461 * Checks the codec for any new dapm widgets and creates them if found.
1463 * Returns 0 for success.
1465 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context
*dapm
)
1467 struct snd_soc_dapm_widget
*w
;
1469 list_for_each_entry(w
, &dapm
->widgets
, list
)
1475 case snd_soc_dapm_switch
:
1476 case snd_soc_dapm_mixer
:
1477 case snd_soc_dapm_mixer_named_ctl
:
1478 w
->power_check
= dapm_generic_check_power
;
1479 dapm_new_mixer(dapm
, w
);
1481 case snd_soc_dapm_mux
:
1482 case snd_soc_dapm_value_mux
:
1483 w
->power_check
= dapm_generic_check_power
;
1484 dapm_new_mux(dapm
, w
);
1486 case snd_soc_dapm_adc
:
1487 case snd_soc_dapm_aif_out
:
1488 w
->power_check
= dapm_adc_check_power
;
1490 case snd_soc_dapm_dac
:
1491 case snd_soc_dapm_aif_in
:
1492 w
->power_check
= dapm_dac_check_power
;
1494 case snd_soc_dapm_pga
:
1495 w
->power_check
= dapm_generic_check_power
;
1496 dapm_new_pga(dapm
, w
);
1498 case snd_soc_dapm_input
:
1499 case snd_soc_dapm_output
:
1500 case snd_soc_dapm_micbias
:
1501 case snd_soc_dapm_spk
:
1502 case snd_soc_dapm_hp
:
1503 case snd_soc_dapm_mic
:
1504 case snd_soc_dapm_line
:
1505 w
->power_check
= dapm_generic_check_power
;
1507 case snd_soc_dapm_supply
:
1508 w
->power_check
= dapm_supply_check_power
;
1509 case snd_soc_dapm_vmid
:
1510 case snd_soc_dapm_pre
:
1511 case snd_soc_dapm_post
:
1517 dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1520 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1523 * snd_soc_dapm_get_volsw - dapm mixer get callback
1524 * @kcontrol: mixer control
1525 * @ucontrol: control element information
1527 * Callback to get the value of a dapm mixer control.
1529 * Returns 0 for success.
1531 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1532 struct snd_ctl_elem_value
*ucontrol
)
1534 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1535 struct soc_mixer_control
*mc
=
1536 (struct soc_mixer_control
*)kcontrol
->private_value
;
1537 unsigned int reg
= mc
->reg
;
1538 unsigned int shift
= mc
->shift
;
1539 unsigned int rshift
= mc
->rshift
;
1541 unsigned int invert
= mc
->invert
;
1542 unsigned int mask
= (1 << fls(max
)) - 1;
1544 ucontrol
->value
.integer
.value
[0] =
1545 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1546 if (shift
!= rshift
)
1547 ucontrol
->value
.integer
.value
[1] =
1548 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1550 ucontrol
->value
.integer
.value
[0] =
1551 max
- ucontrol
->value
.integer
.value
[0];
1552 if (shift
!= rshift
)
1553 ucontrol
->value
.integer
.value
[1] =
1554 max
- ucontrol
->value
.integer
.value
[1];
1559 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1562 * snd_soc_dapm_put_volsw - dapm mixer set callback
1563 * @kcontrol: mixer control
1564 * @ucontrol: control element information
1566 * Callback to set the value of a dapm mixer control.
1568 * Returns 0 for success.
1570 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1571 struct snd_ctl_elem_value
*ucontrol
)
1573 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1574 struct soc_mixer_control
*mc
=
1575 (struct soc_mixer_control
*)kcontrol
->private_value
;
1576 unsigned int reg
= mc
->reg
;
1577 unsigned int shift
= mc
->shift
;
1578 unsigned int rshift
= mc
->rshift
;
1580 unsigned int mask
= (1 << fls(max
)) - 1;
1581 unsigned int invert
= mc
->invert
;
1582 unsigned int val
, val2
, val_mask
;
1586 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1590 val_mask
= mask
<< shift
;
1592 if (shift
!= rshift
) {
1593 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1596 val_mask
|= mask
<< rshift
;
1597 val
|= val2
<< rshift
;
1600 mutex_lock(&widget
->codec
->mutex
);
1601 widget
->value
= val
;
1603 if (snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
)) {
1605 /* new connection */
1606 connect
= invert
? 0:1;
1608 /* old connection must be powered down */
1609 connect
= invert
? 1:0;
1611 dapm_mixer_update_power(widget
, kcontrol
, connect
);
1614 if (widget
->event
) {
1615 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1616 ret
= widget
->event(widget
, kcontrol
,
1617 SND_SOC_DAPM_PRE_REG
);
1623 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1624 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1625 ret
= widget
->event(widget
, kcontrol
,
1626 SND_SOC_DAPM_POST_REG
);
1628 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1631 mutex_unlock(&widget
->codec
->mutex
);
1634 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1637 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1638 * @kcontrol: mixer control
1639 * @ucontrol: control element information
1641 * Callback to get the value of a dapm enumerated double mixer control.
1643 * Returns 0 for success.
1645 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1646 struct snd_ctl_elem_value
*ucontrol
)
1648 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1649 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1650 unsigned int val
, bitmask
;
1652 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1654 val
= snd_soc_read(widget
->codec
, e
->reg
);
1655 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1656 if (e
->shift_l
!= e
->shift_r
)
1657 ucontrol
->value
.enumerated
.item
[1] =
1658 (val
>> e
->shift_r
) & (bitmask
- 1);
1662 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1665 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1666 * @kcontrol: mixer control
1667 * @ucontrol: control element information
1669 * Callback to set the value of a dapm enumerated double mixer control.
1671 * Returns 0 for success.
1673 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1674 struct snd_ctl_elem_value
*ucontrol
)
1676 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1677 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1678 unsigned int val
, mux
, change
;
1679 unsigned int mask
, bitmask
;
1682 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1684 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1686 mux
= ucontrol
->value
.enumerated
.item
[0];
1687 val
= mux
<< e
->shift_l
;
1688 mask
= (bitmask
- 1) << e
->shift_l
;
1689 if (e
->shift_l
!= e
->shift_r
) {
1690 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1692 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1693 mask
|= (bitmask
- 1) << e
->shift_r
;
1696 mutex_lock(&widget
->codec
->mutex
);
1697 widget
->value
= val
;
1698 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1699 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1701 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1702 ret
= widget
->event(widget
,
1703 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1708 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1710 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1711 ret
= widget
->event(widget
,
1712 kcontrol
, SND_SOC_DAPM_POST_REG
);
1715 mutex_unlock(&widget
->codec
->mutex
);
1718 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1721 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1722 * @kcontrol: mixer control
1723 * @ucontrol: control element information
1725 * Returns 0 for success.
1727 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
1728 struct snd_ctl_elem_value
*ucontrol
)
1730 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1732 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
1736 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
1739 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1740 * @kcontrol: mixer control
1741 * @ucontrol: control element information
1743 * Returns 0 for success.
1745 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
1746 struct snd_ctl_elem_value
*ucontrol
)
1748 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1749 struct soc_enum
*e
=
1750 (struct soc_enum
*)kcontrol
->private_value
;
1754 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
1757 mutex_lock(&widget
->codec
->mutex
);
1759 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
1760 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
1761 dapm_mux_update_power(widget
, kcontrol
, change
, widget
->value
, e
);
1763 mutex_unlock(&widget
->codec
->mutex
);
1766 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
1769 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1771 * @kcontrol: mixer control
1772 * @ucontrol: control element information
1774 * Callback to get the value of a dapm semi enumerated double mixer control.
1776 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1777 * used for handling bitfield coded enumeration for example.
1779 * Returns 0 for success.
1781 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1782 struct snd_ctl_elem_value
*ucontrol
)
1784 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1785 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1786 unsigned int reg_val
, val
, mux
;
1788 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1789 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1790 for (mux
= 0; mux
< e
->max
; mux
++) {
1791 if (val
== e
->values
[mux
])
1794 ucontrol
->value
.enumerated
.item
[0] = mux
;
1795 if (e
->shift_l
!= e
->shift_r
) {
1796 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1797 for (mux
= 0; mux
< e
->max
; mux
++) {
1798 if (val
== e
->values
[mux
])
1801 ucontrol
->value
.enumerated
.item
[1] = mux
;
1806 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1809 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1811 * @kcontrol: mixer control
1812 * @ucontrol: control element information
1814 * Callback to set 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_put_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 val
, mux
, change
;
1830 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1832 mux
= ucontrol
->value
.enumerated
.item
[0];
1833 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1834 mask
= e
->mask
<< e
->shift_l
;
1835 if (e
->shift_l
!= e
->shift_r
) {
1836 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1838 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1839 mask
|= e
->mask
<< e
->shift_r
;
1842 mutex_lock(&widget
->codec
->mutex
);
1843 widget
->value
= val
;
1844 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1845 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1847 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1848 ret
= widget
->event(widget
,
1849 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1854 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1856 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1857 ret
= widget
->event(widget
,
1858 kcontrol
, SND_SOC_DAPM_POST_REG
);
1861 mutex_unlock(&widget
->codec
->mutex
);
1864 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1867 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1869 * @kcontrol: mixer control
1870 * @uinfo: control element information
1872 * Callback to provide information about a pin switch control.
1874 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1875 struct snd_ctl_elem_info
*uinfo
)
1877 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1879 uinfo
->value
.integer
.min
= 0;
1880 uinfo
->value
.integer
.max
= 1;
1884 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1887 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1889 * @kcontrol: mixer control
1892 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1893 struct snd_ctl_elem_value
*ucontrol
)
1895 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1896 const char *pin
= (const char *)kcontrol
->private_value
;
1898 mutex_lock(&codec
->mutex
);
1900 ucontrol
->value
.integer
.value
[0] =
1901 snd_soc_dapm_get_pin_status(&codec
->dapm
, pin
);
1903 mutex_unlock(&codec
->mutex
);
1907 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
1910 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1912 * @kcontrol: mixer control
1915 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
1916 struct snd_ctl_elem_value
*ucontrol
)
1918 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1919 const char *pin
= (const char *)kcontrol
->private_value
;
1921 mutex_lock(&codec
->mutex
);
1923 if (ucontrol
->value
.integer
.value
[0])
1924 snd_soc_dapm_enable_pin(&codec
->dapm
, pin
);
1926 snd_soc_dapm_disable_pin(&codec
->dapm
, pin
);
1928 snd_soc_dapm_sync(&codec
->dapm
);
1930 mutex_unlock(&codec
->mutex
);
1934 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
1937 * snd_soc_dapm_new_control - create new dapm control
1938 * @dapm: DAPM context
1939 * @widget: widget template
1941 * Creates a new dapm control based upon the template.
1943 * Returns 0 for success else error.
1945 int snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
1946 const struct snd_soc_dapm_widget
*widget
)
1948 struct snd_soc_dapm_widget
*w
;
1950 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
1954 w
->codec
= dapm
->codec
;
1955 INIT_LIST_HEAD(&w
->sources
);
1956 INIT_LIST_HEAD(&w
->sinks
);
1957 INIT_LIST_HEAD(&w
->list
);
1958 list_add(&w
->list
, &dapm
->widgets
);
1960 /* machine layer set ups unconnected pins and insertions */
1964 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
1967 * snd_soc_dapm_new_controls - create new dapm controls
1968 * @dapm: DAPM context
1969 * @widget: widget array
1970 * @num: number of widgets
1972 * Creates new DAPM controls based upon the templates.
1974 * Returns 0 for success else error.
1976 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
1977 const struct snd_soc_dapm_widget
*widget
,
1982 for (i
= 0; i
< num
; i
++) {
1983 ret
= snd_soc_dapm_new_control(dapm
, widget
);
1986 "ASoC: Failed to create DAPM control %s: %d\n",
1994 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
1996 static void soc_dapm_stream_event(struct snd_soc_dapm_context
*dapm
,
1997 const char *stream
, int event
)
1999 struct snd_soc_dapm_widget
*w
;
2001 list_for_each_entry(w
, &dapm
->widgets
, list
)
2005 pr_debug("widget %s\n %s stream %s event %d\n",
2006 w
->name
, w
->sname
, stream
, event
);
2007 if (strstr(w
->sname
, stream
)) {
2009 case SND_SOC_DAPM_STREAM_START
:
2012 case SND_SOC_DAPM_STREAM_STOP
:
2015 case SND_SOC_DAPM_STREAM_SUSPEND
:
2016 case SND_SOC_DAPM_STREAM_RESUME
:
2017 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2018 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2024 dapm_power_widgets(dapm
, event
);
2028 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2029 * @rtd: PCM runtime data
2030 * @stream: stream name
2031 * @event: stream event
2033 * Sends a stream event to the dapm core. The core then makes any
2034 * necessary widget power changes.
2036 * Returns 0 for success else error.
2038 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
,
2039 const char *stream
, int event
)
2041 struct snd_soc_codec
*codec
= rtd
->codec
;
2046 mutex_lock(&codec
->mutex
);
2047 soc_dapm_stream_event(&codec
->dapm
, stream
, event
);
2048 mutex_unlock(&codec
->mutex
);
2051 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
2054 * snd_soc_dapm_enable_pin - enable pin.
2055 * @dapm: DAPM context
2058 * Enables input/output pin and its parents or children widgets iff there is
2059 * a valid audio route and active audio stream.
2060 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2061 * do any widget power switching.
2063 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2065 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
2067 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2070 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2071 * @dapm: DAPM context
2074 * Enables input/output pin regardless of any other state. This is
2075 * intended for use with microphone bias supplies used in microphone
2078 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2079 * do any widget power switching.
2081 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
2084 struct snd_soc_dapm_widget
*w
;
2086 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2087 if (!strcmp(w
->name
, pin
)) {
2088 pr_debug("dapm: %s: pin %s\n", dapm
->codec
->name
, pin
);
2095 pr_err("dapm: %s: configuring unknown pin %s\n",
2096 dapm
->codec
->name
, pin
);
2099 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
2102 * snd_soc_dapm_disable_pin - disable pin.
2103 * @dapm: DAPM context
2106 * Disables input/output pin and its parents or children widgets.
2107 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2108 * do any widget power switching.
2110 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
2113 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2115 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2118 * snd_soc_dapm_nc_pin - permanently disable pin.
2119 * @dapm: DAPM context
2122 * Marks the specified pin as being not connected, disabling it along
2123 * any parent or child widgets. At present this is identical to
2124 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2125 * additional things such as disabling controls which only affect
2126 * paths through the pin.
2128 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2129 * do any widget power switching.
2131 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2133 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2135 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2138 * snd_soc_dapm_get_pin_status - get audio pin status
2139 * @dapm: DAPM context
2140 * @pin: audio signal pin endpoint (or start point)
2142 * Get audio pin status - connected or disconnected.
2144 * Returns 1 for connected otherwise 0.
2146 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
2149 struct snd_soc_dapm_widget
*w
;
2151 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2152 if (!strcmp(w
->name
, pin
))
2153 return w
->connected
;
2158 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2161 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2162 * @dapm: DAPM context
2163 * @pin: audio signal pin endpoint (or start point)
2165 * Mark the given endpoint or pin as ignoring suspend. When the
2166 * system is disabled a path between two endpoints flagged as ignoring
2167 * suspend will not be disabled. The path must already be enabled via
2168 * normal means at suspend time, it will not be turned on if it was not
2171 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
2174 struct snd_soc_dapm_widget
*w
;
2176 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2177 if (!strcmp(w
->name
, pin
)) {
2178 w
->ignore_suspend
= 1;
2183 pr_err("Unknown DAPM pin: %s\n", pin
);
2186 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
2189 * snd_soc_dapm_free - free dapm resources
2192 * Free all dapm widgets and resources.
2194 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
2196 snd_soc_dapm_sys_remove(dapm
->dev
);
2197 dapm_free_widgets(dapm
);
2199 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2201 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context
*dapm
)
2203 struct snd_soc_dapm_widget
*w
;
2204 LIST_HEAD(down_list
);
2207 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2209 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2215 /* If there were no widgets to power down we're already in
2219 snd_soc_dapm_set_bias_level(NULL
, dapm
, SND_SOC_BIAS_PREPARE
);
2220 dapm_seq_run(dapm
, &down_list
, 0, dapm_down_seq
);
2221 snd_soc_dapm_set_bias_level(NULL
, dapm
, SND_SOC_BIAS_STANDBY
);
2226 * snd_soc_dapm_shutdown - callback for system shutdown
2228 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
2230 struct snd_soc_codec
*codec
;
2232 list_for_each_entry(codec
, &card
->codec_dev_list
, list
) {
2233 soc_dapm_shutdown_codec(&codec
->dapm
);
2234 snd_soc_dapm_set_bias_level(card
, &codec
->dapm
, SND_SOC_BIAS_OFF
);
2238 /* Module information */
2239 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2240 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2241 MODULE_LICENSE("GPL");