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
;
297 /* check for valid widgets */
298 if (widget
->reg
< 0 || widget
->id
== snd_soc_dapm_input
||
299 widget
->id
== snd_soc_dapm_output
||
300 widget
->id
== snd_soc_dapm_hp
||
301 widget
->id
== snd_soc_dapm_mic
||
302 widget
->id
== snd_soc_dapm_line
||
303 widget
->id
== snd_soc_dapm_spk
)
306 power
= widget
->power
;
308 power
= (power
? 0:1);
310 old
= snd_soc_read(codec
, widget
->reg
);
311 new = (old
& ~(0x1 << widget
->shift
)) | (power
<< widget
->shift
);
315 pop_dbg(dapm
->pop_time
, "pop test %s : %s in %d ms\n",
316 widget
->name
, widget
->power
? "on" : "off",
318 pop_wait(dapm
->pop_time
);
319 snd_soc_write(codec
, widget
->reg
, new);
321 pr_debug("reg %x old %x new %x change %d\n", widget
->reg
,
326 /* create new dapm mixer control */
327 static int dapm_new_mixer(struct snd_soc_dapm_context
*dapm
,
328 struct snd_soc_dapm_widget
*w
)
332 struct snd_soc_dapm_path
*path
;
333 struct snd_card
*card
= dapm
->codec
->card
->snd_card
;
336 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
339 list_for_each_entry(path
, &w
->sources
, list_sink
) {
341 /* mixer/mux paths name must match control name */
342 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
345 /* add dapm control with long name.
346 * for dapm_mixer this is the concatenation of the
347 * mixer and kcontrol name.
348 * for dapm_mixer_named_ctl this is simply the
351 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
352 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
353 name_len
+= 1 + strlen(w
->name
);
355 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
357 if (path
->long_name
== NULL
)
362 snprintf(path
->long_name
, name_len
, "%s %s",
363 w
->name
, w
->kcontrols
[i
].name
);
365 case snd_soc_dapm_mixer_named_ctl
:
366 snprintf(path
->long_name
, name_len
, "%s",
367 w
->kcontrols
[i
].name
);
371 path
->long_name
[name_len
- 1] = '\0';
373 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
375 ret
= snd_ctl_add(card
, path
->kcontrol
);
377 printk(KERN_ERR
"asoc: failed to add dapm kcontrol %s: %d\n",
380 kfree(path
->long_name
);
381 path
->long_name
= NULL
;
389 /* create new dapm mux control */
390 static int dapm_new_mux(struct snd_soc_dapm_context
*dapm
,
391 struct snd_soc_dapm_widget
*w
)
393 struct snd_soc_dapm_path
*path
= NULL
;
394 struct snd_kcontrol
*kcontrol
;
395 struct snd_card
*card
= dapm
->codec
->card
->snd_card
;
398 if (!w
->num_kcontrols
) {
399 printk(KERN_ERR
"asoc: mux %s has no controls\n", w
->name
);
403 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
404 ret
= snd_ctl_add(card
, kcontrol
);
409 list_for_each_entry(path
, &w
->sources
, list_sink
)
410 path
->kcontrol
= kcontrol
;
415 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
419 /* create new dapm volume control */
420 static int dapm_new_pga(struct snd_soc_dapm_context
*dapm
,
421 struct snd_soc_dapm_widget
*w
)
423 if (w
->num_kcontrols
)
424 pr_err("asoc: PGA controls not supported: '%s'\n", w
->name
);
429 /* reset 'walked' bit for each dapm path */
430 static inline void dapm_clear_walk(struct snd_soc_dapm_context
*dapm
)
432 struct snd_soc_dapm_path
*p
;
434 list_for_each_entry(p
, &dapm
->paths
, list
)
438 /* We implement power down on suspend by checking the power state of
439 * the ALSA card - when we are suspending the ALSA state for the card
442 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
444 int level
= snd_power_get_state(widget
->dapm
->codec
->card
->snd_card
);
447 case SNDRV_CTL_POWER_D3hot
:
448 case SNDRV_CTL_POWER_D3cold
:
449 if (widget
->ignore_suspend
)
450 pr_debug("%s ignoring suspend\n", widget
->name
);
451 return widget
->ignore_suspend
;
458 * Recursively check for a completed path to an active or physically connected
459 * output widget. Returns number of complete paths.
461 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
463 struct snd_soc_dapm_path
*path
;
466 if (widget
->id
== snd_soc_dapm_supply
)
469 switch (widget
->id
) {
470 case snd_soc_dapm_adc
:
471 case snd_soc_dapm_aif_out
:
473 return snd_soc_dapm_suspend_check(widget
);
478 if (widget
->connected
) {
479 /* connected pin ? */
480 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
481 return snd_soc_dapm_suspend_check(widget
);
483 /* connected jack or spk ? */
484 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
485 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sources
)))
486 return snd_soc_dapm_suspend_check(widget
);
489 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
493 if (path
->sink
&& path
->connect
) {
495 con
+= is_connected_output_ep(path
->sink
);
503 * Recursively check for a completed path to an active or physically connected
504 * input widget. Returns number of complete paths.
506 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
508 struct snd_soc_dapm_path
*path
;
511 if (widget
->id
== snd_soc_dapm_supply
)
514 /* active stream ? */
515 switch (widget
->id
) {
516 case snd_soc_dapm_dac
:
517 case snd_soc_dapm_aif_in
:
519 return snd_soc_dapm_suspend_check(widget
);
524 if (widget
->connected
) {
525 /* connected pin ? */
526 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
527 return snd_soc_dapm_suspend_check(widget
);
529 /* connected VMID/Bias for lower pops */
530 if (widget
->id
== snd_soc_dapm_vmid
)
531 return snd_soc_dapm_suspend_check(widget
);
533 /* connected jack ? */
534 if (widget
->id
== snd_soc_dapm_mic
||
535 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sinks
)))
536 return snd_soc_dapm_suspend_check(widget
);
539 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
543 if (path
->source
&& path
->connect
) {
545 con
+= is_connected_input_ep(path
->source
);
553 * Handler for generic register modifier widget.
555 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
556 struct snd_kcontrol
*kcontrol
, int event
)
560 if (SND_SOC_DAPM_EVENT_ON(event
))
565 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
566 w
->mask
<< w
->shift
, val
<< w
->shift
);
570 EXPORT_SYMBOL_GPL(dapm_reg_event
);
572 /* Standard power change method, used to apply power changes to most
575 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
579 /* call any power change event handlers */
581 pr_debug("power %s event for %s flags %x\n",
582 w
->power
? "on" : "off",
583 w
->name
, w
->event_flags
);
585 /* power up pre event */
586 if (w
->power
&& w
->event
&&
587 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
588 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
593 /* power down pre event */
594 if (!w
->power
&& w
->event
&&
595 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
596 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
603 /* power up post event */
604 if (w
->power
&& w
->event
&&
605 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
607 NULL
, SND_SOC_DAPM_POST_PMU
);
612 /* power down post event */
613 if (!w
->power
&& w
->event
&&
614 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
615 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
623 /* Generic check to see if a widget should be powered.
625 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
629 in
= is_connected_input_ep(w
);
630 dapm_clear_walk(w
->dapm
);
631 out
= is_connected_output_ep(w
);
632 dapm_clear_walk(w
->dapm
);
633 return out
!= 0 && in
!= 0;
636 /* Check to see if an ADC has power */
637 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
642 in
= is_connected_input_ep(w
);
643 dapm_clear_walk(w
->dapm
);
646 return dapm_generic_check_power(w
);
650 /* Check to see if a DAC has power */
651 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
656 out
= is_connected_output_ep(w
);
657 dapm_clear_walk(w
->dapm
);
660 return dapm_generic_check_power(w
);
664 /* Check to see if a power supply is needed */
665 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
667 struct snd_soc_dapm_path
*path
;
670 /* Check if one of our outputs is connected */
671 list_for_each_entry(path
, &w
->sinks
, list_source
) {
672 if (path
->connected
&&
673 !path
->connected(path
->source
, path
->sink
))
676 if (path
->sink
&& path
->sink
->power_check
&&
677 path
->sink
->power_check(path
->sink
)) {
683 dapm_clear_walk(w
->dapm
);
688 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
689 struct snd_soc_dapm_widget
*b
,
692 if (a
->codec
!= b
->codec
)
693 return (unsigned long)a
- (unsigned long)b
;
694 if (sort
[a
->id
] != sort
[b
->id
])
695 return sort
[a
->id
] - sort
[b
->id
];
696 if (a
->reg
!= b
->reg
)
697 return a
->reg
- b
->reg
;
702 /* Insert a widget in order into a DAPM power sequence. */
703 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
704 struct list_head
*list
,
707 struct snd_soc_dapm_widget
*w
;
709 list_for_each_entry(w
, list
, power_list
)
710 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
711 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
715 list_add_tail(&new_widget
->power_list
, list
);
718 /* Apply the coalesced changes from a DAPM sequence */
719 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context
*dapm
,
720 struct list_head
*pending
)
722 struct snd_soc_dapm_widget
*w
;
724 unsigned int value
= 0;
725 unsigned int mask
= 0;
726 unsigned int cur_mask
;
728 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
731 list_for_each_entry(w
, pending
, power_list
) {
732 cur_mask
= 1 << w
->shift
;
733 BUG_ON(reg
!= w
->reg
);
744 pop_dbg(dapm
->pop_time
,
745 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
746 w
->name
, reg
, value
, mask
);
748 /* power up pre event */
749 if (w
->power
&& w
->event
&&
750 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
751 pop_dbg(dapm
->pop_time
, "pop test : %s PRE_PMU\n",
753 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
755 pr_err("%s: pre event failed: %d\n",
759 /* power down pre event */
760 if (!w
->power
&& w
->event
&&
761 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
762 pop_dbg(dapm
->pop_time
, "pop test : %s PRE_PMD\n",
764 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
766 pr_err("%s: pre event failed: %d\n",
772 pop_dbg(dapm
->pop_time
,
773 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
774 value
, mask
, reg
, dapm
->pop_time
);
775 pop_wait(dapm
->pop_time
);
776 snd_soc_update_bits(dapm
->codec
, reg
, mask
, value
);
779 list_for_each_entry(w
, pending
, power_list
) {
780 /* power up post event */
781 if (w
->power
&& w
->event
&&
782 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
783 pop_dbg(dapm
->pop_time
, "pop test : %s POST_PMU\n",
786 NULL
, SND_SOC_DAPM_POST_PMU
);
788 pr_err("%s: post event failed: %d\n",
792 /* power down post event */
793 if (!w
->power
&& w
->event
&&
794 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
795 pop_dbg(dapm
->pop_time
, "pop test : %s POST_PMD\n",
797 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
799 pr_err("%s: post event failed: %d\n",
805 /* Apply a DAPM power sequence.
807 * We walk over a pre-sorted list of widgets to apply power to. In
808 * order to minimise the number of writes to the device required
809 * multiple widgets will be updated in a single write where possible.
810 * Currently anything that requires more than a single write is not
813 static void dapm_seq_run(struct snd_soc_dapm_context
*dapm
,
814 struct list_head
*list
, int event
, int sort
[])
816 struct snd_soc_dapm_widget
*w
, *n
;
819 int cur_reg
= SND_SOC_NOPM
;
822 list_for_each_entry_safe(w
, n
, list
, power_list
) {
825 /* Do we need to apply any queued changes? */
826 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
827 if (!list_empty(&pending
))
828 dapm_seq_run_coalesced(dapm
, &pending
);
830 INIT_LIST_HEAD(&pending
);
832 cur_reg
= SND_SOC_NOPM
;
836 case snd_soc_dapm_pre
:
838 list_for_each_entry_safe_continue(w
, n
, list
,
841 if (event
== SND_SOC_DAPM_STREAM_START
)
843 NULL
, SND_SOC_DAPM_PRE_PMU
);
844 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
846 NULL
, SND_SOC_DAPM_PRE_PMD
);
849 case snd_soc_dapm_post
:
851 list_for_each_entry_safe_continue(w
, n
, list
,
854 if (event
== SND_SOC_DAPM_STREAM_START
)
856 NULL
, SND_SOC_DAPM_POST_PMU
);
857 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
859 NULL
, SND_SOC_DAPM_POST_PMD
);
862 case snd_soc_dapm_input
:
863 case snd_soc_dapm_output
:
864 case snd_soc_dapm_hp
:
865 case snd_soc_dapm_mic
:
866 case snd_soc_dapm_line
:
867 case snd_soc_dapm_spk
:
868 /* No register support currently */
869 ret
= dapm_generic_apply_power(w
);
873 /* Queue it up for application */
874 cur_sort
= sort
[w
->id
];
876 list_move(&w
->power_list
, &pending
);
881 pr_err("Failed to apply widget power: %d\n",
885 if (!list_empty(&pending
))
886 dapm_seq_run_coalesced(dapm
, &pending
);
890 * Scan each dapm widget for complete audio path.
891 * A complete path is a route that has valid endpoints i.e.:-
893 * o DAC to output pin.
894 * o Input Pin to ADC.
895 * o Input pin to Output pin (bypass, sidetone)
896 * o DAC to ADC (loopback).
898 static int dapm_power_widgets(struct snd_soc_dapm_context
*dapm
, int event
)
900 struct snd_soc_card
*card
= dapm
->codec
->card
;
901 struct snd_soc_dapm_widget
*w
;
903 LIST_HEAD(down_list
);
908 /* Check which widgets we need to power and store them in
909 * lists indicating if they should be powered up or down.
911 list_for_each_entry(w
, &dapm
->widgets
, list
) {
913 case snd_soc_dapm_pre
:
914 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
916 case snd_soc_dapm_post
:
917 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
925 power
= w
->power_check(w
);
931 if (w
->power
== power
)
935 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
937 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
944 /* If there are no DAPM widgets then try to figure out power from the
947 if (list_empty(&dapm
->widgets
)) {
949 case SND_SOC_DAPM_STREAM_START
:
950 case SND_SOC_DAPM_STREAM_RESUME
:
953 case SND_SOC_DAPM_STREAM_SUSPEND
:
956 case SND_SOC_DAPM_STREAM_NOP
:
957 switch (dapm
->bias_level
) {
958 case SND_SOC_BIAS_STANDBY
:
959 case SND_SOC_BIAS_OFF
:
972 if (sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_OFF
) {
973 ret
= snd_soc_dapm_set_bias_level(card
, dapm
,
974 SND_SOC_BIAS_STANDBY
);
976 pr_err("Failed to turn on bias: %d\n", ret
);
979 /* If we're changing to all on or all off then prepare */
980 if ((sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
981 (!sys_power
&& dapm
->bias_level
== SND_SOC_BIAS_ON
)) {
982 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_PREPARE
);
984 pr_err("Failed to prepare bias: %d\n", ret
);
987 /* Power down widgets first; try to avoid amplifying pops. */
988 dapm_seq_run(dapm
, &down_list
, event
, dapm_down_seq
);
991 dapm_seq_run(dapm
, &up_list
, event
, dapm_up_seq
);
993 /* If we just powered the last thing off drop to standby bias */
994 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
995 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_STANDBY
);
997 pr_err("Failed to apply standby bias: %d\n", ret
);
1000 /* If we're in standby and can support bias off then do that */
1001 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
&&
1002 dapm
->idle_bias_off
) {
1003 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_OFF
);
1005 pr_err("Failed to turn off bias: %d\n", ret
);
1008 /* If we just powered up then move to active bias */
1009 if (dapm
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
1010 ret
= snd_soc_dapm_set_bias_level(card
, dapm
, SND_SOC_BIAS_ON
);
1012 pr_err("Failed to apply active bias: %d\n", ret
);
1015 pop_dbg(dapm
->pop_time
, "DAPM sequencing finished, waiting %dms\n",
1017 pop_wait(dapm
->pop_time
);
1022 #ifdef CONFIG_DEBUG_FS
1023 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1025 file
->private_data
= inode
->i_private
;
1029 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1030 char __user
*user_buf
,
1031 size_t count
, loff_t
*ppos
)
1033 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1037 struct snd_soc_dapm_path
*p
= NULL
;
1039 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1043 in
= is_connected_input_ep(w
);
1044 dapm_clear_walk(w
->dapm
);
1045 out
= is_connected_output_ep(w
);
1046 dapm_clear_walk(w
->dapm
);
1048 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1049 w
->name
, w
->power
? "On" : "Off", in
, out
);
1052 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1053 " - R%d(0x%x) bit %d",
1054 w
->reg
, w
->reg
, w
->shift
);
1056 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1059 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1061 w
->active
? "active" : "inactive");
1063 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1064 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1068 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1070 p
->name
? p
->name
: "static",
1073 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1074 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1078 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1080 p
->name
? p
->name
: "static",
1084 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1090 static const struct file_operations dapm_widget_power_fops
= {
1091 .open
= dapm_widget_power_open_file
,
1092 .read
= dapm_widget_power_read_file
,
1093 .llseek
= default_llseek
,
1096 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
)
1098 struct snd_soc_dapm_widget
*w
;
1101 if (!dapm
->debugfs_dapm
)
1104 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1108 d
= debugfs_create_file(w
->name
, 0444,
1109 dapm
->debugfs_dapm
, w
,
1110 &dapm_widget_power_fops
);
1113 "ASoC: Failed to create %s debugfs file\n",
1118 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context
*dapm
)
1123 /* test and update the power status of a mux widget */
1124 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1125 struct snd_kcontrol
*kcontrol
, int change
,
1126 int mux
, struct soc_enum
*e
)
1128 struct snd_soc_dapm_path
*path
;
1131 if (widget
->id
!= snd_soc_dapm_mux
&&
1132 widget
->id
!= snd_soc_dapm_value_mux
)
1138 /* find dapm widget path assoc with kcontrol */
1139 list_for_each_entry(path
, &widget
->dapm
->paths
, list
) {
1140 if (path
->kcontrol
!= kcontrol
)
1143 if (!path
->name
|| !e
->texts
[mux
])
1147 /* we now need to match the string in the enum to the path */
1148 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1149 path
->connect
= 1; /* new connection */
1151 path
->connect
= 0; /* old connection must be powered down */
1155 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1160 /* test and update the power status of a mixer or switch widget */
1161 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1162 struct snd_kcontrol
*kcontrol
, int connect
)
1164 struct snd_soc_dapm_path
*path
;
1167 if (widget
->id
!= snd_soc_dapm_mixer
&&
1168 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1169 widget
->id
!= snd_soc_dapm_switch
)
1172 /* find dapm widget path assoc with kcontrol */
1173 list_for_each_entry(path
, &widget
->dapm
->paths
, list
) {
1174 if (path
->kcontrol
!= kcontrol
)
1177 /* found, now check type */
1179 path
->connect
= connect
;
1184 dapm_power_widgets(widget
->dapm
, SND_SOC_DAPM_STREAM_NOP
);
1189 /* show dapm widget status in sys fs */
1190 static ssize_t
dapm_widget_show(struct device
*dev
,
1191 struct device_attribute
*attr
, char *buf
)
1193 struct snd_soc_pcm_runtime
*rtd
=
1194 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
1195 struct snd_soc_codec
*codec
=rtd
->codec
;
1196 struct snd_soc_dapm_widget
*w
;
1198 char *state
= "not set";
1200 list_for_each_entry(w
, &codec
->dapm
.widgets
, list
) {
1202 /* only display widgets that burnm power */
1204 case snd_soc_dapm_hp
:
1205 case snd_soc_dapm_mic
:
1206 case snd_soc_dapm_spk
:
1207 case snd_soc_dapm_line
:
1208 case snd_soc_dapm_micbias
:
1209 case snd_soc_dapm_dac
:
1210 case snd_soc_dapm_adc
:
1211 case snd_soc_dapm_pga
:
1212 case snd_soc_dapm_mixer
:
1213 case snd_soc_dapm_mixer_named_ctl
:
1214 case snd_soc_dapm_supply
:
1216 count
+= sprintf(buf
+ count
, "%s: %s\n",
1217 w
->name
, w
->power
? "On":"Off");
1224 switch (codec
->dapm
.bias_level
) {
1225 case SND_SOC_BIAS_ON
:
1228 case SND_SOC_BIAS_PREPARE
:
1231 case SND_SOC_BIAS_STANDBY
:
1234 case SND_SOC_BIAS_OFF
:
1238 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1243 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1245 int snd_soc_dapm_sys_add(struct device
*dev
)
1247 return device_create_file(dev
, &dev_attr_dapm_widget
);
1250 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1252 device_remove_file(dev
, &dev_attr_dapm_widget
);
1255 /* free all dapm widgets and resources */
1256 static void dapm_free_widgets(struct snd_soc_dapm_context
*dapm
)
1258 struct snd_soc_dapm_widget
*w
, *next_w
;
1259 struct snd_soc_dapm_path
*p
, *next_p
;
1261 list_for_each_entry_safe(w
, next_w
, &dapm
->widgets
, list
) {
1266 list_for_each_entry_safe(p
, next_p
, &dapm
->paths
, list
) {
1268 kfree(p
->long_name
);
1273 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context
*dapm
,
1274 const char *pin
, int status
)
1276 struct snd_soc_dapm_widget
*w
;
1278 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1279 if (!strcmp(w
->name
, pin
)) {
1280 pr_debug("dapm: %s: pin %s\n", dapm
->codec
->name
, pin
);
1281 w
->connected
= status
;
1282 /* Allow disabling of forced pins */
1289 pr_err("dapm: %s: configuring unknown pin %s\n",
1290 dapm
->codec
->name
, pin
);
1295 * snd_soc_dapm_sync - scan and power dapm paths
1296 * @dapm: DAPM context
1298 * Walks all dapm audio paths and powers widgets according to their
1299 * stream or path usage.
1301 * Returns 0 for success.
1303 int snd_soc_dapm_sync(struct snd_soc_dapm_context
*dapm
)
1305 return dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1307 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1309 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context
*dapm
,
1310 const struct snd_soc_dapm_route
*route
)
1312 struct snd_soc_dapm_path
*path
;
1313 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1314 const char *sink
= route
->sink
;
1315 const char *control
= route
->control
;
1316 const char *source
= route
->source
;
1319 /* find src and dest widgets */
1320 list_for_each_entry(w
, &dapm
->widgets
, list
) {
1322 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1326 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1331 if (wsource
== NULL
|| wsink
== NULL
)
1334 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1338 path
->source
= wsource
;
1340 path
->connected
= route
->connected
;
1341 INIT_LIST_HEAD(&path
->list
);
1342 INIT_LIST_HEAD(&path
->list_source
);
1343 INIT_LIST_HEAD(&path
->list_sink
);
1345 /* check for external widgets */
1346 if (wsink
->id
== snd_soc_dapm_input
) {
1347 if (wsource
->id
== snd_soc_dapm_micbias
||
1348 wsource
->id
== snd_soc_dapm_mic
||
1349 wsource
->id
== snd_soc_dapm_line
||
1350 wsource
->id
== snd_soc_dapm_output
)
1353 if (wsource
->id
== snd_soc_dapm_output
) {
1354 if (wsink
->id
== snd_soc_dapm_spk
||
1355 wsink
->id
== snd_soc_dapm_hp
||
1356 wsink
->id
== snd_soc_dapm_line
||
1357 wsink
->id
== snd_soc_dapm_input
)
1361 /* connect static paths */
1362 if (control
== NULL
) {
1363 list_add(&path
->list
, &dapm
->paths
);
1364 list_add(&path
->list_sink
, &wsink
->sources
);
1365 list_add(&path
->list_source
, &wsource
->sinks
);
1370 /* connect dynamic paths */
1372 case snd_soc_dapm_adc
:
1373 case snd_soc_dapm_dac
:
1374 case snd_soc_dapm_pga
:
1375 case snd_soc_dapm_input
:
1376 case snd_soc_dapm_output
:
1377 case snd_soc_dapm_micbias
:
1378 case snd_soc_dapm_vmid
:
1379 case snd_soc_dapm_pre
:
1380 case snd_soc_dapm_post
:
1381 case snd_soc_dapm_supply
:
1382 case snd_soc_dapm_aif_in
:
1383 case snd_soc_dapm_aif_out
:
1384 list_add(&path
->list
, &dapm
->paths
);
1385 list_add(&path
->list_sink
, &wsink
->sources
);
1386 list_add(&path
->list_source
, &wsource
->sinks
);
1389 case snd_soc_dapm_mux
:
1390 case snd_soc_dapm_value_mux
:
1391 ret
= dapm_connect_mux(dapm
, wsource
, wsink
, path
, control
,
1392 &wsink
->kcontrols
[0]);
1396 case snd_soc_dapm_switch
:
1397 case snd_soc_dapm_mixer
:
1398 case snd_soc_dapm_mixer_named_ctl
:
1399 ret
= dapm_connect_mixer(dapm
, wsource
, wsink
, path
, control
);
1403 case snd_soc_dapm_hp
:
1404 case snd_soc_dapm_mic
:
1405 case snd_soc_dapm_line
:
1406 case snd_soc_dapm_spk
:
1407 list_add(&path
->list
, &dapm
->paths
);
1408 list_add(&path
->list_sink
, &wsink
->sources
);
1409 list_add(&path
->list_source
, &wsource
->sinks
);
1416 printk(KERN_WARNING
"asoc: no dapm match for %s --> %s --> %s\n", source
,
1423 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1424 * @dapm: DAPM context
1425 * @route: audio routes
1426 * @num: number of routes
1428 * Connects 2 dapm widgets together via a named audio path. The sink is
1429 * the widget receiving the audio signal, whilst the source is the sender
1430 * of the audio signal.
1432 * Returns 0 for success else error. On error all resources can be freed
1433 * with a call to snd_soc_card_free().
1435 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context
*dapm
,
1436 const struct snd_soc_dapm_route
*route
, int num
)
1440 for (i
= 0; i
< num
; i
++) {
1441 ret
= snd_soc_dapm_add_route(dapm
, route
);
1443 printk(KERN_ERR
"Failed to add route %s->%s\n",
1453 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1456 * snd_soc_dapm_new_widgets - add new dapm widgets
1457 * @dapm: DAPM context
1459 * Checks the codec for any new dapm widgets and creates them if found.
1461 * Returns 0 for success.
1463 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context
*dapm
)
1465 struct snd_soc_dapm_widget
*w
;
1467 list_for_each_entry(w
, &dapm
->widgets
, list
)
1473 case snd_soc_dapm_switch
:
1474 case snd_soc_dapm_mixer
:
1475 case snd_soc_dapm_mixer_named_ctl
:
1476 w
->power_check
= dapm_generic_check_power
;
1477 dapm_new_mixer(dapm
, w
);
1479 case snd_soc_dapm_mux
:
1480 case snd_soc_dapm_value_mux
:
1481 w
->power_check
= dapm_generic_check_power
;
1482 dapm_new_mux(dapm
, w
);
1484 case snd_soc_dapm_adc
:
1485 case snd_soc_dapm_aif_out
:
1486 w
->power_check
= dapm_adc_check_power
;
1488 case snd_soc_dapm_dac
:
1489 case snd_soc_dapm_aif_in
:
1490 w
->power_check
= dapm_dac_check_power
;
1492 case snd_soc_dapm_pga
:
1493 w
->power_check
= dapm_generic_check_power
;
1494 dapm_new_pga(dapm
, w
);
1496 case snd_soc_dapm_input
:
1497 case snd_soc_dapm_output
:
1498 case snd_soc_dapm_micbias
:
1499 case snd_soc_dapm_spk
:
1500 case snd_soc_dapm_hp
:
1501 case snd_soc_dapm_mic
:
1502 case snd_soc_dapm_line
:
1503 w
->power_check
= dapm_generic_check_power
;
1505 case snd_soc_dapm_supply
:
1506 w
->power_check
= dapm_supply_check_power
;
1507 case snd_soc_dapm_vmid
:
1508 case snd_soc_dapm_pre
:
1509 case snd_soc_dapm_post
:
1515 dapm_power_widgets(dapm
, SND_SOC_DAPM_STREAM_NOP
);
1518 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1521 * snd_soc_dapm_get_volsw - dapm mixer get callback
1522 * @kcontrol: mixer control
1523 * @ucontrol: control element information
1525 * Callback to get the value of a dapm mixer control.
1527 * Returns 0 for success.
1529 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1530 struct snd_ctl_elem_value
*ucontrol
)
1532 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1533 struct soc_mixer_control
*mc
=
1534 (struct soc_mixer_control
*)kcontrol
->private_value
;
1535 unsigned int reg
= mc
->reg
;
1536 unsigned int shift
= mc
->shift
;
1537 unsigned int rshift
= mc
->rshift
;
1539 unsigned int invert
= mc
->invert
;
1540 unsigned int mask
= (1 << fls(max
)) - 1;
1542 ucontrol
->value
.integer
.value
[0] =
1543 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1544 if (shift
!= rshift
)
1545 ucontrol
->value
.integer
.value
[1] =
1546 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1548 ucontrol
->value
.integer
.value
[0] =
1549 max
- ucontrol
->value
.integer
.value
[0];
1550 if (shift
!= rshift
)
1551 ucontrol
->value
.integer
.value
[1] =
1552 max
- ucontrol
->value
.integer
.value
[1];
1557 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1560 * snd_soc_dapm_put_volsw - dapm mixer set callback
1561 * @kcontrol: mixer control
1562 * @ucontrol: control element information
1564 * Callback to set the value of a dapm mixer control.
1566 * Returns 0 for success.
1568 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1569 struct snd_ctl_elem_value
*ucontrol
)
1571 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1572 struct soc_mixer_control
*mc
=
1573 (struct soc_mixer_control
*)kcontrol
->private_value
;
1574 unsigned int reg
= mc
->reg
;
1575 unsigned int shift
= mc
->shift
;
1576 unsigned int rshift
= mc
->rshift
;
1578 unsigned int mask
= (1 << fls(max
)) - 1;
1579 unsigned int invert
= mc
->invert
;
1580 unsigned int val
, val2
, val_mask
;
1584 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1588 val_mask
= mask
<< shift
;
1590 if (shift
!= rshift
) {
1591 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1594 val_mask
|= mask
<< rshift
;
1595 val
|= val2
<< rshift
;
1598 mutex_lock(&widget
->codec
->mutex
);
1599 widget
->value
= val
;
1601 if (snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
)) {
1603 /* new connection */
1604 connect
= invert
? 0:1;
1606 /* old connection must be powered down */
1607 connect
= invert
? 1:0;
1609 dapm_mixer_update_power(widget
, kcontrol
, connect
);
1612 if (widget
->event
) {
1613 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1614 ret
= widget
->event(widget
, kcontrol
,
1615 SND_SOC_DAPM_PRE_REG
);
1621 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1622 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1623 ret
= widget
->event(widget
, kcontrol
,
1624 SND_SOC_DAPM_POST_REG
);
1626 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1629 mutex_unlock(&widget
->codec
->mutex
);
1632 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1635 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1636 * @kcontrol: mixer control
1637 * @ucontrol: control element information
1639 * Callback to get the value of a dapm enumerated double mixer control.
1641 * Returns 0 for success.
1643 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1644 struct snd_ctl_elem_value
*ucontrol
)
1646 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1647 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1648 unsigned int val
, bitmask
;
1650 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1652 val
= snd_soc_read(widget
->codec
, e
->reg
);
1653 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1654 if (e
->shift_l
!= e
->shift_r
)
1655 ucontrol
->value
.enumerated
.item
[1] =
1656 (val
>> e
->shift_r
) & (bitmask
- 1);
1660 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1663 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1664 * @kcontrol: mixer control
1665 * @ucontrol: control element information
1667 * Callback to set the value of a dapm enumerated double mixer control.
1669 * Returns 0 for success.
1671 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1672 struct snd_ctl_elem_value
*ucontrol
)
1674 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1675 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1676 unsigned int val
, mux
, change
;
1677 unsigned int mask
, bitmask
;
1680 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1682 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1684 mux
= ucontrol
->value
.enumerated
.item
[0];
1685 val
= mux
<< e
->shift_l
;
1686 mask
= (bitmask
- 1) << e
->shift_l
;
1687 if (e
->shift_l
!= e
->shift_r
) {
1688 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1690 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1691 mask
|= (bitmask
- 1) << e
->shift_r
;
1694 mutex_lock(&widget
->codec
->mutex
);
1695 widget
->value
= val
;
1696 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1697 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1699 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1700 ret
= widget
->event(widget
,
1701 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1706 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1708 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1709 ret
= widget
->event(widget
,
1710 kcontrol
, SND_SOC_DAPM_POST_REG
);
1713 mutex_unlock(&widget
->codec
->mutex
);
1716 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1719 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1720 * @kcontrol: mixer control
1721 * @ucontrol: control element information
1723 * Returns 0 for success.
1725 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
1726 struct snd_ctl_elem_value
*ucontrol
)
1728 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1730 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
1734 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
1737 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1738 * @kcontrol: mixer control
1739 * @ucontrol: control element information
1741 * Returns 0 for success.
1743 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
1744 struct snd_ctl_elem_value
*ucontrol
)
1746 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1747 struct soc_enum
*e
=
1748 (struct soc_enum
*)kcontrol
->private_value
;
1752 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
1755 mutex_lock(&widget
->codec
->mutex
);
1757 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
1758 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
1759 dapm_mux_update_power(widget
, kcontrol
, change
, widget
->value
, e
);
1761 mutex_unlock(&widget
->codec
->mutex
);
1764 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
1767 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1769 * @kcontrol: mixer control
1770 * @ucontrol: control element information
1772 * Callback to get the value of a dapm semi enumerated double mixer control.
1774 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1775 * used for handling bitfield coded enumeration for example.
1777 * Returns 0 for success.
1779 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1780 struct snd_ctl_elem_value
*ucontrol
)
1782 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1783 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1784 unsigned int reg_val
, val
, mux
;
1786 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1787 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1788 for (mux
= 0; mux
< e
->max
; mux
++) {
1789 if (val
== e
->values
[mux
])
1792 ucontrol
->value
.enumerated
.item
[0] = mux
;
1793 if (e
->shift_l
!= e
->shift_r
) {
1794 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1795 for (mux
= 0; mux
< e
->max
; mux
++) {
1796 if (val
== e
->values
[mux
])
1799 ucontrol
->value
.enumerated
.item
[1] = mux
;
1804 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1807 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1809 * @kcontrol: mixer control
1810 * @ucontrol: control element information
1812 * Callback to set the value of a dapm semi enumerated double mixer control.
1814 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1815 * used for handling bitfield coded enumeration for example.
1817 * Returns 0 for success.
1819 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
1820 struct snd_ctl_elem_value
*ucontrol
)
1822 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1823 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1824 unsigned int val
, mux
, change
;
1828 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1830 mux
= ucontrol
->value
.enumerated
.item
[0];
1831 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1832 mask
= e
->mask
<< e
->shift_l
;
1833 if (e
->shift_l
!= e
->shift_r
) {
1834 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1836 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1837 mask
|= e
->mask
<< e
->shift_r
;
1840 mutex_lock(&widget
->codec
->mutex
);
1841 widget
->value
= val
;
1842 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1843 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1845 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1846 ret
= widget
->event(widget
,
1847 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1852 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1854 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1855 ret
= widget
->event(widget
,
1856 kcontrol
, SND_SOC_DAPM_POST_REG
);
1859 mutex_unlock(&widget
->codec
->mutex
);
1862 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1865 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1867 * @kcontrol: mixer control
1868 * @uinfo: control element information
1870 * Callback to provide information about a pin switch control.
1872 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1873 struct snd_ctl_elem_info
*uinfo
)
1875 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1877 uinfo
->value
.integer
.min
= 0;
1878 uinfo
->value
.integer
.max
= 1;
1882 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1885 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1887 * @kcontrol: mixer control
1890 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1891 struct snd_ctl_elem_value
*ucontrol
)
1893 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1894 const char *pin
= (const char *)kcontrol
->private_value
;
1896 mutex_lock(&codec
->mutex
);
1898 ucontrol
->value
.integer
.value
[0] =
1899 snd_soc_dapm_get_pin_status(&codec
->dapm
, pin
);
1901 mutex_unlock(&codec
->mutex
);
1905 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
1908 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1910 * @kcontrol: mixer control
1913 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
1914 struct snd_ctl_elem_value
*ucontrol
)
1916 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1917 const char *pin
= (const char *)kcontrol
->private_value
;
1919 mutex_lock(&codec
->mutex
);
1921 if (ucontrol
->value
.integer
.value
[0])
1922 snd_soc_dapm_enable_pin(&codec
->dapm
, pin
);
1924 snd_soc_dapm_disable_pin(&codec
->dapm
, pin
);
1926 snd_soc_dapm_sync(&codec
->dapm
);
1928 mutex_unlock(&codec
->mutex
);
1932 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
1935 * snd_soc_dapm_new_control - create new dapm control
1936 * @dapm: DAPM context
1937 * @widget: widget template
1939 * Creates a new dapm control based upon the template.
1941 * Returns 0 for success else error.
1943 int snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
1944 const struct snd_soc_dapm_widget
*widget
)
1946 struct snd_soc_dapm_widget
*w
;
1948 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
1952 w
->codec
= dapm
->codec
;
1953 INIT_LIST_HEAD(&w
->sources
);
1954 INIT_LIST_HEAD(&w
->sinks
);
1955 INIT_LIST_HEAD(&w
->list
);
1956 list_add(&w
->list
, &dapm
->widgets
);
1958 /* machine layer set ups unconnected pins and insertions */
1962 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
1965 * snd_soc_dapm_new_controls - create new dapm controls
1966 * @dapm: DAPM context
1967 * @widget: widget array
1968 * @num: number of widgets
1970 * Creates new DAPM controls based upon the templates.
1972 * Returns 0 for success else error.
1974 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context
*dapm
,
1975 const struct snd_soc_dapm_widget
*widget
,
1980 for (i
= 0; i
< num
; i
++) {
1981 ret
= snd_soc_dapm_new_control(dapm
, widget
);
1984 "ASoC: Failed to create DAPM control %s: %d\n",
1992 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
1994 static void soc_dapm_stream_event(struct snd_soc_dapm_context
*dapm
,
1995 const char *stream
, int event
)
1997 struct snd_soc_dapm_widget
*w
;
1999 list_for_each_entry(w
, &dapm
->widgets
, list
)
2003 pr_debug("widget %s\n %s stream %s event %d\n",
2004 w
->name
, w
->sname
, stream
, event
);
2005 if (strstr(w
->sname
, stream
)) {
2007 case SND_SOC_DAPM_STREAM_START
:
2010 case SND_SOC_DAPM_STREAM_STOP
:
2013 case SND_SOC_DAPM_STREAM_SUSPEND
:
2014 case SND_SOC_DAPM_STREAM_RESUME
:
2015 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2016 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2022 dapm_power_widgets(dapm
, event
);
2026 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2027 * @rtd: PCM runtime data
2028 * @stream: stream name
2029 * @event: stream event
2031 * Sends a stream event to the dapm core. The core then makes any
2032 * necessary widget power changes.
2034 * Returns 0 for success else error.
2036 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
,
2037 const char *stream
, int event
)
2039 struct snd_soc_codec
*codec
= rtd
->codec
;
2044 mutex_lock(&codec
->mutex
);
2045 soc_dapm_stream_event(&codec
->dapm
, stream
, event
);
2046 mutex_unlock(&codec
->mutex
);
2049 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
2052 * snd_soc_dapm_enable_pin - enable pin.
2053 * @dapm: DAPM context
2056 * Enables input/output pin and its parents or children widgets iff there is
2057 * a valid audio route and active audio stream.
2058 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2059 * do any widget power switching.
2061 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2063 return snd_soc_dapm_set_pin(dapm
, pin
, 1);
2065 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2068 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2069 * @dapm: DAPM context
2072 * Enables input/output pin regardless of any other state. This is
2073 * intended for use with microphone bias supplies used in microphone
2076 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2077 * do any widget power switching.
2079 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context
*dapm
,
2082 struct snd_soc_dapm_widget
*w
;
2084 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2085 if (!strcmp(w
->name
, pin
)) {
2086 pr_debug("dapm: %s: pin %s\n", dapm
->codec
->name
, pin
);
2093 pr_err("dapm: %s: configuring unknown pin %s\n",
2094 dapm
->codec
->name
, pin
);
2097 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
2100 * snd_soc_dapm_disable_pin - disable pin.
2101 * @dapm: DAPM context
2104 * Disables input/output pin and its parents or children widgets.
2105 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2106 * do any widget power switching.
2108 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context
*dapm
,
2111 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2113 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2116 * snd_soc_dapm_nc_pin - permanently disable pin.
2117 * @dapm: DAPM context
2120 * Marks the specified pin as being not connected, disabling it along
2121 * any parent or child widgets. At present this is identical to
2122 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2123 * additional things such as disabling controls which only affect
2124 * paths through the pin.
2126 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2127 * do any widget power switching.
2129 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context
*dapm
, const char *pin
)
2131 return snd_soc_dapm_set_pin(dapm
, pin
, 0);
2133 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2136 * snd_soc_dapm_get_pin_status - get audio pin status
2137 * @dapm: DAPM context
2138 * @pin: audio signal pin endpoint (or start point)
2140 * Get audio pin status - connected or disconnected.
2142 * Returns 1 for connected otherwise 0.
2144 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context
*dapm
,
2147 struct snd_soc_dapm_widget
*w
;
2149 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2150 if (!strcmp(w
->name
, pin
))
2151 return w
->connected
;
2156 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2159 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2160 * @dapm: DAPM context
2161 * @pin: audio signal pin endpoint (or start point)
2163 * Mark the given endpoint or pin as ignoring suspend. When the
2164 * system is disabled a path between two endpoints flagged as ignoring
2165 * suspend will not be disabled. The path must already be enabled via
2166 * normal means at suspend time, it will not be turned on if it was not
2169 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context
*dapm
,
2172 struct snd_soc_dapm_widget
*w
;
2174 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2175 if (!strcmp(w
->name
, pin
)) {
2176 w
->ignore_suspend
= 1;
2181 pr_err("Unknown DAPM pin: %s\n", pin
);
2184 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
2187 * snd_soc_dapm_free - free dapm resources
2190 * Free all dapm widgets and resources.
2192 void snd_soc_dapm_free(struct snd_soc_dapm_context
*dapm
)
2194 snd_soc_dapm_sys_remove(dapm
->dev
);
2195 dapm_free_widgets(dapm
);
2197 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2199 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context
*dapm
)
2201 struct snd_soc_dapm_widget
*w
;
2202 LIST_HEAD(down_list
);
2205 list_for_each_entry(w
, &dapm
->widgets
, list
) {
2207 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2213 /* If there were no widgets to power down we're already in
2217 snd_soc_dapm_set_bias_level(NULL
, dapm
, SND_SOC_BIAS_PREPARE
);
2218 dapm_seq_run(dapm
, &down_list
, 0, dapm_down_seq
);
2219 snd_soc_dapm_set_bias_level(NULL
, dapm
, SND_SOC_BIAS_STANDBY
);
2224 * snd_soc_dapm_shutdown - callback for system shutdown
2226 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
2228 struct snd_soc_codec
*codec
;
2230 list_for_each_entry(codec
, &card
->codec_dev_list
, list
) {
2231 soc_dapm_shutdown_codec(&codec
->dapm
);
2232 snd_soc_dapm_set_bias_level(card
, &codec
->dapm
, SND_SOC_BIAS_OFF
);
2236 /* Module information */
2237 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2238 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2239 MODULE_LICENSE("GPL");