2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
25 * o DAPM power change sequencing - allow for configurable per
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
37 #include <linux/bitops.h>
38 #include <linux/platform_device.h>
39 #include <linux/jiffies.h>
40 #include <linux/debugfs.h>
41 #include <linux/slab.h>
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc-dapm.h>
46 #include <sound/initval.h>
48 /* dapm power sequences - make this per codec in the future */
49 static int dapm_up_seq
[] = {
50 [snd_soc_dapm_pre
] = 0,
51 [snd_soc_dapm_supply
] = 1,
52 [snd_soc_dapm_micbias
] = 2,
53 [snd_soc_dapm_aif_in
] = 3,
54 [snd_soc_dapm_aif_out
] = 3,
55 [snd_soc_dapm_mic
] = 4,
56 [snd_soc_dapm_mux
] = 5,
57 [snd_soc_dapm_value_mux
] = 5,
58 [snd_soc_dapm_dac
] = 6,
59 [snd_soc_dapm_mixer
] = 7,
60 [snd_soc_dapm_mixer_named_ctl
] = 7,
61 [snd_soc_dapm_pga
] = 8,
62 [snd_soc_dapm_adc
] = 9,
63 [snd_soc_dapm_hp
] = 10,
64 [snd_soc_dapm_spk
] = 10,
65 [snd_soc_dapm_post
] = 11,
68 static int dapm_down_seq
[] = {
69 [snd_soc_dapm_pre
] = 0,
70 [snd_soc_dapm_adc
] = 1,
71 [snd_soc_dapm_hp
] = 2,
72 [snd_soc_dapm_spk
] = 2,
73 [snd_soc_dapm_pga
] = 4,
74 [snd_soc_dapm_mixer_named_ctl
] = 5,
75 [snd_soc_dapm_mixer
] = 5,
76 [snd_soc_dapm_dac
] = 6,
77 [snd_soc_dapm_mic
] = 7,
78 [snd_soc_dapm_micbias
] = 8,
79 [snd_soc_dapm_mux
] = 9,
80 [snd_soc_dapm_value_mux
] = 9,
81 [snd_soc_dapm_aif_in
] = 10,
82 [snd_soc_dapm_aif_out
] = 10,
83 [snd_soc_dapm_supply
] = 11,
84 [snd_soc_dapm_post
] = 12,
87 static void pop_wait(u32 pop_time
)
90 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time
));
93 static void pop_dbg(u32 pop_time
, const char *fmt
, ...)
106 /* create a new dapm widget */
107 static inline struct snd_soc_dapm_widget
*dapm_cnew_widget(
108 const struct snd_soc_dapm_widget
*_widget
)
110 return kmemdup(_widget
, sizeof(*_widget
), GFP_KERNEL
);
114 * snd_soc_dapm_set_bias_level - set the bias level for the system
115 * @card: audio device
116 * @level: level to configure
118 * Configure the bias (power) levels for the SoC audio device.
120 * Returns 0 for success else error.
122 static int snd_soc_dapm_set_bias_level(struct snd_soc_card
*card
,
123 struct snd_soc_codec
*codec
, enum snd_soc_bias_level level
)
128 case SND_SOC_BIAS_ON
:
129 dev_dbg(codec
->dev
, "Setting full bias\n");
131 case SND_SOC_BIAS_PREPARE
:
132 dev_dbg(codec
->dev
, "Setting bias prepare\n");
134 case SND_SOC_BIAS_STANDBY
:
135 dev_dbg(codec
->dev
, "Setting standby bias\n");
137 case SND_SOC_BIAS_OFF
:
138 dev_dbg(codec
->dev
, "Setting bias off\n");
141 dev_err(codec
->dev
, "Setting invalid bias %d\n", level
);
145 if (card
&& card
->set_bias_level
)
146 ret
= card
->set_bias_level(card
, level
);
148 if (codec
->driver
->set_bias_level
)
149 ret
= codec
->driver
->set_bias_level(codec
, level
);
151 codec
->bias_level
= 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
:
227 case snd_soc_dapm_aif_in
:
228 case snd_soc_dapm_aif_out
:
231 /* does effect routing - dynamically connected */
232 case snd_soc_dapm_hp
:
233 case snd_soc_dapm_mic
:
234 case snd_soc_dapm_spk
:
235 case snd_soc_dapm_line
:
236 case snd_soc_dapm_pre
:
237 case snd_soc_dapm_post
:
243 /* connect mux widget to its interconnecting audio paths */
244 static int dapm_connect_mux(struct snd_soc_codec
*codec
,
245 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
246 struct snd_soc_dapm_path
*path
, const char *control_name
,
247 const struct snd_kcontrol_new
*kcontrol
)
249 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
252 for (i
= 0; i
< e
->max
; i
++) {
253 if (!(strcmp(control_name
, e
->texts
[i
]))) {
254 list_add(&path
->list
, &codec
->dapm_paths
);
255 list_add(&path
->list_sink
, &dest
->sources
);
256 list_add(&path
->list_source
, &src
->sinks
);
257 path
->name
= (char*)e
->texts
[i
];
258 dapm_set_path_status(dest
, path
, 0);
266 /* connect mixer widget to its interconnecting audio paths */
267 static int dapm_connect_mixer(struct snd_soc_codec
*codec
,
268 struct snd_soc_dapm_widget
*src
, struct snd_soc_dapm_widget
*dest
,
269 struct snd_soc_dapm_path
*path
, const char *control_name
)
273 /* search for mixer kcontrol */
274 for (i
= 0; i
< dest
->num_kcontrols
; i
++) {
275 if (!strcmp(control_name
, dest
->kcontrols
[i
].name
)) {
276 list_add(&path
->list
, &codec
->dapm_paths
);
277 list_add(&path
->list_sink
, &dest
->sources
);
278 list_add(&path
->list_source
, &src
->sinks
);
279 path
->name
= dest
->kcontrols
[i
].name
;
280 dapm_set_path_status(dest
, path
, i
);
287 /* update dapm codec register bits */
288 static int dapm_update_bits(struct snd_soc_dapm_widget
*widget
)
291 unsigned int old
, new;
292 struct snd_soc_codec
*codec
= widget
->codec
;
294 /* check for valid widgets */
295 if (widget
->reg
< 0 || widget
->id
== snd_soc_dapm_input
||
296 widget
->id
== snd_soc_dapm_output
||
297 widget
->id
== snd_soc_dapm_hp
||
298 widget
->id
== snd_soc_dapm_mic
||
299 widget
->id
== snd_soc_dapm_line
||
300 widget
->id
== snd_soc_dapm_spk
)
303 power
= widget
->power
;
305 power
= (power
? 0:1);
307 old
= snd_soc_read(codec
, widget
->reg
);
308 new = (old
& ~(0x1 << widget
->shift
)) | (power
<< widget
->shift
);
312 pop_dbg(codec
->pop_time
, "pop test %s : %s in %d ms\n",
313 widget
->name
, widget
->power
? "on" : "off",
315 pop_wait(codec
->pop_time
);
316 snd_soc_write(codec
, widget
->reg
, new);
318 pr_debug("reg %x old %x new %x change %d\n", widget
->reg
,
323 /* create new dapm mixer control */
324 static int dapm_new_mixer(struct snd_soc_codec
*codec
,
325 struct snd_soc_dapm_widget
*w
)
329 struct snd_soc_dapm_path
*path
;
332 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
335 list_for_each_entry(path
, &w
->sources
, list_sink
) {
337 /* mixer/mux paths name must match control name */
338 if (path
->name
!= (char*)w
->kcontrols
[i
].name
)
341 /* add dapm control with long name.
342 * for dapm_mixer this is the concatenation of the
343 * mixer and kcontrol name.
344 * for dapm_mixer_named_ctl this is simply the
347 name_len
= strlen(w
->kcontrols
[i
].name
) + 1;
348 if (w
->id
!= snd_soc_dapm_mixer_named_ctl
)
349 name_len
+= 1 + strlen(w
->name
);
351 path
->long_name
= kmalloc(name_len
, GFP_KERNEL
);
353 if (path
->long_name
== NULL
)
358 snprintf(path
->long_name
, name_len
, "%s %s",
359 w
->name
, w
->kcontrols
[i
].name
);
361 case snd_soc_dapm_mixer_named_ctl
:
362 snprintf(path
->long_name
, name_len
, "%s",
363 w
->kcontrols
[i
].name
);
367 path
->long_name
[name_len
- 1] = '\0';
369 path
->kcontrol
= snd_soc_cnew(&w
->kcontrols
[i
], w
,
371 ret
= snd_ctl_add(codec
->card
->snd_card
, path
->kcontrol
);
373 printk(KERN_ERR
"asoc: failed to add dapm kcontrol %s: %d\n",
376 kfree(path
->long_name
);
377 path
->long_name
= NULL
;
385 /* create new dapm mux control */
386 static int dapm_new_mux(struct snd_soc_codec
*codec
,
387 struct snd_soc_dapm_widget
*w
)
389 struct snd_soc_dapm_path
*path
= NULL
;
390 struct snd_kcontrol
*kcontrol
;
393 if (!w
->num_kcontrols
) {
394 printk(KERN_ERR
"asoc: mux %s has no controls\n", w
->name
);
398 kcontrol
= snd_soc_cnew(&w
->kcontrols
[0], w
, w
->name
);
399 ret
= snd_ctl_add(codec
->card
->snd_card
, kcontrol
);
403 list_for_each_entry(path
, &w
->sources
, list_sink
)
404 path
->kcontrol
= kcontrol
;
409 printk(KERN_ERR
"asoc: failed to add kcontrol %s\n", w
->name
);
413 /* create new dapm volume control */
414 static int dapm_new_pga(struct snd_soc_codec
*codec
,
415 struct snd_soc_dapm_widget
*w
)
417 if (w
->num_kcontrols
)
418 pr_err("asoc: PGA controls not supported: '%s'\n", w
->name
);
423 /* reset 'walked' bit for each dapm path */
424 static inline void dapm_clear_walk(struct snd_soc_codec
*codec
)
426 struct snd_soc_dapm_path
*p
;
428 list_for_each_entry(p
, &codec
->dapm_paths
, list
)
432 /* We implement power down on suspend by checking the power state of
433 * the ALSA card - when we are suspending the ALSA state for the card
436 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget
*widget
)
438 int level
= snd_power_get_state(widget
->codec
->card
->snd_card
);
441 case SNDRV_CTL_POWER_D3hot
:
442 case SNDRV_CTL_POWER_D3cold
:
443 if (widget
->ignore_suspend
)
444 pr_debug("%s ignoring suspend\n", widget
->name
);
445 return widget
->ignore_suspend
;
452 * Recursively check for a completed path to an active or physically connected
453 * output widget. Returns number of complete paths.
455 static int is_connected_output_ep(struct snd_soc_dapm_widget
*widget
)
457 struct snd_soc_dapm_path
*path
;
460 if (widget
->id
== snd_soc_dapm_supply
)
463 switch (widget
->id
) {
464 case snd_soc_dapm_adc
:
465 case snd_soc_dapm_aif_out
:
467 return snd_soc_dapm_suspend_check(widget
);
472 if (widget
->connected
) {
473 /* connected pin ? */
474 if (widget
->id
== snd_soc_dapm_output
&& !widget
->ext
)
475 return snd_soc_dapm_suspend_check(widget
);
477 /* connected jack or spk ? */
478 if (widget
->id
== snd_soc_dapm_hp
|| widget
->id
== snd_soc_dapm_spk
||
479 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sources
)))
480 return snd_soc_dapm_suspend_check(widget
);
483 list_for_each_entry(path
, &widget
->sinks
, list_source
) {
487 if (path
->sink
&& path
->connect
) {
489 con
+= is_connected_output_ep(path
->sink
);
497 * Recursively check for a completed path to an active or physically connected
498 * input widget. Returns number of complete paths.
500 static int is_connected_input_ep(struct snd_soc_dapm_widget
*widget
)
502 struct snd_soc_dapm_path
*path
;
505 if (widget
->id
== snd_soc_dapm_supply
)
508 /* active stream ? */
509 switch (widget
->id
) {
510 case snd_soc_dapm_dac
:
511 case snd_soc_dapm_aif_in
:
513 return snd_soc_dapm_suspend_check(widget
);
518 if (widget
->connected
) {
519 /* connected pin ? */
520 if (widget
->id
== snd_soc_dapm_input
&& !widget
->ext
)
521 return snd_soc_dapm_suspend_check(widget
);
523 /* connected VMID/Bias for lower pops */
524 if (widget
->id
== snd_soc_dapm_vmid
)
525 return snd_soc_dapm_suspend_check(widget
);
527 /* connected jack ? */
528 if (widget
->id
== snd_soc_dapm_mic
||
529 (widget
->id
== snd_soc_dapm_line
&& !list_empty(&widget
->sinks
)))
530 return snd_soc_dapm_suspend_check(widget
);
533 list_for_each_entry(path
, &widget
->sources
, list_sink
) {
537 if (path
->source
&& path
->connect
) {
539 con
+= is_connected_input_ep(path
->source
);
547 * Handler for generic register modifier widget.
549 int dapm_reg_event(struct snd_soc_dapm_widget
*w
,
550 struct snd_kcontrol
*kcontrol
, int event
)
554 if (SND_SOC_DAPM_EVENT_ON(event
))
559 snd_soc_update_bits(w
->codec
, -(w
->reg
+ 1),
560 w
->mask
<< w
->shift
, val
<< w
->shift
);
564 EXPORT_SYMBOL_GPL(dapm_reg_event
);
566 /* Standard power change method, used to apply power changes to most
569 static int dapm_generic_apply_power(struct snd_soc_dapm_widget
*w
)
573 /* call any power change event handlers */
575 pr_debug("power %s event for %s flags %x\n",
576 w
->power
? "on" : "off",
577 w
->name
, w
->event_flags
);
579 /* power up pre event */
580 if (w
->power
&& w
->event
&&
581 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
582 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
587 /* power down pre event */
588 if (!w
->power
&& w
->event
&&
589 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
590 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
597 /* power up post event */
598 if (w
->power
&& w
->event
&&
599 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
601 NULL
, SND_SOC_DAPM_POST_PMU
);
606 /* power down post event */
607 if (!w
->power
&& w
->event
&&
608 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
609 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
617 /* Generic check to see if a widget should be powered.
619 static int dapm_generic_check_power(struct snd_soc_dapm_widget
*w
)
623 in
= is_connected_input_ep(w
);
624 dapm_clear_walk(w
->codec
);
625 out
= is_connected_output_ep(w
);
626 dapm_clear_walk(w
->codec
);
627 return out
!= 0 && in
!= 0;
630 /* Check to see if an ADC has power */
631 static int dapm_adc_check_power(struct snd_soc_dapm_widget
*w
)
636 in
= is_connected_input_ep(w
);
637 dapm_clear_walk(w
->codec
);
640 return dapm_generic_check_power(w
);
644 /* Check to see if a DAC has power */
645 static int dapm_dac_check_power(struct snd_soc_dapm_widget
*w
)
650 out
= is_connected_output_ep(w
);
651 dapm_clear_walk(w
->codec
);
654 return dapm_generic_check_power(w
);
658 /* Check to see if a power supply is needed */
659 static int dapm_supply_check_power(struct snd_soc_dapm_widget
*w
)
661 struct snd_soc_dapm_path
*path
;
664 /* Check if one of our outputs is connected */
665 list_for_each_entry(path
, &w
->sinks
, list_source
) {
666 if (path
->connected
&&
667 !path
->connected(path
->source
, path
->sink
))
670 if (path
->sink
&& path
->sink
->power_check
&&
671 path
->sink
->power_check(path
->sink
)) {
677 dapm_clear_walk(w
->codec
);
682 static int dapm_seq_compare(struct snd_soc_dapm_widget
*a
,
683 struct snd_soc_dapm_widget
*b
,
686 if (a
->codec
!= b
->codec
)
687 return (unsigned long)a
- (unsigned long)b
;
688 if (sort
[a
->id
] != sort
[b
->id
])
689 return sort
[a
->id
] - sort
[b
->id
];
690 if (a
->reg
!= b
->reg
)
691 return a
->reg
- b
->reg
;
696 /* Insert a widget in order into a DAPM power sequence. */
697 static void dapm_seq_insert(struct snd_soc_dapm_widget
*new_widget
,
698 struct list_head
*list
,
701 struct snd_soc_dapm_widget
*w
;
703 list_for_each_entry(w
, list
, power_list
)
704 if (dapm_seq_compare(new_widget
, w
, sort
) < 0) {
705 list_add_tail(&new_widget
->power_list
, &w
->power_list
);
709 list_add_tail(&new_widget
->power_list
, list
);
712 /* Apply the coalesced changes from a DAPM sequence */
713 static void dapm_seq_run_coalesced(struct snd_soc_codec
*codec
,
714 struct list_head
*pending
)
716 struct snd_soc_dapm_widget
*w
;
718 unsigned int value
= 0;
719 unsigned int mask
= 0;
720 unsigned int cur_mask
;
722 reg
= list_first_entry(pending
, struct snd_soc_dapm_widget
,
725 list_for_each_entry(w
, pending
, power_list
) {
726 cur_mask
= 1 << w
->shift
;
727 BUG_ON(reg
!= w
->reg
);
738 pop_dbg(codec
->pop_time
,
739 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
740 w
->name
, reg
, value
, mask
);
742 /* power up pre event */
743 if (w
->power
&& w
->event
&&
744 (w
->event_flags
& SND_SOC_DAPM_PRE_PMU
)) {
745 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMU\n",
747 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMU
);
749 pr_err("%s: pre event failed: %d\n",
753 /* power down pre event */
754 if (!w
->power
&& w
->event
&&
755 (w
->event_flags
& SND_SOC_DAPM_PRE_PMD
)) {
756 pop_dbg(codec
->pop_time
, "pop test : %s PRE_PMD\n",
758 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_PRE_PMD
);
760 pr_err("%s: pre event failed: %d\n",
766 pop_dbg(codec
->pop_time
,
767 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
768 value
, mask
, reg
, codec
->pop_time
);
769 pop_wait(codec
->pop_time
);
770 snd_soc_update_bits(codec
, reg
, mask
, value
);
773 list_for_each_entry(w
, pending
, power_list
) {
774 /* power up post event */
775 if (w
->power
&& w
->event
&&
776 (w
->event_flags
& SND_SOC_DAPM_POST_PMU
)) {
777 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMU\n",
780 NULL
, SND_SOC_DAPM_POST_PMU
);
782 pr_err("%s: post event failed: %d\n",
786 /* power down post event */
787 if (!w
->power
&& w
->event
&&
788 (w
->event_flags
& SND_SOC_DAPM_POST_PMD
)) {
789 pop_dbg(codec
->pop_time
, "pop test : %s POST_PMD\n",
791 ret
= w
->event(w
, NULL
, SND_SOC_DAPM_POST_PMD
);
793 pr_err("%s: post event failed: %d\n",
799 /* Apply a DAPM power sequence.
801 * We walk over a pre-sorted list of widgets to apply power to. In
802 * order to minimise the number of writes to the device required
803 * multiple widgets will be updated in a single write where possible.
804 * Currently anything that requires more than a single write is not
807 static void dapm_seq_run(struct snd_soc_codec
*codec
, struct list_head
*list
,
808 int event
, int sort
[])
810 struct snd_soc_dapm_widget
*w
, *n
;
813 int cur_reg
= SND_SOC_NOPM
;
816 list_for_each_entry_safe(w
, n
, list
, power_list
) {
819 /* Do we need to apply any queued changes? */
820 if (sort
[w
->id
] != cur_sort
|| w
->reg
!= cur_reg
) {
821 if (!list_empty(&pending
))
822 dapm_seq_run_coalesced(codec
, &pending
);
824 INIT_LIST_HEAD(&pending
);
826 cur_reg
= SND_SOC_NOPM
;
830 case snd_soc_dapm_pre
:
832 list_for_each_entry_safe_continue(w
, n
, list
,
835 if (event
== SND_SOC_DAPM_STREAM_START
)
837 NULL
, SND_SOC_DAPM_PRE_PMU
);
838 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
840 NULL
, SND_SOC_DAPM_PRE_PMD
);
843 case snd_soc_dapm_post
:
845 list_for_each_entry_safe_continue(w
, n
, list
,
848 if (event
== SND_SOC_DAPM_STREAM_START
)
850 NULL
, SND_SOC_DAPM_POST_PMU
);
851 else if (event
== SND_SOC_DAPM_STREAM_STOP
)
853 NULL
, SND_SOC_DAPM_POST_PMD
);
856 case snd_soc_dapm_input
:
857 case snd_soc_dapm_output
:
858 case snd_soc_dapm_hp
:
859 case snd_soc_dapm_mic
:
860 case snd_soc_dapm_line
:
861 case snd_soc_dapm_spk
:
862 /* No register support currently */
863 ret
= dapm_generic_apply_power(w
);
867 /* Queue it up for application */
868 cur_sort
= sort
[w
->id
];
870 list_move(&w
->power_list
, &pending
);
875 pr_err("Failed to apply widget power: %d\n",
879 if (!list_empty(&pending
))
880 dapm_seq_run_coalesced(codec
, &pending
);
884 * Scan each dapm widget for complete audio path.
885 * A complete path is a route that has valid endpoints i.e.:-
887 * o DAC to output pin.
888 * o Input Pin to ADC.
889 * o Input pin to Output pin (bypass, sidetone)
890 * o DAC to ADC (loopback).
892 static int dapm_power_widgets(struct snd_soc_codec
*codec
, int event
)
894 struct snd_soc_card
*card
= codec
->card
;
895 struct snd_soc_dapm_widget
*w
;
897 LIST_HEAD(down_list
);
902 /* Check which widgets we need to power and store them in
903 * lists indicating if they should be powered up or down.
905 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
907 case snd_soc_dapm_pre
:
908 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
910 case snd_soc_dapm_post
:
911 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
919 power
= w
->power_check(w
);
925 if (w
->power
== power
)
929 dapm_seq_insert(w
, &up_list
, dapm_up_seq
);
931 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
938 /* If there are no DAPM widgets then try to figure out power from the
941 if (list_empty(&codec
->dapm_widgets
)) {
943 case SND_SOC_DAPM_STREAM_START
:
944 case SND_SOC_DAPM_STREAM_RESUME
:
947 case SND_SOC_DAPM_STREAM_SUSPEND
:
950 case SND_SOC_DAPM_STREAM_NOP
:
951 switch (codec
->bias_level
) {
952 case SND_SOC_BIAS_STANDBY
:
953 case SND_SOC_BIAS_OFF
:
966 if (sys_power
&& codec
->bias_level
== SND_SOC_BIAS_OFF
) {
967 ret
= snd_soc_dapm_set_bias_level(card
, codec
,
968 SND_SOC_BIAS_STANDBY
);
970 pr_err("Failed to turn on bias: %d\n", ret
);
973 /* If we're changing to all on or all off then prepare */
974 if ((sys_power
&& codec
->bias_level
== SND_SOC_BIAS_STANDBY
) ||
975 (!sys_power
&& codec
->bias_level
== SND_SOC_BIAS_ON
)) {
976 ret
= snd_soc_dapm_set_bias_level(card
, codec
, SND_SOC_BIAS_PREPARE
);
978 pr_err("Failed to prepare bias: %d\n", ret
);
981 /* Power down widgets first; try to avoid amplifying pops. */
982 dapm_seq_run(codec
, &down_list
, event
, dapm_down_seq
);
985 dapm_seq_run(codec
, &up_list
, event
, dapm_up_seq
);
987 /* If we just powered the last thing off drop to standby bias */
988 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& !sys_power
) {
989 ret
= snd_soc_dapm_set_bias_level(card
, codec
, SND_SOC_BIAS_STANDBY
);
991 pr_err("Failed to apply standby bias: %d\n", ret
);
994 /* If we're in standby and can support bias off then do that */
995 if (codec
->bias_level
== SND_SOC_BIAS_STANDBY
&&
996 codec
->idle_bias_off
) {
997 ret
= snd_soc_dapm_set_bias_level(card
, codec
, SND_SOC_BIAS_OFF
);
999 pr_err("Failed to turn off bias: %d\n", ret
);
1002 /* If we just powered up then move to active bias */
1003 if (codec
->bias_level
== SND_SOC_BIAS_PREPARE
&& sys_power
) {
1004 ret
= snd_soc_dapm_set_bias_level(card
, codec
, SND_SOC_BIAS_ON
);
1006 pr_err("Failed to apply active bias: %d\n", ret
);
1009 pop_dbg(codec
->pop_time
, "DAPM sequencing finished, waiting %dms\n",
1011 pop_wait(codec
->pop_time
);
1016 #ifdef CONFIG_DEBUG_FS
1017 static int dapm_widget_power_open_file(struct inode
*inode
, struct file
*file
)
1019 file
->private_data
= inode
->i_private
;
1023 static ssize_t
dapm_widget_power_read_file(struct file
*file
,
1024 char __user
*user_buf
,
1025 size_t count
, loff_t
*ppos
)
1027 struct snd_soc_dapm_widget
*w
= file
->private_data
;
1031 struct snd_soc_dapm_path
*p
= NULL
;
1033 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
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
);
1042 ret
= snprintf(buf
, PAGE_SIZE
, "%s: %s in %d out %d",
1043 w
->name
, w
->power
? "On" : "Off", in
, out
);
1046 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1047 " - R%d(0x%x) bit %d",
1048 w
->reg
, w
->reg
, w
->shift
);
1050 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
1053 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, " stream %s %s\n",
1055 w
->active
? "active" : "inactive");
1057 list_for_each_entry(p
, &w
->sources
, list_sink
) {
1058 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1062 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1064 p
->name
? p
->name
: "static",
1067 list_for_each_entry(p
, &w
->sinks
, list_source
) {
1068 if (p
->connected
&& !p
->connected(w
, p
->sink
))
1072 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
1074 p
->name
? p
->name
: "static",
1078 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1084 static const struct file_operations dapm_widget_power_fops
= {
1085 .open
= dapm_widget_power_open_file
,
1086 .read
= dapm_widget_power_read_file
,
1089 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1091 struct snd_soc_dapm_widget
*w
;
1094 if (!codec
->debugfs_dapm
)
1097 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1101 d
= debugfs_create_file(w
->name
, 0444,
1102 codec
->debugfs_dapm
, w
,
1103 &dapm_widget_power_fops
);
1106 "ASoC: Failed to create %s debugfs file\n",
1111 void snd_soc_dapm_debugfs_init(struct snd_soc_codec
*codec
)
1116 /* test and update the power status of a mux widget */
1117 static int dapm_mux_update_power(struct snd_soc_dapm_widget
*widget
,
1118 struct snd_kcontrol
*kcontrol
, int change
,
1119 int mux
, struct soc_enum
*e
)
1121 struct snd_soc_dapm_path
*path
;
1124 if (widget
->id
!= snd_soc_dapm_mux
&&
1125 widget
->id
!= snd_soc_dapm_value_mux
)
1131 /* find dapm widget path assoc with kcontrol */
1132 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1133 if (path
->kcontrol
!= kcontrol
)
1136 if (!path
->name
|| !e
->texts
[mux
])
1140 /* we now need to match the string in the enum to the path */
1141 if (!(strcmp(path
->name
, e
->texts
[mux
])))
1142 path
->connect
= 1; /* new connection */
1144 path
->connect
= 0; /* old connection must be powered down */
1148 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1153 /* test and update the power status of a mixer or switch widget */
1154 static int dapm_mixer_update_power(struct snd_soc_dapm_widget
*widget
,
1155 struct snd_kcontrol
*kcontrol
, int connect
)
1157 struct snd_soc_dapm_path
*path
;
1160 if (widget
->id
!= snd_soc_dapm_mixer
&&
1161 widget
->id
!= snd_soc_dapm_mixer_named_ctl
&&
1162 widget
->id
!= snd_soc_dapm_switch
)
1165 /* find dapm widget path assoc with kcontrol */
1166 list_for_each_entry(path
, &widget
->codec
->dapm_paths
, list
) {
1167 if (path
->kcontrol
!= kcontrol
)
1170 /* found, now check type */
1172 path
->connect
= connect
;
1177 dapm_power_widgets(widget
->codec
, SND_SOC_DAPM_STREAM_NOP
);
1182 /* show dapm widget status in sys fs */
1183 static ssize_t
dapm_widget_show(struct device
*dev
,
1184 struct device_attribute
*attr
, char *buf
)
1186 struct snd_soc_pcm_runtime
*rtd
=
1187 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
1188 struct snd_soc_codec
*codec
=rtd
->codec
;
1189 struct snd_soc_dapm_widget
*w
;
1191 char *state
= "not set";
1193 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1195 /* only display widgets that burnm power */
1197 case snd_soc_dapm_hp
:
1198 case snd_soc_dapm_mic
:
1199 case snd_soc_dapm_spk
:
1200 case snd_soc_dapm_line
:
1201 case snd_soc_dapm_micbias
:
1202 case snd_soc_dapm_dac
:
1203 case snd_soc_dapm_adc
:
1204 case snd_soc_dapm_pga
:
1205 case snd_soc_dapm_mixer
:
1206 case snd_soc_dapm_mixer_named_ctl
:
1207 case snd_soc_dapm_supply
:
1209 count
+= sprintf(buf
+ count
, "%s: %s\n",
1210 w
->name
, w
->power
? "On":"Off");
1217 switch (codec
->bias_level
) {
1218 case SND_SOC_BIAS_ON
:
1221 case SND_SOC_BIAS_PREPARE
:
1224 case SND_SOC_BIAS_STANDBY
:
1227 case SND_SOC_BIAS_OFF
:
1231 count
+= sprintf(buf
+ count
, "PM State: %s\n", state
);
1236 static DEVICE_ATTR(dapm_widget
, 0444, dapm_widget_show
, NULL
);
1238 int snd_soc_dapm_sys_add(struct device
*dev
)
1240 return device_create_file(dev
, &dev_attr_dapm_widget
);
1243 static void snd_soc_dapm_sys_remove(struct device
*dev
)
1245 device_remove_file(dev
, &dev_attr_dapm_widget
);
1248 /* free all dapm widgets and resources */
1249 static void dapm_free_widgets(struct snd_soc_codec
*codec
)
1251 struct snd_soc_dapm_widget
*w
, *next_w
;
1252 struct snd_soc_dapm_path
*p
, *next_p
;
1254 list_for_each_entry_safe(w
, next_w
, &codec
->dapm_widgets
, list
) {
1259 list_for_each_entry_safe(p
, next_p
, &codec
->dapm_paths
, list
) {
1261 kfree(p
->long_name
);
1266 static int snd_soc_dapm_set_pin(struct snd_soc_codec
*codec
,
1267 const char *pin
, int status
)
1269 struct snd_soc_dapm_widget
*w
;
1271 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1272 if (!strcmp(w
->name
, pin
)) {
1273 pr_debug("dapm: %s: pin %s\n", codec
->name
, pin
);
1274 w
->connected
= status
;
1275 /* Allow disabling of forced pins */
1282 pr_err("dapm: %s: configuring unknown pin %s\n", codec
->name
, pin
);
1287 * snd_soc_dapm_sync - scan and power dapm paths
1288 * @codec: audio codec
1290 * Walks all dapm audio paths and powers widgets according to their
1291 * stream or path usage.
1293 * Returns 0 for success.
1295 int snd_soc_dapm_sync(struct snd_soc_codec
*codec
)
1297 return dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1299 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync
);
1301 static int snd_soc_dapm_add_route(struct snd_soc_codec
*codec
,
1302 const struct snd_soc_dapm_route
*route
)
1304 struct snd_soc_dapm_path
*path
;
1305 struct snd_soc_dapm_widget
*wsource
= NULL
, *wsink
= NULL
, *w
;
1306 const char *sink
= route
->sink
;
1307 const char *control
= route
->control
;
1308 const char *source
= route
->source
;
1311 /* find src and dest widgets */
1312 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
1314 if (!wsink
&& !(strcmp(w
->name
, sink
))) {
1318 if (!wsource
&& !(strcmp(w
->name
, source
))) {
1323 if (wsource
== NULL
|| wsink
== NULL
)
1326 path
= kzalloc(sizeof(struct snd_soc_dapm_path
), GFP_KERNEL
);
1330 path
->source
= wsource
;
1332 path
->connected
= route
->connected
;
1333 INIT_LIST_HEAD(&path
->list
);
1334 INIT_LIST_HEAD(&path
->list_source
);
1335 INIT_LIST_HEAD(&path
->list_sink
);
1337 /* check for external widgets */
1338 if (wsink
->id
== snd_soc_dapm_input
) {
1339 if (wsource
->id
== snd_soc_dapm_micbias
||
1340 wsource
->id
== snd_soc_dapm_mic
||
1341 wsource
->id
== snd_soc_dapm_line
||
1342 wsource
->id
== snd_soc_dapm_output
)
1345 if (wsource
->id
== snd_soc_dapm_output
) {
1346 if (wsink
->id
== snd_soc_dapm_spk
||
1347 wsink
->id
== snd_soc_dapm_hp
||
1348 wsink
->id
== snd_soc_dapm_line
||
1349 wsink
->id
== snd_soc_dapm_input
)
1353 /* connect static paths */
1354 if (control
== NULL
) {
1355 list_add(&path
->list
, &codec
->dapm_paths
);
1356 list_add(&path
->list_sink
, &wsink
->sources
);
1357 list_add(&path
->list_source
, &wsource
->sinks
);
1362 /* connect dynamic paths */
1364 case snd_soc_dapm_adc
:
1365 case snd_soc_dapm_dac
:
1366 case snd_soc_dapm_pga
:
1367 case snd_soc_dapm_input
:
1368 case snd_soc_dapm_output
:
1369 case snd_soc_dapm_micbias
:
1370 case snd_soc_dapm_vmid
:
1371 case snd_soc_dapm_pre
:
1372 case snd_soc_dapm_post
:
1373 case snd_soc_dapm_supply
:
1374 case snd_soc_dapm_aif_in
:
1375 case snd_soc_dapm_aif_out
:
1376 list_add(&path
->list
, &codec
->dapm_paths
);
1377 list_add(&path
->list_sink
, &wsink
->sources
);
1378 list_add(&path
->list_source
, &wsource
->sinks
);
1381 case snd_soc_dapm_mux
:
1382 case snd_soc_dapm_value_mux
:
1383 ret
= dapm_connect_mux(codec
, wsource
, wsink
, path
, control
,
1384 &wsink
->kcontrols
[0]);
1388 case snd_soc_dapm_switch
:
1389 case snd_soc_dapm_mixer
:
1390 case snd_soc_dapm_mixer_named_ctl
:
1391 ret
= dapm_connect_mixer(codec
, wsource
, wsink
, path
, control
);
1395 case snd_soc_dapm_hp
:
1396 case snd_soc_dapm_mic
:
1397 case snd_soc_dapm_line
:
1398 case snd_soc_dapm_spk
:
1399 list_add(&path
->list
, &codec
->dapm_paths
);
1400 list_add(&path
->list_sink
, &wsink
->sources
);
1401 list_add(&path
->list_source
, &wsource
->sinks
);
1408 printk(KERN_WARNING
"asoc: no dapm match for %s --> %s --> %s\n", source
,
1415 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1417 * @route: audio routes
1418 * @num: number of routes
1420 * Connects 2 dapm widgets together via a named audio path. The sink is
1421 * the widget receiving the audio signal, whilst the source is the sender
1422 * of the audio signal.
1424 * Returns 0 for success else error. On error all resources can be freed
1425 * with a call to snd_soc_card_free().
1427 int snd_soc_dapm_add_routes(struct snd_soc_codec
*codec
,
1428 const struct snd_soc_dapm_route
*route
, int num
)
1432 for (i
= 0; i
< num
; i
++) {
1433 ret
= snd_soc_dapm_add_route(codec
, route
);
1435 printk(KERN_ERR
"Failed to add route %s->%s\n",
1445 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes
);
1448 * snd_soc_dapm_new_widgets - add new dapm widgets
1449 * @codec: audio codec
1451 * Checks the codec for any new dapm widgets and creates them if found.
1453 * Returns 0 for success.
1455 int snd_soc_dapm_new_widgets(struct snd_soc_codec
*codec
)
1457 struct snd_soc_dapm_widget
*w
;
1459 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
1465 case snd_soc_dapm_switch
:
1466 case snd_soc_dapm_mixer
:
1467 case snd_soc_dapm_mixer_named_ctl
:
1468 w
->power_check
= dapm_generic_check_power
;
1469 dapm_new_mixer(codec
, w
);
1471 case snd_soc_dapm_mux
:
1472 case snd_soc_dapm_value_mux
:
1473 w
->power_check
= dapm_generic_check_power
;
1474 dapm_new_mux(codec
, w
);
1476 case snd_soc_dapm_adc
:
1477 case snd_soc_dapm_aif_out
:
1478 w
->power_check
= dapm_adc_check_power
;
1480 case snd_soc_dapm_dac
:
1481 case snd_soc_dapm_aif_in
:
1482 w
->power_check
= dapm_dac_check_power
;
1484 case snd_soc_dapm_pga
:
1485 w
->power_check
= dapm_generic_check_power
;
1486 dapm_new_pga(codec
, w
);
1488 case snd_soc_dapm_input
:
1489 case snd_soc_dapm_output
:
1490 case snd_soc_dapm_micbias
:
1491 case snd_soc_dapm_spk
:
1492 case snd_soc_dapm_hp
:
1493 case snd_soc_dapm_mic
:
1494 case snd_soc_dapm_line
:
1495 w
->power_check
= dapm_generic_check_power
;
1497 case snd_soc_dapm_supply
:
1498 w
->power_check
= dapm_supply_check_power
;
1499 case snd_soc_dapm_vmid
:
1500 case snd_soc_dapm_pre
:
1501 case snd_soc_dapm_post
:
1507 dapm_power_widgets(codec
, SND_SOC_DAPM_STREAM_NOP
);
1510 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets
);
1513 * snd_soc_dapm_get_volsw - dapm mixer get callback
1514 * @kcontrol: mixer control
1515 * @ucontrol: control element information
1517 * Callback to get the value of a dapm mixer control.
1519 * Returns 0 for success.
1521 int snd_soc_dapm_get_volsw(struct snd_kcontrol
*kcontrol
,
1522 struct snd_ctl_elem_value
*ucontrol
)
1524 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1525 struct soc_mixer_control
*mc
=
1526 (struct soc_mixer_control
*)kcontrol
->private_value
;
1527 unsigned int reg
= mc
->reg
;
1528 unsigned int shift
= mc
->shift
;
1529 unsigned int rshift
= mc
->rshift
;
1531 unsigned int invert
= mc
->invert
;
1532 unsigned int mask
= (1 << fls(max
)) - 1;
1534 ucontrol
->value
.integer
.value
[0] =
1535 (snd_soc_read(widget
->codec
, reg
) >> shift
) & mask
;
1536 if (shift
!= rshift
)
1537 ucontrol
->value
.integer
.value
[1] =
1538 (snd_soc_read(widget
->codec
, reg
) >> rshift
) & mask
;
1540 ucontrol
->value
.integer
.value
[0] =
1541 max
- ucontrol
->value
.integer
.value
[0];
1542 if (shift
!= rshift
)
1543 ucontrol
->value
.integer
.value
[1] =
1544 max
- ucontrol
->value
.integer
.value
[1];
1549 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw
);
1552 * snd_soc_dapm_put_volsw - dapm mixer set callback
1553 * @kcontrol: mixer control
1554 * @ucontrol: control element information
1556 * Callback to set the value of a dapm mixer control.
1558 * Returns 0 for success.
1560 int snd_soc_dapm_put_volsw(struct snd_kcontrol
*kcontrol
,
1561 struct snd_ctl_elem_value
*ucontrol
)
1563 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1564 struct soc_mixer_control
*mc
=
1565 (struct soc_mixer_control
*)kcontrol
->private_value
;
1566 unsigned int reg
= mc
->reg
;
1567 unsigned int shift
= mc
->shift
;
1568 unsigned int rshift
= mc
->rshift
;
1570 unsigned int mask
= (1 << fls(max
)) - 1;
1571 unsigned int invert
= mc
->invert
;
1572 unsigned int val
, val2
, val_mask
;
1576 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1580 val_mask
= mask
<< shift
;
1582 if (shift
!= rshift
) {
1583 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1586 val_mask
|= mask
<< rshift
;
1587 val
|= val2
<< rshift
;
1590 mutex_lock(&widget
->codec
->mutex
);
1591 widget
->value
= val
;
1593 if (snd_soc_test_bits(widget
->codec
, reg
, val_mask
, val
)) {
1595 /* new connection */
1596 connect
= invert
? 0:1;
1598 /* old connection must be powered down */
1599 connect
= invert
? 1:0;
1601 dapm_mixer_update_power(widget
, kcontrol
, connect
);
1604 if (widget
->event
) {
1605 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1606 ret
= widget
->event(widget
, kcontrol
,
1607 SND_SOC_DAPM_PRE_REG
);
1613 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1614 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1615 ret
= widget
->event(widget
, kcontrol
,
1616 SND_SOC_DAPM_POST_REG
);
1618 ret
= snd_soc_update_bits(widget
->codec
, reg
, val_mask
, val
);
1621 mutex_unlock(&widget
->codec
->mutex
);
1624 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw
);
1627 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1628 * @kcontrol: mixer control
1629 * @ucontrol: control element information
1631 * Callback to get the value of a dapm enumerated double mixer control.
1633 * Returns 0 for success.
1635 int snd_soc_dapm_get_enum_double(struct snd_kcontrol
*kcontrol
,
1636 struct snd_ctl_elem_value
*ucontrol
)
1638 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1639 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1640 unsigned int val
, bitmask
;
1642 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1644 val
= snd_soc_read(widget
->codec
, e
->reg
);
1645 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1646 if (e
->shift_l
!= e
->shift_r
)
1647 ucontrol
->value
.enumerated
.item
[1] =
1648 (val
>> e
->shift_r
) & (bitmask
- 1);
1652 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double
);
1655 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1656 * @kcontrol: mixer control
1657 * @ucontrol: control element information
1659 * Callback to set the value of a dapm enumerated double mixer control.
1661 * Returns 0 for success.
1663 int snd_soc_dapm_put_enum_double(struct snd_kcontrol
*kcontrol
,
1664 struct snd_ctl_elem_value
*ucontrol
)
1666 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1667 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1668 unsigned int val
, mux
, change
;
1669 unsigned int mask
, bitmask
;
1672 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1674 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1676 mux
= ucontrol
->value
.enumerated
.item
[0];
1677 val
= mux
<< e
->shift_l
;
1678 mask
= (bitmask
- 1) << e
->shift_l
;
1679 if (e
->shift_l
!= e
->shift_r
) {
1680 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1682 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1683 mask
|= (bitmask
- 1) << e
->shift_r
;
1686 mutex_lock(&widget
->codec
->mutex
);
1687 widget
->value
= val
;
1688 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1689 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1691 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1692 ret
= widget
->event(widget
,
1693 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1698 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1700 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1701 ret
= widget
->event(widget
,
1702 kcontrol
, SND_SOC_DAPM_POST_REG
);
1705 mutex_unlock(&widget
->codec
->mutex
);
1708 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double
);
1711 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
1712 * @kcontrol: mixer control
1713 * @ucontrol: control element information
1715 * Returns 0 for success.
1717 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol
*kcontrol
,
1718 struct snd_ctl_elem_value
*ucontrol
)
1720 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1722 ucontrol
->value
.enumerated
.item
[0] = widget
->value
;
1726 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt
);
1729 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
1730 * @kcontrol: mixer control
1731 * @ucontrol: control element information
1733 * Returns 0 for success.
1735 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol
*kcontrol
,
1736 struct snd_ctl_elem_value
*ucontrol
)
1738 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1739 struct soc_enum
*e
=
1740 (struct soc_enum
*)kcontrol
->private_value
;
1744 if (ucontrol
->value
.enumerated
.item
[0] >= e
->max
)
1747 mutex_lock(&widget
->codec
->mutex
);
1749 change
= widget
->value
!= ucontrol
->value
.enumerated
.item
[0];
1750 widget
->value
= ucontrol
->value
.enumerated
.item
[0];
1751 dapm_mux_update_power(widget
, kcontrol
, change
, widget
->value
, e
);
1753 mutex_unlock(&widget
->codec
->mutex
);
1756 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt
);
1759 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1761 * @kcontrol: mixer control
1762 * @ucontrol: control element information
1764 * Callback to get the value of a dapm semi enumerated double mixer control.
1766 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1767 * used for handling bitfield coded enumeration for example.
1769 * Returns 0 for success.
1771 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
1772 struct snd_ctl_elem_value
*ucontrol
)
1774 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1775 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1776 unsigned int reg_val
, val
, mux
;
1778 reg_val
= snd_soc_read(widget
->codec
, e
->reg
);
1779 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
1780 for (mux
= 0; mux
< e
->max
; mux
++) {
1781 if (val
== e
->values
[mux
])
1784 ucontrol
->value
.enumerated
.item
[0] = mux
;
1785 if (e
->shift_l
!= e
->shift_r
) {
1786 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
1787 for (mux
= 0; mux
< e
->max
; mux
++) {
1788 if (val
== e
->values
[mux
])
1791 ucontrol
->value
.enumerated
.item
[1] = mux
;
1796 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double
);
1799 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1801 * @kcontrol: mixer control
1802 * @ucontrol: control element information
1804 * Callback to set the value of a dapm semi enumerated double mixer control.
1806 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1807 * used for handling bitfield coded enumeration for example.
1809 * Returns 0 for success.
1811 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
1812 struct snd_ctl_elem_value
*ucontrol
)
1814 struct snd_soc_dapm_widget
*widget
= snd_kcontrol_chip(kcontrol
);
1815 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1816 unsigned int val
, mux
, change
;
1820 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1822 mux
= ucontrol
->value
.enumerated
.item
[0];
1823 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
1824 mask
= e
->mask
<< e
->shift_l
;
1825 if (e
->shift_l
!= e
->shift_r
) {
1826 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1828 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
1829 mask
|= e
->mask
<< e
->shift_r
;
1832 mutex_lock(&widget
->codec
->mutex
);
1833 widget
->value
= val
;
1834 change
= snd_soc_test_bits(widget
->codec
, e
->reg
, mask
, val
);
1835 dapm_mux_update_power(widget
, kcontrol
, change
, mux
, e
);
1837 if (widget
->event_flags
& SND_SOC_DAPM_PRE_REG
) {
1838 ret
= widget
->event(widget
,
1839 kcontrol
, SND_SOC_DAPM_PRE_REG
);
1844 ret
= snd_soc_update_bits(widget
->codec
, e
->reg
, mask
, val
);
1846 if (widget
->event_flags
& SND_SOC_DAPM_POST_REG
)
1847 ret
= widget
->event(widget
,
1848 kcontrol
, SND_SOC_DAPM_POST_REG
);
1851 mutex_unlock(&widget
->codec
->mutex
);
1854 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double
);
1857 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1859 * @kcontrol: mixer control
1860 * @uinfo: control element information
1862 * Callback to provide information about a pin switch control.
1864 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol
*kcontrol
,
1865 struct snd_ctl_elem_info
*uinfo
)
1867 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1869 uinfo
->value
.integer
.min
= 0;
1870 uinfo
->value
.integer
.max
= 1;
1874 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch
);
1877 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1879 * @kcontrol: mixer control
1882 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol
*kcontrol
,
1883 struct snd_ctl_elem_value
*ucontrol
)
1885 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1886 const char *pin
= (const char *)kcontrol
->private_value
;
1888 mutex_lock(&codec
->mutex
);
1890 ucontrol
->value
.integer
.value
[0] =
1891 snd_soc_dapm_get_pin_status(codec
, pin
);
1893 mutex_unlock(&codec
->mutex
);
1897 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch
);
1900 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1902 * @kcontrol: mixer control
1905 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol
*kcontrol
,
1906 struct snd_ctl_elem_value
*ucontrol
)
1908 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1909 const char *pin
= (const char *)kcontrol
->private_value
;
1911 mutex_lock(&codec
->mutex
);
1913 if (ucontrol
->value
.integer
.value
[0])
1914 snd_soc_dapm_enable_pin(codec
, pin
);
1916 snd_soc_dapm_disable_pin(codec
, pin
);
1918 snd_soc_dapm_sync(codec
);
1920 mutex_unlock(&codec
->mutex
);
1924 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch
);
1927 * snd_soc_dapm_new_control - create new dapm control
1928 * @codec: audio codec
1929 * @widget: widget template
1931 * Creates a new dapm control based upon the template.
1933 * Returns 0 for success else error.
1935 int snd_soc_dapm_new_control(struct snd_soc_codec
*codec
,
1936 const struct snd_soc_dapm_widget
*widget
)
1938 struct snd_soc_dapm_widget
*w
;
1940 if ((w
= dapm_cnew_widget(widget
)) == NULL
)
1944 INIT_LIST_HEAD(&w
->sources
);
1945 INIT_LIST_HEAD(&w
->sinks
);
1946 INIT_LIST_HEAD(&w
->list
);
1947 list_add(&w
->list
, &codec
->dapm_widgets
);
1949 /* machine layer set ups unconnected pins and insertions */
1953 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control
);
1956 * snd_soc_dapm_new_controls - create new dapm controls
1957 * @codec: audio codec
1958 * @widget: widget array
1959 * @num: number of widgets
1961 * Creates new DAPM controls based upon the templates.
1963 * Returns 0 for success else error.
1965 int snd_soc_dapm_new_controls(struct snd_soc_codec
*codec
,
1966 const struct snd_soc_dapm_widget
*widget
,
1971 for (i
= 0; i
< num
; i
++) {
1972 ret
= snd_soc_dapm_new_control(codec
, widget
);
1975 "ASoC: Failed to create DAPM control %s: %d\n",
1983 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls
);
1987 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1988 * @codec: audio codec
1989 * @stream: stream name
1990 * @event: stream event
1992 * Sends a stream event to the dapm core. The core then makes any
1993 * necessary widget power changes.
1995 * Returns 0 for success else error.
1997 int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime
*rtd
,
1998 const char *stream
, int event
)
2000 struct snd_soc_codec
*codec
= rtd
->codec
;
2001 struct snd_soc_dapm_widget
*w
;
2006 mutex_lock(&codec
->mutex
);
2007 list_for_each_entry(w
, &codec
->dapm_widgets
, list
)
2011 pr_debug("widget %s\n %s stream %s event %d\n",
2012 w
->name
, w
->sname
, stream
, event
);
2013 if (strstr(w
->sname
, stream
)) {
2015 case SND_SOC_DAPM_STREAM_START
:
2018 case SND_SOC_DAPM_STREAM_STOP
:
2021 case SND_SOC_DAPM_STREAM_SUSPEND
:
2022 case SND_SOC_DAPM_STREAM_RESUME
:
2023 case SND_SOC_DAPM_STREAM_PAUSE_PUSH
:
2024 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE
:
2030 dapm_power_widgets(codec
, event
);
2031 mutex_unlock(&codec
->mutex
);
2034 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event
);
2037 * snd_soc_dapm_enable_pin - enable pin.
2041 * Enables input/output pin and its parents or children widgets iff there is
2042 * a valid audio route and active audio stream.
2043 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2044 * do any widget power switching.
2046 int snd_soc_dapm_enable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2048 return snd_soc_dapm_set_pin(codec
, pin
, 1);
2050 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin
);
2053 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2057 * Enables input/output pin regardless of any other state. This is
2058 * intended for use with microphone bias supplies used in microphone
2061 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2062 * do any widget power switching.
2064 int snd_soc_dapm_force_enable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2066 struct snd_soc_dapm_widget
*w
;
2068 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2069 if (!strcmp(w
->name
, pin
)) {
2070 pr_debug("dapm: %s: pin %s\n", codec
->name
, pin
);
2077 pr_err("dapm: %s: configuring unknown pin %s\n", codec
->name
, pin
);
2080 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin
);
2083 * snd_soc_dapm_disable_pin - disable pin.
2087 * Disables input/output pin and its parents or children widgets.
2088 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2089 * do any widget power switching.
2091 int snd_soc_dapm_disable_pin(struct snd_soc_codec
*codec
, const char *pin
)
2093 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2095 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin
);
2098 * snd_soc_dapm_nc_pin - permanently disable pin.
2102 * Marks the specified pin as being not connected, disabling it along
2103 * any parent or child widgets. At present this is identical to
2104 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2105 * additional things such as disabling controls which only affect
2106 * paths through the pin.
2108 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2109 * do any widget power switching.
2111 int snd_soc_dapm_nc_pin(struct snd_soc_codec
*codec
, const char *pin
)
2113 return snd_soc_dapm_set_pin(codec
, pin
, 0);
2115 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin
);
2118 * snd_soc_dapm_get_pin_status - get audio pin status
2119 * @codec: audio codec
2120 * @pin: audio signal pin endpoint (or start point)
2122 * Get audio pin status - connected or disconnected.
2124 * Returns 1 for connected otherwise 0.
2126 int snd_soc_dapm_get_pin_status(struct snd_soc_codec
*codec
, const char *pin
)
2128 struct snd_soc_dapm_widget
*w
;
2130 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2131 if (!strcmp(w
->name
, pin
))
2132 return w
->connected
;
2137 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status
);
2140 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2141 * @codec: audio codec
2142 * @pin: audio signal pin endpoint (or start point)
2144 * Mark the given endpoint or pin as ignoring suspend. When the
2145 * system is disabled a path between two endpoints flagged as ignoring
2146 * suspend will not be disabled. The path must already be enabled via
2147 * normal means at suspend time, it will not be turned on if it was not
2150 int snd_soc_dapm_ignore_suspend(struct snd_soc_codec
*codec
, const char *pin
)
2152 struct snd_soc_dapm_widget
*w
;
2154 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2155 if (!strcmp(w
->name
, pin
)) {
2156 w
->ignore_suspend
= 1;
2161 pr_err("Unknown DAPM pin: %s\n", pin
);
2164 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend
);
2167 * snd_soc_dapm_free - free dapm resources
2170 * Free all dapm widgets and resources.
2172 void snd_soc_dapm_free(struct snd_soc_codec
*codec
)
2174 snd_soc_dapm_sys_remove(codec
->dev
);
2175 dapm_free_widgets(codec
);
2177 EXPORT_SYMBOL_GPL(snd_soc_dapm_free
);
2179 static void soc_dapm_shutdown_codec(struct snd_soc_codec
*codec
)
2181 struct snd_soc_dapm_widget
*w
;
2182 LIST_HEAD(down_list
);
2185 list_for_each_entry(w
, &codec
->dapm_widgets
, list
) {
2187 dapm_seq_insert(w
, &down_list
, dapm_down_seq
);
2193 /* If there were no widgets to power down we're already in
2197 snd_soc_dapm_set_bias_level(NULL
, codec
, SND_SOC_BIAS_PREPARE
);
2198 dapm_seq_run(codec
, &down_list
, 0, dapm_down_seq
);
2199 snd_soc_dapm_set_bias_level(NULL
, codec
, SND_SOC_BIAS_STANDBY
);
2204 * snd_soc_dapm_shutdown - callback for system shutdown
2206 void snd_soc_dapm_shutdown(struct snd_soc_card
*card
)
2208 struct snd_soc_codec
*codec
;
2210 list_for_each_entry(codec
, &card
->codec_dev_list
, list
)
2211 soc_dapm_shutdown_codec(codec
);
2213 snd_soc_dapm_set_bias_level(card
, codec
, SND_SOC_BIAS_OFF
);
2216 /* Module information */
2217 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2218 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2219 MODULE_LICENSE("GPL");