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
] = 10,
68 [snd_soc_dapm_post
] = 11,
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
] = 3,
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
);
151 if (ret
== 0 && codec
->set_bias_level
)
152 ret
= codec
->set_bias_level(codec
, level
);
157 /* set up initial codec paths */
158 static void dapm_set_path_status(struct snd_soc_dapm_widget
*w
,
159 struct snd_soc_dapm_path
*p
, int i
)
162 case snd_soc_dapm_switch
:
163 case snd_soc_dapm_mixer
:
164 case snd_soc_dapm_mixer_named_ctl
: {
166 struct soc_mixer_control
*mc
= (struct soc_mixer_control
*)
167 w
->kcontrols
[i
].private_value
;
168 unsigned int reg
= mc
->reg
;
169 unsigned int shift
= mc
->shift
;
171 unsigned int mask
= (1 << fls(max
)) - 1;
172 unsigned int invert
= mc
->invert
;
174 val
= snd_soc_read(w
->codec
, reg
);
175 val
= (val
>> shift
) & mask
;
177 if ((invert
&& !val
) || (!invert
&& val
))
183 case snd_soc_dapm_mux
: {
184 struct soc_enum
*e
= (struct soc_enum
*)w
->kcontrols
[i
].private_value
;
185 int val
, item
, bitmask
;
187 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
189 val
= snd_soc_read(w
->codec
, e
->reg
);
190 item
= (val
>> e
->shift_l
) & (bitmask
- 1);
193 for (i
= 0; i
< e
->max
; i
++) {
194 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
199 case snd_soc_dapm_value_mux
: {
200 struct soc_enum
*e
= (struct soc_enum
*)
201 w
->kcontrols
[i
].private_value
;
204 val
= snd_soc_read(w
->codec
, e
->reg
);
205 val
= (val
>> e
->shift_l
) & e
->mask
;
206 for (item
= 0; item
< e
->max
; item
++) {
207 if (val
== e
->values
[item
])
212 for (i
= 0; i
< e
->max
; i
++) {
213 if (!(strcmp(p
->name
, e
->texts
[i
])) && item
== i
)
218 /* does not effect routing - always connected */
219 case snd_soc_dapm_pga
:
220 case snd_soc_dapm_output
:
221 case snd_soc_dapm_adc
:
222 case snd_soc_dapm_input
:
223 case snd_soc_dapm_dac
:
224 case snd_soc_dapm_micbias
:
225 case snd_soc_dapm_vmid
:
226 case snd_soc_dapm_supply
:
229 /* does effect routing - dynamically connected */
230 case snd_soc_dapm_hp
:
231 case snd_soc_dapm_mic
:
232 case snd_soc_dapm_spk
:
233 case snd_soc_dapm_line
:
234 case snd_soc_dapm_pre
:
235 case snd_soc_dapm_post
:
241 /* connect mux widget to its interconnecting audio paths */
242 static int dapm_connect_mux(struct snd_soc_codec
*codec
,
243 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
244 struct snd_soc_dapm_path
*path
, const char *control_name
,
245 const struct snd_kcontrol_new
*kcontrol
)
247 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
250 for (i
= 0; i
< e
->max
; i
++) {
251 if (!(strcmp(control_name
, e
->texts
[i
]))) {
252 list_add(&path
->list
, &codec
->dapm_paths
);
253 list_add(&path
->list_sink
, &dest
->sources
);
254 list_add(&path
->list_source
, &src
->sinks
);
255 path
->name
= (char*)e
->texts
[i
];
256 dapm_set_path_status(dest
, path
, 0);
264 /* connect mixer widget to its interconnecting audio paths */
265 static int dapm_connect_mixer(struct snd_soc_codec
*codec
,
266 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
267 struct snd_soc_dapm_path
*path
, const char *control_name
)
271 /* search for mixer kcontrol */
272 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
273 if (!strcmp(control_name
, dest
->kcontrols
[i
].name
)) {
274 list_add(&path
->list
, &codec
->dapm_paths
);
275 list_add(&path
->list_sink
, &dest
->sources
);
276 list_add(&path
->list_source
, &src
->sinks
);
277 path
->name
= dest
->kcontrols
[i
].name
;
278 dapm_set_path_status(dest
, path
, i
);
285 /* update dapm codec register bits */
286 static int dapm_update_bits(struct snd_soc_dapm_widget
*widget
)
289 unsigned int old
, new;
290 struct snd_soc_codec
*codec
= widget
->codec
;
292 /* check for valid widgets */
293 if (widget
->reg
< 0 || widget
->id
== snd_soc_dapm_input
||
294 widget
->id
== snd_soc_dapm_output
||
295 widget
->id
== snd_soc_dapm_hp
||
296 widget
->id
== snd_soc_dapm_mic
||
297 widget
->id
== snd_soc_dapm_line
||
298 widget
->id
== snd_soc_dapm_spk
)
301 power
= widget
->power
;
303 power
= (power
? 0:1);
305 old
= snd_soc_read(codec
, widget
->reg
);
306 new = (old
& ~(0x1 << widget
->shift
)) | (power
<< widget
->shift
);
310 pop_dbg(codec
->pop_time
, "pop test %s : %s in %d ms\n",
311 widget
->name
, widget
->power
? "on" : "off",
313 snd_soc_write(codec
, widget
->reg
, new);
314 pop_wait(codec
->pop_time
);
316 pr_debug("reg %x old %x new %x change %d\n", widget
->reg
,
321 /* ramps the volume up or down to minimise pops before or after a
322 * DAPM power event */
323 static int dapm_set_pga(struct snd_soc_dapm_widget
*widget
, int power
)
325 const struct snd_kcontrol_new
*k
= widget
->kcontrols
;
327 if (widget
->muted
&& !power
)
329 if (!widget
->muted
&& power
)
332 if (widget
->num_kcontrols
&& k
) {
333 struct soc_mixer_control
*mc
=
334 (struct soc_mixer_control
*)k
->private_value
;
335 unsigned int reg
= mc
->reg
;
336 unsigned int shift
= mc
->shift
;
338 unsigned int mask
= (1 << fls(max
)) - 1;
339 unsigned int invert
= mc
->invert
;
343 /* power up has happended, increase volume to last level */
345 for (i
= max
; i
> widget
->saved_value
; i
--)
346 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
348 for (i
= 0; i
< widget
->saved_value
; i
++)
349 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
353 /* power down is about to occur, decrease volume to mute */
354 int val
= snd_soc_read(widget
->codec
, reg
);
355 int i
= widget
->saved_value
= (val
>> shift
) & mask
;
357 for (; i
< mask
; i
++)
358 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
361 snd_soc_update_bits(widget
->codec
, reg
, mask
, i
);
369 /* create new dapm mixer control */
370 static int dapm_new_mixer(struct snd_soc_codec
*codec
,
371 struct snd_soc_dapm_widget
*w
)
375 struct snd_soc_dapm_path
*path
;
378 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
381 list_for_each_entry(path
, &w
->sources
, list_sink
) {
383 /* mixer/mux paths name must match control name */
384 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
387 /* add dapm control with long name.
388 * for dapm_mixer this is the concatenation of the
389 * mixer and kcontrol name.
390 * for dapm_mixer_named_ctl this is simply the
393 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
394 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
395 name_len
+= 1 + strlen(w
->name
);
397 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
399 if (path
->long_name
== NULL
)
404 snprintf(path
->long_name
, name_len
, "%s %s",
405 w
->name
, w
->kcontrols
[i
].name
);
407 case snd_soc_dapm_mixer_named_ctl
:
408 snprintf(path
->long_name
, name_len
, "%s",
409 w
->kcontrols
[i
].name
);
413 path
->long_name
[name_len
- 1] = '\0';
415 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
417 ret
= snd_ctl_add(codec
->card
, path
->kcontrol
);
419 printk(KERN_ERR
"asoc: failed to add dapm kcontrol %s: %d\n",
422 kfree(path
->long_name
);
423 path
->long_name
= NULL
;
431 /* create new dapm mux control */
432 static int dapm_new_mux(struct snd_soc_codec
*codec
,
433 struct snd_soc_dapm_widget
*w
)
435 struct snd_soc_dapm_path
*path
= NULL
;
436 struct snd_kcontrol
*kcontrol
;
439 if (!w
->num_kcontrols
) {
440 printk(KERN_ERR
"asoc: mux %s has no controls\n", w
->name
);
444 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
445 ret
= snd_ctl_add(codec
->card
, kcontrol
);
449 list_for_each_entry(path
, &w
->sources
, list_sink
)
450 path
->kcontrol
= kcontrol
;
455 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
459 /* create new dapm volume control */
460 static int dapm_new_pga(struct snd_soc_codec
*codec
,
461 struct snd_soc_dapm_widget
*w
)
463 struct snd_kcontrol
*kcontrol
;
466 if (!w
->num_kcontrols
)
469 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
470 ret
= snd_ctl_add(codec
->card
, kcontrol
);
472 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
479 /* reset 'walked' bit for each dapm path */
480 static inline void dapm_clear_walk(struct snd_soc_codec
*codec
)
482 struct snd_soc_dapm_path
*p
;
484 list_for_each_entry(p
, &codec
->dapm_paths
, list
)
489 * Recursively check for a completed path to an active or physically connected
490 * output widget. Returns number of complete paths.
492 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
494 struct snd_soc_dapm_path
*path
;
497 if (widget
->id
== snd_soc_dapm_supply
)
500 if (widget
->id
== snd_soc_dapm_adc
&& widget
->active
)
503 if (widget
->connected
) {
504 /* connected pin ? */
505 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
508 /* connected jack or spk ? */
509 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
510 widget
->id
== snd_soc_dapm_line
)
514 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
518 if (path
->sink
&& path
->connect
) {
520 con
+= is_connected_output_ep(path
->sink
);
528 * Recursively check for a completed path to an active or physically connected
529 * input widget. Returns number of complete paths.
531 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
533 struct snd_soc_dapm_path
*path
;
536 if (widget
->id
== snd_soc_dapm_supply
)
539 /* active stream ? */
540 if (widget
->id
== snd_soc_dapm_dac
&& widget
->active
)
543 if (widget
->connected
) {
544 /* connected pin ? */
545 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
548 /* connected VMID/Bias for lower pops */
549 if (widget
->id
== snd_soc_dapm_vmid
)
552 /* connected jack ? */
553 if (widget
->id
== snd_soc_dapm_mic
|| widget
->id
== snd_soc_dapm_line
)
557 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
561 if (path
->source
&& path
->connect
) {
563 con
+= is_connected_input_ep(path
->source
);
571 * Handler for generic register modifier widget.
573 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
574 struct snd_kcontrol
*kcontrol
, int event
)
578 if (SND_SOC_DAPM_EVENT_ON(event
))
583 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
584 w
->mask
<< w
->shift
, val
<< w
->shift
);
588 EXPORT_SYMBOL_GPL(dapm_reg_event
);
590 /* Standard power change method, used to apply power changes to most
593 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
597 /* call any power change event handlers */
599 pr_debug("power %s event for %s flags %x\n",
600 w
->power
? "on" : "off",
601 w
->name
, w
->event_flags
);
603 /* power up pre event */
604 if (w
->power
&& w
->event
&&
605 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
606 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
611 /* power down pre event */
612 if (!w
->power
&& w
->event
&&
613 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
614 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
619 /* Lower PGA volume to reduce pops */
620 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
621 dapm_set_pga(w
, w
->power
);
625 /* Raise PGA volume to reduce pops */
626 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
627 dapm_set_pga(w
, w
->power
);
629 /* power up post event */
630 if (w
->power
&& w
->event
&&
631 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
633 NULL
, SND_SOC_DAPM_POST_PMU
);
638 /* power down post event */
639 if (!w
->power
&& w
->event
&&
640 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
641 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
649 /* Generic check to see if a widget should be powered.
651 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
655 in
= is_connected_input_ep(w
);
656 dapm_clear_walk(w
->codec
);
657 out
= is_connected_output_ep(w
);
658 dapm_clear_walk(w
->codec
);
659 return out
!= 0 && in
!= 0;
662 /* Check to see if an ADC has power */
663 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
668 in
= is_connected_input_ep(w
);
669 dapm_clear_walk(w
->codec
);
672 return dapm_generic_check_power(w
);
676 /* Check to see if a DAC has power */
677 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
682 out
= is_connected_output_ep(w
);
683 dapm_clear_walk(w
->codec
);
686 return dapm_generic_check_power(w
);
690 /* Check to see if a power supply is needed */
691 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
693 struct snd_soc_dapm_path
*path
;
696 /* Check if one of our outputs is connected */
697 list_for_each_entry(path
, &w
->sinks
, list_source
) {
698 if (path
->sink
&& path
->sink
->power_check
&&
699 path
->sink
->power_check(path
->sink
)) {
705 dapm_clear_walk(w
->codec
);
710 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
711 struct snd_soc_dapm_widget
*b
,
714 if (sort
[a
->id
] != sort
[b
->id
])
715 return sort
[a
->id
] - sort
[b
->id
];
716 if (a
->reg
!= b
->reg
)
717 return a
->reg
- b
->reg
;
722 /* Insert a widget in order into a DAPM power sequence. */
723 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
724 struct list_head
*list
,
727 struct snd_soc_dapm_widget
*w
;
729 list_for_each_entry(w
, list
, power_list
)
730 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
731 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
735 list_add_tail(&new_widget
->power_list
, list
);
738 /* Apply the coalesced changes from a DAPM sequence */
739 static void dapm_seq_run_coalesced(struct snd_soc_codec
*codec
,
740 struct list_head
*pending
)
742 struct snd_soc_dapm_widget
*w
;
744 unsigned int value
= 0;
745 unsigned int mask
= 0;
746 unsigned int cur_mask
;
748 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
751 list_for_each_entry(w
, pending
, power_list
) {
752 cur_mask
= 1 << w
->shift
;
753 BUG_ON(reg
!= w
->reg
);
764 pop_dbg(codec
->pop_time
,
765 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
766 w
->name
, reg
, value
, mask
);
768 /* power up pre event */
769 if (w
->power
&& w
->event
&&
770 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
771 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMU\n",
773 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
775 pr_err("%s: pre event failed: %d\n",
779 /* power down pre event */
780 if (!w
->power
&& w
->event
&&
781 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
782 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMD\n",
784 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
786 pr_err("%s: pre event failed: %d\n",
790 /* Lower PGA volume to reduce pops */
791 if (w
->id
== snd_soc_dapm_pga
&& !w
->power
)
792 dapm_set_pga(w
, w
->power
);
796 pop_dbg(codec
->pop_time
,
797 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
798 value
, mask
, reg
, codec
->pop_time
);
799 pop_wait(codec
->pop_time
);
800 snd_soc_update_bits(codec
, reg
, mask
, value
);
803 list_for_each_entry(w
, pending
, power_list
) {
804 /* Raise PGA volume to reduce pops */
805 if (w
->id
== snd_soc_dapm_pga
&& w
->power
)
806 dapm_set_pga(w
, w
->power
);
808 /* power up post event */
809 if (w
->power
&& w
->event
&&
810 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
811 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMU\n",
814 NULL
, SND_SOC_DAPM_POST_PMU
);
816 pr_err("%s: post event failed: %d\n",
820 /* power down post event */
821 if (!w
->power
&& w
->event
&&
822 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
823 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMD\n",
825 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
827 pr_err("%s: post event failed: %d\n",
833 /* Apply a DAPM power sequence.
835 * We walk over a pre-sorted list of widgets to apply power to. In
836 * order to minimise the number of writes to the device required
837 * multiple widgets will be updated in a single write where possible.
838 * Currently anything that requires more than a single write is not
841 static void dapm_seq_run(struct snd_soc_codec
*codec
, struct list_head
*list
,
842 int event
, int sort
[])
844 struct snd_soc_dapm_widget
*w
, *n
;
847 int cur_reg
= SND_SOC_NOPM
;
850 list_for_each_entry_safe(w
, n
, list
, power_list
) {
853 /* Do we need to apply any queued changes? */
854 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
855 if (!list_empty(&pending
))
856 dapm_seq_run_coalesced(codec
, &pending
);
858 INIT_LIST_HEAD(&pending
);
860 cur_reg
= SND_SOC_NOPM
;
864 case snd_soc_dapm_pre
:
866 list_for_each_entry_safe_continue(w
, n
, list
,
869 if (event
== SND_SOC_DAPM_STREAM_START
)
871 NULL
, SND_SOC_DAPM_PRE_PMU
);
872 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
874 NULL
, SND_SOC_DAPM_PRE_PMD
);
877 case snd_soc_dapm_post
:
879 list_for_each_entry_safe_continue(w
, n
, list
,
882 if (event
== SND_SOC_DAPM_STREAM_START
)
884 NULL
, SND_SOC_DAPM_POST_PMU
);
885 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
887 NULL
, SND_SOC_DAPM_POST_PMD
);
890 case snd_soc_dapm_input
:
891 case snd_soc_dapm_output
:
892 case snd_soc_dapm_hp
:
893 case snd_soc_dapm_mic
:
894 case snd_soc_dapm_line
:
895 case snd_soc_dapm_spk
:
896 /* No register support currently */
897 ret
= dapm_generic_apply_power(w
);
901 /* Queue it up for application */
902 cur_sort
= sort
[w
->id
];
904 list_move(&w
->power_list
, &pending
);
909 pr_err("Failed to apply widget power: %d\n",
913 if (!list_empty(&pending
))
914 dapm_seq_run_coalesced(codec
, &pending
);
918 * Scan each dapm widget for complete audio path.
919 * A complete path is a route that has valid endpoints i.e.:-
921 * o DAC to output pin.
922 * o Input Pin to ADC.
923 * o Input pin to Output pin (bypass, sidetone)
924 * o DAC to ADC (loopback).
926 static int dapm_power_widgets(struct snd_soc_codec
*codec
, int event
)
928 struct snd_soc_device
*socdev
= codec
->socdev
;
929 struct snd_soc_dapm_widget
*w
;
931 LIST_HEAD(down_list
);
936 /* Check which widgets we need to power and store them in
937 * lists indicating if they should be powered up or down.
939 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
941 case snd_soc_dapm_pre
:
942 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
944 case snd_soc_dapm_post
:
945 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
952 power
= w
->power_check(w
);
956 if (w
->power
== power
)
960 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
962 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
969 /* If we're changing to all on or all off then prepare */
970 if ((sys_power
&& codec
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
971 (!sys_power
&& codec
->bias_level
== SND_SOC_BIAS_ON
)) {
972 ret
= snd_soc_dapm_set_bias_level(socdev
,
973 SND_SOC_BIAS_PREPARE
);
975 pr_err("Failed to prepare bias: %d\n", ret
);
978 /* Power down widgets first; try to avoid amplifying pops. */
979 dapm_seq_run(codec
, &down_list
, event
, dapm_down_seq
);
982 dapm_seq_run(codec
, &up_list
, event
, dapm_up_seq
);
984 /* If we just powered the last thing off drop to standby bias */
985 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
986 ret
= snd_soc_dapm_set_bias_level(socdev
,
987 SND_SOC_BIAS_STANDBY
);
989 pr_err("Failed to apply standby bias: %d\n", ret
);
992 /* If we just powered up then move to active bias */
993 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
994 ret
= snd_soc_dapm_set_bias_level(socdev
,
997 pr_err("Failed to apply active bias: %d\n", ret
);
1004 static void dbg_dump_dapm(struct snd_soc_codec
* codec
, const char *action
)
1006 struct snd_soc_dapm_widget
*w
;
1007 struct snd_soc_dapm_path
*p
= NULL
;
1010 printk("DAPM %s %s\n", codec
->name
, action
);
1012 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1014 /* only display widgets that effect routing */
1016 case snd_soc_dapm_pre
:
1017 case snd_soc_dapm_post
:
1018 case snd_soc_dapm_vmid
:
1020 case snd_soc_dapm_mux
:
1021 case snd_soc_dapm_value_mux
:
1022 case snd_soc_dapm_output
:
1023 case snd_soc_dapm_input
:
1024 case snd_soc_dapm_switch
:
1025 case snd_soc_dapm_hp
:
1026 case snd_soc_dapm_mic
:
1027 case snd_soc_dapm_spk
:
1028 case snd_soc_dapm_line
:
1029 case snd_soc_dapm_micbias
:
1030 case snd_soc_dapm_dac
:
1031 case snd_soc_dapm_adc
:
1032 case snd_soc_dapm_pga
:
1033 case snd_soc_dapm_mixer
:
1034 case snd_soc_dapm_mixer_named_ctl
:
1035 case snd_soc_dapm_supply
:
1037 in
= is_connected_input_ep(w
);
1038 dapm_clear_walk(w
->codec
);
1039 out
= is_connected_output_ep(w
);
1040 dapm_clear_walk(w
->codec
);
1041 printk("%s: %s in %d out %d\n", w
->name
,
1042 w
->power
? "On":"Off",in
, out
);
1044 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1046 printk(" in %s %s\n", p
->name
? p
->name
: "static",
1049 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1051 printk(" out %s %s\n", p
->name
? p
->name
: "static",
1061 /* test and update the power status of a mux widget */
1062 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1063 struct snd_kcontrol
*kcontrol
, int mask
,
1064 int mux
, int val
, struct soc_enum
*e
)
1066 struct snd_soc_dapm_path
*path
;
1069 if (widget
->id
!= snd_soc_dapm_mux
&&
1070 widget
->id
!= snd_soc_dapm_value_mux
)
1073 if (!snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
))
1076 /* find dapm widget path assoc with kcontrol */
1077 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1078 if (path
->kcontrol
!= kcontrol
)
1081 if (!path
->name
|| !e
->texts
[mux
])
1085 /* we now need to match the string in the enum to the path */
1086 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1087 path
->connect
= 1; /* new connection */
1089 path
->connect
= 0; /* old connection must be powered down */
1093 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1094 dump_dapm(widget
->codec
, "mux power update");
1100 /* test and update the power status of a mixer or switch widget */
1101 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1102 struct snd_kcontrol
*kcontrol
, int reg
,
1103 int val_mask
, int val
, int invert
)
1105 struct snd_soc_dapm_path
*path
;
1108 if (widget
->id
!= snd_soc_dapm_mixer
&&
1109 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1110 widget
->id
!= snd_soc_dapm_switch
)
1113 if (!snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
))
1116 /* find dapm widget path assoc with kcontrol */
1117 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1118 if (path
->kcontrol
!= kcontrol
)
1121 /* found, now check type */
1124 /* new connection */
1125 path
->connect
= invert
? 0:1;
1127 /* old connection must be powered down */
1128 path
->connect
= invert
? 1:0;
1133 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1134 dump_dapm(widget
->codec
, "mixer power update");
1140 /* show dapm widget status in sys fs */
1141 static ssize_t
dapm_widget_show(struct device
*dev
,
1142 struct device_attribute
*attr
, char *buf
)
1144 struct snd_soc_device
*devdata
= dev_get_drvdata(dev
);
1145 struct snd_soc_codec
*codec
= devdata
->card
->codec
;
1146 struct snd_soc_dapm_widget
*w
;
1148 char *state
= "not set";
1150 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1152 /* only display widgets that burnm power */
1154 case snd_soc_dapm_hp
:
1155 case snd_soc_dapm_mic
:
1156 case snd_soc_dapm_spk
:
1157 case snd_soc_dapm_line
:
1158 case snd_soc_dapm_micbias
:
1159 case snd_soc_dapm_dac
:
1160 case snd_soc_dapm_adc
:
1161 case snd_soc_dapm_pga
:
1162 case snd_soc_dapm_mixer
:
1163 case snd_soc_dapm_mixer_named_ctl
:
1164 case snd_soc_dapm_supply
:
1166 count
+= sprintf(buf
+ count
, "%s: %s\n",
1167 w
->name
, w
->power
? "On":"Off");
1174 switch (codec
->bias_level
) {
1175 case SND_SOC_BIAS_ON
:
1178 case SND_SOC_BIAS_PREPARE
:
1181 case SND_SOC_BIAS_STANDBY
:
1184 case SND_SOC_BIAS_OFF
:
1188 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1193 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1195 int snd_soc_dapm_sys_add(struct device
*dev
)
1197 return device_create_file(dev
, &dev_attr_dapm_widget
);
1200 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1202 device_remove_file(dev
, &dev_attr_dapm_widget
);
1205 /* free all dapm widgets and resources */
1206 static void dapm_free_widgets(struct snd_soc_codec
*codec
)
1208 struct snd_soc_dapm_widget
*w
, *next_w
;
1209 struct snd_soc_dapm_path
*p
, *next_p
;
1211 list_for_each_entry_safe(w
, next_w
, &codec
->dapm_widgets
, list
) {
1216 list_for_each_entry_safe(p
, next_p
, &codec
->dapm_paths
, list
) {
1218 kfree(p
->long_name
);
1223 static int snd_soc_dapm_set_pin(struct snd_soc_codec
*codec
,
1224 const char *pin
, int status
)
1226 struct snd_soc_dapm_widget
*w
;
1228 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1229 if (!strcmp(w
->name
, pin
)) {
1230 pr_debug("dapm: %s: pin %s\n", codec
->name
, pin
);
1231 w
->connected
= status
;
1236 pr_err("dapm: %s: configuring unknown pin %s\n", codec
->name
, pin
);
1241 * snd_soc_dapm_sync - scan and power dapm paths
1242 * @codec: audio codec
1244 * Walks all dapm audio paths and powers widgets according to their
1245 * stream or path usage.
1247 * Returns 0 for success.
1249 int snd_soc_dapm_sync(struct snd_soc_codec
*codec
)
1251 int ret
= dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1252 dump_dapm(codec
, "sync");
1255 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1257 static int snd_soc_dapm_add_route(struct snd_soc_codec
*codec
,
1258 const char *sink
, const char *control
, const char *source
)
1260 struct snd_soc_dapm_path
*path
;
1261 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1264 /* find src and dest widgets */
1265 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1267 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1271 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1276 if (wsource
== NULL
|| wsink
== NULL
)
1279 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1283 path
->source
= wsource
;
1285 INIT_LIST_HEAD(&path
->list
);
1286 INIT_LIST_HEAD(&path
->list_source
);
1287 INIT_LIST_HEAD(&path
->list_sink
);
1289 /* check for external widgets */
1290 if (wsink
->id
== snd_soc_dapm_input
) {
1291 if (wsource
->id
== snd_soc_dapm_micbias
||
1292 wsource
->id
== snd_soc_dapm_mic
||
1293 wsink
->id
== snd_soc_dapm_line
||
1294 wsink
->id
== snd_soc_dapm_output
)
1297 if (wsource
->id
== snd_soc_dapm_output
) {
1298 if (wsink
->id
== snd_soc_dapm_spk
||
1299 wsink
->id
== snd_soc_dapm_hp
||
1300 wsink
->id
== snd_soc_dapm_line
||
1301 wsink
->id
== snd_soc_dapm_input
)
1305 /* connect static paths */
1306 if (control
== NULL
) {
1307 list_add(&path
->list
, &codec
->dapm_paths
);
1308 list_add(&path
->list_sink
, &wsink
->sources
);
1309 list_add(&path
->list_source
, &wsource
->sinks
);
1314 /* connect dynamic paths */
1316 case snd_soc_dapm_adc
:
1317 case snd_soc_dapm_dac
:
1318 case snd_soc_dapm_pga
:
1319 case snd_soc_dapm_input
:
1320 case snd_soc_dapm_output
:
1321 case snd_soc_dapm_micbias
:
1322 case snd_soc_dapm_vmid
:
1323 case snd_soc_dapm_pre
:
1324 case snd_soc_dapm_post
:
1325 case snd_soc_dapm_supply
:
1326 list_add(&path
->list
, &codec
->dapm_paths
);
1327 list_add(&path
->list_sink
, &wsink
->sources
);
1328 list_add(&path
->list_source
, &wsource
->sinks
);
1331 case snd_soc_dapm_mux
:
1332 case snd_soc_dapm_value_mux
:
1333 ret
= dapm_connect_mux(codec
, wsource
, wsink
, path
, control
,
1334 &wsink
->kcontrols
[0]);
1338 case snd_soc_dapm_switch
:
1339 case snd_soc_dapm_mixer
:
1340 case snd_soc_dapm_mixer_named_ctl
:
1341 ret
= dapm_connect_mixer(codec
, wsource
, wsink
, path
, control
);
1345 case snd_soc_dapm_hp
:
1346 case snd_soc_dapm_mic
:
1347 case snd_soc_dapm_line
:
1348 case snd_soc_dapm_spk
:
1349 list_add(&path
->list
, &codec
->dapm_paths
);
1350 list_add(&path
->list_sink
, &wsink
->sources
);
1351 list_add(&path
->list_source
, &wsource
->sinks
);
1358 printk(KERN_WARNING
"asoc: no dapm match for %s --> %s --> %s\n", source
,
1365 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1367 * @route: audio routes
1368 * @num: number of routes
1370 * Connects 2 dapm widgets together via a named audio path. The sink is
1371 * the widget receiving the audio signal, whilst the source is the sender
1372 * of the audio signal.
1374 * Returns 0 for success else error. On error all resources can be freed
1375 * with a call to snd_soc_card_free().
1377 int snd_soc_dapm_add_routes(struct snd_soc_codec
*codec
,
1378 const struct snd_soc_dapm_route
*route
, int num
)
1382 for (i
= 0; i
< num
; i
++) {
1383 ret
= snd_soc_dapm_add_route(codec
, route
->sink
,
1384 route
->control
, route
->source
);
1386 printk(KERN_ERR
"Failed to add route %s->%s\n",
1396 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1399 * snd_soc_dapm_new_widgets - add new dapm widgets
1400 * @codec: audio codec
1402 * Checks the codec for any new dapm widgets and creates them if found.
1404 * Returns 0 for success.
1406 int snd_soc_dapm_new_widgets(struct snd_soc_codec
*codec
)
1408 struct snd_soc_dapm_widget
*w
;
1410 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
1416 case snd_soc_dapm_switch
:
1417 case snd_soc_dapm_mixer
:
1418 case snd_soc_dapm_mixer_named_ctl
:
1419 w
->power_check
= dapm_generic_check_power
;
1420 dapm_new_mixer(codec
, w
);
1422 case snd_soc_dapm_mux
:
1423 case snd_soc_dapm_value_mux
:
1424 w
->power_check
= dapm_generic_check_power
;
1425 dapm_new_mux(codec
, w
);
1427 case snd_soc_dapm_adc
:
1428 w
->power_check
= dapm_adc_check_power
;
1430 case snd_soc_dapm_dac
:
1431 w
->power_check
= dapm_dac_check_power
;
1433 case snd_soc_dapm_pga
:
1434 w
->power_check
= dapm_generic_check_power
;
1435 dapm_new_pga(codec
, w
);
1437 case snd_soc_dapm_input
:
1438 case snd_soc_dapm_output
:
1439 case snd_soc_dapm_micbias
:
1440 case snd_soc_dapm_spk
:
1441 case snd_soc_dapm_hp
:
1442 case snd_soc_dapm_mic
:
1443 case snd_soc_dapm_line
:
1444 w
->power_check
= dapm_generic_check_power
;
1446 case snd_soc_dapm_supply
:
1447 w
->power_check
= dapm_supply_check_power
;
1448 case snd_soc_dapm_vmid
:
1449 case snd_soc_dapm_pre
:
1450 case snd_soc_dapm_post
:
1456 dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1459 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1462 * snd_soc_dapm_get_volsw - dapm mixer get callback
1463 * @kcontrol: mixer control
1464 * @ucontrol: control element information
1466 * Callback to get the value of a dapm mixer control.
1468 * Returns 0 for success.
1470 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1471 struct snd_ctl_elem_value
*ucontrol
)
1473 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1474 struct soc_mixer_control
*mc
=
1475 (struct soc_mixer_control
*)kcontrol
->private_value
;
1476 unsigned int reg
= mc
->reg
;
1477 unsigned int shift
= mc
->shift
;
1478 unsigned int rshift
= mc
->rshift
;
1480 unsigned int invert
= mc
->invert
;
1481 unsigned int mask
= (1 << fls(max
)) - 1;
1483 /* return the saved value if we are powered down */
1484 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1485 ucontrol
->value
.integer
.value
[0] = widget
->saved_value
;
1489 ucontrol
->value
.integer
.value
[0] =
1490 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1491 if (shift
!= rshift
)
1492 ucontrol
->value
.integer
.value
[1] =
1493 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1495 ucontrol
->value
.integer
.value
[0] =
1496 max
- ucontrol
->value
.integer
.value
[0];
1497 if (shift
!= rshift
)
1498 ucontrol
->value
.integer
.value
[1] =
1499 max
- ucontrol
->value
.integer
.value
[1];
1504 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1507 * snd_soc_dapm_put_volsw - dapm mixer set callback
1508 * @kcontrol: mixer control
1509 * @ucontrol: control element information
1511 * Callback to set the value of a dapm mixer control.
1513 * Returns 0 for success.
1515 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1516 struct snd_ctl_elem_value
*ucontrol
)
1518 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1519 struct soc_mixer_control
*mc
=
1520 (struct soc_mixer_control
*)kcontrol
->private_value
;
1521 unsigned int reg
= mc
->reg
;
1522 unsigned int shift
= mc
->shift
;
1523 unsigned int rshift
= mc
->rshift
;
1525 unsigned int mask
= (1 << fls(max
)) - 1;
1526 unsigned int invert
= mc
->invert
;
1527 unsigned int val
, val2
, val_mask
;
1530 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1534 val_mask
= mask
<< shift
;
1536 if (shift
!= rshift
) {
1537 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1540 val_mask
|= mask
<< rshift
;
1541 val
|= val2
<< rshift
;
1544 mutex_lock(&widget
->codec
->mutex
);
1545 widget
->value
= val
;
1547 /* save volume value if the widget is powered down */
1548 if (widget
->id
== snd_soc_dapm_pga
&& !widget
->power
) {
1549 widget
->saved_value
= val
;
1550 mutex_unlock(&widget
->codec
->mutex
);
1554 dapm_mixer_update_power(widget
, kcontrol
, reg
, val_mask
, val
, invert
);
1555 if (widget
->event
) {
1556 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1557 ret
= widget
->event(widget
, kcontrol
,
1558 SND_SOC_DAPM_PRE_REG
);
1564 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1565 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1566 ret
= widget
->event(widget
, kcontrol
,
1567 SND_SOC_DAPM_POST_REG
);
1569 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1572 mutex_unlock(&widget
->codec
->mutex
);
1575 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1578 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1579 * @kcontrol: mixer control
1580 * @ucontrol: control element information
1582 * Callback to get the value of a dapm enumerated double mixer control.
1584 * Returns 0 for success.
1586 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1587 struct snd_ctl_elem_value
*ucontrol
)
1589 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1590 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1591 unsigned int val
, bitmask
;
1593 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1595 val
= snd_soc_read(widget
->codec
, e
->reg
);
1596 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1597 if (e
->shift_l
!= e
->shift_r
)
1598 ucontrol
->value
.enumerated
.item
[1] =
1599 (val
>> e
->shift_r
) & (bitmask
- 1);
1603 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1606 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1607 * @kcontrol: mixer control
1608 * @ucontrol: control element information
1610 * Callback to set the value of a dapm enumerated double mixer control.
1612 * Returns 0 for success.
1614 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1615 struct snd_ctl_elem_value
*ucontrol
)
1617 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1618 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1619 unsigned int val
, mux
;
1620 unsigned int mask
, bitmask
;
1623 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1625 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1627 mux
= ucontrol
->value
.enumerated
.item
[0];
1628 val
= mux
<< e
->shift_l
;
1629 mask
= (bitmask
- 1) << e
->shift_l
;
1630 if (e
->shift_l
!= e
->shift_r
) {
1631 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1633 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1634 mask
|= (bitmask
- 1) << e
->shift_r
;
1637 mutex_lock(&widget
->codec
->mutex
);
1638 widget
->value
= val
;
1639 dapm_mux_update_power(widget
, kcontrol
, mask
, mux
, val
, e
);
1640 if (widget
->event
) {
1641 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1642 ret
= widget
->event(widget
,
1643 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1647 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1648 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1649 ret
= widget
->event(widget
,
1650 kcontrol
, SND_SOC_DAPM_POST_REG
);
1652 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1655 mutex_unlock(&widget
->codec
->mutex
);
1658 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1661 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1663 * @kcontrol: mixer control
1664 * @ucontrol: control element information
1666 * Callback to get the value of a dapm semi enumerated double mixer control.
1668 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1669 * used for handling bitfield coded enumeration for example.
1671 * Returns 0 for success.
1673 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1674 struct snd_ctl_elem_value
*ucontrol
)
1676 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1677 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1678 unsigned int reg_val
, val
, mux
;
1680 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1681 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1682 for (mux
= 0; mux
< e
->max
; mux
++) {
1683 if (val
== e
->values
[mux
])
1686 ucontrol
->value
.enumerated
.item
[0] = mux
;
1687 if (e
->shift_l
!= e
->shift_r
) {
1688 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1689 for (mux
= 0; mux
< e
->max
; mux
++) {
1690 if (val
== e
->values
[mux
])
1693 ucontrol
->value
.enumerated
.item
[1] = mux
;
1698 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1701 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1703 * @kcontrol: mixer control
1704 * @ucontrol: control element information
1706 * Callback to set the value of a dapm semi enumerated double mixer control.
1708 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1709 * used for handling bitfield coded enumeration for example.
1711 * Returns 0 for success.
1713 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
1714 struct snd_ctl_elem_value
*ucontrol
)
1716 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1717 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1718 unsigned int val
, mux
;
1722 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1724 mux
= ucontrol
->value
.enumerated
.item
[0];
1725 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1726 mask
= e
->mask
<< e
->shift_l
;
1727 if (e
->shift_l
!= e
->shift_r
) {
1728 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1730 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1731 mask
|= e
->mask
<< e
->shift_r
;
1734 mutex_lock(&widget
->codec
->mutex
);
1735 widget
->value
= val
;
1736 dapm_mux_update_power(widget
, kcontrol
, mask
, mux
, val
, e
);
1737 if (widget
->event
) {
1738 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1739 ret
= widget
->event(widget
,
1740 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1744 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1745 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1746 ret
= widget
->event(widget
,
1747 kcontrol
, SND_SOC_DAPM_POST_REG
);
1749 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1752 mutex_unlock(&widget
->codec
->mutex
);
1755 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1758 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1760 * @kcontrol: mixer control
1761 * @uinfo: control element information
1763 * Callback to provide information about a pin switch control.
1765 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1766 struct snd_ctl_elem_info
*uinfo
)
1768 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1770 uinfo
->value
.integer
.min
= 0;
1771 uinfo
->value
.integer
.max
= 1;
1775 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1778 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1780 * @kcontrol: mixer control
1783 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1784 struct snd_ctl_elem_value
*ucontrol
)
1786 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1787 const char *pin
= (const char *)kcontrol
->private_value
;
1789 mutex_lock(&codec
->mutex
);
1791 ucontrol
->value
.integer
.value
[0] =
1792 snd_soc_dapm_get_pin_status(codec
, pin
);
1794 mutex_unlock(&codec
->mutex
);
1798 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
1801 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1803 * @kcontrol: mixer control
1806 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
1807 struct snd_ctl_elem_value
*ucontrol
)
1809 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1810 const char *pin
= (const char *)kcontrol
->private_value
;
1812 mutex_lock(&codec
->mutex
);
1814 if (ucontrol
->value
.integer
.value
[0])
1815 snd_soc_dapm_enable_pin(codec
, pin
);
1817 snd_soc_dapm_disable_pin(codec
, pin
);
1819 snd_soc_dapm_sync(codec
);
1821 mutex_unlock(&codec
->mutex
);
1825 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
1828 * snd_soc_dapm_new_control - create new dapm control
1829 * @codec: audio codec
1830 * @widget: widget template
1832 * Creates a new dapm control based upon the template.
1834 * Returns 0 for success else error.
1836 int snd_soc_dapm_new_control(struct snd_soc_codec
*codec
,
1837 const struct snd_soc_dapm_widget
*widget
)
1839 struct snd_soc_dapm_widget
*w
;
1841 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
1845 INIT_LIST_HEAD(&w
->sources
);
1846 INIT_LIST_HEAD(&w
->sinks
);
1847 INIT_LIST_HEAD(&w
->list
);
1848 list_add(&w
->list
, &codec
->dapm_widgets
);
1850 /* machine layer set ups unconnected pins and insertions */
1854 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
1857 * snd_soc_dapm_new_controls - create new dapm controls
1858 * @codec: audio codec
1859 * @widget: widget array
1860 * @num: number of widgets
1862 * Creates new DAPM controls based upon the templates.
1864 * Returns 0 for success else error.
1866 int snd_soc_dapm_new_controls(struct snd_soc_codec
*codec
,
1867 const struct snd_soc_dapm_widget
*widget
,
1872 for (i
= 0; i
< num
; i
++) {
1873 ret
= snd_soc_dapm_new_control(codec
, widget
);
1876 "ASoC: Failed to create DAPM control %s: %d\n",
1884 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
1888 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1889 * @codec: audio codec
1890 * @stream: stream name
1891 * @event: stream event
1893 * Sends a stream event to the dapm core. The core then makes any
1894 * necessary widget power changes.
1896 * Returns 0 for success else error.
1898 int snd_soc_dapm_stream_event(struct snd_soc_codec
*codec
,
1899 char *stream
, int event
)
1901 struct snd_soc_dapm_widget
*w
;
1906 mutex_lock(&codec
->mutex
);
1907 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
1911 pr_debug("widget %s\n %s stream %s event %d\n",
1912 w
->name
, w
->sname
, stream
, event
);
1913 if (strstr(w
->sname
, stream
)) {
1915 case SND_SOC_DAPM_STREAM_START
:
1918 case SND_SOC_DAPM_STREAM_STOP
:
1921 case SND_SOC_DAPM_STREAM_SUSPEND
:
1926 case SND_SOC_DAPM_STREAM_RESUME
:
1932 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
1934 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
1939 mutex_unlock(&codec
->mutex
);
1941 dapm_power_widgets(codec
, event
);
1942 dump_dapm(codec
, __func__
);
1945 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
1948 * snd_soc_dapm_enable_pin - enable pin.
1952 * Enables input/output pin and its parents or children widgets iff there is
1953 * a valid audio route and active audio stream.
1954 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1955 * do any widget power switching.
1957 int snd_soc_dapm_enable_pin(struct snd_soc_codec
*codec
, const char *pin
)
1959 return snd_soc_dapm_set_pin(codec
, pin
, 1);
1961 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
1964 * snd_soc_dapm_disable_pin - disable pin.
1968 * Disables input/output pin and its parents or children widgets.
1969 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1970 * do any widget power switching.
1972 int snd_soc_dapm_disable_pin(struct snd_soc_codec
*codec
, const char *pin
)
1974 return snd_soc_dapm_set_pin(codec
, pin
, 0);
1976 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
1979 * snd_soc_dapm_nc_pin - permanently disable pin.
1983 * Marks the specified pin as being not connected, disabling it along
1984 * any parent or child widgets. At present this is identical to
1985 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1986 * additional things such as disabling controls which only affect
1987 * paths through the pin.
1989 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1990 * do any widget power switching.
1992 int snd_soc_dapm_nc_pin(struct snd_soc_codec
*codec
, const char *pin
)
1994 return snd_soc_dapm_set_pin(codec
, pin
, 0);
1996 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
1999 * snd_soc_dapm_get_pin_status - get audio pin status
2000 * @codec: audio codec
2001 * @pin: audio signal pin endpoint (or start point)
2003 * Get audio pin status - connected or disconnected.
2005 * Returns 1 for connected otherwise 0.
2007 int snd_soc_dapm_get_pin_status(struct snd_soc_codec
*codec
, const char *pin
)
2009 struct snd_soc_dapm_widget
*w
;
2011 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2012 if (!strcmp(w
->name
, pin
))
2013 return w
->connected
;
2018 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2021 * snd_soc_dapm_free - free dapm resources
2022 * @socdev: SoC device
2024 * Free all dapm widgets and resources.
2026 void snd_soc_dapm_free(struct snd_soc_device
*socdev
)
2028 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2030 snd_soc_dapm_sys_remove(socdev
->dev
);
2031 dapm_free_widgets(codec
);
2033 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2036 * snd_soc_dapm_shutdown - callback for system shutdown
2038 void snd_soc_dapm_shutdown(struct snd_soc_device
*socdev
)
2040 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
2041 struct snd_soc_dapm_widget
*w
;
2042 LIST_HEAD(down_list
);
2045 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2047 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2053 /* If there were no widgets to power down we're already in
2057 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_PREPARE
);
2058 dapm_seq_run(codec
, &down_list
, 0, dapm_down_seq
);
2059 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_STANDBY
);
2062 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_OFF
);
2065 /* Module information */
2066 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2067 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2068 MODULE_LICENSE("GPL");