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/initval.h>
48 #include <trace/events/asoc.h>
50 /* dapm power sequences - make this per codec in the future */
51 static int dapm_up_seq
[] = {
52 [snd_soc_dapm_pre
] = 0,
53 [snd_soc_dapm_supply
] = 1,
54 [snd_soc_dapm_micbias
] = 2,
55 [snd_soc_dapm_aif_in
] = 3,
56 [snd_soc_dapm_aif_out
] = 3,
57 [snd_soc_dapm_mic
] = 4,
58 [snd_soc_dapm_mux
] = 5,
59 [snd_soc_dapm_value_mux
] = 5,
60 [snd_soc_dapm_dac
] = 6,
61 [snd_soc_dapm_mixer
] = 7,
62 [snd_soc_dapm_mixer_named_ctl
] = 7,
63 [snd_soc_dapm_pga
] = 8,
64 [snd_soc_dapm_adc
] = 9,
65 [snd_soc_dapm_hp
] = 10,
66 [snd_soc_dapm_spk
] = 10,
67 [snd_soc_dapm_post
] = 11,
70 static int dapm_down_seq
[] = {
71 [snd_soc_dapm_pre
] = 0,
72 [snd_soc_dapm_adc
] = 1,
73 [snd_soc_dapm_hp
] = 2,
74 [snd_soc_dapm_spk
] = 2,
75 [snd_soc_dapm_pga
] = 4,
76 [snd_soc_dapm_mixer_named_ctl
] = 5,
77 [snd_soc_dapm_mixer
] = 5,
78 [snd_soc_dapm_dac
] = 6,
79 [snd_soc_dapm_mic
] = 7,
80 [snd_soc_dapm_micbias
] = 8,
81 [snd_soc_dapm_mux
] = 9,
82 [snd_soc_dapm_value_mux
] = 9,
83 [snd_soc_dapm_aif_in
] = 10,
84 [snd_soc_dapm_aif_out
] = 10,
85 [snd_soc_dapm_supply
] = 11,
86 [snd_soc_dapm_post
] = 12,
89 static void pop_wait(u32 pop_time
)
92 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
95 static void pop_dbg(struct device
*dev
, u32 pop_time
, const char *fmt
, ...)
103 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
108 vsnprintf(buf
, PAGE_SIZE
, fmt
, args
);
115 /* create a new dapm widget */
116 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
117 const struct snd_soc_dapm_widget
*_widget
)
119 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
123 * snd_soc_dapm_set_bias_level - set the bias level for the system
124 * @card: audio device
125 * @level: level to configure
127 * Configure the bias (power) levels for the SoC audio device.
129 * Returns 0 for success else error.
131 static int snd_soc_dapm_set_bias_level(struct snd_soc_card
*card
,
132 struct snd_soc_dapm_context
*dapm
,
133 enum snd_soc_bias_level level
)
138 case SND_SOC_BIAS_ON
:
139 dev_dbg(dapm
->dev
, "Setting full bias\n");
141 case SND_SOC_BIAS_PREPARE
:
142 dev_dbg(dapm
->dev
, "Setting bias prepare\n");
144 case SND_SOC_BIAS_STANDBY
:
145 dev_dbg(dapm
->dev
, "Setting standby bias\n");
147 case SND_SOC_BIAS_OFF
:
148 dev_dbg(dapm
->dev
, "Setting bias off\n");
151 dev_err(dapm
->dev
, "Setting invalid bias %d\n", level
);
155 trace_snd_soc_bias_level_start(card
, level
);
157 if (card
&& card
->set_bias_level
)
158 ret
= card
->set_bias_level(card
, level
);
160 if (dapm
->codec
&& dapm
->codec
->driver
->set_bias_level
)
161 ret
= dapm
->codec
->driver
->set_bias_level(dapm
->codec
, level
);
163 dapm
->bias_level
= level
;
166 trace_snd_soc_bias_level_done(card
, level
);
171 /* set up initial codec paths */
172 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
173 struct snd_soc_dapm_path
*p
, int i
)
176 case snd_soc_dapm_switch
:
177 case snd_soc_dapm_mixer
:
178 case snd_soc_dapm_mixer_named_ctl
: {
180 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
181 w
->kcontrols
[i
].private_value
;
182 unsigned int reg
= mc
->reg
;
183 unsigned int shift
= mc
->shift
;
185 unsigned int mask
= (1 << fls(max
)) - 1;
186 unsigned int invert
= mc
->invert
;
188 val
= snd_soc_read(w
->codec
, reg
);
189 val
= (val
>> shift
) & mask
;
191 if ((invert
&& !val
) || (!invert
&& val
))
197 case snd_soc_dapm_mux
: {
198 struct soc_enum
*e
= (struct soc_enum
*)w
->kcontrols
[i
].private_value
;
199 int val
, item
, bitmask
;
201 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
203 val
= snd_soc_read(w
->codec
, e
->reg
);
204 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
207 for (i
= 0; i
< e
->max
; i
++) {
208 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
213 case snd_soc_dapm_value_mux
: {
214 struct soc_enum
*e
= (struct soc_enum
*)
215 w
->kcontrols
[i
].private_value
;
218 val
= snd_soc_read(w
->codec
, e
->reg
);
219 val
= (val
>> e
->shift_l
) & e
->mask
;
220 for (item
= 0; item
< e
->max
; item
++) {
221 if (val
== e
->values
[item
])
226 for (i
= 0; i
< e
->max
; i
++) {
227 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
232 /* does not effect routing - always connected */
233 case snd_soc_dapm_pga
:
234 case snd_soc_dapm_output
:
235 case snd_soc_dapm_adc
:
236 case snd_soc_dapm_input
:
237 case snd_soc_dapm_dac
:
238 case snd_soc_dapm_micbias
:
239 case snd_soc_dapm_vmid
:
240 case snd_soc_dapm_supply
:
241 case snd_soc_dapm_aif_in
:
242 case snd_soc_dapm_aif_out
:
245 /* does effect routing - dynamically connected */
246 case snd_soc_dapm_hp
:
247 case snd_soc_dapm_mic
:
248 case snd_soc_dapm_spk
:
249 case snd_soc_dapm_line
:
250 case snd_soc_dapm_pre
:
251 case snd_soc_dapm_post
:
257 /* connect mux widget to its interconnecting audio paths */
258 static int dapm_connect_mux(struct snd_soc_dapm_context
*dapm
,
259 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
260 struct snd_soc_dapm_path
*path
, const char *control_name
,
261 const struct snd_kcontrol_new
*kcontrol
)
263 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
266 for (i
= 0; i
< e
->max
; i
++) {
267 if (!(strcmp(control_name
, e
->texts
[i
]))) {
268 list_add(&path
->list
, &dapm
->paths
);
269 list_add(&path
->list_sink
, &dest
->sources
);
270 list_add(&path
->list_source
, &src
->sinks
);
271 path
->name
= (char*)e
->texts
[i
];
272 dapm_set_path_status(dest
, path
, 0);
280 /* connect mixer widget to its interconnecting audio paths */
281 static int dapm_connect_mixer(struct snd_soc_dapm_context
*dapm
,
282 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
283 struct snd_soc_dapm_path
*path
, const char *control_name
)
287 /* search for mixer kcontrol */
288 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
289 if (!strcmp(control_name
, dest
->kcontrols
[i
].name
)) {
290 list_add(&path
->list
, &dapm
->paths
);
291 list_add(&path
->list_sink
, &dest
->sources
);
292 list_add(&path
->list_source
, &src
->sinks
);
293 path
->name
= dest
->kcontrols
[i
].name
;
294 dapm_set_path_status(dest
, path
, i
);
301 /* update dapm codec register bits */
302 static int dapm_update_bits(struct snd_soc_dapm_widget
*widget
)
305 unsigned int old
, new;
306 struct snd_soc_codec
*codec
= widget
->codec
;
307 struct snd_soc_dapm_context
*dapm
= widget
->dapm
;
308 struct snd_soc_card
*card
= dapm
->card
;
310 /* check for valid widgets */
311 if (widget
->reg
< 0 || widget
->id
== snd_soc_dapm_input
||
312 widget
->id
== snd_soc_dapm_output
||
313 widget
->id
== snd_soc_dapm_hp
||
314 widget
->id
== snd_soc_dapm_mic
||
315 widget
->id
== snd_soc_dapm_line
||
316 widget
->id
== snd_soc_dapm_spk
)
319 power
= widget
->power
;
321 power
= (power
? 0:1);
323 old
= snd_soc_read(codec
, widget
->reg
);
324 new = (old
& ~(0x1 << widget
->shift
)) | (power
<< widget
->shift
);
328 pop_dbg(dapm
->dev
, card
->pop_time
,
329 "pop test %s : %s in %d ms\n",
330 widget
->name
, widget
->power
? "on" : "off",
332 pop_wait(card
->pop_time
);
333 snd_soc_write(codec
, widget
->reg
, new);
335 dev_dbg(dapm
->dev
, "reg %x old %x new %x change %d\n", widget
->reg
,
340 /* create new dapm mixer control */
341 static int dapm_new_mixer(struct snd_soc_dapm_context
*dapm
,
342 struct snd_soc_dapm_widget
*w
)
346 struct snd_soc_dapm_path
*path
;
347 struct snd_card
*card
= dapm
->codec
->card
->snd_card
;
350 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
353 list_for_each_entry(path
, &w
->sources
, list_sink
) {
355 /* mixer/mux paths name must match control name */
356 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
359 /* add dapm control with long name.
360 * for dapm_mixer this is the concatenation of the
361 * mixer and kcontrol name.
362 * for dapm_mixer_named_ctl this is simply the
365 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
366 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
367 name_len
+= 1 + strlen(w
->name
);
369 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
371 if (path
->long_name
== NULL
)
376 snprintf(path
->long_name
, name_len
, "%s %s",
377 w
->name
, w
->kcontrols
[i
].name
);
379 case snd_soc_dapm_mixer_named_ctl
:
380 snprintf(path
->long_name
, name_len
, "%s",
381 w
->kcontrols
[i
].name
);
385 path
->long_name
[name_len
- 1] = '\0';
387 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
389 ret
= snd_ctl_add(card
, path
->kcontrol
);
392 "asoc: failed to add dapm kcontrol %s: %d\n",
393 path
->long_name
, ret
);
394 kfree(path
->long_name
);
395 path
->long_name
= NULL
;
403 /* create new dapm mux control */
404 static int dapm_new_mux(struct snd_soc_dapm_context
*dapm
,
405 struct snd_soc_dapm_widget
*w
)
407 struct snd_soc_dapm_path
*path
= NULL
;
408 struct snd_kcontrol
*kcontrol
;
409 struct snd_card
*card
= dapm
->codec
->card
->snd_card
;
412 if (!w
->num_kcontrols
) {
413 dev_err(dapm
->dev
, "asoc: mux %s has no controls\n", w
->name
);
417 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
418 ret
= snd_ctl_add(card
, kcontrol
);
423 list_for_each_entry(path
, &w
->sources
, list_sink
)
424 path
->kcontrol
= kcontrol
;
429 dev_err(dapm
->dev
, "asoc: failed to add kcontrol %s\n", w
->name
);
433 /* create new dapm volume control */
434 static int dapm_new_pga(struct snd_soc_dapm_context
*dapm
,
435 struct snd_soc_dapm_widget
*w
)
437 if (w
->num_kcontrols
)
438 dev_err(w
->dapm
->dev
,
439 "asoc: PGA controls not supported: '%s'\n", w
->name
);
444 /* reset 'walked' bit for each dapm path */
445 static inline void dapm_clear_walk(struct snd_soc_dapm_context
*dapm
)
447 struct snd_soc_dapm_path
*p
;
449 list_for_each_entry(p
, &dapm
->paths
, list
)
453 /* We implement power down on suspend by checking the power state of
454 * the ALSA card - when we are suspending the ALSA state for the card
457 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
459 int level
= snd_power_get_state(widget
->dapm
->codec
->card
->snd_card
);
462 case SNDRV_CTL_POWER_D3hot
:
463 case SNDRV_CTL_POWER_D3cold
:
464 if (widget
->ignore_suspend
)
465 dev_dbg(widget
->dapm
->dev
, "%s ignoring suspend\n",
467 return widget
->ignore_suspend
;
474 * Recursively check for a completed path to an active or physically connected
475 * output widget. Returns number of complete paths.
477 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
479 struct snd_soc_dapm_path
*path
;
482 if (widget
->id
== snd_soc_dapm_supply
)
485 switch (widget
->id
) {
486 case snd_soc_dapm_adc
:
487 case snd_soc_dapm_aif_out
:
489 return snd_soc_dapm_suspend_check(widget
);
494 if (widget
->connected
) {
495 /* connected pin ? */
496 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
497 return snd_soc_dapm_suspend_check(widget
);
499 /* connected jack or spk ? */
500 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
501 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sources
)))
502 return snd_soc_dapm_suspend_check(widget
);
505 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
509 if (path
->sink
&& path
->connect
) {
511 con
+= is_connected_output_ep(path
->sink
);
519 * Recursively check for a completed path to an active or physically connected
520 * input widget. Returns number of complete paths.
522 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
524 struct snd_soc_dapm_path
*path
;
527 if (widget
->id
== snd_soc_dapm_supply
)
530 /* active stream ? */
531 switch (widget
->id
) {
532 case snd_soc_dapm_dac
:
533 case snd_soc_dapm_aif_in
:
535 return snd_soc_dapm_suspend_check(widget
);
540 if (widget
->connected
) {
541 /* connected pin ? */
542 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
543 return snd_soc_dapm_suspend_check(widget
);
545 /* connected VMID/Bias for lower pops */
546 if (widget
->id
== snd_soc_dapm_vmid
)
547 return snd_soc_dapm_suspend_check(widget
);
549 /* connected jack ? */
550 if (widget
->id
== snd_soc_dapm_mic
||
551 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sinks
)))
552 return snd_soc_dapm_suspend_check(widget
);
555 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
559 if (path
->source
&& path
->connect
) {
561 con
+= is_connected_input_ep(path
->source
);
569 * Handler for generic register modifier widget.
571 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
572 struct snd_kcontrol
*kcontrol
, int event
)
576 if (SND_SOC_DAPM_EVENT_ON(event
))
581 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
582 w
->mask
<< w
->shift
, val
<< w
->shift
);
586 EXPORT_SYMBOL_GPL(dapm_reg_event
);
588 /* Standard power change method, used to apply power changes to most
591 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
595 /* call any power change event handlers */
597 dev_dbg(w
->dapm
->dev
, "power %s event for %s flags %x\n",
598 w
->power
? "on" : "off",
599 w
->name
, w
->event_flags
);
601 /* power up pre event */
602 if (w
->power
&& w
->event
&&
603 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
604 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
609 /* power down pre event */
610 if (!w
->power
&& w
->event
&&
611 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
612 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
619 /* power up post event */
620 if (w
->power
&& w
->event
&&
621 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
623 NULL
, SND_SOC_DAPM_POST_PMU
);
628 /* power down post event */
629 if (!w
->power
&& w
->event
&&
630 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
631 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
639 /* Generic check to see if a widget should be powered.
641 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
645 in
= is_connected_input_ep(w
);
646 dapm_clear_walk(w
->dapm
);
647 out
= is_connected_output_ep(w
);
648 dapm_clear_walk(w
->dapm
);
649 return out
!= 0 && in
!= 0;
652 /* Check to see if an ADC has power */
653 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
658 in
= is_connected_input_ep(w
);
659 dapm_clear_walk(w
->dapm
);
662 return dapm_generic_check_power(w
);
666 /* Check to see if a DAC has power */
667 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
672 out
= is_connected_output_ep(w
);
673 dapm_clear_walk(w
->dapm
);
676 return dapm_generic_check_power(w
);
680 /* Check to see if a power supply is needed */
681 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
683 struct snd_soc_dapm_path
*path
;
686 /* Check if one of our outputs is connected */
687 list_for_each_entry(path
, &w
->sinks
, list_source
) {
688 if (path
->connected
&&
689 !path
->connected(path
->source
, path
->sink
))
692 if (path
->sink
&& path
->sink
->power_check
&&
693 path
->sink
->power_check(path
->sink
)) {
699 dapm_clear_walk(w
->dapm
);
704 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
705 struct snd_soc_dapm_widget
*b
,
708 if (sort
[a
->id
] != sort
[b
->id
])
709 return sort
[a
->id
] - sort
[b
->id
];
710 if (a
->reg
!= b
->reg
)
711 return a
->reg
- b
->reg
;
712 if (a
->dapm
!= b
->dapm
)
713 return (unsigned long)a
->dapm
- (unsigned long)b
->dapm
;
718 /* Insert a widget in order into a DAPM power sequence. */
719 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
720 struct list_head
*list
,
723 struct snd_soc_dapm_widget
*w
;
725 list_for_each_entry(w
, list
, power_list
)
726 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
727 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
731 list_add_tail(&new_widget
->power_list
, list
);
734 static void dapm_seq_check_event(struct snd_soc_dapm_context
*dapm
,
735 struct snd_soc_dapm_widget
*w
, int event
)
737 struct snd_soc_card
*card
= dapm
->card
;
742 case SND_SOC_DAPM_PRE_PMU
:
746 case SND_SOC_DAPM_POST_PMU
:
747 ev_name
= "POST_PMU";
750 case SND_SOC_DAPM_PRE_PMD
:
754 case SND_SOC_DAPM_POST_PMD
:
755 ev_name
= "POST_PMD";
763 if (w
->power
!= power
)
766 if (w
->event
&& (w
->event_flags
& event
)) {
767 pop_dbg(dapm
->dev
, card
->pop_time
, "pop test : %s %s\n",
769 trace_snd_soc_dapm_widget_event_start(w
, event
);
770 ret
= w
->event(w
, NULL
, event
);
771 trace_snd_soc_dapm_widget_event_done(w
, event
);
773 pr_err("%s: %s event failed: %d\n",
774 ev_name
, w
->name
, ret
);
778 /* Apply the coalesced changes from a DAPM sequence */
779 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context
*dapm
,
780 struct list_head
*pending
)
782 struct snd_soc_card
*card
= dapm
->card
;
783 struct snd_soc_dapm_widget
*w
;
785 unsigned int value
= 0;
786 unsigned int mask
= 0;
787 unsigned int cur_mask
;
789 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
792 list_for_each_entry(w
, pending
, power_list
) {
793 cur_mask
= 1 << w
->shift
;
794 BUG_ON(reg
!= w
->reg
);
805 pop_dbg(dapm
->dev
, card
->pop_time
,
806 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
807 w
->name
, reg
, value
, mask
);
809 /* Check for events */
810 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMU
);
811 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_PRE_PMD
);
815 pop_dbg(dapm
->dev
, card
->pop_time
,
816 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
817 value
, mask
, reg
, card
->pop_time
);
818 pop_wait(card
->pop_time
);
819 snd_soc_update_bits(dapm
->codec
, reg
, mask
, value
);
822 list_for_each_entry(w
, pending
, power_list
) {
823 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMU
);
824 dapm_seq_check_event(dapm
, w
, SND_SOC_DAPM_POST_PMD
);
828 /* Apply a DAPM power sequence.
830 * We walk over a pre-sorted list of widgets to apply power to. In
831 * order to minimise the number of writes to the device required
832 * multiple widgets will be updated in a single write where possible.
833 * Currently anything that requires more than a single write is not
836 static void dapm_seq_run(struct snd_soc_dapm_context
*dapm
,
837 struct list_head
*list
, int event
, int sort
[])
839 struct snd_soc_dapm_widget
*w
, *n
;
842 int cur_reg
= SND_SOC_NOPM
;
845 list_for_each_entry_safe(w
, n
, list
, power_list
) {
848 /* Do we need to apply any queued changes? */
849 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
850 if (!list_empty(&pending
))
851 dapm_seq_run_coalesced(dapm
, &pending
);
853 INIT_LIST_HEAD(&pending
);
855 cur_reg
= SND_SOC_NOPM
;
859 case snd_soc_dapm_pre
:
861 list_for_each_entry_safe_continue(w
, n
, list
,
864 if (event
== SND_SOC_DAPM_STREAM_START
)
866 NULL
, SND_SOC_DAPM_PRE_PMU
);
867 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
869 NULL
, SND_SOC_DAPM_PRE_PMD
);
872 case snd_soc_dapm_post
:
874 list_for_each_entry_safe_continue(w
, n
, list
,
877 if (event
== SND_SOC_DAPM_STREAM_START
)
879 NULL
, SND_SOC_DAPM_POST_PMU
);
880 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
882 NULL
, SND_SOC_DAPM_POST_PMD
);
885 case snd_soc_dapm_input
:
886 case snd_soc_dapm_output
:
887 case snd_soc_dapm_hp
:
888 case snd_soc_dapm_mic
:
889 case snd_soc_dapm_line
:
890 case snd_soc_dapm_spk
:
891 /* No register support currently */
892 ret
= dapm_generic_apply_power(w
);
896 /* Queue it up for application */
897 cur_sort
= sort
[w
->id
];
899 list_move(&w
->power_list
, &pending
);
904 dev_err(w
->dapm
->dev
,
905 "Failed to apply widget power: %d\n", ret
);
908 if (!list_empty(&pending
))
909 dapm_seq_run_coalesced(dapm
, &pending
);
913 * Scan each dapm widget for complete audio path.
914 * A complete path is a route that has valid endpoints i.e.:-
916 * o DAC to output pin.
917 * o Input Pin to ADC.
918 * o Input pin to Output pin (bypass, sidetone)
919 * o DAC to ADC (loopback).
921 static int dapm_power_widgets(struct snd_soc_dapm_context
*dapm
, int event
)
923 struct snd_soc_card
*card
= dapm
->codec
->card
;
924 struct snd_soc_dapm_widget
*w
;
926 LIST_HEAD(down_list
);
931 trace_snd_soc_dapm_start(card
);
933 /* Check which widgets we need to power and store them in
934 * lists indicating if they should be powered up or down.
936 list_for_each_entry(w
, &dapm
->widgets
, list
) {
938 case snd_soc_dapm_pre
:
939 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
941 case snd_soc_dapm_post
:
942 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
950 power
= w
->power_check(w
);
956 if (w
->power
== power
)
959 trace_snd_soc_dapm_widget_power(w
, power
);
962 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
964 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
971 /* If there are no DAPM widgets then try to figure out power from the
974 if (list_empty(&dapm
->widgets
)) {
976 case SND_SOC_DAPM_STREAM_START
:
977 case SND_SOC_DAPM_STREAM_RESUME
:
980 case SND_SOC_DAPM_STREAM_SUSPEND
:
983 case SND_SOC_DAPM_STREAM_NOP
:
984 switch (dapm
->bias_level
) {
985 case SND_SOC_BIAS_STANDBY
:
986 case SND_SOC_BIAS_OFF
:
999 if (sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_OFF
) {
1000 ret
= snd_soc_dapm_set_bias_level(card
, dapm
,
1001 SND_SOC_BIAS_STANDBY
);
1004 "Failed to turn on bias: %d\n", ret
);
1007 /* If we're changing to all on or all off then prepare */
1008 if ((sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
1009 (!sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_ON
)) {
1010 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_PREPARE
);
1013 "Failed to prepare bias: %d\n", ret
);
1016 /* Power down widgets first; try to avoid amplifying pops. */
1017 dapm_seq_run(dapm
, &down_list
, event
, dapm_down_seq
);
1020 dapm_seq_run(dapm
, &up_list
, event
, dapm_up_seq
);
1022 /* If we just powered the last thing off drop to standby bias */
1023 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
1024 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_STANDBY
);
1027 "Failed to apply standby bias: %d\n", ret
);
1030 /* If we're in standby and can support bias off then do that */
1031 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1032 dapm
->idle_bias_off
) {
1033 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_OFF
);
1036 "Failed to turn off bias: %d\n", ret
);
1039 /* If we just powered up then move to active bias */
1040 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
1041 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_ON
);
1044 "Failed to apply active bias: %d\n", ret
);
1047 pop_dbg(dapm
->dev
, card
->pop_time
,
1048 "DAPM sequencing finished, waiting %dms\n", card
->pop_time
);
1049 pop_wait(card
->pop_time
);
1051 trace_snd_soc_dapm_done(card
);
1056 #ifdef CONFIG_DEBUG_FS
1057 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1059 file
->private_data
= inode
->i_private
;
1063 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1064 char __user
*user_buf
,
1065 size_t count
, loff_t
*ppos
)
1067 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1071 struct snd_soc_dapm_path
*p
= NULL
;
1073 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1077 in
= is_connected_input_ep(w
);
1078 dapm_clear_walk(w
->dapm
);
1079 out
= is_connected_output_ep(w
);
1080 dapm_clear_walk(w
->dapm
);
1082 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1083 w
->name
, w
->power
? "On" : "Off", in
, out
);
1086 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1087 " - R%d(0x%x) bit %d",
1088 w
->reg
, w
->reg
, w
->shift
);
1090 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1093 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1095 w
->active
? "active" : "inactive");
1097 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1098 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1102 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1104 p
->name
? p
->name
: "static",
1107 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1108 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1112 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1114 p
->name
? p
->name
: "static",
1118 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1124 static const struct file_operations dapm_widget_power_fops
= {
1125 .open
= dapm_widget_power_open_file
,
1126 .read
= dapm_widget_power_read_file
,
1127 .llseek
= default_llseek
,
1130 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
)
1132 struct snd_soc_dapm_widget
*w
;
1135 if (!dapm
->debugfs_dapm
)
1138 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1142 d
= debugfs_create_file(w
->name
, 0444,
1143 dapm
->debugfs_dapm
, w
,
1144 &dapm_widget_power_fops
);
1146 dev_warn(w
->dapm
->dev
,
1147 "ASoC: Failed to create %s debugfs file\n",
1152 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
)
1157 /* test and update the power status of a mux widget */
1158 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1159 struct snd_kcontrol
*kcontrol
, int change
,
1160 int mux
, struct soc_enum
*e
)
1162 struct snd_soc_dapm_path
*path
;
1165 if (widget
->id
!= snd_soc_dapm_mux
&&
1166 widget
->id
!= snd_soc_dapm_value_mux
)
1172 /* find dapm widget path assoc with kcontrol */
1173 list_for_each_entry(path
, &widget
->dapm
->paths
, list
) {
1174 if (path
->kcontrol
!= kcontrol
)
1177 if (!path
->name
|| !e
->texts
[mux
])
1181 /* we now need to match the string in the enum to the path */
1182 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1183 path
->connect
= 1; /* new connection */
1185 path
->connect
= 0; /* old connection must be powered down */
1189 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1194 /* test and update the power status of a mixer or switch widget */
1195 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1196 struct snd_kcontrol
*kcontrol
, int connect
)
1198 struct snd_soc_dapm_path
*path
;
1201 if (widget
->id
!= snd_soc_dapm_mixer
&&
1202 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1203 widget
->id
!= snd_soc_dapm_switch
)
1206 /* find dapm widget path assoc with kcontrol */
1207 list_for_each_entry(path
, &widget
->dapm
->paths
, list
) {
1208 if (path
->kcontrol
!= kcontrol
)
1211 /* found, now check type */
1213 path
->connect
= connect
;
1218 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1223 /* show dapm widget status in sys fs */
1224 static ssize_t
dapm_widget_show(struct device
*dev
,
1225 struct device_attribute
*attr
, char *buf
)
1227 struct snd_soc_pcm_runtime
*rtd
=
1228 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
1229 struct snd_soc_codec
*codec
=rtd
->codec
;
1230 struct snd_soc_dapm_widget
*w
;
1232 char *state
= "not set";
1234 list_for_each_entry(w
, &codec
->dapm
.widgets
, list
) {
1236 /* only display widgets that burnm power */
1238 case snd_soc_dapm_hp
:
1239 case snd_soc_dapm_mic
:
1240 case snd_soc_dapm_spk
:
1241 case snd_soc_dapm_line
:
1242 case snd_soc_dapm_micbias
:
1243 case snd_soc_dapm_dac
:
1244 case snd_soc_dapm_adc
:
1245 case snd_soc_dapm_pga
:
1246 case snd_soc_dapm_mixer
:
1247 case snd_soc_dapm_mixer_named_ctl
:
1248 case snd_soc_dapm_supply
:
1250 count
+= sprintf(buf
+ count
, "%s: %s\n",
1251 w
->name
, w
->power
? "On":"Off");
1258 switch (codec
->dapm
.bias_level
) {
1259 case SND_SOC_BIAS_ON
:
1262 case SND_SOC_BIAS_PREPARE
:
1265 case SND_SOC_BIAS_STANDBY
:
1268 case SND_SOC_BIAS_OFF
:
1272 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1277 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1279 int snd_soc_dapm_sys_add(struct device
*dev
)
1281 return device_create_file(dev
, &dev_attr_dapm_widget
);
1284 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1286 device_remove_file(dev
, &dev_attr_dapm_widget
);
1289 /* free all dapm widgets and resources */
1290 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
1292 struct snd_soc_dapm_widget
*w
, *next_w
;
1293 struct snd_soc_dapm_path
*p
, *next_p
;
1295 list_for_each_entry_safe(w
, next_w
, &dapm
->widgets
, list
) {
1301 list_for_each_entry_safe(p
, next_p
, &dapm
->paths
, list
) {
1303 kfree(p
->long_name
);
1308 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
1309 const char *pin
, int status
)
1311 struct snd_soc_dapm_widget
*w
;
1313 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1314 if (!strcmp(w
->name
, pin
)) {
1315 dev_dbg(w
->dapm
->dev
, "dapm: pin %s = %d\n",
1317 w
->connected
= status
;
1318 /* Allow disabling of forced pins */
1325 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
1330 * snd_soc_dapm_sync - scan and power dapm paths
1331 * @dapm: DAPM context
1333 * Walks all dapm audio paths and powers widgets according to their
1334 * stream or path usage.
1336 * Returns 0 for success.
1338 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
1340 return dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1342 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1344 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
1345 const struct snd_soc_dapm_route
*route
)
1347 struct snd_soc_dapm_path
*path
;
1348 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1350 const char *control
= route
->control
;
1352 char prefixed_sink
[80];
1353 char prefixed_source
[80];
1356 if (dapm
->codec
->name_prefix
) {
1357 snprintf(prefixed_sink
, sizeof(prefixed_sink
), "%s %s",
1358 dapm
->codec
->name_prefix
, route
->sink
);
1359 sink
= prefixed_sink
;
1360 snprintf(prefixed_source
, sizeof(prefixed_source
), "%s %s",
1361 dapm
->codec
->name_prefix
, route
->source
);
1362 source
= prefixed_source
;
1365 source
= route
->source
;
1368 /* find src and dest widgets */
1369 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1371 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1375 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1380 if (wsource
== NULL
|| wsink
== NULL
)
1383 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1387 path
->source
= wsource
;
1389 path
->connected
= route
->connected
;
1390 INIT_LIST_HEAD(&path
->list
);
1391 INIT_LIST_HEAD(&path
->list_source
);
1392 INIT_LIST_HEAD(&path
->list_sink
);
1394 /* check for external widgets */
1395 if (wsink
->id
== snd_soc_dapm_input
) {
1396 if (wsource
->id
== snd_soc_dapm_micbias
||
1397 wsource
->id
== snd_soc_dapm_mic
||
1398 wsource
->id
== snd_soc_dapm_line
||
1399 wsource
->id
== snd_soc_dapm_output
)
1402 if (wsource
->id
== snd_soc_dapm_output
) {
1403 if (wsink
->id
== snd_soc_dapm_spk
||
1404 wsink
->id
== snd_soc_dapm_hp
||
1405 wsink
->id
== snd_soc_dapm_line
||
1406 wsink
->id
== snd_soc_dapm_input
)
1410 /* connect static paths */
1411 if (control
== NULL
) {
1412 list_add(&path
->list
, &dapm
->paths
);
1413 list_add(&path
->list_sink
, &wsink
->sources
);
1414 list_add(&path
->list_source
, &wsource
->sinks
);
1419 /* connect dynamic paths */
1421 case snd_soc_dapm_adc
:
1422 case snd_soc_dapm_dac
:
1423 case snd_soc_dapm_pga
:
1424 case snd_soc_dapm_input
:
1425 case snd_soc_dapm_output
:
1426 case snd_soc_dapm_micbias
:
1427 case snd_soc_dapm_vmid
:
1428 case snd_soc_dapm_pre
:
1429 case snd_soc_dapm_post
:
1430 case snd_soc_dapm_supply
:
1431 case snd_soc_dapm_aif_in
:
1432 case snd_soc_dapm_aif_out
:
1433 list_add(&path
->list
, &dapm
->paths
);
1434 list_add(&path
->list_sink
, &wsink
->sources
);
1435 list_add(&path
->list_source
, &wsource
->sinks
);
1438 case snd_soc_dapm_mux
:
1439 case snd_soc_dapm_value_mux
:
1440 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
1441 &wsink
->kcontrols
[0]);
1445 case snd_soc_dapm_switch
:
1446 case snd_soc_dapm_mixer
:
1447 case snd_soc_dapm_mixer_named_ctl
:
1448 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
1452 case snd_soc_dapm_hp
:
1453 case snd_soc_dapm_mic
:
1454 case snd_soc_dapm_line
:
1455 case snd_soc_dapm_spk
:
1456 list_add(&path
->list
, &dapm
->paths
);
1457 list_add(&path
->list_sink
, &wsink
->sources
);
1458 list_add(&path
->list_source
, &wsource
->sinks
);
1465 dev_warn(dapm
->dev
, "asoc: no dapm match for %s --> %s --> %s\n",
1466 source
, control
, sink
);
1472 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1473 * @dapm: DAPM context
1474 * @route: audio routes
1475 * @num: number of routes
1477 * Connects 2 dapm widgets together via a named audio path. The sink is
1478 * the widget receiving the audio signal, whilst the source is the sender
1479 * of the audio signal.
1481 * Returns 0 for success else error. On error all resources can be freed
1482 * with a call to snd_soc_card_free().
1484 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
1485 const struct snd_soc_dapm_route
*route
, int num
)
1489 for (i
= 0; i
< num
; i
++) {
1490 ret
= snd_soc_dapm_add_route(dapm
, route
);
1492 dev_err(dapm
->dev
, "Failed to add route %s->%s\n",
1493 route
->source
, route
->sink
);
1501 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1504 * snd_soc_dapm_new_widgets - add new dapm widgets
1505 * @dapm: DAPM context
1507 * Checks the codec for any new dapm widgets and creates them if found.
1509 * Returns 0 for success.
1511 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context
*dapm
)
1513 struct snd_soc_dapm_widget
*w
;
1515 list_for_each_entry(w
, &dapm
->widgets
, list
)
1521 case snd_soc_dapm_switch
:
1522 case snd_soc_dapm_mixer
:
1523 case snd_soc_dapm_mixer_named_ctl
:
1524 w
->power_check
= dapm_generic_check_power
;
1525 dapm_new_mixer(dapm
, w
);
1527 case snd_soc_dapm_mux
:
1528 case snd_soc_dapm_value_mux
:
1529 w
->power_check
= dapm_generic_check_power
;
1530 dapm_new_mux(dapm
, w
);
1532 case snd_soc_dapm_adc
:
1533 case snd_soc_dapm_aif_out
:
1534 w
->power_check
= dapm_adc_check_power
;
1536 case snd_soc_dapm_dac
:
1537 case snd_soc_dapm_aif_in
:
1538 w
->power_check
= dapm_dac_check_power
;
1540 case snd_soc_dapm_pga
:
1541 w
->power_check
= dapm_generic_check_power
;
1542 dapm_new_pga(dapm
, w
);
1544 case snd_soc_dapm_input
:
1545 case snd_soc_dapm_output
:
1546 case snd_soc_dapm_micbias
:
1547 case snd_soc_dapm_spk
:
1548 case snd_soc_dapm_hp
:
1549 case snd_soc_dapm_mic
:
1550 case snd_soc_dapm_line
:
1551 w
->power_check
= dapm_generic_check_power
;
1553 case snd_soc_dapm_supply
:
1554 w
->power_check
= dapm_supply_check_power
;
1555 case snd_soc_dapm_vmid
:
1556 case snd_soc_dapm_pre
:
1557 case snd_soc_dapm_post
:
1563 dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1566 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1569 * snd_soc_dapm_get_volsw - dapm mixer get callback
1570 * @kcontrol: mixer control
1571 * @ucontrol: control element information
1573 * Callback to get the value of a dapm mixer control.
1575 * Returns 0 for success.
1577 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1578 struct snd_ctl_elem_value
*ucontrol
)
1580 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1581 struct soc_mixer_control
*mc
=
1582 (struct soc_mixer_control
*)kcontrol
->private_value
;
1583 unsigned int reg
= mc
->reg
;
1584 unsigned int shift
= mc
->shift
;
1585 unsigned int rshift
= mc
->rshift
;
1587 unsigned int invert
= mc
->invert
;
1588 unsigned int mask
= (1 << fls(max
)) - 1;
1590 ucontrol
->value
.integer
.value
[0] =
1591 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1592 if (shift
!= rshift
)
1593 ucontrol
->value
.integer
.value
[1] =
1594 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1596 ucontrol
->value
.integer
.value
[0] =
1597 max
- ucontrol
->value
.integer
.value
[0];
1598 if (shift
!= rshift
)
1599 ucontrol
->value
.integer
.value
[1] =
1600 max
- ucontrol
->value
.integer
.value
[1];
1605 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1608 * snd_soc_dapm_put_volsw - dapm mixer set callback
1609 * @kcontrol: mixer control
1610 * @ucontrol: control element information
1612 * Callback to set the value of a dapm mixer control.
1614 * Returns 0 for success.
1616 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1617 struct snd_ctl_elem_value
*ucontrol
)
1619 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1620 struct soc_mixer_control
*mc
=
1621 (struct soc_mixer_control
*)kcontrol
->private_value
;
1622 unsigned int reg
= mc
->reg
;
1623 unsigned int shift
= mc
->shift
;
1624 unsigned int rshift
= mc
->rshift
;
1626 unsigned int mask
= (1 << fls(max
)) - 1;
1627 unsigned int invert
= mc
->invert
;
1628 unsigned int val
, val2
, val_mask
;
1632 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1636 val_mask
= mask
<< shift
;
1638 if (shift
!= rshift
) {
1639 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1642 val_mask
|= mask
<< rshift
;
1643 val
|= val2
<< rshift
;
1646 mutex_lock(&widget
->codec
->mutex
);
1647 widget
->value
= val
;
1649 if (snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
)) {
1651 /* new connection */
1652 connect
= invert
? 0:1;
1654 /* old connection must be powered down */
1655 connect
= invert
? 1:0;
1657 dapm_mixer_update_power(widget
, kcontrol
, connect
);
1660 if (widget
->event
) {
1661 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1662 ret
= widget
->event(widget
, kcontrol
,
1663 SND_SOC_DAPM_PRE_REG
);
1669 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1670 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1671 ret
= widget
->event(widget
, kcontrol
,
1672 SND_SOC_DAPM_POST_REG
);
1674 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1677 mutex_unlock(&widget
->codec
->mutex
);
1680 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1683 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1684 * @kcontrol: mixer control
1685 * @ucontrol: control element information
1687 * Callback to get the value of a dapm enumerated double mixer control.
1689 * Returns 0 for success.
1691 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1692 struct snd_ctl_elem_value
*ucontrol
)
1694 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1695 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1696 unsigned int val
, bitmask
;
1698 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1700 val
= snd_soc_read(widget
->codec
, e
->reg
);
1701 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1702 if (e
->shift_l
!= e
->shift_r
)
1703 ucontrol
->value
.enumerated
.item
[1] =
1704 (val
>> e
->shift_r
) & (bitmask
- 1);
1708 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1711 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1712 * @kcontrol: mixer control
1713 * @ucontrol: control element information
1715 * Callback to set the value of a dapm enumerated double mixer control.
1717 * Returns 0 for success.
1719 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1720 struct snd_ctl_elem_value
*ucontrol
)
1722 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1723 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1724 unsigned int val
, mux
, change
;
1725 unsigned int mask
, bitmask
;
1728 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1730 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1732 mux
= ucontrol
->value
.enumerated
.item
[0];
1733 val
= mux
<< e
->shift_l
;
1734 mask
= (bitmask
- 1) << e
->shift_l
;
1735 if (e
->shift_l
!= e
->shift_r
) {
1736 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1738 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1739 mask
|= (bitmask
- 1) << e
->shift_r
;
1742 mutex_lock(&widget
->codec
->mutex
);
1743 widget
->value
= val
;
1744 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1745 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1747 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1748 ret
= widget
->event(widget
,
1749 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1754 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1756 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1757 ret
= widget
->event(widget
,
1758 kcontrol
, SND_SOC_DAPM_POST_REG
);
1761 mutex_unlock(&widget
->codec
->mutex
);
1764 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1767 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1768 * @kcontrol: mixer control
1769 * @ucontrol: control element information
1771 * Returns 0 for success.
1773 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
1774 struct snd_ctl_elem_value
*ucontrol
)
1776 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1778 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
1782 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
1785 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1786 * @kcontrol: mixer control
1787 * @ucontrol: control element information
1789 * Returns 0 for success.
1791 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
1792 struct snd_ctl_elem_value
*ucontrol
)
1794 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1795 struct soc_enum
*e
=
1796 (struct soc_enum
*)kcontrol
->private_value
;
1800 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
1803 mutex_lock(&widget
->codec
->mutex
);
1805 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
1806 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
1807 dapm_mux_update_power(widget
, kcontrol
, change
, widget
->value
, e
);
1809 mutex_unlock(&widget
->codec
->mutex
);
1812 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
1815 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1817 * @kcontrol: mixer control
1818 * @ucontrol: control element information
1820 * Callback to get the value of a dapm semi enumerated double mixer control.
1822 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1823 * used for handling bitfield coded enumeration for example.
1825 * Returns 0 for success.
1827 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1828 struct snd_ctl_elem_value
*ucontrol
)
1830 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1831 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1832 unsigned int reg_val
, val
, mux
;
1834 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1835 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1836 for (mux
= 0; mux
< e
->max
; mux
++) {
1837 if (val
== e
->values
[mux
])
1840 ucontrol
->value
.enumerated
.item
[0] = mux
;
1841 if (e
->shift_l
!= e
->shift_r
) {
1842 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1843 for (mux
= 0; mux
< e
->max
; mux
++) {
1844 if (val
== e
->values
[mux
])
1847 ucontrol
->value
.enumerated
.item
[1] = mux
;
1852 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1855 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1857 * @kcontrol: mixer control
1858 * @ucontrol: control element information
1860 * Callback to set the value of a dapm semi enumerated double mixer control.
1862 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1863 * used for handling bitfield coded enumeration for example.
1865 * Returns 0 for success.
1867 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
1868 struct snd_ctl_elem_value
*ucontrol
)
1870 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1871 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1872 unsigned int val
, mux
, change
;
1876 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1878 mux
= ucontrol
->value
.enumerated
.item
[0];
1879 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1880 mask
= e
->mask
<< e
->shift_l
;
1881 if (e
->shift_l
!= e
->shift_r
) {
1882 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1884 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1885 mask
|= e
->mask
<< e
->shift_r
;
1888 mutex_lock(&widget
->codec
->mutex
);
1889 widget
->value
= val
;
1890 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1891 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1893 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1894 ret
= widget
->event(widget
,
1895 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1900 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1902 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1903 ret
= widget
->event(widget
,
1904 kcontrol
, SND_SOC_DAPM_POST_REG
);
1907 mutex_unlock(&widget
->codec
->mutex
);
1910 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1913 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1915 * @kcontrol: mixer control
1916 * @uinfo: control element information
1918 * Callback to provide information about a pin switch control.
1920 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1921 struct snd_ctl_elem_info
*uinfo
)
1923 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1925 uinfo
->value
.integer
.min
= 0;
1926 uinfo
->value
.integer
.max
= 1;
1930 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1933 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1935 * @kcontrol: mixer control
1938 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1939 struct snd_ctl_elem_value
*ucontrol
)
1941 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1942 const char *pin
= (const char *)kcontrol
->private_value
;
1944 mutex_lock(&codec
->mutex
);
1946 ucontrol
->value
.integer
.value
[0] =
1947 snd_soc_dapm_get_pin_status(&codec
->dapm
, pin
);
1949 mutex_unlock(&codec
->mutex
);
1953 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
1956 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1958 * @kcontrol: mixer control
1961 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
1962 struct snd_ctl_elem_value
*ucontrol
)
1964 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1965 const char *pin
= (const char *)kcontrol
->private_value
;
1967 mutex_lock(&codec
->mutex
);
1969 if (ucontrol
->value
.integer
.value
[0])
1970 snd_soc_dapm_enable_pin(&codec
->dapm
, pin
);
1972 snd_soc_dapm_disable_pin(&codec
->dapm
, pin
);
1974 snd_soc_dapm_sync(&codec
->dapm
);
1976 mutex_unlock(&codec
->mutex
);
1980 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
1983 * snd_soc_dapm_new_control - create new dapm control
1984 * @dapm: DAPM context
1985 * @widget: widget template
1987 * Creates a new dapm control based upon the template.
1989 * Returns 0 for success else error.
1991 int snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
1992 const struct snd_soc_dapm_widget
*widget
)
1994 struct snd_soc_dapm_widget
*w
;
1997 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
2000 name_len
= strlen(widget
->name
) + 1;
2001 if (dapm
->codec
->name_prefix
)
2002 name_len
+= 1 + strlen(dapm
->codec
->name_prefix
);
2003 w
->name
= kmalloc(name_len
, GFP_KERNEL
);
2004 if (w
->name
== NULL
) {
2008 if (dapm
->codec
->name_prefix
)
2009 snprintf(w
->name
, name_len
, "%s %s",
2010 dapm
->codec
->name_prefix
, widget
->name
);
2012 snprintf(w
->name
, name_len
, "%s", widget
->name
);
2015 w
->codec
= dapm
->codec
;
2016 INIT_LIST_HEAD(&w
->sources
);
2017 INIT_LIST_HEAD(&w
->sinks
);
2018 INIT_LIST_HEAD(&w
->list
);
2019 list_add(&w
->list
, &dapm
->widgets
);
2021 /* machine layer set ups unconnected pins and insertions */
2025 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
2028 * snd_soc_dapm_new_controls - create new dapm controls
2029 * @dapm: DAPM context
2030 * @widget: widget array
2031 * @num: number of widgets
2033 * Creates new DAPM controls based upon the templates.
2035 * Returns 0 for success else error.
2037 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
2038 const struct snd_soc_dapm_widget
*widget
,
2043 for (i
= 0; i
< num
; i
++) {
2044 ret
= snd_soc_dapm_new_control(dapm
, widget
);
2047 "ASoC: Failed to create DAPM control %s: %d\n",
2055 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
2057 static void soc_dapm_stream_event(struct snd_soc_dapm_context
*dapm
,
2058 const char *stream
, int event
)
2060 struct snd_soc_dapm_widget
*w
;
2062 list_for_each_entry(w
, &dapm
->widgets
, list
)
2066 dev_dbg(w
->dapm
->dev
, "widget %s\n %s stream %s event %d\n",
2067 w
->name
, w
->sname
, stream
, event
);
2068 if (strstr(w
->sname
, stream
)) {
2070 case SND_SOC_DAPM_STREAM_START
:
2073 case SND_SOC_DAPM_STREAM_STOP
:
2076 case SND_SOC_DAPM_STREAM_SUSPEND
:
2077 case SND_SOC_DAPM_STREAM_RESUME
:
2078 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2079 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2085 dapm_power_widgets(dapm
, event
);
2089 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2090 * @rtd: PCM runtime data
2091 * @stream: stream name
2092 * @event: stream event
2094 * Sends a stream event to the dapm core. The core then makes any
2095 * necessary widget power changes.
2097 * Returns 0 for success else error.
2099 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
,
2100 const char *stream
, int event
)
2102 struct snd_soc_codec
*codec
= rtd
->codec
;
2107 mutex_lock(&codec
->mutex
);
2108 soc_dapm_stream_event(&codec
->dapm
, stream
, event
);
2109 mutex_unlock(&codec
->mutex
);
2112 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
2115 * snd_soc_dapm_enable_pin - enable pin.
2116 * @dapm: DAPM context
2119 * Enables input/output pin and its parents or children widgets iff there is
2120 * a valid audio route and active audio stream.
2121 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2122 * do any widget power switching.
2124 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2126 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
2128 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2131 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2132 * @dapm: DAPM context
2135 * Enables input/output pin regardless of any other state. This is
2136 * intended for use with microphone bias supplies used in microphone
2139 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2140 * do any widget power switching.
2142 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
2145 struct snd_soc_dapm_widget
*w
;
2147 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2148 if (!strcmp(w
->name
, pin
)) {
2149 dev_dbg(w
->dapm
->dev
,
2150 "dapm: force enable pin %s\n", pin
);
2157 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2160 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
2163 * snd_soc_dapm_disable_pin - disable pin.
2164 * @dapm: DAPM context
2167 * Disables input/output pin and its parents or children widgets.
2168 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2169 * do any widget power switching.
2171 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
2174 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2176 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2179 * snd_soc_dapm_nc_pin - permanently disable pin.
2180 * @dapm: DAPM context
2183 * Marks the specified pin as being not connected, disabling it along
2184 * any parent or child widgets. At present this is identical to
2185 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2186 * additional things such as disabling controls which only affect
2187 * paths through the pin.
2189 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2190 * do any widget power switching.
2192 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2194 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2196 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2199 * snd_soc_dapm_get_pin_status - get audio pin status
2200 * @dapm: DAPM context
2201 * @pin: audio signal pin endpoint (or start point)
2203 * Get audio pin status - connected or disconnected.
2205 * Returns 1 for connected otherwise 0.
2207 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
2210 struct snd_soc_dapm_widget
*w
;
2212 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2213 if (!strcmp(w
->name
, pin
))
2214 return w
->connected
;
2219 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2222 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2223 * @dapm: DAPM context
2224 * @pin: audio signal pin endpoint (or start point)
2226 * Mark the given endpoint or pin as ignoring suspend. When the
2227 * system is disabled a path between two endpoints flagged as ignoring
2228 * suspend will not be disabled. The path must already be enabled via
2229 * normal means at suspend time, it will not be turned on if it was not
2232 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
2235 struct snd_soc_dapm_widget
*w
;
2237 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2238 if (!strcmp(w
->name
, pin
)) {
2239 w
->ignore_suspend
= 1;
2244 dev_err(dapm
->dev
, "dapm: unknown pin %s\n", pin
);
2247 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
2250 * snd_soc_dapm_free - free dapm resources
2253 * Free all dapm widgets and resources.
2255 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
2257 snd_soc_dapm_sys_remove(dapm
->dev
);
2258 dapm_free_widgets(dapm
);
2260 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2262 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context
*dapm
)
2264 struct snd_soc_dapm_widget
*w
;
2265 LIST_HEAD(down_list
);
2268 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2270 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2276 /* If there were no widgets to power down we're already in
2280 snd_soc_dapm_set_bias_level(NULL
, dapm
, SND_SOC_BIAS_PREPARE
);
2281 dapm_seq_run(dapm
, &down_list
, 0, dapm_down_seq
);
2282 snd_soc_dapm_set_bias_level(NULL
, dapm
, SND_SOC_BIAS_STANDBY
);
2287 * snd_soc_dapm_shutdown - callback for system shutdown
2289 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
2291 struct snd_soc_codec
*codec
;
2293 list_for_each_entry(codec
, &card
->codec_dev_list
, list
) {
2294 soc_dapm_shutdown_codec(&codec
->dapm
);
2295 snd_soc_dapm_set_bias_level(card
, &codec
->dapm
, SND_SOC_BIAS_OFF
);
2299 /* Module information */
2300 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2301 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2302 MODULE_LICENSE("GPL");