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 <sound/core.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc-dapm.h>
44 #include <sound/initval.h>
48 #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
50 #define dump_dapm(codec, action)
53 /* dapm power sequences - make this per codec in the future */
54 static int dapm_up_seq
[] = {
55 [snd_soc_dapm_pre
] = 0,
56 [snd_soc_dapm_supply
] = 1,
57 [snd_soc_dapm_micbias
] = 2,
58 [snd_soc_dapm_mic
] = 3,
59 [snd_soc_dapm_mux
] = 4,
60 [snd_soc_dapm_value_mux
] = 4,
61 [snd_soc_dapm_dac
] = 5,
62 [snd_soc_dapm_mixer
] = 6,
63 [snd_soc_dapm_mixer_named_ctl
] = 6,
64 [snd_soc_dapm_pga
] = 7,
65 [snd_soc_dapm_adc
] = 8,
66 [snd_soc_dapm_hp
] = 9,
67 [snd_soc_dapm_spk
] = 9,
68 [snd_soc_dapm_post
] = 10,
71 static int dapm_down_seq
[] = {
72 [snd_soc_dapm_pre
] = 0,
73 [snd_soc_dapm_adc
] = 1,
74 [snd_soc_dapm_hp
] = 2,
75 [snd_soc_dapm_spk
] = 2,
76 [snd_soc_dapm_pga
] = 4,
77 [snd_soc_dapm_mixer_named_ctl
] = 5,
78 [snd_soc_dapm_mixer
] = 5,
79 [snd_soc_dapm_dac
] = 6,
80 [snd_soc_dapm_mic
] = 7,
81 [snd_soc_dapm_micbias
] = 8,
82 [snd_soc_dapm_mux
] = 9,
83 [snd_soc_dapm_value_mux
] = 9,
84 [snd_soc_dapm_supply
] = 10,
85 [snd_soc_dapm_post
] = 11,
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
, ...)
108 /* create a new dapm widget */
109 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
110 const struct snd_soc_dapm_widget
*_widget
)
112 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
116 * snd_soc_dapm_set_bias_level - set the bias level for the system
117 * @socdev: audio device
118 * @level: level to configure
120 * Configure the bias (power) levels for the SoC audio device.
122 * Returns 0 for success else error.
124 static int snd_soc_dapm_set_bias_level(struct snd_soc_device
*socdev
,
125 enum snd_soc_bias_level level
)
127 struct snd_soc_card
*card
= socdev
->card
;
128 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
132 case SND_SOC_BIAS_ON
:
133 dev_dbg(socdev
->dev
, "Setting full bias\n");
135 case SND_SOC_BIAS_PREPARE
:
136 dev_dbg(socdev
->dev
, "Setting bias prepare\n");
138 case SND_SOC_BIAS_STANDBY
:
139 dev_dbg(socdev
->dev
, "Setting standby bias\n");
141 case SND_SOC_BIAS_OFF
:
142 dev_dbg(socdev
->dev
, "Setting bias off\n");
145 dev_err(socdev
->dev
, "Setting invalid bias %d\n", level
);
149 if (card
->set_bias_level
)
150 ret
= card
->set_bias_level(card
, level
);
152 if (codec
->set_bias_level
)
153 ret
= codec
->set_bias_level(codec
, level
);
155 codec
->bias_level
= level
;
161 /* set up initial codec paths */
162 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
163 struct snd_soc_dapm_path
*p
, int i
)
166 case snd_soc_dapm_switch
:
167 case snd_soc_dapm_mixer
:
168 case snd_soc_dapm_mixer_named_ctl
: {
170 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
171 w
->kcontrols
[i
].private_value
;
172 unsigned int reg
= mc
->reg
;
173 unsigned int shift
= mc
->shift
;
175 unsigned int mask
= (1 << fls(max
)) - 1;
176 unsigned int invert
= mc
->invert
;
178 val
= snd_soc_read(w
->codec
, reg
);
179 val
= (val
>> shift
) & mask
;
181 if ((invert
&& !val
) || (!invert
&& val
))
187 case snd_soc_dapm_mux
: {
188 struct soc_enum
*e
= (struct soc_enum
*)w
->kcontrols
[i
].private_value
;
189 int val
, item
, bitmask
;
191 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
193 val
= snd_soc_read(w
->codec
, e
->reg
);
194 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
197 for (i
= 0; i
< e
->max
; i
++) {
198 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
203 case snd_soc_dapm_value_mux
: {
204 struct soc_enum
*e
= (struct soc_enum
*)
205 w
->kcontrols
[i
].private_value
;
208 val
= snd_soc_read(w
->codec
, e
->reg
);
209 val
= (val
>> e
->shift_l
) & e
->mask
;
210 for (item
= 0; item
< e
->max
; item
++) {
211 if (val
== e
->values
[item
])
216 for (i
= 0; i
< e
->max
; i
++) {
217 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
222 /* does not effect routing - always connected */
223 case snd_soc_dapm_pga
:
224 case snd_soc_dapm_output
:
225 case snd_soc_dapm_adc
:
226 case snd_soc_dapm_input
:
227 case snd_soc_dapm_dac
:
228 case snd_soc_dapm_micbias
:
229 case snd_soc_dapm_vmid
:
230 case snd_soc_dapm_supply
:
233 /* does effect routing - dynamically connected */
234 case snd_soc_dapm_hp
:
235 case snd_soc_dapm_mic
:
236 case snd_soc_dapm_spk
:
237 case snd_soc_dapm_line
:
238 case snd_soc_dapm_pre
:
239 case snd_soc_dapm_post
:
245 /* connect mux widget to its interconnecting audio paths */
246 static int dapm_connect_mux(struct snd_soc_codec
*codec
,
247 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
248 struct snd_soc_dapm_path
*path
, const char *control_name
,
249 const struct snd_kcontrol_new
*kcontrol
)
251 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
254 for (i
= 0; i
< e
->max
; i
++) {
255 if (!(strcmp(control_name
, e
->texts
[i
]))) {
256 list_add(&path
->list
, &codec
->dapm_paths
);
257 list_add(&path
->list_sink
, &dest
->sources
);
258 list_add(&path
->list_source
, &src
->sinks
);
259 path
->name
= (char*)e
->texts
[i
];
260 dapm_set_path_status(dest
, path
, 0);
268 /* connect mixer widget to its interconnecting audio paths */
269 static int dapm_connect_mixer(struct snd_soc_codec
*codec
,
270 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
271 struct snd_soc_dapm_path
*path
, const char *control_name
)
275 /* search for mixer kcontrol */
276 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
277 if (!strcmp(control_name
, dest
->kcontrols
[i
].name
)) {
278 list_add(&path
->list
, &codec
->dapm_paths
);
279 list_add(&path
->list_sink
, &dest
->sources
);
280 list_add(&path
->list_source
, &src
->sinks
);
281 path
->name
= dest
->kcontrols
[i
].name
;
282 dapm_set_path_status(dest
, path
, i
);
289 /* update dapm codec register bits */
290 static int dapm_update_bits(struct snd_soc_dapm_widget
*widget
)
293 unsigned int old
, new;
294 struct snd_soc_codec
*codec
= widget
->codec
;
296 /* check for valid widgets */
297 if (widget
->reg
< 0 || widget
->id
== snd_soc_dapm_input
||
298 widget
->id
== snd_soc_dapm_output
||
299 widget
->id
== snd_soc_dapm_hp
||
300 widget
->id
== snd_soc_dapm_mic
||
301 widget
->id
== snd_soc_dapm_line
||
302 widget
->id
== snd_soc_dapm_spk
)
305 power
= widget
->power
;
307 power
= (power
? 0:1);
309 old
= snd_soc_read(codec
, widget
->reg
);
310 new = (old
& ~(0x1 << widget
->shift
)) | (power
<< widget
->shift
);
314 pop_dbg(codec
->pop_time
, "pop test %s : %s in %d ms\n",
315 widget
->name
, widget
->power
? "on" : "off",
317 snd_soc_write(codec
, widget
->reg
, new);
318 pop_wait(codec
->pop_time
);
320 pr_debug("reg %x old %x new %x change %d\n", widget
->reg
,
325 /* ramps the volume up or down to minimise pops before or after a
326 * DAPM power event */
327 static int dapm_set_pga(struct snd_soc_dapm_widget
*widget
, int power
)
329 const struct snd_kcontrol_new
*k
= widget
->kcontrols
;
331 if (widget
->muted
&& !power
)
333 if (!widget
->muted
&& power
)
336 if (widget
->num_kcontrols
&& k
) {
337 struct soc_mixer_control
*mc
=
338 (struct soc_mixer_control
*)k
->private_value
;
339 unsigned int reg
= mc
->reg
;
340 unsigned int shift
= mc
->shift
;
342 unsigned int mask
= (1 << fls(max
)) - 1;
343 unsigned int invert
= mc
->invert
;
347 /* power up has happended, increase volume to last level */
349 for (i
= max
; i
> widget
->saved_value
; i
--)
350 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
352 for (i
= 0; i
< widget
->saved_value
; i
++)
353 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
357 /* power down is about to occur, decrease volume to mute */
358 int val
= snd_soc_read(widget
->codec
, reg
);
359 int i
= widget
->saved_value
= (val
>> shift
) & mask
;
361 for (; i
< mask
; i
++)
362 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
365 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
373 /* create new dapm mixer control */
374 static int dapm_new_mixer(struct snd_soc_codec
*codec
,
375 struct snd_soc_dapm_widget
*w
)
379 struct snd_soc_dapm_path
*path
;
382 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
385 list_for_each_entry(path
, &w
->sources
, list_sink
) {
387 /* mixer/mux paths name must match control name */
388 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
391 /* add dapm control with long name.
392 * for dapm_mixer this is the concatenation of the
393 * mixer and kcontrol name.
394 * for dapm_mixer_named_ctl this is simply the
397 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
398 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
399 name_len
+= 1 + strlen(w
->name
);
401 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
403 if (path
->long_name
== NULL
)
408 snprintf(path
->long_name
, name_len
, "%s %s",
409 w
->name
, w
->kcontrols
[i
].name
);
411 case snd_soc_dapm_mixer_named_ctl
:
412 snprintf(path
->long_name
, name_len
, "%s",
413 w
->kcontrols
[i
].name
);
417 path
->long_name
[name_len
- 1] = '\0';
419 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
421 ret
= snd_ctl_add(codec
->card
, path
->kcontrol
);
423 printk(KERN_ERR
"asoc: failed to add dapm kcontrol %s: %d\n",
426 kfree(path
->long_name
);
427 path
->long_name
= NULL
;
435 /* create new dapm mux control */
436 static int dapm_new_mux(struct snd_soc_codec
*codec
,
437 struct snd_soc_dapm_widget
*w
)
439 struct snd_soc_dapm_path
*path
= NULL
;
440 struct snd_kcontrol
*kcontrol
;
443 if (!w
->num_kcontrols
) {
444 printk(KERN_ERR
"asoc: mux %s has no controls\n", w
->name
);
448 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
449 ret
= snd_ctl_add(codec
->card
, kcontrol
);
453 list_for_each_entry(path
, &w
->sources
, list_sink
)
454 path
->kcontrol
= kcontrol
;
459 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
463 /* create new dapm volume control */
464 static int dapm_new_pga(struct snd_soc_codec
*codec
,
465 struct snd_soc_dapm_widget
*w
)
467 struct snd_kcontrol
*kcontrol
;
470 if (!w
->num_kcontrols
)
473 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
474 ret
= snd_ctl_add(codec
->card
, kcontrol
);
476 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
483 /* reset 'walked' bit for each dapm path */
484 static inline void dapm_clear_walk(struct snd_soc_codec
*codec
)
486 struct snd_soc_dapm_path
*p
;
488 list_for_each_entry(p
, &codec
->dapm_paths
, list
)
493 * Recursively check for a completed path to an active or physically connected
494 * output widget. Returns number of complete paths.
496 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
498 struct snd_soc_dapm_path
*path
;
501 if (widget
->id
== snd_soc_dapm_supply
)
504 if (widget
->id
== snd_soc_dapm_adc
&& widget
->active
)
507 if (widget
->connected
) {
508 /* connected pin ? */
509 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
512 /* connected jack or spk ? */
513 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
514 widget
->id
== snd_soc_dapm_line
)
518 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
522 if (path
->sink
&& path
->connect
) {
524 con
+= is_connected_output_ep(path
->sink
);
532 * Recursively check for a completed path to an active or physically connected
533 * input widget. Returns number of complete paths.
535 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
537 struct snd_soc_dapm_path
*path
;
540 if (widget
->id
== snd_soc_dapm_supply
)
543 /* active stream ? */
544 if (widget
->id
== snd_soc_dapm_dac
&& widget
->active
)
547 if (widget
->connected
) {
548 /* connected pin ? */
549 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
552 /* connected VMID/Bias for lower pops */
553 if (widget
->id
== snd_soc_dapm_vmid
)
556 /* connected jack ? */
557 if (widget
->id
== snd_soc_dapm_mic
|| widget
->id
== snd_soc_dapm_line
)
561 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
565 if (path
->source
&& path
->connect
) {
567 con
+= is_connected_input_ep(path
->source
);
575 * Handler for generic register modifier widget.
577 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
578 struct snd_kcontrol
*kcontrol
, int event
)
582 if (SND_SOC_DAPM_EVENT_ON(event
))
587 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
588 w
->mask
<< w
->shift
, val
<< w
->shift
);
592 EXPORT_SYMBOL_GPL(dapm_reg_event
);
594 /* Standard power change method, used to apply power changes to most
597 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
601 /* call any power change event handlers */
603 pr_debug("power %s event for %s flags %x\n",
604 w
->power
? "on" : "off",
605 w
->name
, w
->event_flags
);
607 /* power up pre event */
608 if (w
->power
&& w
->event
&&
609 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
610 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
615 /* power down pre event */
616 if (!w
->power
&& w
->event
&&
617 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
618 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
623 /* Lower PGA volume to reduce pops */
624 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
625 dapm_set_pga(w
, w
->power
);
629 /* Raise PGA volume to reduce pops */
630 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
631 dapm_set_pga(w
, w
->power
);
633 /* power up post event */
634 if (w
->power
&& w
->event
&&
635 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
637 NULL
, SND_SOC_DAPM_POST_PMU
);
642 /* power down post event */
643 if (!w
->power
&& w
->event
&&
644 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
645 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
653 /* Generic check to see if a widget should be powered.
655 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
659 in
= is_connected_input_ep(w
);
660 dapm_clear_walk(w
->codec
);
661 out
= is_connected_output_ep(w
);
662 dapm_clear_walk(w
->codec
);
663 return out
!= 0 && in
!= 0;
666 /* Check to see if an ADC has power */
667 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
672 in
= is_connected_input_ep(w
);
673 dapm_clear_walk(w
->codec
);
676 return dapm_generic_check_power(w
);
680 /* Check to see if a DAC has power */
681 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
686 out
= is_connected_output_ep(w
);
687 dapm_clear_walk(w
->codec
);
690 return dapm_generic_check_power(w
);
694 /* Check to see if a power supply is needed */
695 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
697 struct snd_soc_dapm_path
*path
;
700 /* Check if one of our outputs is connected */
701 list_for_each_entry(path
, &w
->sinks
, list_source
) {
702 if (path
->sink
&& path
->sink
->power_check
&&
703 path
->sink
->power_check(path
->sink
)) {
709 dapm_clear_walk(w
->codec
);
714 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
715 struct snd_soc_dapm_widget
*b
,
718 if (sort
[a
->id
] != sort
[b
->id
])
719 return sort
[a
->id
] - sort
[b
->id
];
720 if (a
->reg
!= b
->reg
)
721 return a
->reg
- b
->reg
;
726 /* Insert a widget in order into a DAPM power sequence. */
727 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
728 struct list_head
*list
,
731 struct snd_soc_dapm_widget
*w
;
733 list_for_each_entry(w
, list
, power_list
)
734 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
735 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
739 list_add_tail(&new_widget
->power_list
, list
);
742 /* Apply the coalesced changes from a DAPM sequence */
743 static void dapm_seq_run_coalesced(struct snd_soc_codec
*codec
,
744 struct list_head
*pending
)
746 struct snd_soc_dapm_widget
*w
;
748 unsigned int value
= 0;
749 unsigned int mask
= 0;
750 unsigned int cur_mask
;
752 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
755 list_for_each_entry(w
, pending
, power_list
) {
756 cur_mask
= 1 << w
->shift
;
757 BUG_ON(reg
!= w
->reg
);
768 pop_dbg(codec
->pop_time
,
769 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
770 w
->name
, reg
, value
, mask
);
772 /* power up pre event */
773 if (w
->power
&& w
->event
&&
774 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
775 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMU\n",
777 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
779 pr_err("%s: pre event failed: %d\n",
783 /* power down pre event */
784 if (!w
->power
&& w
->event
&&
785 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
786 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMD\n",
788 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
790 pr_err("%s: pre event failed: %d\n",
794 /* Lower PGA volume to reduce pops */
795 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
796 dapm_set_pga(w
, w
->power
);
800 pop_dbg(codec
->pop_time
,
801 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
802 value
, mask
, reg
, codec
->pop_time
);
803 pop_wait(codec
->pop_time
);
804 snd_soc_update_bits(codec
, reg
, mask
, value
);
807 list_for_each_entry(w
, pending
, power_list
) {
808 /* Raise PGA volume to reduce pops */
809 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
810 dapm_set_pga(w
, w
->power
);
812 /* power up post event */
813 if (w
->power
&& w
->event
&&
814 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
815 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMU\n",
818 NULL
, SND_SOC_DAPM_POST_PMU
);
820 pr_err("%s: post event failed: %d\n",
824 /* power down post event */
825 if (!w
->power
&& w
->event
&&
826 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
827 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMD\n",
829 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
831 pr_err("%s: post event failed: %d\n",
837 /* Apply a DAPM power sequence.
839 * We walk over a pre-sorted list of widgets to apply power to. In
840 * order to minimise the number of writes to the device required
841 * multiple widgets will be updated in a single write where possible.
842 * Currently anything that requires more than a single write is not
845 static void dapm_seq_run(struct snd_soc_codec
*codec
, struct list_head
*list
,
846 int event
, int sort
[])
848 struct snd_soc_dapm_widget
*w
, *n
;
851 int cur_reg
= SND_SOC_NOPM
;
854 list_for_each_entry_safe(w
, n
, list
, power_list
) {
857 /* Do we need to apply any queued changes? */
858 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
859 if (!list_empty(&pending
))
860 dapm_seq_run_coalesced(codec
, &pending
);
862 INIT_LIST_HEAD(&pending
);
864 cur_reg
= SND_SOC_NOPM
;
868 case snd_soc_dapm_pre
:
870 list_for_each_entry_safe_continue(w
, n
, list
,
873 if (event
== SND_SOC_DAPM_STREAM_START
)
875 NULL
, SND_SOC_DAPM_PRE_PMU
);
876 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
878 NULL
, SND_SOC_DAPM_PRE_PMD
);
881 case snd_soc_dapm_post
:
883 list_for_each_entry_safe_continue(w
, n
, list
,
886 if (event
== SND_SOC_DAPM_STREAM_START
)
888 NULL
, SND_SOC_DAPM_POST_PMU
);
889 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
891 NULL
, SND_SOC_DAPM_POST_PMD
);
894 case snd_soc_dapm_input
:
895 case snd_soc_dapm_output
:
896 case snd_soc_dapm_hp
:
897 case snd_soc_dapm_mic
:
898 case snd_soc_dapm_line
:
899 case snd_soc_dapm_spk
:
900 /* No register support currently */
901 ret
= dapm_generic_apply_power(w
);
905 /* Queue it up for application */
906 cur_sort
= sort
[w
->id
];
908 list_move(&w
->power_list
, &pending
);
913 pr_err("Failed to apply widget power: %d\n",
917 if (!list_empty(&pending
))
918 dapm_seq_run_coalesced(codec
, &pending
);
922 * Scan each dapm widget for complete audio path.
923 * A complete path is a route that has valid endpoints i.e.:-
925 * o DAC to output pin.
926 * o Input Pin to ADC.
927 * o Input pin to Output pin (bypass, sidetone)
928 * o DAC to ADC (loopback).
930 static int dapm_power_widgets(struct snd_soc_codec
*codec
, int event
)
932 struct snd_soc_device
*socdev
= codec
->socdev
;
933 struct snd_soc_dapm_widget
*w
;
935 LIST_HEAD(down_list
);
940 /* Check which widgets we need to power and store them in
941 * lists indicating if they should be powered up or down.
943 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
945 case snd_soc_dapm_pre
:
946 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
948 case snd_soc_dapm_post
:
949 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
956 power
= w
->power_check(w
);
960 if (w
->power
== power
)
964 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
966 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
973 /* If there are no DAPM widgets then try to figure out power from the
976 if (list_empty(&codec
->dapm_widgets
)) {
978 case SND_SOC_DAPM_STREAM_START
:
979 case SND_SOC_DAPM_STREAM_RESUME
:
982 case SND_SOC_DAPM_STREAM_NOP
:
983 sys_power
= codec
->bias_level
!= SND_SOC_BIAS_STANDBY
;
989 /* If we're changing to all on or all off then prepare */
990 if ((sys_power
&& codec
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
991 (!sys_power
&& codec
->bias_level
== SND_SOC_BIAS_ON
)) {
992 ret
= snd_soc_dapm_set_bias_level(socdev
,
993 SND_SOC_BIAS_PREPARE
);
995 pr_err("Failed to prepare bias: %d\n", ret
);
998 /* Power down widgets first; try to avoid amplifying pops. */
999 dapm_seq_run(codec
, &down_list
, event
, dapm_down_seq
);
1002 dapm_seq_run(codec
, &up_list
, event
, dapm_up_seq
);
1004 /* If we just powered the last thing off drop to standby bias */
1005 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
1006 ret
= snd_soc_dapm_set_bias_level(socdev
,
1007 SND_SOC_BIAS_STANDBY
);
1009 pr_err("Failed to apply standby bias: %d\n", ret
);
1012 /* If we just powered up then move to active bias */
1013 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
1014 ret
= snd_soc_dapm_set_bias_level(socdev
,
1017 pr_err("Failed to apply active bias: %d\n", ret
);
1020 pop_dbg(codec
->pop_time
, "DAPM sequencing finished, waiting %dms\n",
1027 static void dbg_dump_dapm(struct snd_soc_codec
* codec
, const char *action
)
1029 struct snd_soc_dapm_widget
*w
;
1030 struct snd_soc_dapm_path
*p
= NULL
;
1033 printk("DAPM %s %s\n", codec
->name
, action
);
1035 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1037 /* only display widgets that effect routing */
1039 case snd_soc_dapm_pre
:
1040 case snd_soc_dapm_post
:
1041 case snd_soc_dapm_vmid
:
1043 case snd_soc_dapm_mux
:
1044 case snd_soc_dapm_value_mux
:
1045 case snd_soc_dapm_output
:
1046 case snd_soc_dapm_input
:
1047 case snd_soc_dapm_switch
:
1048 case snd_soc_dapm_hp
:
1049 case snd_soc_dapm_mic
:
1050 case snd_soc_dapm_spk
:
1051 case snd_soc_dapm_line
:
1052 case snd_soc_dapm_micbias
:
1053 case snd_soc_dapm_dac
:
1054 case snd_soc_dapm_adc
:
1055 case snd_soc_dapm_pga
:
1056 case snd_soc_dapm_mixer
:
1057 case snd_soc_dapm_mixer_named_ctl
:
1058 case snd_soc_dapm_supply
:
1060 in
= is_connected_input_ep(w
);
1061 dapm_clear_walk(w
->codec
);
1062 out
= is_connected_output_ep(w
);
1063 dapm_clear_walk(w
->codec
);
1064 printk("%s: %s in %d out %d\n", w
->name
,
1065 w
->power
? "On":"Off",in
, out
);
1067 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1069 printk(" in %s %s\n", p
->name
? p
->name
: "static",
1072 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1074 printk(" out %s %s\n", p
->name
? p
->name
: "static",
1084 #ifdef CONFIG_DEBUG_FS
1085 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1087 file
->private_data
= inode
->i_private
;
1091 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1092 char __user
*user_buf
,
1093 size_t count
, loff_t
*ppos
)
1095 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1099 struct snd_soc_dapm_path
*p
= NULL
;
1101 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1105 in
= is_connected_input_ep(w
);
1106 dapm_clear_walk(w
->codec
);
1107 out
= is_connected_output_ep(w
);
1108 dapm_clear_walk(w
->codec
);
1110 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d\n",
1111 w
->name
, w
->power
? "On" : "Off", in
, out
);
1113 if (w
->active
&& w
->sname
)
1114 ret
+= snprintf(buf
, PAGE_SIZE
- ret
, " stream %s active\n",
1117 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1119 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1121 p
->name
? p
->name
: "static",
1124 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1126 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1128 p
->name
? p
->name
: "static",
1132 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1138 static const struct file_operations dapm_widget_power_fops
= {
1139 .open
= dapm_widget_power_open_file
,
1140 .read
= dapm_widget_power_read_file
,
1143 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1145 struct snd_soc_dapm_widget
*w
;
1148 if (!codec
->debugfs_dapm
)
1151 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1155 d
= debugfs_create_file(w
->name
, 0444,
1156 codec
->debugfs_dapm
, w
,
1157 &dapm_widget_power_fops
);
1160 "ASoC: Failed to create %s debugfs file\n",
1165 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1170 /* test and update the power status of a mux widget */
1171 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1172 struct snd_kcontrol
*kcontrol
, int mask
,
1173 int mux
, int val
, struct soc_enum
*e
)
1175 struct snd_soc_dapm_path
*path
;
1178 if (widget
->id
!= snd_soc_dapm_mux
&&
1179 widget
->id
!= snd_soc_dapm_value_mux
)
1182 if (!snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
))
1185 /* find dapm widget path assoc with kcontrol */
1186 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1187 if (path
->kcontrol
!= kcontrol
)
1190 if (!path
->name
|| !e
->texts
[mux
])
1194 /* we now need to match the string in the enum to the path */
1195 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1196 path
->connect
= 1; /* new connection */
1198 path
->connect
= 0; /* old connection must be powered down */
1202 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1203 dump_dapm(widget
->codec
, "mux power update");
1209 /* test and update the power status of a mixer or switch widget */
1210 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1211 struct snd_kcontrol
*kcontrol
, int reg
,
1212 int val_mask
, int val
, int invert
)
1214 struct snd_soc_dapm_path
*path
;
1217 if (widget
->id
!= snd_soc_dapm_mixer
&&
1218 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1219 widget
->id
!= snd_soc_dapm_switch
)
1222 if (!snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
))
1225 /* find dapm widget path assoc with kcontrol */
1226 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1227 if (path
->kcontrol
!= kcontrol
)
1230 /* found, now check type */
1233 /* new connection */
1234 path
->connect
= invert
? 0:1;
1236 /* old connection must be powered down */
1237 path
->connect
= invert
? 1:0;
1242 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1243 dump_dapm(widget
->codec
, "mixer power update");
1249 /* show dapm widget status in sys fs */
1250 static ssize_t
dapm_widget_show(struct device
*dev
,
1251 struct device_attribute
*attr
, char *buf
)
1253 struct snd_soc_device
*devdata
= dev_get_drvdata(dev
);
1254 struct snd_soc_codec
*codec
= devdata
->card
->codec
;
1255 struct snd_soc_dapm_widget
*w
;
1257 char *state
= "not set";
1259 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1261 /* only display widgets that burnm power */
1263 case snd_soc_dapm_hp
:
1264 case snd_soc_dapm_mic
:
1265 case snd_soc_dapm_spk
:
1266 case snd_soc_dapm_line
:
1267 case snd_soc_dapm_micbias
:
1268 case snd_soc_dapm_dac
:
1269 case snd_soc_dapm_adc
:
1270 case snd_soc_dapm_pga
:
1271 case snd_soc_dapm_mixer
:
1272 case snd_soc_dapm_mixer_named_ctl
:
1273 case snd_soc_dapm_supply
:
1275 count
+= sprintf(buf
+ count
, "%s: %s\n",
1276 w
->name
, w
->power
? "On":"Off");
1283 switch (codec
->bias_level
) {
1284 case SND_SOC_BIAS_ON
:
1287 case SND_SOC_BIAS_PREPARE
:
1290 case SND_SOC_BIAS_STANDBY
:
1293 case SND_SOC_BIAS_OFF
:
1297 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1302 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1304 int snd_soc_dapm_sys_add(struct device
*dev
)
1306 return device_create_file(dev
, &dev_attr_dapm_widget
);
1309 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1311 device_remove_file(dev
, &dev_attr_dapm_widget
);
1314 /* free all dapm widgets and resources */
1315 static void dapm_free_widgets(struct snd_soc_codec
*codec
)
1317 struct snd_soc_dapm_widget
*w
, *next_w
;
1318 struct snd_soc_dapm_path
*p
, *next_p
;
1320 list_for_each_entry_safe(w
, next_w
, &codec
->dapm_widgets
, list
) {
1325 list_for_each_entry_safe(p
, next_p
, &codec
->dapm_paths
, list
) {
1327 kfree(p
->long_name
);
1332 static int snd_soc_dapm_set_pin(struct snd_soc_codec
*codec
,
1333 const char *pin
, int status
)
1335 struct snd_soc_dapm_widget
*w
;
1337 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1338 if (!strcmp(w
->name
, pin
)) {
1339 pr_debug("dapm: %s: pin %s\n", codec
->name
, pin
);
1340 w
->connected
= status
;
1345 pr_err("dapm: %s: configuring unknown pin %s\n", codec
->name
, pin
);
1350 * snd_soc_dapm_sync - scan and power dapm paths
1351 * @codec: audio codec
1353 * Walks all dapm audio paths and powers widgets according to their
1354 * stream or path usage.
1356 * Returns 0 for success.
1358 int snd_soc_dapm_sync(struct snd_soc_codec
*codec
)
1360 int ret
= dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1361 dump_dapm(codec
, "sync");
1364 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1366 static int snd_soc_dapm_add_route(struct snd_soc_codec
*codec
,
1367 const char *sink
, const char *control
, const char *source
)
1369 struct snd_soc_dapm_path
*path
;
1370 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1373 /* find src and dest widgets */
1374 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1376 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1380 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1385 if (wsource
== NULL
|| wsink
== NULL
)
1388 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1392 path
->source
= wsource
;
1394 INIT_LIST_HEAD(&path
->list
);
1395 INIT_LIST_HEAD(&path
->list_source
);
1396 INIT_LIST_HEAD(&path
->list_sink
);
1398 /* check for external widgets */
1399 if (wsink
->id
== snd_soc_dapm_input
) {
1400 if (wsource
->id
== snd_soc_dapm_micbias
||
1401 wsource
->id
== snd_soc_dapm_mic
||
1402 wsource
->id
== snd_soc_dapm_line
||
1403 wsource
->id
== snd_soc_dapm_output
)
1406 if (wsource
->id
== snd_soc_dapm_output
) {
1407 if (wsink
->id
== snd_soc_dapm_spk
||
1408 wsink
->id
== snd_soc_dapm_hp
||
1409 wsink
->id
== snd_soc_dapm_line
||
1410 wsink
->id
== snd_soc_dapm_input
)
1414 /* connect static paths */
1415 if (control
== NULL
) {
1416 list_add(&path
->list
, &codec
->dapm_paths
);
1417 list_add(&path
->list_sink
, &wsink
->sources
);
1418 list_add(&path
->list_source
, &wsource
->sinks
);
1423 /* connect dynamic paths */
1425 case snd_soc_dapm_adc
:
1426 case snd_soc_dapm_dac
:
1427 case snd_soc_dapm_pga
:
1428 case snd_soc_dapm_input
:
1429 case snd_soc_dapm_output
:
1430 case snd_soc_dapm_micbias
:
1431 case snd_soc_dapm_vmid
:
1432 case snd_soc_dapm_pre
:
1433 case snd_soc_dapm_post
:
1434 case snd_soc_dapm_supply
:
1435 list_add(&path
->list
, &codec
->dapm_paths
);
1436 list_add(&path
->list_sink
, &wsink
->sources
);
1437 list_add(&path
->list_source
, &wsource
->sinks
);
1440 case snd_soc_dapm_mux
:
1441 case snd_soc_dapm_value_mux
:
1442 ret
= dapm_connect_mux(codec
, wsource
, wsink
, path
, control
,
1443 &wsink
->kcontrols
[0]);
1447 case snd_soc_dapm_switch
:
1448 case snd_soc_dapm_mixer
:
1449 case snd_soc_dapm_mixer_named_ctl
:
1450 ret
= dapm_connect_mixer(codec
, wsource
, wsink
, path
, control
);
1454 case snd_soc_dapm_hp
:
1455 case snd_soc_dapm_mic
:
1456 case snd_soc_dapm_line
:
1457 case snd_soc_dapm_spk
:
1458 list_add(&path
->list
, &codec
->dapm_paths
);
1459 list_add(&path
->list_sink
, &wsink
->sources
);
1460 list_add(&path
->list_source
, &wsource
->sinks
);
1467 printk(KERN_WARNING
"asoc: no dapm match for %s --> %s --> %s\n", source
,
1474 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1476 * @route: audio routes
1477 * @num: number of routes
1479 * Connects 2 dapm widgets together via a named audio path. The sink is
1480 * the widget receiving the audio signal, whilst the source is the sender
1481 * of the audio signal.
1483 * Returns 0 for success else error. On error all resources can be freed
1484 * with a call to snd_soc_card_free().
1486 int snd_soc_dapm_add_routes(struct snd_soc_codec
*codec
,
1487 const struct snd_soc_dapm_route
*route
, int num
)
1491 for (i
= 0; i
< num
; i
++) {
1492 ret
= snd_soc_dapm_add_route(codec
, route
->sink
,
1493 route
->control
, route
->source
);
1495 printk(KERN_ERR
"Failed to add route %s->%s\n",
1505 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1508 * snd_soc_dapm_new_widgets - add new dapm widgets
1509 * @codec: audio codec
1511 * Checks the codec for any new dapm widgets and creates them if found.
1513 * Returns 0 for success.
1515 int snd_soc_dapm_new_widgets(struct snd_soc_codec
*codec
)
1517 struct snd_soc_dapm_widget
*w
;
1519 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
1525 case snd_soc_dapm_switch
:
1526 case snd_soc_dapm_mixer
:
1527 case snd_soc_dapm_mixer_named_ctl
:
1528 w
->power_check
= dapm_generic_check_power
;
1529 dapm_new_mixer(codec
, w
);
1531 case snd_soc_dapm_mux
:
1532 case snd_soc_dapm_value_mux
:
1533 w
->power_check
= dapm_generic_check_power
;
1534 dapm_new_mux(codec
, w
);
1536 case snd_soc_dapm_adc
:
1537 w
->power_check
= dapm_adc_check_power
;
1539 case snd_soc_dapm_dac
:
1540 w
->power_check
= dapm_dac_check_power
;
1542 case snd_soc_dapm_pga
:
1543 w
->power_check
= dapm_generic_check_power
;
1544 dapm_new_pga(codec
, w
);
1546 case snd_soc_dapm_input
:
1547 case snd_soc_dapm_output
:
1548 case snd_soc_dapm_micbias
:
1549 case snd_soc_dapm_spk
:
1550 case snd_soc_dapm_hp
:
1551 case snd_soc_dapm_mic
:
1552 case snd_soc_dapm_line
:
1553 w
->power_check
= dapm_generic_check_power
;
1555 case snd_soc_dapm_supply
:
1556 w
->power_check
= dapm_supply_check_power
;
1557 case snd_soc_dapm_vmid
:
1558 case snd_soc_dapm_pre
:
1559 case snd_soc_dapm_post
:
1565 dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1568 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1571 * snd_soc_dapm_get_volsw - dapm mixer get callback
1572 * @kcontrol: mixer control
1573 * @ucontrol: control element information
1575 * Callback to get the value of a dapm mixer control.
1577 * Returns 0 for success.
1579 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1580 struct snd_ctl_elem_value
*ucontrol
)
1582 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1583 struct soc_mixer_control
*mc
=
1584 (struct soc_mixer_control
*)kcontrol
->private_value
;
1585 unsigned int reg
= mc
->reg
;
1586 unsigned int shift
= mc
->shift
;
1587 unsigned int rshift
= mc
->rshift
;
1589 unsigned int invert
= mc
->invert
;
1590 unsigned int mask
= (1 << fls(max
)) - 1;
1592 /* return the saved value if we are powered down */
1593 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1594 ucontrol
->value
.integer
.value
[0] = widget
->saved_value
;
1598 ucontrol
->value
.integer
.value
[0] =
1599 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1600 if (shift
!= rshift
)
1601 ucontrol
->value
.integer
.value
[1] =
1602 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1604 ucontrol
->value
.integer
.value
[0] =
1605 max
- ucontrol
->value
.integer
.value
[0];
1606 if (shift
!= rshift
)
1607 ucontrol
->value
.integer
.value
[1] =
1608 max
- ucontrol
->value
.integer
.value
[1];
1613 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1616 * snd_soc_dapm_put_volsw - dapm mixer set callback
1617 * @kcontrol: mixer control
1618 * @ucontrol: control element information
1620 * Callback to set the value of a dapm mixer control.
1622 * Returns 0 for success.
1624 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1625 struct snd_ctl_elem_value
*ucontrol
)
1627 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1628 struct soc_mixer_control
*mc
=
1629 (struct soc_mixer_control
*)kcontrol
->private_value
;
1630 unsigned int reg
= mc
->reg
;
1631 unsigned int shift
= mc
->shift
;
1632 unsigned int rshift
= mc
->rshift
;
1634 unsigned int mask
= (1 << fls(max
)) - 1;
1635 unsigned int invert
= mc
->invert
;
1636 unsigned int val
, val2
, val_mask
;
1639 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1643 val_mask
= mask
<< shift
;
1645 if (shift
!= rshift
) {
1646 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1649 val_mask
|= mask
<< rshift
;
1650 val
|= val2
<< rshift
;
1653 mutex_lock(&widget
->codec
->mutex
);
1654 widget
->value
= val
;
1656 /* save volume value if the widget is powered down */
1657 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1658 widget
->saved_value
= val
;
1659 mutex_unlock(&widget
->codec
->mutex
);
1663 dapm_mixer_update_power(widget
, kcontrol
, reg
, val_mask
, val
, invert
);
1664 if (widget
->event
) {
1665 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1666 ret
= widget
->event(widget
, kcontrol
,
1667 SND_SOC_DAPM_PRE_REG
);
1673 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1674 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1675 ret
= widget
->event(widget
, kcontrol
,
1676 SND_SOC_DAPM_POST_REG
);
1678 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1681 mutex_unlock(&widget
->codec
->mutex
);
1684 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1687 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1688 * @kcontrol: mixer control
1689 * @ucontrol: control element information
1691 * Callback to get the value of a dapm enumerated double mixer control.
1693 * Returns 0 for success.
1695 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1696 struct snd_ctl_elem_value
*ucontrol
)
1698 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1699 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1700 unsigned int val
, bitmask
;
1702 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1704 val
= snd_soc_read(widget
->codec
, e
->reg
);
1705 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1706 if (e
->shift_l
!= e
->shift_r
)
1707 ucontrol
->value
.enumerated
.item
[1] =
1708 (val
>> e
->shift_r
) & (bitmask
- 1);
1712 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1715 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1716 * @kcontrol: mixer control
1717 * @ucontrol: control element information
1719 * Callback to set the value of a dapm enumerated double mixer control.
1721 * Returns 0 for success.
1723 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1724 struct snd_ctl_elem_value
*ucontrol
)
1726 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1727 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1728 unsigned int val
, mux
;
1729 unsigned int mask
, bitmask
;
1732 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1734 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1736 mux
= ucontrol
->value
.enumerated
.item
[0];
1737 val
= mux
<< e
->shift_l
;
1738 mask
= (bitmask
- 1) << e
->shift_l
;
1739 if (e
->shift_l
!= e
->shift_r
) {
1740 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1742 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1743 mask
|= (bitmask
- 1) << e
->shift_r
;
1746 mutex_lock(&widget
->codec
->mutex
);
1747 widget
->value
= val
;
1748 dapm_mux_update_power(widget
, kcontrol
, mask
, mux
, val
, e
);
1749 if (widget
->event
) {
1750 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1751 ret
= widget
->event(widget
,
1752 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1756 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1757 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1758 ret
= widget
->event(widget
,
1759 kcontrol
, SND_SOC_DAPM_POST_REG
);
1761 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1764 mutex_unlock(&widget
->codec
->mutex
);
1767 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1770 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1772 * @kcontrol: mixer control
1773 * @ucontrol: control element information
1775 * Callback to get the value of a dapm semi enumerated double mixer control.
1777 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1778 * used for handling bitfield coded enumeration for example.
1780 * Returns 0 for success.
1782 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1783 struct snd_ctl_elem_value
*ucontrol
)
1785 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1786 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1787 unsigned int reg_val
, val
, mux
;
1789 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1790 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1791 for (mux
= 0; mux
< e
->max
; mux
++) {
1792 if (val
== e
->values
[mux
])
1795 ucontrol
->value
.enumerated
.item
[0] = mux
;
1796 if (e
->shift_l
!= e
->shift_r
) {
1797 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1798 for (mux
= 0; mux
< e
->max
; mux
++) {
1799 if (val
== e
->values
[mux
])
1802 ucontrol
->value
.enumerated
.item
[1] = mux
;
1807 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1810 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1812 * @kcontrol: mixer control
1813 * @ucontrol: control element information
1815 * Callback to set the value of a dapm semi enumerated double mixer control.
1817 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1818 * used for handling bitfield coded enumeration for example.
1820 * Returns 0 for success.
1822 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
1823 struct snd_ctl_elem_value
*ucontrol
)
1825 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1826 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1827 unsigned int val
, mux
;
1831 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1833 mux
= ucontrol
->value
.enumerated
.item
[0];
1834 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1835 mask
= e
->mask
<< e
->shift_l
;
1836 if (e
->shift_l
!= e
->shift_r
) {
1837 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1839 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1840 mask
|= e
->mask
<< e
->shift_r
;
1843 mutex_lock(&widget
->codec
->mutex
);
1844 widget
->value
= val
;
1845 dapm_mux_update_power(widget
, kcontrol
, mask
, mux
, val
, e
);
1846 if (widget
->event
) {
1847 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1848 ret
= widget
->event(widget
,
1849 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1853 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
);
1858 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1861 mutex_unlock(&widget
->codec
->mutex
);
1864 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1867 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1869 * @kcontrol: mixer control
1870 * @uinfo: control element information
1872 * Callback to provide information about a pin switch control.
1874 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1875 struct snd_ctl_elem_info
*uinfo
)
1877 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1879 uinfo
->value
.integer
.min
= 0;
1880 uinfo
->value
.integer
.max
= 1;
1884 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1887 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1889 * @kcontrol: mixer control
1892 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1893 struct snd_ctl_elem_value
*ucontrol
)
1895 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1896 const char *pin
= (const char *)kcontrol
->private_value
;
1898 mutex_lock(&codec
->mutex
);
1900 ucontrol
->value
.integer
.value
[0] =
1901 snd_soc_dapm_get_pin_status(codec
, pin
);
1903 mutex_unlock(&codec
->mutex
);
1907 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
1910 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1912 * @kcontrol: mixer control
1915 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
1916 struct snd_ctl_elem_value
*ucontrol
)
1918 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1919 const char *pin
= (const char *)kcontrol
->private_value
;
1921 mutex_lock(&codec
->mutex
);
1923 if (ucontrol
->value
.integer
.value
[0])
1924 snd_soc_dapm_enable_pin(codec
, pin
);
1926 snd_soc_dapm_disable_pin(codec
, pin
);
1928 snd_soc_dapm_sync(codec
);
1930 mutex_unlock(&codec
->mutex
);
1934 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
1937 * snd_soc_dapm_new_control - create new dapm control
1938 * @codec: audio codec
1939 * @widget: widget template
1941 * Creates a new dapm control based upon the template.
1943 * Returns 0 for success else error.
1945 int snd_soc_dapm_new_control(struct snd_soc_codec
*codec
,
1946 const struct snd_soc_dapm_widget
*widget
)
1948 struct snd_soc_dapm_widget
*w
;
1950 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
1954 INIT_LIST_HEAD(&w
->sources
);
1955 INIT_LIST_HEAD(&w
->sinks
);
1956 INIT_LIST_HEAD(&w
->list
);
1957 list_add(&w
->list
, &codec
->dapm_widgets
);
1959 /* machine layer set ups unconnected pins and insertions */
1963 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
1966 * snd_soc_dapm_new_controls - create new dapm controls
1967 * @codec: audio codec
1968 * @widget: widget array
1969 * @num: number of widgets
1971 * Creates new DAPM controls based upon the templates.
1973 * Returns 0 for success else error.
1975 int snd_soc_dapm_new_controls(struct snd_soc_codec
*codec
,
1976 const struct snd_soc_dapm_widget
*widget
,
1981 for (i
= 0; i
< num
; i
++) {
1982 ret
= snd_soc_dapm_new_control(codec
, widget
);
1985 "ASoC: Failed to create DAPM control %s: %d\n",
1993 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
1997 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1998 * @codec: audio codec
1999 * @stream: stream name
2000 * @event: stream event
2002 * Sends a stream event to the dapm core. The core then makes any
2003 * necessary widget power changes.
2005 * Returns 0 for success else error.
2007 int snd_soc_dapm_stream_event(struct snd_soc_codec
*codec
,
2008 char *stream
, int event
)
2010 struct snd_soc_dapm_widget
*w
;
2015 mutex_lock(&codec
->mutex
);
2016 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
2020 pr_debug("widget %s\n %s stream %s event %d\n",
2021 w
->name
, w
->sname
, stream
, event
);
2022 if (strstr(w
->sname
, stream
)) {
2024 case SND_SOC_DAPM_STREAM_START
:
2027 case SND_SOC_DAPM_STREAM_STOP
:
2030 case SND_SOC_DAPM_STREAM_SUSPEND
:
2035 case SND_SOC_DAPM_STREAM_RESUME
:
2041 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2043 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2048 mutex_unlock(&codec
->mutex
);
2050 dapm_power_widgets(codec
, event
);
2051 dump_dapm(codec
, __func__
);
2054 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
2057 * snd_soc_dapm_enable_pin - enable pin.
2061 * Enables input/output pin and its parents or children widgets iff there is
2062 * a valid audio route and active audio stream.
2063 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2064 * do any widget power switching.
2066 int snd_soc_dapm_enable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2068 return snd_soc_dapm_set_pin(codec
, pin
, 1);
2070 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2073 * snd_soc_dapm_disable_pin - disable pin.
2077 * Disables input/output pin and its parents or children widgets.
2078 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2079 * do any widget power switching.
2081 int snd_soc_dapm_disable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2083 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2085 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2088 * snd_soc_dapm_nc_pin - permanently disable pin.
2092 * Marks the specified pin as being not connected, disabling it along
2093 * any parent or child widgets. At present this is identical to
2094 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2095 * additional things such as disabling controls which only affect
2096 * paths through the pin.
2098 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2099 * do any widget power switching.
2101 int snd_soc_dapm_nc_pin(struct snd_soc_codec
*codec
, const char *pin
)
2103 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2105 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2108 * snd_soc_dapm_get_pin_status - get audio pin status
2109 * @codec: audio codec
2110 * @pin: audio signal pin endpoint (or start point)
2112 * Get audio pin status - connected or disconnected.
2114 * Returns 1 for connected otherwise 0.
2116 int snd_soc_dapm_get_pin_status(struct snd_soc_codec
*codec
, const char *pin
)
2118 struct snd_soc_dapm_widget
*w
;
2120 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2121 if (!strcmp(w
->name
, pin
))
2122 return w
->connected
;
2127 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2130 * snd_soc_dapm_free - free dapm resources
2131 * @socdev: SoC device
2133 * Free all dapm widgets and resources.
2135 void snd_soc_dapm_free(struct snd_soc_device
*socdev
)
2137 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2139 snd_soc_dapm_sys_remove(socdev
->dev
);
2140 dapm_free_widgets(codec
);
2142 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2145 * snd_soc_dapm_shutdown - callback for system shutdown
2147 void snd_soc_dapm_shutdown(struct snd_soc_device
*socdev
)
2149 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2150 struct snd_soc_dapm_widget
*w
;
2151 LIST_HEAD(down_list
);
2154 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2156 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2162 /* If there were no widgets to power down we're already in
2166 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_PREPARE
);
2167 dapm_seq_run(codec
, &down_list
, 0, dapm_down_seq
);
2168 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_STANDBY
);
2171 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_OFF
);
2174 /* Module information */
2175 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2176 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2177 MODULE_LICENSE("GPL");